1 | import osgeo.ogr |
---|
2 | import libxml2 |
---|
3 | |
---|
4 | def createGeometryFromWFS(my_wfs_response): |
---|
5 | doc=libxml2.parseMemory(my_wfs_response,len(my_wfs_response)) |
---|
6 | ctxt = doc.xpathNewContext() |
---|
7 | res=ctxt.xpathEval("/*/*/*/*/*[local-name()='Polygon' or local-name()='MultiPolygon']") |
---|
8 | for node in res: |
---|
9 | geometry_as_string=node.serialize() |
---|
10 | geometry=osgeo.ogr.CreateGeometryFromGML(geometry_as_string) |
---|
11 | return geometry |
---|
12 | return geometry |
---|
13 | |
---|
14 | def extractInputs(obj): |
---|
15 | if obj["mimeType"]=="application/json": |
---|
16 | return osgeo.ogr.CreateGeometryFromJson(obj["value"]) |
---|
17 | else: |
---|
18 | try: |
---|
19 | return createGeometryFromWFS(obj["value"]) |
---|
20 | except: |
---|
21 | return osgeo.ogr.CreateGeometryFromJson(obj["value"]) |
---|
22 | return null |
---|
23 | |
---|
24 | def outputResult(obj,geom): |
---|
25 | if obj["mimeType"]=="application/json": |
---|
26 | obj["value"]=geom.ExportToJson() |
---|
27 | obj["mimeType"]="text/plain" |
---|
28 | else: |
---|
29 | obj["value"]=geom.ExportToGML() |
---|
30 | |
---|
31 | def BoundaryPy(conf,inputs,outputs): |
---|
32 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
33 | rgeom=geometry.GetBoundary() |
---|
34 | if outputs["Result"]["mimeType"]=="application/json": |
---|
35 | outputs["Result"]["value"]=rgeom.ExportToJson() |
---|
36 | outputs["Result"]["mimeType"]="text/plain" |
---|
37 | else: |
---|
38 | outputs["Result"]["value"]=rgeom.ExportToGML() |
---|
39 | return 3 |
---|
40 | |
---|
41 | def CentroidPy(conf,inputs,outputs): |
---|
42 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
43 | if geometry.GetGeometryType()!=3: |
---|
44 | geometry=geometry.ConvexHull() |
---|
45 | rgeom=geometry.Centroid() |
---|
46 | outputResult(outputs["Result"],rgeom) |
---|
47 | return 3 |
---|
48 | |
---|
49 | def ConvexHullPy(conf,inputs,outputs): |
---|
50 | geometry=extractInputs(inputs["InputPolygon"]) |
---|
51 | rgeom=geometry.ConvexHull() |
---|
52 | outputResult(outputs["Result"],rgeom) |
---|
53 | return 3 |
---|
54 | |
---|
55 | def BufferPy(conf,inputs,outputs): |
---|
56 | geometry=createGeometryFromWFS(inputs["InputPolygon"]["value"]) |
---|
57 | try: |
---|
58 | bdist=int(inputs["BufferDistance"]["value"]) |
---|
59 | except: |
---|
60 | bdist=10 |
---|
61 | rgeom=geometry.Buffer(bdist) |
---|
62 | if outputs["Result"]["mimeType"]=="application/json": |
---|
63 | outputs["Result"]["value"]=rgeom.ExportToJson() |
---|
64 | outputs["Result"]["mimeType"]="text/plain" |
---|
65 | else: |
---|
66 | outputs["Result"]["value"]=rgeom.ExportToGML() |
---|
67 | return 3 |
---|
68 | |
---|
69 | def UnionPy(conf,inputs,outputs): |
---|
70 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
71 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
72 | rgeom=geometry1.Union(geometry2) |
---|
73 | outputResult(outputs["Result"],rgeom) |
---|
74 | return 3 |
---|
75 | |
---|
76 | def IntersectionPy(conf,inputs,outputs): |
---|
77 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
78 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
79 | rgeom=geometry1.Intersection(geometry2) |
---|
80 | outputResult(outputs["Result"],rgeom) |
---|
81 | return 3 |
---|
82 | |
---|
83 | def DifferencePy(conf,inputs,outputs): |
---|
84 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
85 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
86 | rgeom=geometry1.Difference(geometry2) |
---|
87 | outputResult(outputs["Result"],rgeom) |
---|
88 | return 3 |
---|
89 | |
---|
90 | def SymDifferencePy(conf,inputs,outputs): |
---|
91 | geometry1=extractInputs(inputs["InputEntity1"]) |
---|
92 | geometry2=extractInputs(inputs["InputEntity2"]) |
---|
93 | rgeom=geometry1.SymmetricDifference(geometry2) |
---|
94 | outputResult(outputs["Result"],rgeom) |
---|
95 | return 3 |
---|