source: branches/branch-1.2/zoo-services/ogr/base-vect-ops-py/cgi-env/ogr_sp.py @ 268

Last change on this file since 268 was 268, checked in by djay, 13 years ago

Merge branch-1.2 to current rev. 267. Tag release-1.2.0-rc2.

File size: 7.0 KB
Line 
1from osgeo import *
2import osgeo.ogr
3import osgeo.gdal
4import libxml2
5import os
6import sys
7
8def createGeometryFromWFS(conf,my_wfs_response):
9    geometry=[]
10    try:
11        # Create virtual file or parse XML file depending on the GDAL Version
12        gV=int(osgeo.gdal.VersionInfo())
13        if gV >= 1800:
14            osgeo.gdal.FileFromMemBuffer('/vsimem//temp', my_wfs_response)
15            ds = osgeo.ogr.Open('/vsimem//temp')
16            lyr = ds.GetLayer(0)
17            feat = lyr.GetNextFeature()
18            while feat is not None:
19                geometry+=[feat.GetGeometryRef().Clone()]
20                feat.Destroy()
21                feat = lyr.GetNextFeature()
22            ds.Destroy()
23            osgeo.gdal.Unlink('/vsimem//temp')
24        else:
25            doc=libxml2.parseMemory(my_wfs_response,len(my_wfs_response))
26            ctxt = doc.xpathNewContext()
27            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' ]")
28            for node in res:
29                geometry_as_string=node.serialize()
30                geometry+=[osgeo.ogr.CreateGeometryFromGML(geometry_as_string)]
31    except:
32        print >> sys.stderr,"Unable to load file from mem buffer\n\n\n"
33    return geometry
34
35def extractInputs(conf,obj):
36    if obj["mimeType"]=="application/json":
37        return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
38    else:
39        try:
40                return createGeometryFromWFS(conf,obj["value"])
41        except:
42                return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
43    return null
44   
45def outputResult(conf,obj,geom):
46    driverName = "GML"
47    extension = [ ".xml" , ".xsd" ]
48    if obj["mimeType"]=="application/json":
49        driverName = "GeoJSON"
50        extension = [ ".js" ]
51    drv = osgeo.ogr.GetDriverByName( driverName )
52    # Create virtual file or real one depending on the GDAL Version
53    gV=int(osgeo.gdal.VersionInfo())
54    if gV >= 1800:
55        ds = drv.CreateDataSource( "/vsimem/store"+conf["lenv"]["sid"]+extension[0] )
56    else:
57        ds = drv.CreateDataSource( conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0] )
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    i=0
63    while i < len(geom):
64        feat = osgeo.ogr.Feature( lyr.GetLayerDefn())
65        feat.SetField( "Name", "Result"+str(i) )
66        feat.SetGeometry(geom[i])
67        lyr.CreateFeature(feat)
68        feat.Destroy()
69        geom[i].Destroy()
70        i+=1
71    ds.Destroy()
72    if gV >= 1800:
73        vsiFile=osgeo.gdal.VSIFOpenL("/vsimem/store"+conf["lenv"]["sid"]+extension[0],"r")
74        i=0
75        while osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_END)>0:
76            i+=1
77        fileSize=osgeo.gdal.VSIFTellL(vsiFile)
78        osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_SET)
79        obj["value"]=osgeo.gdal.VSIFReadL(fileSize,1,vsiFile)
80        osgeo.gdal.Unlink("/vsimem/store"+conf["lenv"]["sid"]+extension[0])
81    else:
82        obj["value"]=open(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0],"r").read()
83        os.unlink(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0])
84        if len(extension)>1:
85            os.unlink(conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[1])
86   
87
88def BoundaryPy(conf,inputs,outputs):
89    geometry=extractInputs(conf,inputs["InputPolygon"])
90    i=0
91    rgeometries=[]
92    while i < len(geometry):
93        rgeometries+=[geometry[i].GetBoundary()]
94        geometry[i].Destroy()
95        i+=1
96    outputResult(conf,outputs["Result"],rgeometries)
97    return 3
98
99def CentroidPy(conf,inputs,outputs):
100    geometry=extractInputs(conf,inputs["InputPolygon"])
101    i=0
102    rgeometries=[]
103    while i < len(geometry):
104        if geometry[i].GetGeometryType()!=3:
105            geometry[i]=geometry[i].ConvexHull()
106        rgeometries+=[geometry[i].Centroid()]
107        geometry[i].Destroy()
108        i+=1
109    outputResult(conf,outputs["Result"],rgeometries)
110    return 3
111
112def ConvexHullPy(conf,inputs,outputs):
113    geometry=extractInputs(conf,inputs["InputPolygon"])
114    i=0
115    rgeometries=[]
116    while i < len(geometry):
117        rgeometries+=[geometry[i].ConvexHull()]
118        geometry[i].Destroy()
119        i+=1
120    outputResult(conf,outputs["Result"],rgeometries)
121    return 3
122
123def BufferPy(conf,inputs,outputs):
124    try:
125        bdist=float(inputs["BufferDistance"]["value"])
126    except:
127        bdist=10
128    geometry=extractInputs(conf,inputs["InputPolygon"])
129    i=0
130    rgeometries=[]
131    while i < len(geometry):
132        rgeometries+=[geometry[i].Buffer(bdist)]
133        geometry[i].Destroy()
134        i+=1
135    outputResult(conf,outputs["Result"],rgeometries)
136    i=0
137    return 3
138
139def UnionPy(conf,inputs,outputs):
140    geometry1=extractInputs(conf,inputs["InputEntity1"])
141    geometry2=extractInputs(conf,inputs["InputEntity2"])
142    rgeometries=[]
143    i=0
144    while i < len(geometry1):
145        j=0
146        while j < len(geometry2):
147            tres=geometry1[i].Union(geometry2[j])
148            if not(tres.IsEmpty()):
149                rgeometries+=[tres]
150            j+=1
151        geometry1[i].Destroy()
152        i+=1
153    i=0
154    while i < len(geometry2):
155        geometry2[i].Destroy()
156        i+=1
157    outputResult(conf,outputs["Result"],rgeometries)
158    return 3
159
160def IntersectionPy(conf,inputs,outputs):
161    geometry1=extractInputs(conf,inputs["InputEntity1"])
162    geometry2=extractInputs(conf,inputs["InputEntity2"])
163    rgeometries=[]
164    i=0
165    while i < len(geometry1):
166        j=0
167        while j < len(geometry2):
168            tres=geometry1[i].Intersection(geometry2[j])
169            if not(tres.IsEmpty()):
170                rgeometries+=[tres]
171            j+=1
172        geometry1[i].Destroy()
173        i+=1
174    i=0
175    while i < len(geometry2):
176        geometry2[i].Destroy()
177        i+=1
178    outputResult(conf,outputs["Result"],rgeometries)
179    return 3
180
181def DifferencePy(conf,inputs,outputs):
182    geometry1=extractInputs(conf,inputs["InputEntity1"])
183    geometry2=extractInputs(conf,inputs["InputEntity2"])
184    rgeometries=[]
185    i=0
186    while i < len(geometry1):
187        j=0
188        while j < len(geometry2):
189            tres=geometry1[i].Difference(geometry2[j])
190            if not(tres.IsEmpty()):
191                rgeometries+=[tres]
192            j+=1
193        geometry1[i].Destroy()
194        i+=1
195    i=0
196    while i < len(geometry2):
197        geometry2[i].Destroy()
198        i+=1
199    outputResult(conf,outputs["Result"],rgeometries)
200    return 3
201
202def SymDifferencePy(conf,inputs,outputs):
203    geometry1=extractInputs(conf,inputs["InputEntity1"])
204    geometry2=extractInputs(conf,inputs["InputEntity2"])
205    rgeometries=[]
206    i=0
207    while i < len(geometry1):
208        j=0
209        while j < len(geometry2):
210            rgeometries+=[geometry1[i].SymmetricDifference(geometry2[j])]
211            j+=1
212        geometry1[i].Destroy()
213        i+=1
214    i=0
215    while i < len(geometry2):
216        geometry2[i].Destroy()
217        i+=1
218    outputResult(conf,outputs["Result"],rgeometries)
219    return 3
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

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