[447] | 1 | # -*- coding: utf-8 -*- |
---|
[422] | 2 | # |
---|
| 3 | # Author : Gérald FENOY |
---|
| 4 | # |
---|
| 5 | # Copyright 2009-2013 GeoLabs SARL. All rights reserved. |
---|
| 6 | # |
---|
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
---|
| 8 | # copy of this software and associated documentation files (the |
---|
| 9 | # "Software"), to deal in the Software without restriction, including with |
---|
| 10 | # out limitation the rights to use, copy, modify, merge, publish, |
---|
| 11 | # distribute, sublicense, and/or sell copies of the Software, and to |
---|
| 12 | # permit persons to whom the Software is furnished to do so, subject to |
---|
| 13 | # the following conditions: |
---|
| 14 | # |
---|
| 15 | # The above copyright notice and this permission notice shall be included |
---|
| 16 | # in all copies or substantial portions of the Software. |
---|
| 17 | # |
---|
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
| 19 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
| 20 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
| 21 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
---|
| 22 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
| 23 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
---|
| 24 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
| 25 | # |
---|
| 26 | |
---|
[257] | 27 | from osgeo import * |
---|
[9] | 28 | import osgeo.ogr |
---|
[257] | 29 | import osgeo.gdal |
---|
[9] | 30 | import libxml2 |
---|
[106] | 31 | import os |
---|
| 32 | import sys |
---|
[382] | 33 | import zoo |
---|
[9] | 34 | |
---|
[360] | 35 | def readFileFromBuffer(data,ext): |
---|
[382] | 36 | try: |
---|
| 37 | geometry=[] |
---|
| 38 | print >> sys.stderr,'/vsimem//temp1'+ext |
---|
| 39 | #print >> sys.stderr,data |
---|
| 40 | osgeo.gdal.FileFromMemBuffer('/vsimem//temp1'+ext,data) |
---|
| 41 | ds = osgeo.ogr.Open('/vsimem//temp1'+ext) |
---|
| 42 | lyr = ds.GetLayer(0) |
---|
[360] | 43 | feat = lyr.GetNextFeature() |
---|
[382] | 44 | while feat is not None: |
---|
| 45 | geometry+=[feat.Clone()] |
---|
| 46 | feat.Destroy() |
---|
| 47 | feat = lyr.GetNextFeature() |
---|
| 48 | ds.Destroy() |
---|
| 49 | osgeo.gdal.Unlink('/vsimem//temp1'+ext) |
---|
| 50 | return geometry |
---|
| 51 | except Exception,e: |
---|
| 52 | print >> sys.stderr,e |
---|
| 53 | return [] |
---|
[360] | 54 | |
---|
| 55 | def buildFeatureFromGeomtry(conf,geom,driverName,ext): |
---|
| 56 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
| 57 | ds = drv.CreateDataSource( "/vsimem//store"+conf["lenv"]["sid"]+"0."+ext ) |
---|
| 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 | feat = osgeo.ogr.Feature(lyr.GetLayerDefn()) |
---|
| 63 | feat.SetField( "Name", "Input0" ) |
---|
| 64 | feat.SetGeometry(geom) |
---|
| 65 | lyr.CreateFeature(feat) |
---|
| 66 | ds.Destroy() |
---|
| 67 | return [feat] |
---|
| 68 | |
---|
[257] | 69 | def createGeometryFromWFS(conf,my_wfs_response): |
---|
[106] | 70 | try: |
---|
[360] | 71 | geom=osgeo.ogr.CreateGeometryFromGML(my_wfs_response.replace('<?xml version="1.0" encoding="utf-8"?>\n','')) |
---|
| 72 | except: |
---|
| 73 | geom=None |
---|
| 74 | try: |
---|
| 75 | if geom is None: |
---|
[382] | 76 | if not(conf["lenv"].has_key("cnt")): |
---|
| 77 | conf["lenv"]["cnt"]=0 |
---|
| 78 | else: |
---|
| 79 | conf["lenv"]["cnt"]+=1 |
---|
| 80 | return readFileFromBuffer(my_wfs_response,str(conf["lenv"]["cnt"])) |
---|
[257] | 81 | else: |
---|
[360] | 82 | return buildFeatureFromGeomtry(conf,geom,"GML","xml") |
---|
[106] | 83 | except: |
---|
[360] | 84 | print >> sys.stderr,"Unable to load file input data !!!\n\n\n" |
---|
[9] | 85 | |
---|
[360] | 86 | def createLayerFromJson(conf,obj): |
---|
| 87 | geom=osgeo.ogr.CreateGeometryFromJson(obj) |
---|
| 88 | if geom is None: |
---|
| 89 | return readFileFromBuffer(obj,".json") |
---|
| 90 | else: |
---|
| 91 | return buildFeatureFromGeomtry(conf,geom,"GeoJSON","json") |
---|
| 92 | |
---|
[257] | 93 | def extractInputs(conf,obj): |
---|
[9] | 94 | if obj["mimeType"]=="application/json": |
---|
[360] | 95 | return createLayerFromJson(conf,obj["value"]) |
---|
[9] | 96 | else: |
---|
[360] | 97 | return createGeometryFromWFS(conf,obj["value"]) |
---|
[9] | 98 | |
---|
[106] | 99 | def outputResult(conf,obj,geom): |
---|
| 100 | driverName = "GML" |
---|
| 101 | extension = [ ".xml" , ".xsd" ] |
---|
[9] | 102 | if obj["mimeType"]=="application/json": |
---|
[106] | 103 | driverName = "GeoJSON" |
---|
| 104 | extension = [ ".js" ] |
---|
[360] | 105 | if obj.keys().count("schema")>0 and \ |
---|
| 106 | obj["schema"]=="http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd": |
---|
| 107 | driverName = "KML" |
---|
| 108 | extension = [ ".kml" ] |
---|
[106] | 109 | drv = osgeo.ogr.GetDriverByName( driverName ) |
---|
[360] | 110 | # Create virtual file |
---|
| 111 | ds = drv.CreateDataSource( "/vsimem/store"+conf["lenv"]["sid"]+extension[0] ) |
---|
[106] | 112 | lyr = ds.CreateLayer( "Result", None, osgeo.ogr.wkbUnknown ) |
---|
| 113 | i=0 |
---|
| 114 | while i < len(geom): |
---|
[382] | 115 | if i==0 and driverName!="GeoJSON": |
---|
| 116 | poDstFDefn=geom[i].GetDefnRef() |
---|
| 117 | if poDstFDefn is not None: |
---|
| 118 | nDstFieldCount = poDstFDefn.GetFieldCount() |
---|
| 119 | for iField in range(nDstFieldCount): |
---|
| 120 | poSrcFieldDefn = poDstFDefn.GetFieldDefn(iField) |
---|
| 121 | oFieldDefn = osgeo.ogr.FieldDefn(poSrcFieldDefn.GetNameRef(),poSrcFieldDefn.GetType()) |
---|
| 122 | oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() ) |
---|
| 123 | oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() ) |
---|
| 124 | lyr.CreateField( oFieldDefn ) |
---|
[360] | 125 | lyr.CreateFeature(geom[i]) |
---|
[106] | 126 | geom[i].Destroy() |
---|
| 127 | i+=1 |
---|
| 128 | ds.Destroy() |
---|
[360] | 129 | vsiFile=osgeo.gdal.VSIFOpenL("/vsimem/store"+conf["lenv"]["sid"]+extension[0],"r") |
---|
| 130 | i=0 |
---|
| 131 | while osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_END)>0: |
---|
| 132 | i+=1 |
---|
| 133 | fileSize=osgeo.gdal.VSIFTellL(vsiFile) |
---|
| 134 | osgeo.gdal.VSIFSeekL(vsiFile,0,os.SEEK_SET) |
---|
| 135 | obj["value"]=osgeo.gdal.VSIFReadL(fileSize,1,vsiFile) |
---|
| 136 | osgeo.gdal.Unlink("/vsimem/store"+conf["lenv"]["sid"]+extension[0]) |
---|
| 137 | |
---|
| 138 | def BufferPy(conf,inputs,outputs): |
---|
[382] | 139 | print >> sys.stderr, "Starting service ..." |
---|
[360] | 140 | try: |
---|
| 141 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
| 142 | except: |
---|
| 143 | bdist=1 |
---|
| 144 | print >> sys.stderr, bdist |
---|
| 145 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
| 146 | i=0 |
---|
| 147 | rgeometries=[] |
---|
| 148 | while i < len(geometry): |
---|
| 149 | tmp=geometry[i].Clone() |
---|
| 150 | resg=geometry[i].GetGeometryRef().Buffer(bdist) |
---|
| 151 | tmp.SetGeometryDirectly(resg) |
---|
| 152 | rgeometries+=[tmp] |
---|
| 153 | geometry[i].Destroy() |
---|
| 154 | resg.thisown=False |
---|
| 155 | tmp.thisown=False |
---|
| 156 | i+=1 |
---|
| 157 | outputResult(conf,outputs["Result"],rgeometries) |
---|
| 158 | i=0 |
---|
[382] | 159 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 160 | |
---|
| 161 | def BoundaryPy(conf,inputs,outputs): |
---|
[257] | 162 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 163 | i=0 |
---|
| 164 | rgeometries=[] |
---|
| 165 | while i < len(geometry): |
---|
[360] | 166 | tmp=geometry[i].Clone() |
---|
| 167 | resg=geometry[i].GetGeometryRef() |
---|
| 168 | resg=resg.GetBoundary() |
---|
| 169 | tmp.SetGeometryDirectly(resg) |
---|
| 170 | rgeometries+=[tmp] |
---|
[106] | 171 | geometry[i].Destroy() |
---|
| 172 | i+=1 |
---|
| 173 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[382] | 174 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 175 | |
---|
| 176 | def CentroidPy(conf,inputs,outputs): |
---|
[257] | 177 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 178 | i=0 |
---|
| 179 | rgeometries=[] |
---|
| 180 | while i < len(geometry): |
---|
[360] | 181 | tmp=geometry[i].Clone() |
---|
| 182 | resg=geometry[i].GetGeometryRef() |
---|
| 183 | if resg.GetGeometryType()!=3: |
---|
| 184 | resg=resg.ConvexHull() |
---|
| 185 | resg=resg.Centroid() |
---|
| 186 | tmp.SetGeometryDirectly(resg) |
---|
| 187 | rgeometries+=[tmp] |
---|
[106] | 188 | geometry[i].Destroy() |
---|
| 189 | i+=1 |
---|
| 190 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[388] | 191 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 192 | |
---|
| 193 | def ConvexHullPy(conf,inputs,outputs): |
---|
[257] | 194 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[106] | 195 | i=0 |
---|
| 196 | rgeometries=[] |
---|
| 197 | while i < len(geometry): |
---|
[360] | 198 | tmp=geometry[i].Clone() |
---|
| 199 | resg=geometry[i].GetGeometryRef().ConvexHull() |
---|
| 200 | tmp.SetGeometryDirectly(resg) |
---|
| 201 | rgeometries+=[tmp] |
---|
[106] | 202 | geometry[i].Destroy() |
---|
| 203 | i+=1 |
---|
| 204 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[388] | 205 | return zoo.SERVICE_SUCCEEDED |
---|
[9] | 206 | |
---|
[360] | 207 | |
---|
| 208 | |
---|
| 209 | def EnvelopePy(conf,inputs,outputs): |
---|
| 210 | print >> sys.stderr, inputs |
---|
[9] | 211 | try: |
---|
[107] | 212 | bdist=float(inputs["BufferDistance"]["value"]) |
---|
[9] | 213 | except: |
---|
| 214 | bdist=10 |
---|
[257] | 215 | geometry=extractInputs(conf,inputs["InputPolygon"]) |
---|
[360] | 216 | tmp=geometry[0].GetGeometryRef().GetEnvelope() |
---|
| 217 | outputs["Result"]["value"]=str(tmp[0])+','+str(tmp[2])+','+str(tmp[1])+','+str(tmp[3])+','+'urn:ogc:def:crs:OGC:1.3:CRS84' |
---|
| 218 | print >> sys.stderr,outputs["Result"] |
---|
[9] | 219 | return 3 |
---|
| 220 | |
---|
| 221 | def UnionPy(conf,inputs,outputs): |
---|
[257] | 222 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 223 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 224 | rgeometries=[] |
---|
| 225 | i=0 |
---|
| 226 | while i < len(geometry1): |
---|
| 227 | j=0 |
---|
| 228 | while j < len(geometry2): |
---|
[389] | 229 | tmp=geometry1[i].Clone() |
---|
| 230 | resg=geometry2[j].GetGeometryRef() |
---|
[388] | 231 | resg=resg.Union(geometry1[i].GetGeometryRef()) |
---|
| 232 | tmp.SetGeometryDirectly(resg) |
---|
| 233 | if not(resg.IsEmpty()): |
---|
| 234 | rgeometries+=[tmp] |
---|
[106] | 235 | j+=1 |
---|
| 236 | geometry1[i].Destroy() |
---|
| 237 | i+=1 |
---|
| 238 | i=0 |
---|
| 239 | while i < len(geometry2): |
---|
| 240 | geometry2[i].Destroy() |
---|
| 241 | i+=1 |
---|
| 242 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 243 | return 3 |
---|
| 244 | |
---|
| 245 | def IntersectionPy(conf,inputs,outputs): |
---|
[360] | 246 | |
---|
[257] | 247 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 248 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[360] | 249 | |
---|
[382] | 250 | print >> sys.stderr,str(len(geometry1))+" "+str(len(geometry2)) |
---|
| 251 | |
---|
[106] | 252 | rgeometries=[] |
---|
[382] | 253 | fids=[] |
---|
[106] | 254 | i=0 |
---|
| 255 | while i < len(geometry1): |
---|
| 256 | j=0 |
---|
| 257 | while j < len(geometry2): |
---|
[360] | 258 | tmp=geometry2[j].Clone() |
---|
| 259 | resg=geometry2[j].GetGeometryRef() |
---|
| 260 | #resg=resg.Intersection(geometry1[i].GetGeometryRef()) |
---|
| 261 | resg=geometry1[i].GetGeometryRef().Intersection(resg) |
---|
| 262 | tmp.SetGeometryDirectly(resg) |
---|
[382] | 263 | if resg is not None and not(resg.IsEmpty()) and fids.count(tmp.GetFID())==0: |
---|
[360] | 264 | rgeometries+=[tmp] |
---|
[382] | 265 | fids+=[tmp.GetFID()] |
---|
| 266 | else: |
---|
| 267 | tmp.Destroy() |
---|
[106] | 268 | j+=1 |
---|
| 269 | geometry1[i].Destroy() |
---|
| 270 | i+=1 |
---|
| 271 | i=0 |
---|
| 272 | while i < len(geometry2): |
---|
| 273 | geometry2[i].Destroy() |
---|
| 274 | i+=1 |
---|
| 275 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[360] | 276 | print >> sys.stderr,"/outputResult" |
---|
[9] | 277 | return 3 |
---|
| 278 | |
---|
| 279 | def DifferencePy(conf,inputs,outputs): |
---|
[257] | 280 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 281 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 282 | rgeometries=[] |
---|
| 283 | i=0 |
---|
| 284 | while i < len(geometry1): |
---|
| 285 | j=0 |
---|
| 286 | while j < len(geometry2): |
---|
[360] | 287 | tmp=geometry2[j].Clone() |
---|
| 288 | resg=geometry1[i].GetGeometryRef() |
---|
| 289 | resg=resg.Difference(geometry2[i].GetGeometryRef()) |
---|
| 290 | tmp.SetGeometryDirectly(resg) |
---|
| 291 | if not(resg.IsEmpty()): |
---|
| 292 | rgeometries+=[tmp] |
---|
[106] | 293 | j+=1 |
---|
| 294 | geometry1[i].Destroy() |
---|
| 295 | i+=1 |
---|
| 296 | i=0 |
---|
| 297 | while i < len(geometry2): |
---|
| 298 | geometry2[i].Destroy() |
---|
| 299 | i+=1 |
---|
| 300 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 301 | return 3 |
---|
| 302 | |
---|
| 303 | def SymDifferencePy(conf,inputs,outputs): |
---|
[257] | 304 | geometry1=extractInputs(conf,inputs["InputEntity1"]) |
---|
| 305 | geometry2=extractInputs(conf,inputs["InputEntity2"]) |
---|
[106] | 306 | rgeometries=[] |
---|
| 307 | i=0 |
---|
| 308 | while i < len(geometry1): |
---|
| 309 | j=0 |
---|
| 310 | while j < len(geometry2): |
---|
[360] | 311 | tmp=geometry2[j].Clone() |
---|
| 312 | resg=geometry1[i].GetGeometryRef() |
---|
| 313 | resg=resg.SymmetricDifference(geometry2[i].GetGeometryRef()) |
---|
| 314 | tmp.SetGeometryDirectly(resg) |
---|
| 315 | rgeometries+=[tmp] |
---|
[106] | 316 | j+=1 |
---|
| 317 | geometry1[i].Destroy() |
---|
| 318 | i+=1 |
---|
| 319 | i=0 |
---|
| 320 | while i < len(geometry2): |
---|
| 321 | geometry2[i].Destroy() |
---|
| 322 | i+=1 |
---|
| 323 | outputResult(conf,outputs["Result"],rgeometries) |
---|
[9] | 324 | return 3 |
---|
[360] | 325 | |
---|