0

As a coding enthusiast I am currently creating my own "geospatial" tool as how I would like it to work. However right on the start I am already facing a problem. My tool should work using GeoPandas to extract information and then OGR/GDAL for the data edits as I want it to work fast. I like to analyse a lot and big data!

The code-snipped with the problem should rasterize a single GeoPandas polygon. I try to do that using this path. - extract using geopandas WKT-polygon from a polygon - Create an OGR-feature using the WKT-polygon - rasterize this using GDAL.

The problem I am facing is that I only retrieve a raster which consists of 0's, instead 0's and 1's...

the code is displayed below:

import geopandas as gpd
import ogr, osr
import gdal
import uuid

tf = r'f:test2.shp'

def vector_to_raster(source_layer, output_path, x_size, y_size, options, data_type=gdal.GDT_Byte):
    '''
    This method should create a raster object by burning the values of a source layer to values.
    '''

    x_min, x_max, y_min, y_max = source_layer.GetExtent()
    print(source_layer.GetExtent())
    x_resolution = int((x_max - x_min) / x_size)
    y_resolution = int((y_max - y_min) / -y_size)  
    print(x_resolution, y_resolution)

    target_ds = gdal.GetDriverByName(str('GTiff')).Create(output_path, x_resolution, y_resolution, 1, data_type)
    spatial_reference = source_layer.GetSpatialRef()         
    target_ds.SetProjection(spatial_reference.ExportToWkt())
    target_ds.SetGeoTransform((x_min, x_size, 0, y_max, 0, -y_size))
    gdal.RasterizeLayer(target_ds, [1], source_layer, options=options)
    target_ds.FlushCache()
    return target_ds


#create geopandas dataframe
gdf = gpd.read_file(tf)

#grab projection from the gdf
projection = gdf.crs['init']

#get geometry from 1 polygon (now just the 1st one)
polygon = gdf.loc[0].geometry 

#grab epsg from projection
epsg = int(projection.split(':')[1])

#create geometry
geom = ogr.CreateGeometryFromWkt(polygon.wkt)

#create spatial reference
proj = osr.SpatialReference()
proj.ImportFromEPSG(epsg) 

#get driver
rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('wrk')

#create polylayer with projection
rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=proj)

#create feature
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())

#set geometry in feature
feat.SetGeometryDirectly(geom) 

#add feature to memory layer
rast_mem_lyr.CreateFeature(feat)

#create memory location
tif_output = '/vsimem/' + uuid.uuid4().hex + '.vrt'

#rasterize
lel = vector_to_raster(rast_mem_lyr, tif_output, 0.001, -0.001,['ATTRIBUTE=Shape__Len', 'COMPRESS=LZW', 'TILED=YES', 'NBITS=4'])

# output should consist of 0's and 1's
print(np.unique(lel.ReadAsArray()))

Many thanks to the person who can give me a hint into the right direction :-).

Cheers!

Mr unkown
  • 1
  • 1

2 Answers2

1

Hi I did not run your code, but I can give a couple of suggestions. At the moment you are rasterizing according to the 'Shape__Len' field from your polygon, and you specify you output raster as GDT_Byte (which can only contain values between 0 and 255), make sure that 'Shape__Len' is a match in datatype, or create a new field in your polygon containing integers between 0 and 255 to rasterize by, or change your output data type to GDT_Float32.

Alternatively if you only want 1 and 0's you can burn the value 1 where there is a polygon:

gdal.RasterizeLayer(target_ds, [1], source_layer,burn_values=[1])

It is also wise, since you are creating some tools for yourself, to keep track of/manage your NoData values. If you only want to display your rasterized polygons you can add the following steps:

target_ds = gdal.GetDriverByName(str('GTiff')).Create(output_path, x_resolution, y_resolution, 1, data_type)
spatial_reference = source_layer.GetSpatialRef()         
target_ds.SetProjection(spatial_reference.ExportToWkt())
target_ds.SetGeoTransform((x_min, x_size, 0, y_max, 0, -y_size))
NowBand = target_ds.GetRasterBand(1) # ADD
NowBand.SetNoDataValue(0) # ADD NoData Value of your choice (according to your specified data type)
NowBand.Fill(0) # ADD Fill the band with NoData as to only display your rasterized features
gdal.RasterizeLayer(target_ds, [1], source_layer, burn_values=[1]) # if you only want to burn 1 in your values
target_ds.FlushCache()
Jascha Muller
  • 243
  • 1
  • 2
  • 7
0

I did not run the code, neither, but just in case someone is looking for an answer, I think that you've made mistake within your "vector_to_raster" function:

def vector_to_raster(source_layer, output_path, x_size, y_size, options, data_type=gdal.GDT_Byte):
'''
This method should create a raster object by burning the values of a source layer to values.
'''

x_min, x_max, y_min, y_max = source_layer.GetExtent()
print(source_layer.GetExtent())
x_resolution = (x_max - x_min) / x_size
y_resolution = (y_max - y_min) / -y_size
print(x_resolution, y_resolution)

target_ds = gdal.GetDriverByName(str('GTiff')).Create(output_path, x_resolution, y_resolution, 1, data_type)
spatial_reference = source_layer.GetSpatialRef()         
target_ds.SetProjection(spatial_reference.ExportToWkt())
target_ds.SetGeoTransform((x_min, x_resolution, 0, y_max, 0, y_resolution))
gdal.RasterizeLayer(target_ds, [1], source_layer, options=options)
target_ds.FlushCache()
return target_ds

x_resolution and y_resolution should not be cast as int. GeoTransform should take the x_resolution/y_resolution instead of x_size/(-)y_size.

Selim
  • 1