1

Python beginner here. I am converting a raster to a vector in shapefile format. I am successful in doing this but my output isn't quite what I'm looking for. I need the output to have a "square" pixel for each attribute/row of data. My output currently is merging and dissolving like features that are connecting. Instead, each pixel needs its own row of data. I am not sure if this is do-able with GDAL and how I currently have my code set-up. Here's my code:

ClipRas_raster = gdal.Open(r'path to my raster I'm converting')
band = ClipRas_raster.GetRasterBand(1)
band.ReadAsArray()
proj = ClipRas_raster.GetProjection()
WGS84_proj = osr.SpatialReference()
WGS84_proj.ImportFromWkt(proj)

GP_POP = 'path to my output shapefile'
call_drive = ogr.GetDriverByName('ESRI Shapefile')
create_shp = call_drive.CreateDataSource(GP_POP)
shp_layer = create_shp.CreateLayer('layername', srs = WGS84_proj)
new_field = ogr.FieldDefn(str('ID'), ogr.OFTInteger)
shp_layer.CreateField(new_field)
gdal.Polygonize(band, None, shp_layer, 0, [], callback = None)
create_shp.Destroy()
ClipRas_raster = None

Current Output: enter image description here Desired Output (Difficult to tell here, but each pixel is a square polygon: enter image description here Any suggestions? Can I make this a function so I can use again?

1 Answer 1

0

GDAL does not have function for converting pixels into polygons. QGIS does have such a function named "Raster pixels to polygons"

Raster pixels to polygons

This algorithm converts a raster layer to a vector layer, by creating polygon features for each individual pixel's extent in the raster layer.

Any nodata pixels are skipped in the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.