[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 |
---|
[382] | 7 | import zoo |
---|
[9] | 8 | |
---|
[360] | 9 | def readFileFromBuffer(data,ext): |
---|
[382] | 10 | try: |
---|
| 11 | geometry=[] |
---|
| 12 | print >> sys.stderr,'/vsimem//temp1'+ext |
---|
| 13 | #print >> sys.stderr,data |
---|
| 14 | osgeo.gdal.FileFromMemBuffer('/vsimem//temp1'+ext,data) |
---|
| 15 | ds = osgeo.ogr.Open('/vsimem//temp1'+ext) |
---|
| 16 | lyr = ds.GetLayer(0) |
---|
[360] | 17 | feat = lyr.GetNextFeature() |
---|
[382] | 18 | while feat is not None: |
---|
| 19 | geometry+=[feat.Clone()] |
---|
| 20 | feat.Destroy() |
---|
| 21 | feat = lyr.GetNextFeature() |
---|
| 22 | ds.Destroy() |
---|
| 23 | osgeo.gdal.Unlink('/vsimem//temp1'+ext) |
---|
| 24 | return geometry |
---|
| 25 | except Exception,e: |
---|
| 26 | print >> sys.stderr,e |
---|
| 27 | return [] |
---|
[360] | 28 | |
---|
| 29 | def buildFeatureFromGeomtry(conf,geom,driverName,ext): |
---|
| 30 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
| 31 | ds = drv.CreateDataSource( "/vsimem//store"+conf["lenv"]["sid"]+"0."+ext ) |
---|
| 32 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
| 33 | field_defn = osgeo.ogr.FieldDefn( "Name", osgeo.ogr.OFTString ) |
---|
| 34 | field_defn.SetWidth( len("Result10000") ) |
---|
| 35 | lyr.CreateField ( field_defn ) |
---|
| 36 | feat = osgeo.ogr.Feature(lyr.GetLayerDefn()) |
---|
| 37 | feat.SetField( "Name", "Input0" ) |
---|
| 38 | feat.SetGeometry(geom) |
---|
| 39 | lyr.CreateFeature(feat) |
---|
| 40 | ds.Destroy() |
---|
| 41 | return [feat] |
---|
| 42 | |
---|
[257] | 43 | def createGeometryFromWFS(conf,my_wfs_response): |
---|
[106] | 44 | try: |
---|
[360] | 45 | geom=osgeo.ogr.CreateGeometryFromGML(my_wfs_response.replace('<?xml version="1.0" encoding="utf-8"?>\n','')) |
---|
| 46 | except: |
---|
| 47 | geom=None |
---|
| 48 | try: |
---|
| 49 | if geom is None: |
---|
[382] | 50 | if not(conf["lenv"].has_key("cnt")): |
---|
| 51 | conf["lenv"]["cnt"]=0 |
---|
| 52 | else: |
---|
| 53 | conf["lenv"]["cnt"]+=1 |
---|
| 54 | return readFileFromBuffer(my_wfs_response,str(conf["lenv"]["cnt"])) |
---|
[257] | 55 | else: |
---|
[360] | 56 | return buildFeatureFromGeomtry(conf,geom,"GML","xml") |
---|
[106] | 57 | except: |
---|
[360] | 58 | print >> sys.stderr,"Unable to load file input data !!!\n\n\n" |
---|
[9] | 59 | |
---|
[360] | 60 | def createLayerFromJson(conf,obj): |
---|
| 61 | geom=osgeo.ogr.CreateGeometryFromJson(obj) |
---|
| 62 | if geom is None: |
---|
| 63 | return readFileFromBuffer(obj,".json") |
---|
| 64 | else: |
---|
| 65 | return buildFeatureFromGeomtry(conf,geom,"GeoJSON","json") |
---|
| 66 | |
---|
[257] | 67 | def extractInputs(conf,obj): |
---|
[9] | 68 | if obj["mimeType"]=="application/json": |
---|
[360] | 69 | return createLayerFromJson(conf,obj["value"]) |
---|
[9] | 70 | else: |
---|
[360] | 71 | return createGeometryFromWFS(conf,obj["value"]) |
---|
[9] | 72 | |
---|
[106] | 73 | def outputResult(conf,obj,geom): |
---|
| 74 | driverName = "GML" |
---|
| 75 | extension = [ ".xml" , ".xsd" ] |
---|
[9] | 76 | if obj["mimeType"]=="application/json": |
---|
[106] | 77 | driverName = "GeoJSON" |
---|
| 78 | extension = [ ".js" ] |
---|
[360] | 79 | if obj.keys().count("schema")>0 and \ |
---|
| 80 | obj["schema"]=="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd": |
---|
| 81 | driverName = "KML" |
---|
| 82 | extension = [ ".kml" ] |
---|
[106] | 83 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
[360] | 84 | # Create virtual file |
---|
| 85 | ds = drv.CreateDataSource( "/vsimem/store"+conf["lenv"]["sid"]+extension[0] ) |
---|
[106] | 86 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
| 87 | i=0 |
---|
| 88 | while i < len(geom): |
---|
[382] | 89 | if i==0 and driverName!="GeoJSON": |
---|
| 90 | poDstFDefn=geom[i].GetDefnRef() |
---|
| 91 | if poDstFDefn is not None: |
---|
| 92 | nDstFieldCount = poDstFDefn.GetFieldCount() |
---|
| 93 | for iField in range(nDstFieldCount): |
---|
| 94 | poSrcFieldDefn = poDstFDefn.GetFieldDefn(iField) |
---|
| 95 | oFieldDefn = osgeo.ogr.FieldDefn(poSrcFieldDefn.GetNameRef(),poSrcFieldDefn.GetType()) |
---|
| 96 | oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() ) |
---|
| 97 | oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() ) |
---|
| 98 | lyr.CreateField( oFieldDefn ) |
---|
[360] | 99 | lyr.CreateFeature(geom[i]) |
---|
[106] | 100 | geom[i].Destroy() |
---|
| 101 | i+=1 |
---|
| 102 | ds.Destroy() |
---|
[360] | 103 | vsiFile=osgeo.gdal.VSIFOpenL("/vsimem/store"+conf["lenv"]["sid"]+extension[0],"r") |
---|
| 104 | i=0 |
---|
| 105 | while osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_END)>0: |
---|
| 106 | i+=1 |
---|
| 107 | fileSize=osgeo.gdal.VSIFTellL(vsiFile) |
---|
| 108 | osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_SET) |
---|
| 109 | obj["value"]=osgeo.gdal.VSIFReadL(fileSize,1,vsiFile) |
---|
| 110 | osgeo.gdal.Unlink("/vsimem/store"+conf["lenv"]["sid"]+extension[0]) |
---|
| 111 | |
---|
| 112 | def BufferPy(conf,inputs,outputs): |
---|
[382] | 113 | print >> sys.stderr, "Starting service ..." |
---|
[360] | 114 | try: |
---|
| 115 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
| 116 | except: |
---|
| 117 | bdist=1 |
---|
| 118 | print >> sys.stderr, bdist |
---|
| 119 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
| 120 | i=0 |
---|
| 121 | rgeometries=[] |
---|
| 122 | while i < len(geometry): |
---|
| 123 | tmp=geometry[i].Clone() |
---|
| 124 | resg=geometry[i].GetGeometryRef().Buffer(bdist) |
---|
| 125 | tmp.SetGeometryDirectly(resg) |
---|
| 126 | rgeometries+=[tmp] |
---|
| 127 | geometry[i].Destroy() |
---|
| 128 | resg.thisown=False |
---|
| 129 | tmp.thisown=False |
---|
| 130 | i+=1 |
---|
| 131 | outputResult(conf,outputs["Result"],rgeometries) |
---|
| 132 | i=0 |
---|
[382] | 133 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 134 | |
---|
| 135 | def BoundaryPy(conf,inputs,outputs): |
---|
[257] | 136 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 137 | i=0 |
---|
| 138 | rgeometries=[] |
---|
| 139 | while i < len(geometry): |
---|
[360] | 140 | tmp=geometry[i].Clone() |
---|
| 141 | resg=geometry[i].GetGeometryRef() |
---|
| 142 | resg=resg.GetBoundary() |
---|
| 143 | tmp.SetGeometryDirectly(resg) |
---|
| 144 | rgeometries+=[tmp] |
---|
[106] | 145 | geometry[i].Destroy() |
---|
| 146 | i+=1 |
---|
| 147 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[382] | 148 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 149 | |
---|
| 150 | def CentroidPy(conf,inputs,outputs): |
---|
[257] | 151 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 152 | i=0 |
---|
| 153 | rgeometries=[] |
---|
| 154 | while i < len(geometry): |
---|
[360] | 155 | tmp=geometry[i].Clone() |
---|
| 156 | resg=geometry[i].GetGeometryRef() |
---|
| 157 | if resg.GetGeometryType()!=3: |
---|
| 158 | resg=resg.ConvexHull() |
---|
| 159 | resg=resg.Centroid() |
---|
| 160 | tmp.SetGeometryDirectly(resg) |
---|
| 161 | rgeometries+=[tmp] |
---|
[106] | 162 | geometry[i].Destroy() |
---|
| 163 | i+=1 |
---|
| 164 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[388] | 165 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 166 | |
---|
| 167 | def ConvexHullPy(conf,inputs,outputs): |
---|
[257] | 168 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 169 | i=0 |
---|
| 170 | rgeometries=[] |
---|
| 171 | while i < len(geometry): |
---|
[360] | 172 | tmp=geometry[i].Clone() |
---|
| 173 | resg=geometry[i].GetGeometryRef().ConvexHull() |
---|
| 174 | tmp.SetGeometryDirectly(resg) |
---|
| 175 | rgeometries+=[tmp] |
---|
[106] | 176 | geometry[i].Destroy() |
---|
| 177 | i+=1 |
---|
| 178 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[388] | 179 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 180 | |
---|
[360] | 181 | |
---|
| 182 | |
---|
| 183 | def EnvelopePy(conf,inputs,outputs): |
---|
| 184 | print >> sys.stderr, inputs |
---|
[9] | 185 | try: |
---|
[107] | 186 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
[9] | 187 | except: |
---|
| 188 | bdist=10 |
---|
[257] | 189 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[360] | 190 | tmp=geometry[0].GetGeometryRef().GetEnvelope() |
---|
| 191 | outputs["Result"]["value"]=str(tmp[0])+','+str(tmp[2])+','+str(tmp[1])+','+str(tmp[3])+','+'urn:ogc:def:crs:OGC:1.3:CRS84' |
---|
| 192 | print >> sys.stderr,outputs["Result"] |
---|
[9] | 193 | return 3 |
---|
| 194 | |
---|
| 195 | def UnionPy(conf,inputs,outputs): |
---|
[257] | 196 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 197 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 198 | rgeometries=[] |
---|
| 199 | i=0 |
---|
| 200 | while i < len(geometry1): |
---|
| 201 | j=0 |
---|
| 202 | while j < len(geometry2): |
---|
[389] | 203 | tmp=geometry1[i].Clone() |
---|
| 204 | resg=geometry2[j].GetGeometryRef() |
---|
[388] | 205 | resg=resg.Union(geometry1[i].GetGeometryRef()) |
---|
| 206 | tmp.SetGeometryDirectly(resg) |
---|
| 207 | if not(resg.IsEmpty()): |
---|
| 208 | rgeometries+=[tmp] |
---|
[106] | 209 | j+=1 |
---|
| 210 | geometry1[i].Destroy() |
---|
| 211 | i+=1 |
---|
| 212 | i=0 |
---|
| 213 | while i < len(geometry2): |
---|
| 214 | geometry2[i].Destroy() |
---|
| 215 | i+=1 |
---|
| 216 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 217 | return 3 |
---|
| 218 | |
---|
| 219 | def IntersectionPy(conf,inputs,outputs): |
---|
[360] | 220 | |
---|
[257] | 221 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 222 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[360] | 223 | |
---|
[382] | 224 | print >> sys.stderr,str(len(geometry1))+" "+str(len(geometry2)) |
---|
| 225 | |
---|
[106] | 226 | rgeometries=[] |
---|
[382] | 227 | fids=[] |
---|
[106] | 228 | i=0 |
---|
| 229 | while i < len(geometry1): |
---|
| 230 | j=0 |
---|
| 231 | while j < len(geometry2): |
---|
[360] | 232 | tmp=geometry2[j].Clone() |
---|
| 233 | resg=geometry2[j].GetGeometryRef() |
---|
| 234 | #resg=resg.Intersection(geometry1[i].GetGeometryRef()) |
---|
| 235 | resg=geometry1[i].GetGeometryRef().Intersection(resg) |
---|
| 236 | tmp.SetGeometryDirectly(resg) |
---|
[382] | 237 | if resg is not None and not(resg.IsEmpty()) and fids.count(tmp.GetFID())==0: |
---|
[360] | 238 | rgeometries+=[tmp] |
---|
[382] | 239 | fids+=[tmp.GetFID()] |
---|
| 240 | else: |
---|
| 241 | tmp.Destroy() |
---|
[106] | 242 | j+=1 |
---|
| 243 | geometry1[i].Destroy() |
---|
| 244 | i+=1 |
---|
| 245 | i=0 |
---|
| 246 | while i < len(geometry2): |
---|
| 247 | geometry2[i].Destroy() |
---|
| 248 | i+=1 |
---|
| 249 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[360] | 250 | print >> sys.stderr,"/outputResult" |
---|
[9] | 251 | return 3 |
---|
| 252 | |
---|
| 253 | def DifferencePy(conf,inputs,outputs): |
---|
[257] | 254 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 255 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 256 | rgeometries=[] |
---|
| 257 | i=0 |
---|
| 258 | while i < len(geometry1): |
---|
| 259 | j=0 |
---|
| 260 | while j < len(geometry2): |
---|
[360] | 261 | tmp=geometry2[j].Clone() |
---|
| 262 | resg=geometry1[i].GetGeometryRef() |
---|
| 263 | resg=resg.Difference(geometry2[i].GetGeometryRef()) |
---|
| 264 | tmp.SetGeometryDirectly(resg) |
---|
| 265 | if not(resg.IsEmpty()): |
---|
| 266 | rgeometries+=[tmp] |
---|
[106] | 267 | j+=1 |
---|
| 268 | geometry1[i].Destroy() |
---|
| 269 | i+=1 |
---|
| 270 | i=0 |
---|
| 271 | while i < len(geometry2): |
---|
| 272 | geometry2[i].Destroy() |
---|
| 273 | i+=1 |
---|
| 274 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 275 | return 3 |
---|
| 276 | |
---|
| 277 | def SymDifferencePy(conf,inputs,outputs): |
---|
[257] | 278 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 279 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 280 | rgeometries=[] |
---|
| 281 | i=0 |
---|
| 282 | while i < len(geometry1): |
---|
| 283 | j=0 |
---|
| 284 | while j < len(geometry2): |
---|
[360] | 285 | tmp=geometry2[j].Clone() |
---|
| 286 | resg=geometry1[i].GetGeometryRef() |
---|
| 287 | resg=resg.SymmetricDifference(geometry2[i].GetGeometryRef()) |
---|
| 288 | tmp.SetGeometryDirectly(resg) |
---|
| 289 | rgeometries+=[tmp] |
---|
[106] | 290 | j+=1 |
---|
| 291 | geometry1[i].Destroy() |
---|
| 292 | i+=1 |
---|
| 293 | i=0 |
---|
| 294 | while i < len(geometry2): |
---|
| 295 | geometry2[i].Destroy() |
---|
| 296 | i+=1 |
---|
| 297 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 298 | return 3 |
---|
[360] | 299 | |
---|