InsideDarkWeb.com

GDAL polygonize processing

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 polygonize function.

This is what the input mask looks like:

enter image description here

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:

enter image description here

As you may notice there are 2 problems that I see with the resulting shapefile:

  1. It seems to be flipped upside down and
  2. The contours of the objects seem to be extremely messy.

Would anyone have pointers on how the above issues could be resolved or improved?

One Answer

Don't know about the flipping, but I wonder if your (lossy) jpeg has artefacts which are visually imperceptible but decode into different values in the source raster, that then generate distinct polygons.

I would

  1. Try to make the source raster a tiff or png or some other nonlossy format (not by converting it back now, since the artefacts, if any, will be kept; rather by saving it in a nonlossy format wherever it came from so the artefacts are not introduced.)

  2. In an interactive tool like QGIS plot a histogram of the input raster to see if there is anything else than 0s and 1s

  3. Examine the features in the output to figure out if the "speckles" are additional polygons with slightly different values of the DN field (which is the value read from the input raster).

Answered by Houska on November 14, 2021

Add your own answers!

Related Questions

How to open shapefile dataset using ArcGIS Pro SDK .NET?

1  Asked on June 24, 2021 by pavel-butusov

   

GRASS 6.4.3 Crashes on Startup; libintl-8.dll cited

4  Asked on June 24, 2021 by darin

     

Plotting polygons as separate plots using Python

3  Asked on June 23, 2021 by ralt_jol

       

Seeking formula for Distance Calculation on Mars

0  Asked on June 23, 2021 by douglas-marsh

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.