[257] | 1 | from osgeo import * |
---|
[9] | 2 | import osgeo.ogr |
---|
[257] | 3 | import osgeo.gdal |
---|
[9] | 4 | import libxml2 |
---|
[106] | 5 | import os |
---|
| 6 | import sys |
---|
[9] | 7 | |
---|
[257] | 8 | def createGeometryFromWFS(conf,my_wfs_response): |
---|
[106] | 9 | geometry=[] |
---|
| 10 | try: |
---|
[257] | 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)] |
---|
[106] | 31 | except: |
---|
[257] | 32 | print >> sys.stderr,"Unable to load file from mem buffer\n\n\n" |
---|
[9] | 33 | return geometry |
---|
| 34 | |
---|
[257] | 35 | def extractInputs(conf,obj): |
---|
[9] | 36 | if obj["mimeType"]=="application/json": |
---|
[106] | 37 | return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] |
---|
[9] | 38 | else: |
---|
| 39 | try: |
---|
[257] | 40 | return createGeometryFromWFS(conf,obj["value"]) |
---|
[9] | 41 | except: |
---|
[106] | 42 | return [osgeo.ogr.CreateGeometryFromJson(obj["value"])] |
---|
[9] | 43 | return null |
---|
| 44 | |
---|
[106] | 45 | def outputResult(conf,obj,geom): |
---|
| 46 | driverName = "GML" |
---|
| 47 | extension = [ ".xml" , ".xsd" ] |
---|
[9] | 48 | if obj["mimeType"]=="application/json": |
---|
[106] | 49 | driverName = "GeoJSON" |
---|
| 50 | extension = [ ".js" ] |
---|
| 51 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
[257] | 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] ) |
---|
[106] | 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() |
---|
[257] | 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 | |
---|
[9] | 87 | |
---|
| 88 | def BoundaryPy(conf,inputs,outputs): |
---|
[257] | 89 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 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) |
---|
[9] | 97 | return 3 |
---|
| 98 | |
---|
| 99 | def CentroidPy(conf,inputs,outputs): |
---|
[257] | 100 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 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) |
---|
[9] | 110 | return 3 |
---|
| 111 | |
---|
| 112 | def ConvexHullPy(conf,inputs,outputs): |
---|
[257] | 113 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 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) |
---|
[9] | 121 | return 3 |
---|
| 122 | |
---|
| 123 | def BufferPy(conf,inputs,outputs): |
---|
| 124 | try: |
---|
[107] | 125 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
[9] | 126 | except: |
---|
| 127 | bdist=10 |
---|
[257] | 128 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 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 |
---|
[9] | 137 | return 3 |
---|
| 138 | |
---|
| 139 | def UnionPy(conf,inputs,outputs): |
---|
[257] | 140 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 141 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 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) |
---|
[9] | 158 | return 3 |
---|
| 159 | |
---|
| 160 | def IntersectionPy(conf,inputs,outputs): |
---|
[257] | 161 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 162 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 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) |
---|
[9] | 179 | return 3 |
---|
| 180 | |
---|
| 181 | def DifferencePy(conf,inputs,outputs): |
---|
[257] | 182 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 183 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 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) |
---|
[9] | 200 | return 3 |
---|
| 201 | |
---|
| 202 | def SymDifferencePy(conf,inputs,outputs): |
---|
[257] | 203 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 204 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 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) |
---|
[9] | 219 | return 3 |
---|