Changeset 257


Ignore:
Timestamp:
Jul 14, 2011, 7:05:41 PM (10 years ago)
Author:
djay
Message:

Use OGR vsimem if gdal version >= 1.8.0 is available.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-services/ogr/base-vect-ops-py/cgi-env/ogr_sp.py

    r107 r257  
     1from osgeo import *
    12import osgeo.ogr
     3import osgeo.gdal
    24import libxml2
    35import os
    46import sys
    57
    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' ]")
     8def createGeometryFromWFS(conf,my_wfs_response):
    109    geometry=[]
    1110    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)]
    1531    except:
    16         print >> sys.stderr,"Error"
     32        print >> sys.stderr,"Unable to load file from mem buffer\n\n\n"
    1733    return geometry
    1834
    19 def extractInputs(obj):
     35def extractInputs(conf,obj):
    2036    if obj["mimeType"]=="application/json":
    2137        return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
    2238    else:
    2339        try:
    24                 return createGeometryFromWFS(obj["value"])
     40                return createGeometryFromWFS(conf,obj["value"])
    2541        except:
    2642                return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
     
    3450        extension = [ ".js" ]
    3551    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] )
    3758    lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown )
    3859    field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString )
     
    4970        i+=1
    5071    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   
    5587
    5688def BoundaryPy(conf,inputs,outputs):
    57     geometry=extractInputs(inputs["InputPolygon"])
     89    geometry=extractInputs(conf,inputs["InputPolygon"])
    5890    i=0
    5991    rgeometries=[]
     
    6698
    6799def CentroidPy(conf,inputs,outputs):
    68     geometry=extractInputs(inputs["InputPolygon"])
     100    geometry=extractInputs(conf,inputs["InputPolygon"])
    69101    i=0
    70102    rgeometries=[]
     
    79111
    80112def ConvexHullPy(conf,inputs,outputs):
    81     geometry=extractInputs(inputs["InputPolygon"])
     113    geometry=extractInputs(conf,inputs["InputPolygon"])
    82114    i=0
    83115    rgeometries=[]
     
    94126    except:
    95127        bdist=10
    96     geometry=extractInputs(inputs["InputPolygon"])
     128    geometry=extractInputs(conf,inputs["InputPolygon"])
    97129    i=0
    98130    rgeometries=[]
     
    106138
    107139def 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"])
    110142    rgeometries=[]
    111143    i=0
     
    127159
    128160def 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"])
    131163    rgeometries=[]
    132164    i=0
     
    148180
    149181def 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"])
    152184    rgeometries=[]
    153185    i=0
     
    169201
    170202def 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"])
    173205    rgeometries=[]
    174206    i=0
Note: See TracChangeset for help on using the changeset viewer.

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