1 | # |
---|
2 | # Author : Gérald FENOY |
---|
3 | # |
---|
4 | # Copyright 2009-2013 GeoLabs SARL. All rights reserved. |
---|
5 | # |
---|
6 | # Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | # copy of this software and associated documentation files (the |
---|
8 | # "Software"), to deal in the Software without restriction, including with |
---|
9 | # out limitation the rights to use, copy, modify, merge, publish, |
---|
10 | # distribute, sublicense, and/or sell copies of the Software, and to |
---|
11 | # permit persons to whom the Software is furnished to do so, subject to |
---|
12 | # the following conditions: |
---|
13 | # |
---|
14 | # The above copyright notice and this permission notice shall be included |
---|
15 | # in all copies or substantial portions of the Software. |
---|
16 | # |
---|
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
18 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
19 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
20 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
---|
21 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
22 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
---|
23 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
24 | # |
---|
25 | |
---|
26 | from osgeo import * |
---|
27 | import osgeo.ogr |
---|
28 | import osgeo.gdal |
---|
29 | import libxml2 |
---|
30 | import os |
---|
31 | import sys |
---|
32 | import zoo |
---|
33 | |
---|
34 | def readFileFromBuffer(data,ext): |
---|
35 | try: |
---|
36 | geometry=[] |
---|
37 | print >> sys.stderr,'/vsimem//temp1'+ext |
---|
38 | #print >> sys.stderr,data |
---|
39 | osgeo.gdal.FileFromMemBuffer('/vsimem//temp1'+ext,data) |
---|
40 | ds = osgeo.ogr.Open('/vsimem//temp1'+ext) |
---|
41 | lyr = ds.GetLayer(0) |
---|
42 | feat = lyr.GetNextFeature() |
---|
43 | while feat is not None: |
---|
44 | geometry+=[feat.Clone()] |
---|
45 | feat.Destroy() |
---|
46 | feat = lyr.GetNextFeature() |
---|
47 | ds.Destroy() |
---|
48 | osgeo.gdal.Unlink('/vsimem//temp1'+ext) |
---|
49 | return geometry |
---|
50 | except Exception,e: |
---|
51 | print >> sys.stderr,e |
---|
52 | return [] |
---|
53 | |
---|
54 | def buildFeatureFromGeomtry(conf,geom,driverName,ext): |
---|
55 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
56 | ds = drv.CreateDataSource( "/vsimem//store"+conf["lenv"]["sid"]+"0."+ext ) |
---|
57 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
58 | field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString ) |
---|
59 | field_defn.SetWidth( len("Result10000") ) |
---|
60 | lyr.CreateField ( field_defn ) |
---|
61 | feat = osgeo.ogr.Feature(lyr.GetLayerDefn()) |
---|
62 | feat.SetField( "Name", "Input0" ) |
---|
63 | feat.SetGeometry(geom) |
---|
64 | lyr.CreateFeature(feat) |
---|
65 | ds.Destroy() |
---|
66 | return [feat] |
---|
67 | |
---|
68 | def createGeometryFromWFS(conf,my_wfs_response): |
---|
69 | try: |
---|
70 | geom=osgeo.ogr.CreateGeometryFromGML(my_wfs_response.replace('<?xml version="1.0" encoding="utf-8"?>\n','')) |
---|
71 | except: |
---|
72 | geom=None |
---|
73 | try: |
---|
74 | if geom is None: |
---|
75 | if not(conf["lenv"].has_key("cnt")): |
---|
76 | conf["lenv"]["cnt"]=0 |
---|
77 | else: |
---|
78 | conf["lenv"]["cnt"]+=1 |
---|
79 | return readFileFromBuffer(my_wfs_response,str(conf["lenv"]["cnt"])) |
---|
80 | else: |
---|
81 | return buildFeatureFromGeomtry(conf,geom,"GML","xml") |
---|
82 | except: |
---|
83 | print >> sys.stderr,"Unable to load file input data !!!\n\n\n" |
---|
84 | |
---|
85 | def createLayerFromJson(conf,obj): |
---|
86 | geom=osgeo.ogr.CreateGeometryFromJson(obj) |
---|
87 | if geom is None: |
---|
88 | return readFileFromBuffer(obj,".json") |
---|
89 | else: |
---|
90 | return buildFeatureFromGeomtry(conf,geom,"GeoJSON","json") |
---|
91 | |
---|
92 | def extractInputs(conf,obj): |
---|
93 | if obj["mimeType"]=="application/json": |
---|
94 | return createLayerFromJson(conf,obj["value"]) |
---|
95 | else: |
---|
96 | return createGeometryFromWFS(conf,obj["value"]) |
---|
97 | |
---|
98 | def outputResult(conf,obj,geom): |
---|
99 | driverName = "GML" |
---|
100 | extension = [ ".xml" , ".xsd" ] |
---|
101 | if obj["mimeType"]=="application/json": |
---|
102 | driverName = "GeoJSON" |
---|
103 | extension = [ ".js" ] |
---|
104 | if obj.keys().count("schema")>0 and \ |
---|
105 | obj["schema"]=="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd": |
---|
106 | driverName = "KML" |
---|
107 | extension = [ ".kml" ] |
---|
108 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
109 | # Create virtual file |
---|
110 | ds = drv.CreateDataSource( "/vsimem/store"+conf["lenv"]["sid"]+extension[0] ) |
---|
111 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
112 | i=0 |
---|
113 | while i < len(geom): |
---|
114 | if i==0 and driverName!="GeoJSON": |
---|
115 | poDstFDefn=geom[i].GetDefnRef() |
---|
116 | if poDstFDefn is not None: |
---|
117 | nDstFieldCount = poDstFDefn.GetFieldCount() |
---|
118 | for iField in range(nDstFieldCount): |
---|
119 | poSrcFieldDefn = poDstFDefn.GetFieldDefn(iField) |
---|
120 | oFieldDefn = osgeo.ogr.FieldDefn(poSrcFieldDefn.GetNameRef(),poSrcFieldDefn.GetType()) |
---|
121 | oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() ) |
---|
122 | oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() ) |
---|
123 | lyr.CreateField( oFieldDefn ) |
---|
124 | lyr.CreateFeature(geom[i]) |
---|
125 | geom[i].Destroy() |
---|
126 | i+=1 |
---|
127 | ds.Destroy() |
---|
128 | vsiFile=osgeo.gdal.VSIFOpenL("/vsimem/store"+conf["lenv"]["sid"]+extension[0],"r") |
---|
129 | i=0 |
---|
130 | while osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_END)>0: |
---|
131 | i+=1 |
---|
132 | fileSize=osgeo.gdal.VSIFTellL(vsiFile) |
---|
133 | osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_SET) |
---|
134 | obj["value"]=osgeo.gdal.VSIFReadL(fileSize,1,vsiFile) |
---|
135 | osgeo.gdal.Unlink("/vsimem/store"+conf["lenv"]["sid"]+extension[0]) |
---|
136 | |
---|
137 | def BufferPy(conf,inputs,outputs): |
---|
138 | print >> sys.stderr, "Starting service ..." |
---|
139 | try: |
---|
140 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
141 | except: |
---|
142 | bdist=1 |
---|
143 | print >> sys.stderr, bdist |
---|
144 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
145 | i=0 |
---|
146 | rgeometries=[] |
---|
147 | while i < len(geometry): |
---|
148 | tmp=geometry[i].Clone() |
---|
149 | resg=geometry[i].GetGeometryRef().Buffer(bdist) |
---|
150 | tmp.SetGeometryDirectly(resg) |
---|
151 | rgeometries+=[tmp] |
---|
152 | geometry[i].Destroy() |
---|
153 | resg.thisown=False |
---|
154 | tmp.thisown=False |
---|
155 | i+=1 |
---|
156 | outputResult(conf,outputs["Result"],rgeometries) |
---|
157 | i=0 |
---|
158 | return zoo.SERVICE_SUCCEEDED |
---|
159 | |
---|
160 | def BoundaryPy(conf,inputs,outputs): |
---|
161 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
162 | i=0 |
---|
163 | rgeometries=[] |
---|
164 | while i < len(geometry): |
---|
165 | tmp=geometry[i].Clone() |
---|
166 | resg=geometry[i].GetGeometryRef() |
---|
167 | resg=resg.GetBoundary() |
---|
168 | tmp.SetGeometryDirectly(resg) |
---|
169 | rgeometries+=[tmp] |
---|
170 | geometry[i].Destroy() |
---|
171 | i+=1 |
---|
172 | outputResult(conf,outputs["Result"],rgeometries) |
---|
173 | return zoo.SERVICE_SUCCEEDED |
---|
174 | |
---|
175 | def CentroidPy(conf,inputs,outputs): |
---|
176 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
177 | i=0 |
---|
178 | rgeometries=[] |
---|
179 | while i < len(geometry): |
---|
180 | tmp=geometry[i].Clone() |
---|
181 | resg=geometry[i].GetGeometryRef() |
---|
182 | if resg.GetGeometryType()!=3: |
---|
183 | resg=resg.ConvexHull() |
---|
184 | resg=resg.Centroid() |
---|
185 | tmp.SetGeometryDirectly(resg) |
---|
186 | rgeometries+=[tmp] |
---|
187 | geometry[i].Destroy() |
---|
188 | i+=1 |
---|
189 | outputResult(conf,outputs["Result"],rgeometries) |
---|
190 | return zoo.SERVICE_SUCCEEDED |
---|
191 | |
---|
192 | def ConvexHullPy(conf,inputs,outputs): |
---|
193 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
194 | i=0 |
---|
195 | rgeometries=[] |
---|
196 | while i < len(geometry): |
---|
197 | tmp=geometry[i].Clone() |
---|
198 | resg=geometry[i].GetGeometryRef().ConvexHull() |
---|
199 | tmp.SetGeometryDirectly(resg) |
---|
200 | rgeometries+=[tmp] |
---|
201 | geometry[i].Destroy() |
---|
202 | i+=1 |
---|
203 | outputResult(conf,outputs["Result"],rgeometries) |
---|
204 | return zoo.SERVICE_SUCCEEDED |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | def EnvelopePy(conf,inputs,outputs): |
---|
209 | print >> sys.stderr, inputs |
---|
210 | try: |
---|
211 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
212 | except: |
---|
213 | bdist=10 |
---|
214 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
215 | tmp=geometry[0].GetGeometryRef().GetEnvelope() |
---|
216 | outputs["Result"]["value"]=str(tmp[0])+','+str(tmp[2])+','+str(tmp[1])+','+str(tmp[3])+','+'urn:ogc:def:crs:OGC:1.3:CRS84' |
---|
217 | print >> sys.stderr,outputs["Result"] |
---|
218 | return 3 |
---|
219 | |
---|
220 | def UnionPy(conf,inputs,outputs): |
---|
221 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
222 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
223 | rgeometries=[] |
---|
224 | i=0 |
---|
225 | while i < len(geometry1): |
---|
226 | j=0 |
---|
227 | while j < len(geometry2): |
---|
228 | tmp=geometry1[i].Clone() |
---|
229 | resg=geometry2[j].GetGeometryRef() |
---|
230 | resg=resg.Union(geometry1[i].GetGeometryRef()) |
---|
231 | tmp.SetGeometryDirectly(resg) |
---|
232 | if not(resg.IsEmpty()): |
---|
233 | rgeometries+=[tmp] |
---|
234 | j+=1 |
---|
235 | geometry1[i].Destroy() |
---|
236 | i+=1 |
---|
237 | i=0 |
---|
238 | while i < len(geometry2): |
---|
239 | geometry2[i].Destroy() |
---|
240 | i+=1 |
---|
241 | outputResult(conf,outputs["Result"],rgeometries) |
---|
242 | return 3 |
---|
243 | |
---|
244 | def IntersectionPy(conf,inputs,outputs): |
---|
245 | |
---|
246 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
247 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
248 | |
---|
249 | print >> sys.stderr,str(len(geometry1))+" "+str(len(geometry2)) |
---|
250 | |
---|
251 | rgeometries=[] |
---|
252 | fids=[] |
---|
253 | i=0 |
---|
254 | while i < len(geometry1): |
---|
255 | j=0 |
---|
256 | while j < len(geometry2): |
---|
257 | tmp=geometry2[j].Clone() |
---|
258 | resg=geometry2[j].GetGeometryRef() |
---|
259 | #resg=resg.Intersection(geometry1[i].GetGeometryRef()) |
---|
260 | resg=geometry1[i].GetGeometryRef().Intersection(resg) |
---|
261 | tmp.SetGeometryDirectly(resg) |
---|
262 | if resg is not None and not(resg.IsEmpty()) and fids.count(tmp.GetFID())==0: |
---|
263 | rgeometries+=[tmp] |
---|
264 | fids+=[tmp.GetFID()] |
---|
265 | else: |
---|
266 | tmp.Destroy() |
---|
267 | j+=1 |
---|
268 | geometry1[i].Destroy() |
---|
269 | i+=1 |
---|
270 | i=0 |
---|
271 | while i < len(geometry2): |
---|
272 | geometry2[i].Destroy() |
---|
273 | i+=1 |
---|
274 | outputResult(conf,outputs["Result"],rgeometries) |
---|
275 | print >> sys.stderr,"/outputResult" |
---|
276 | return 3 |
---|
277 | |
---|
278 | def DifferencePy(conf,inputs,outputs): |
---|
279 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
280 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
281 | rgeometries=[] |
---|
282 | i=0 |
---|
283 | while i < len(geometry1): |
---|
284 | j=0 |
---|
285 | while j < len(geometry2): |
---|
286 | tmp=geometry2[j].Clone() |
---|
287 | resg=geometry1[i].GetGeometryRef() |
---|
288 | resg=resg.Difference(geometry2[i].GetGeometryRef()) |
---|
289 | tmp.SetGeometryDirectly(resg) |
---|
290 | if not(resg.IsEmpty()): |
---|
291 | rgeometries+=[tmp] |
---|
292 | j+=1 |
---|
293 | geometry1[i].Destroy() |
---|
294 | i+=1 |
---|
295 | i=0 |
---|
296 | while i < len(geometry2): |
---|
297 | geometry2[i].Destroy() |
---|
298 | i+=1 |
---|
299 | outputResult(conf,outputs["Result"],rgeometries) |
---|
300 | return 3 |
---|
301 | |
---|
302 | def SymDifferencePy(conf,inputs,outputs): |
---|
303 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
304 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
305 | rgeometries=[] |
---|
306 | i=0 |
---|
307 | while i < len(geometry1): |
---|
308 | j=0 |
---|
309 | while j < len(geometry2): |
---|
310 | tmp=geometry2[j].Clone() |
---|
311 | resg=geometry1[i].GetGeometryRef() |
---|
312 | resg=resg.SymmetricDifference(geometry2[i].GetGeometryRef()) |
---|
313 | tmp.SetGeometryDirectly(resg) |
---|
314 | rgeometries+=[tmp] |
---|
315 | j+=1 |
---|
316 | geometry1[i].Destroy() |
---|
317 | i+=1 |
---|
318 | i=0 |
---|
319 | while i < len(geometry2): |
---|
320 | geometry2[i].Destroy() |
---|
321 | i+=1 |
---|
322 | outputResult(conf,outputs["Result"],rgeometries) |
---|
323 | return 3 |
---|
324 | |
---|