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