source: trunk/zoo-services/ogr/base-vect-ops-py/cgi-env/ogr_sp.py @ 111

Last change on this file since 111 was 107, checked in by djay, 14 years ago

Fix typo.

File size: 5.6 KB
Line 
1import osgeo.ogr
2import libxml2
3import os
4import sys
5
6def 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
19def 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   
29def 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
56def 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
67def 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
80def 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
91def 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
107def 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
128def 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
149def 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
170def 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
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png