Skip to content

Instantly share code, notes, and snippets.

@hagerty
Last active September 21, 2016 19:15
Show Gist options
  • Save hagerty/22e30057f6acd672398569f8de1219d0 to your computer and use it in GitHub Desktop.
Save hagerty/22e30057f6acd672398569f8de1219d0 to your computer and use it in GitHub Desktop.
Create geoJSON from cluster array
def CreateGeoJSON ( fn, cluster, geom, proj ):
memdrv = gdal.GetDriverByName ( 'MEM')
src_ds = memdrv.Create('',cluster.shape[1],cluster.shape[0],1)
src_ds.SetGeoTransform(geom)
src_ds.SetProjection(proj)
band = src_ds.GetRasterBand(1)
band.WriteArray(cluster)
dst_layername = "BuildingID"
drv = ogr.GetDriverByName("geojson")
if os.path.exists('./geojson/' + fn + dst_layername + ".geojson"):
drv.DeleteDataSource('./geojson/' + fn + dst_layername + ".geojson")
dst_ds = drv.CreateDataSource ( './geojson/' + fn + dst_layername + ".geojson")
dst_layer = dst_ds.CreateLayer( dst_layername, srs=None )
dst_layer.CreateField( ogr.FieldDefn("DN", ogr.OFTInteger) )
gdal.Polygonize( band , None, dst_layer, 0, ['8CONNECTED=8'], callback=None )
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment