1 | import osgeo.ogr |
---|
2 | import libxml2 |
---|
3 | import os |
---|
4 | import sys |
---|
5 | |
---|
6 | def createGeometryFromWFS(my_wfs_response): |
---|
7 | doc=libxml2.parseMemory(my_wfs_response,len(my_wfs_response)) |
---|
8 | ctxt = doc.xpathNewContext() |
---|
9 | res=ctxt.xpathEval("/*/*/*/*/*[local-name()='Polygon' or local-name()='MultiPolygon' or local-name()='Point' or local-name()='MultiPoint' or local-name()='MultiLineString' or local-name()='LineString' ]") |
---|
10 | geometry=[] |
---|
11 | try: |
---|
12 | for node in res: |
---|
13 | geometry_as_string=node.serialize() |
---|
14 | geometry+=[osgeo.ogr.CreateGeometryFromGML(geometry_as_string)] |
---|
15 | except: |
---|
16 | print >> sys.stderr,"Error" |
---|
17 | return geometry |
---|
18 | |
---|
19 | def extractInputs(obj): |
---|
20 | if obj["mimeType"]=="application/json": |
---|
21 | return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] |
---|
22 | else: |
---|
23 | try: |
---|
24 | return createGeometryFromWFS(obj["value"]) |
---|
25 | except: |
---|
26 | return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] |
---|
27 | return null |
---|
28 | |
---|
29 | def outputResult(conf,obj,geom): |
---|
30 | driverName = "GML" |
---|
31 | extension = [ ".xml" , ".xsd" ] |
---|
32 | if obj["mimeType"]=="application/json": |
---|
33 | driverName = "GeoJSON" |
---|
34 | extension = [ ".js" ] |
---|
35 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
36 | ds = drv.CreateDataSource( conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0] ) |
---|
37 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
38 | field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString ) |
---|
39 | field_defn.SetWidth( len("Result10000") ) |
---|
40 | lyr.CreateField ( field_defn ) |
---|
41 | i=0 |
---|
42 | while i < len(geom): |
---|
43 | feat = osgeo.ogr.Feature( lyr.GetLayerDefn()) |
---|
44 | feat.SetField( "Name", "Result"+str(i) ) |
---|
45 | feat.SetGeometry(geom[i]) |
---|
46 | lyr.CreateFeature(feat) |
---|
47 | feat.Destroy() |
---|
48 | geom[i].Destroy() |
---|
49 | i+=1 |
---|
50 | ds.Destroy() |
---|
51 | obj["value"]=open(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0],"r").read() |
---|
52 | os.unlink(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0]) |
---|
53 | if len(extension)>1: |
---|
54 | os.unlink(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[1]) |
---|
55 | |
---|
56 | def BoundaryPy(conf,inputs,outputs): |
---|
57 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
58 | i=0 |
---|
59 | rgeometries=[] |
---|
60 | while i < len(geometry): |
---|
61 | rgeometries+=[geometry[i].GetBoundary()] |
---|
62 | geometry[i].Destroy() |
---|
63 | i+=1 |
---|
64 | outputResult(conf,outputs["Result"],rgeometries) |
---|
65 | return 3 |
---|
66 | |
---|
67 | def CentroidPy(conf,inputs,outputs): |
---|
68 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
69 | i=0 |
---|
70 | rgeometries=[] |
---|
71 | while i < len(geometry): |
---|
72 | if geometry[i].GetGeometryType()!=3: |
---|
73 | geometry[i]=geometry[i].ConvexHull() |
---|
74 | rgeometries+=[geometry[i].Centroid()] |
---|
75 | geometry[i].Destroy() |
---|
76 | i+=1 |
---|
77 | outputResult(conf,outputs["Result"],rgeometries) |
---|
78 | return 3 |
---|
79 | |
---|
80 | def ConvexHullPy(conf,inputs,outputs): |
---|
81 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
82 | i=0 |
---|
83 | rgeometries=[] |
---|
84 | while i < len(geometry): |
---|
85 | rgeometries+=[geometry[i].ConvexHull()] |
---|
86 | geometry[i].Destroy() |
---|
87 | i+=1 |
---|
88 | outputResult(conf,outputs["Result"],rgeometries) |
---|
89 | return 3 |
---|
90 | |
---|
91 | def BufferPy(conf,inputs,outputs): |
---|
92 | try: |
---|
93 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
94 | except: |
---|
95 | bdist=10 |
---|
96 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
97 | i=0 |
---|
98 | rgeometries=[] |
---|
99 | while i < len(geometry): |
---|
100 | rgeometries+=[geometry[i].Buffer(bdist)] |
---|
101 | geometry[i].Destroy() |
---|
102 | i+=1 |
---|
103 | outputResult(conf,outputs["Result"],rgeometries) |
---|
104 | i=0 |
---|
105 | return 3 |
---|
106 | |
---|
107 | def UnionPy(conf,inputs,outputs): |
---|
108 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
109 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
110 | rgeometries=[] |
---|
111 | i=0 |
---|
112 | while i < len(geometry1): |
---|
113 | j=0 |
---|
114 | while j < len(geometry2): |
---|
115 | tres=geometry1[i].Union(geometry2[j]) |
---|
116 | if not(tres.IsEmpty()): |
---|
117 | rgeometries+=[tres] |
---|
118 | j+=1 |
---|
119 | geometry1[i].Destroy() |
---|
120 | i+=1 |
---|
121 | i=0 |
---|
122 | while i < len(geometry2): |
---|
123 | geometry2[i].Destroy() |
---|
124 | i+=1 |
---|
125 | outputResult(conf,outputs["Result"],rgeometries) |
---|
126 | return 3 |
---|
127 | |
---|
128 | def IntersectionPy(conf,inputs,outputs): |
---|
129 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
130 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
131 | rgeometries=[] |
---|
132 | i=0 |
---|
133 | while i < len(geometry1): |
---|
134 | j=0 |
---|
135 | while j < len(geometry2): |
---|
136 | tres=geometry1[i].Intersection(geometry2[j]) |
---|
137 | if not(tres.IsEmpty()): |
---|
138 | rgeometries+=[tres] |
---|
139 | j+=1 |
---|
140 | geometry1[i].Destroy() |
---|
141 | i+=1 |
---|
142 | i=0 |
---|
143 | while i < len(geometry2): |
---|
144 | geometry2[i].Destroy() |
---|
145 | i+=1 |
---|
146 | outputResult(conf,outputs["Result"],rgeometries) |
---|
147 | return 3 |
---|
148 | |
---|
149 | def DifferencePy(conf,inputs,outputs): |
---|
150 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
151 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
152 | rgeometries=[] |
---|
153 | i=0 |
---|
154 | while i < len(geometry1): |
---|
155 | j=0 |
---|
156 | while j < len(geometry2): |
---|
157 | tres=geometry1[i].Difference(geometry2[j]) |
---|
158 | if not(tres.IsEmpty()): |
---|
159 | rgeometries+=[tres] |
---|
160 | j+=1 |
---|
161 | geometry1[i].Destroy() |
---|
162 | i+=1 |
---|
163 | i=0 |
---|
164 | while i < len(geometry2): |
---|
165 | geometry2[i].Destroy() |
---|
166 | i+=1 |
---|
167 | outputResult(conf,outputs["Result"],rgeometries) |
---|
168 | return 3 |
---|
169 | |
---|
170 | def SymDifferencePy(conf,inputs,outputs): |
---|
171 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
172 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
173 | rgeometries=[] |
---|
174 | i=0 |
---|
175 | while i < len(geometry1): |
---|
176 | j=0 |
---|
177 | while j < len(geometry2): |
---|
178 | rgeometries+=[geometry1[i].SymmetricDifference(geometry2[j])] |
---|
179 | j+=1 |
---|
180 | geometry1[i].Destroy() |
---|
181 | i+=1 |
---|
182 | i=0 |
---|
183 | while i < len(geometry2): |
---|
184 | geometry2[i].Destroy() |
---|
185 | i+=1 |
---|
186 | outputResult(conf,outputs["Result"],rgeometries) |
---|
187 | return 3 |
---|