Trying to rasterize a polylines shapefile with a specific attribute using rasterize function from Rasterio library. This function needs an iterable containing tuples of (geometry, value), with the geometry being a GeoJSON-like object (see documentation). This geometry can be extracted with fiona or geopandas, I've read this question and tried it (so with geopandas) but the geometry is not correct, as I tested it with the "is_valid_geom" and the result is "false" which seems logical because when I print this geometry it displays: <generator object <genexpr> at 0x000001903856C048>
. I also tried with Fiona like this :
shapeee = fiona.open(shapefile)
geom_fiona = [shapes['geometry'] for shapes in shapeee]
attrib_fiona = [shapes['properties']['OBJECTID'] for shapes in shapeee] # attribute
print(features.is_valid_geom(geom_fiona)) # FALSE
which also returns "false" but I don't understand why because the geometry seems to be correct :
print(geom_fiona)
"[{'type': 'LineString', 'coordinates': [(177421.98120000213, 142766.21020000055), (177409.1555000022, 142781.71609999985), (177392.76659999788, 142801.65300000086) and so on..."
So at the end when I try to rasterize I don't get any error message but the output is a blank image with all values to 0... The rasterization code :
tuples = []
for i in range(0,len(geom_fiona)-1,1):
tuples.append([geom_fiona[i],attrib_fiona[i]])
burned = features.rasterize(tuples, out_shape=new_dataset.shape,default_value=-99,dtype=rasterio.float64)
Note : I already read this and the projections are the same between my shapefile and the raster that is used to raterized
Anyone has an idea of what could be the problem ?
The geometry was actually valid! features.is_valid_geom(geom_fiona)
returned false
because I put the entire geometry as argument. Writing features.is_valid_geom(geom_fiona[0])
returned True
. But the rasterization is still incorrect and nothing changed...