Ignore:
Timestamp:
Jun 7, 2012, 10:54:31 PM (12 years ago)
Author:
djay
Message:

Add support for multiple inputs values for the same identifier.

File:
1 edited

Legend:

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

    r348 r360  
    66import sys
    77
     8def readFileFromBuffer(data,ext):
     9    geometry=[]
     10    osgeo.gdal.FileFromMemBuffer('/vsimem//temp1'+ext,data)
     11    ds = osgeo.ogr.Open('/vsimem//temp1'+ext)
     12    lyr = ds.GetLayer(0)
     13    feat = lyr.GetNextFeature()
     14    while feat is not None:
     15        geometry+=[feat.Clone()]
     16        feat.Destroy()
     17        feat = lyr.GetNextFeature()
     18    ds.Destroy()
     19    osgeo.gdal.Unlink('/vsimem//temp1'+ext)
     20    return geometry
     21
     22def buildFeatureFromGeomtry(conf,geom,driverName,ext):
     23    drv = osgeo.ogr.GetDriverByName( driverName )
     24    ds = drv.CreateDataSource( "/vsimem//store"+conf["lenv"]["sid"]+"0."+ext )
     25    lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown )
     26    field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString )
     27    field_defn.SetWidth( len("Result10000") )
     28    lyr.CreateField ( field_defn )
     29    feat = osgeo.ogr.Feature(lyr.GetLayerDefn())
     30    feat.SetField( "Name", "Input0" )
     31    feat.SetGeometry(geom)
     32    lyr.CreateFeature(feat)
     33    ds.Destroy()
     34    return [feat]
     35
    836def 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')
     37    try:
     38        geom=osgeo.ogr.CreateGeometryFromGML(my_wfs_response.replace('<?xml version="1.0" encoding="utf-8"?>\n',''))
     39    except:
     40        geom=None
     41    try:
     42        if geom is None:
     43            return readFileFromBuffer(my_wfs_response,"")
    2444        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
     45            return buildFeatureFromGeomtry(conf,geom,"GML","xml")
     46    except:
     47        print >> sys.stderr,"Unable to load file input data !!!\n\n\n"
     48
     49def createLayerFromJson(conf,obj):
     50    geom=osgeo.ogr.CreateGeometryFromJson(obj)
     51    if geom is None:
     52        return readFileFromBuffer(obj,".json")
     53    else:
     54        return buildFeatureFromGeomtry(conf,geom,"GeoJSON","json")
    3455
    3556def extractInputs(conf,obj):
    3657    if obj["mimeType"]=="application/json":
    37         return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
     58        return createLayerFromJson(conf,obj["value"])
    3859    else:
    39         try:
    40                 return createGeometryFromWFS(conf,obj["value"])
    41         except:
    42                 return [osgeo.ogr.CreateGeometryFromJson(obj["value"])]
    43     return null
     60        return createGeometryFromWFS(conf,obj["value"])
    4461   
    4562def outputResult(conf,obj,geom):
     
    4966        driverName = "GeoJSON"
    5067        extension = [ ".js" ]
     68    if obj.keys().count("schema")>0 and \
     69            obj["schema"]=="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd":
     70        driverName = "KML"
     71        extension = [ ".kml" ]
    5172    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] )
     73    # Create virtual file
     74    ds = drv.CreateDataSource( "/vsimem/store"+conf["lenv"]["sid"]+extension[0] )
    5875    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 )
    6276    i=0
    6377    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()
     78        lyr.CreateFeature(geom[i])
    6979        geom[i].Destroy()
    7080        i+=1
    7181    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])
     82    vsiFile=osgeo.gdal.VSIFOpenL("/vsimem/store"+conf["lenv"]["sid"]+extension[0],"r")
     83    i=0
     84    while osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_END)>0:
     85        i+=1
     86    fileSize=osgeo.gdal.VSIFTellL(vsiFile)
     87    osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_SET)
     88    obj["value"]=osgeo.gdal.VSIFReadL(fileSize,1,vsiFile)
     89    osgeo.gdal.Unlink("/vsimem/store"+conf["lenv"]["sid"]+extension[0])
     90
     91def BufferPy(conf,inputs,outputs):
     92    print >> sys.stderr, inputs
     93    print >> sys.stderr, outputs
     94    try:
     95        bdist=float(inputs["BufferDistance"]["value"])
     96    except:
     97        bdist=1
     98    print >> sys.stderr, bdist
    8699   
     100    geometry=extractInputs(conf,inputs["InputPolygon"])
     101    i=0
     102    rgeometries=[]
     103    while i < len(geometry):
     104        tmp=geometry[i].Clone()
     105        resg=geometry[i].GetGeometryRef().Buffer(bdist)
     106        tmp.SetGeometryDirectly(resg)
     107        rgeometries+=[tmp]
     108        geometry[i].Destroy()
     109        resg.thisown=False
     110        tmp.thisown=False
     111        i+=1
     112    outputResult(conf,outputs["Result"],rgeometries)
     113    i=0
     114    return 3
    87115
    88116def BoundaryPy(conf,inputs,outputs):
     
    91119    rgeometries=[]
    92120    while i < len(geometry):
    93         rgeometries+=[geometry[i].GetBoundary()]
     121        tmp=geometry[i].Clone()
     122        resg=geometry[i].GetGeometryRef()
     123        resg=resg.GetBoundary()
     124        tmp.SetGeometryDirectly(resg)
     125        rgeometries+=[tmp]
    94126        geometry[i].Destroy()
    95127        i+=1
     
    102134    rgeometries=[]
    103135    while i < len(geometry):
    104         if geometry[i].GetGeometryType()!=3:
    105             geometry[i]=geometry[i].ConvexHull()
    106         rgeometries+=[geometry[i].Centroid()]
     136        tmp=geometry[i].Clone()
     137        resg=geometry[i].GetGeometryRef()
     138        if resg.GetGeometryType()!=3:
     139            resg=resg.ConvexHull()
     140        resg=resg.Centroid()
     141        tmp.SetGeometryDirectly(resg)
     142        rgeometries+=[tmp]
    107143        geometry[i].Destroy()
    108144        i+=1
     
    115151    rgeometries=[]
    116152    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 
    123 def BufferPy(conf,inputs,outputs):
     153        tmp=geometry[i].Clone()
     154        resg=geometry[i].GetGeometryRef().ConvexHull()
     155        tmp.SetGeometryDirectly(resg)
     156        rgeometries+=[tmp]
     157        geometry[i].Destroy()
     158        i+=1
     159    outputResult(conf,outputs["Result"],rgeometries)
     160    return 3
     161
     162
     163
     164def EnvelopePy(conf,inputs,outputs):
     165    print >> sys.stderr, inputs
    124166    try:
    125167        bdist=float(inputs["BufferDistance"]["value"])
     
    127169        bdist=10
    128170    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
     171    tmp=geometry[0].GetGeometryRef().GetEnvelope()
     172    outputs["Result"]["value"]=str(tmp[0])+','+str(tmp[2])+','+str(tmp[1])+','+str(tmp[3])+','+'urn:ogc:def:crs:OGC:1.3:CRS84'
     173    print >> sys.stderr,outputs["Result"]
    137174    return 3
    138175
     
    159196
    160197def 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)
     198
     199    geometry1=extractInputs(conf,inputs["InputEntity1"])
     200    print >> sys.stderr,inputs["InputEntity2"]
     201    geometry2=extractInputs(conf,inputs["InputEntity2"])
     202
     203    rgeometries=[]
     204    i=0
     205    while i < len(geometry1):
     206        j=0
     207        while j < len(geometry2):
     208            tmp=geometry2[j].Clone()
     209            resg=geometry2[j].GetGeometryRef()
     210            #resg=resg.Intersection(geometry1[i].GetGeometryRef())
     211            resg=geometry1[i].GetGeometryRef().Intersection(resg)
     212            tmp.SetGeometryDirectly(resg)
     213            if not(resg.IsEmpty()):
     214                rgeometries+=[tmp]
     215            j+=1
     216        geometry1[i].Destroy()
     217        i+=1
     218    i=0
     219    while i < len(geometry2):
     220        geometry2[i].Destroy()
     221        i+=1
     222    outputResult(conf,outputs["Result"],rgeometries)
     223    print >> sys.stderr,"/outputResult"
    179224    return 3
    180225
     
    187232        j=0
    188233        while j < len(geometry2):
    189             tres=geometry1[i].Difference(geometry2[j])
    190             if not(tres.IsEmpty()):
    191                 rgeometries+=[tres]
     234            tmp=geometry2[j].Clone()
     235            resg=geometry1[i].GetGeometryRef()
     236            resg=resg.Difference(geometry2[i].GetGeometryRef())
     237            tmp.SetGeometryDirectly(resg)
     238            if not(resg.IsEmpty()):
     239                rgeometries+=[tmp]
    192240            j+=1
    193241        geometry1[i].Destroy()
     
    208256        j=0
    209257        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
     258            tmp=geometry2[j].Clone()
     259            resg=geometry1[i].GetGeometryRef()
     260            resg=resg.SymmetricDifference(geometry2[i].GetGeometryRef())
     261            tmp.SetGeometryDirectly(resg)
     262            rgeometries+=[tmp]
     263            j+=1
     264        geometry1[i].Destroy()
     265        i+=1
     266    i=0
     267    while i < len(geometry2):
     268        geometry2[i].Destroy()
     269        i+=1
     270    outputResult(conf,outputs["Result"],rgeometries)
     271    return 3
     272
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