I am a bit of a GIS beginner here.
I've got an image mask in .jpg format that I'm trying to convert to a shapefile .shp format. To do so, I use the GDAL
This is what the input mask looks like:
I then use the following code to polygonize the image
sourceRaster = gdal.Open('./storage/processed/71ee8db7-d2b0-45e6-a232-e218d6977266/result.jpg') band = sourceRaster.GetRasterBand(1) bandArray = band.ReadAsArray() outShapefile = "polygonized" driver = ogr.GetDriverByName("ESRI Shapefile") outDatasource = driver.CreateDataSource(outShapefile+ ".shp") outLayer = outDatasource.CreateLayer("polygonized", srs=None) newField = ogr.FieldDefn('MYFLD', ogr.OFTInteger) outLayer.CreateField(newField) gdal.Polygonize(band, None, outLayer, 0, , callback=None ) outDatasource.Destroy() sourceRaster = None
The output of the above code is the following:
As you may notice there are 2 problems that I see with the resulting shapefile:
- It seems to be flipped upside down and
- The contours of the objects seem to be extremely messy.
Would anyone have pointers on how the above issues could be resolved or improved?