- Timestamp:
- Jul 16, 2011, 12:58:47 PM (13 years ago)
- Location:
- branches/branch-1.2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/branch-1.2
-
branches/branch-1.2/zoo-services/ogr/base-vect-ops-py/cgi-env/ogr_sp.py
r107 r268 1 from osgeo import * 1 2 import osgeo.ogr 3 import osgeo.gdal 2 4 import libxml2 3 5 import os 4 6 import sys 5 7 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' ]") 8 def createGeometryFromWFS(conf,my_wfs_response): 10 9 geometry=[] 11 10 try: 12 for node in res: 13 geometry_as_string=node.serialize() 14 geometry+=[osgeo.ogr.CreateGeometryFromGML(geometry_as_string)] 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)] 15 31 except: 16 print >> sys.stderr," Error"32 print >> sys.stderr,"Unable to load file from mem buffer\n\n\n" 17 33 return geometry 18 34 19 def extractInputs( obj):35 def extractInputs(conf,obj): 20 36 if obj["mimeType"]=="application/json": 21 37 return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] 22 38 else: 23 39 try: 24 return createGeometryFromWFS( obj["value"])40 return createGeometryFromWFS(conf,obj["value"]) 25 41 except: 26 42 return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] … … 34 50 extension = [ ".js" ] 35 51 drv = osgeo.ogr.GetDriverByName( driverName ) 36 ds = drv.CreateDataSource( conf["main"]["tmpPath"]+"/store"+conf["lenv"]["sid"]+extension[0] ) 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] ) 37 58 lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) 38 59 field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString ) … … 49 70 i+=1 50 71 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]) 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 55 87 56 88 def BoundaryPy(conf,inputs,outputs): 57 geometry=extractInputs( inputs["InputPolygon"])89 geometry=extractInputs(conf,inputs["InputPolygon"]) 58 90 i=0 59 91 rgeometries=[] … … 66 98 67 99 def CentroidPy(conf,inputs,outputs): 68 geometry=extractInputs( inputs["InputPolygon"])100 geometry=extractInputs(conf,inputs["InputPolygon"]) 69 101 i=0 70 102 rgeometries=[] … … 79 111 80 112 def ConvexHullPy(conf,inputs,outputs): 81 geometry=extractInputs( inputs["InputPolygon"])113 geometry=extractInputs(conf,inputs["InputPolygon"]) 82 114 i=0 83 115 rgeometries=[] … … 94 126 except: 95 127 bdist=10 96 geometry=extractInputs( inputs["InputPolygon"])128 geometry=extractInputs(conf,inputs["InputPolygon"]) 97 129 i=0 98 130 rgeometries=[] … … 106 138 107 139 def UnionPy(conf,inputs,outputs): 108 geometry1=extractInputs( inputs["InputEntity1"])109 geometry2=extractInputs( inputs["InputEntity2"])140 geometry1=extractInputs(conf,inputs["InputEntity1"]) 141 geometry2=extractInputs(conf,inputs["InputEntity2"]) 110 142 rgeometries=[] 111 143 i=0 … … 127 159 128 160 def IntersectionPy(conf,inputs,outputs): 129 geometry1=extractInputs( inputs["InputEntity1"])130 geometry2=extractInputs( inputs["InputEntity2"])161 geometry1=extractInputs(conf,inputs["InputEntity1"]) 162 geometry2=extractInputs(conf,inputs["InputEntity2"]) 131 163 rgeometries=[] 132 164 i=0 … … 148 180 149 181 def DifferencePy(conf,inputs,outputs): 150 geometry1=extractInputs( inputs["InputEntity1"])151 geometry2=extractInputs( inputs["InputEntity2"])182 geometry1=extractInputs(conf,inputs["InputEntity1"]) 183 geometry2=extractInputs(conf,inputs["InputEntity2"]) 152 184 rgeometries=[] 153 185 i=0 … … 169 201 170 202 def SymDifferencePy(conf,inputs,outputs): 171 geometry1=extractInputs( inputs["InputEntity1"])172 geometry2=extractInputs( inputs["InputEntity2"])203 geometry1=extractInputs(conf,inputs["InputEntity1"]) 204 geometry2=extractInputs(conf,inputs["InputEntity2"]) 173 205 rgeometries=[] 174 206 i=0
Note: See TracChangeset
for help on using the changeset viewer.