Saddam Hussain
Saddam Hussain

Reputation: 1

How to convert raster map to shapefile (raster map boundaries to vector center line shapefile)

I have a raster map in tiff format and I am trying to convert it into smooth shape file. but I guess due to pencil drawn lines its output is in the weird shape file which you can show in the screen shots.

Input tiff file (it is in greyscale):

This is input_image.tif

This is its zoom version where we can there are some pixels which are not visible correctly

Code: i am using gdal library:

from osgeo import ogr
from osgeo import gdal, osr

# Specify the input TIFF file path
input_tiff = 'input_image3.tif'

# Specify the output shapefile path
output_shapefile = 'outputs/output3.shp'

# Open the TIFF file
ds = gdal.Open(input_tiff)

# Get the raster layer
layer = ds.GetRasterBand(1)

# Create a new shapefile layer for polylines
driver = ogr.GetDriverByName('ESRI Shapefile')
shapefile = driver.CreateDataSource(output_shapefile)
layer_name = 'polylines'

# Create a new layer with the correct spatial reference
spatial_ref = osr.SpatialReference(wkt=ds.GetProjection())
new_layer = shapefile.CreateLayer(layer_name, geom_type=ogr.wkbLineString, srs=spatial_ref)

# Convert raster to vector

gdal.Polygonize(layer, None, new_layer, -1, [], callback=None)

# Perform simplification

# Clean up
ds = None
shapefile = None

print("Conversion completed successfully.")

output shape files:

This is the output of above code in shape file

This is zoom version of same above shapefile

This full zoom version where we can see the small polygons which is not required

In the output I want just single line not tiny small polygonized lines.

here is required Output shapefile:

Ignore the inside numbers, I just want boundaries of raster tiff file into shape file like this single lines instead weird tiny small boxes.

If anyone knows how to deal with this problem please let me. I have tries many methods all the Chat GPT suggest but those not working. I guess first I have to digitized my pencil map sketch. I have tested by giving a tiff file which was drawn by computer then its output was correct. I don't know to how to preprocess this image to extract lines.

required output shapefile (ignore the numbers inside the boxes)

Upvotes: 0

Views: 476

Answers (1)

iuandbp
iuandbp

Reputation: 1

I also encountered a similar problem. I wanted to convert several crack tif files into shp files, but the conversion effect was similar to yours. The first problem was that there would be many branches like those small tentacles, and then the second problem The problem is that like you there are a lot of polygon meshes that we don't actually need.

Upvotes: 0

Related Questions