0

I have multiple raster files (RGB, 255) and I want to convert those raster files to shapefiles but I want only that area as a polygon where there is data, which means I want to exclude the zero values

As illustrated below, I want the polygon of the strip only where there are some values other than zeros as a shapefile.

enter image description here

I need a Python solution because there are too many raster files and doing it with QGIS or ArcGIS is not a viable solution.

Raster Sample: Raster file

Attempt 1: So far I am only able to get the bounding box of the whole raster which is not the ideal solution but it works, following Make shapefile from raster-bounds in Python

# make shp from bounding box
import rasterio
ra = rasterio.open('20211124_092953_Data-001.vrt')
bounds  = ra.bounds

from shapely.geometry import box
geom = box(*bounds)

import geopandas as gpd
df = gpd.GeoDataFrame({"id":1,"geometry":[geom]}, crs =ra.crs )
df.to_file("boundary.shp")

Attempt 2: Then I tried the solution mentioned at How to polygonize raster to shapely polygons

I thought first I will try to write the rasterio-numpy array as it is and then will try applying filters but this solution didn't work for me at all, tried many tweaks but didn't work. for a single raster array = band shape:(8343, 11370) it takes forever for running and I don't get any output at all

import rasterio
from rasterio.features import shapes
mask = None
with rasterio.Env():
    with rasterio.open('20211124_092953_Data-001.vrt') as src:
        image = src.read(1) # first band
        results = (
        {'properties': {'raster_val': v}, 'geometry': s}
        for i, (s, v) 
        in enumerate(
            shapes(image, mask=mask, transform=src.transform)))
        
geoms = list(results)

from shapely.geometry import shape
shape(geoms[0]['geometry'])

geoms = list(results)
import geopandas as gp
gpd_polygonized_raster  = gp.GeoDataFrame.from_features(geoms)

Attempt 3: Then I tried applying a filter to an array so that there will be only ones and zeros in the raster so it could make it easier for making polygons. Here there is an issue that len(mypoly) = 13102, but I thought it would be only two polygons.

import rasterio
import rasterio.features
from shapely.geometry import shape

ds = rasterio.open('20211124_092953_Data-001.jpg')
ds2 = rasterio.open('20211124_092953_Data-001.vrt')
band = ds2.read(1)
# make a copy of original single band numpy array
raster_array = band.copy()
# replace multiple values with 1 to speed up the process of polgenizing
raster_array = np.where(raster_array>0,1,raster_array)


#  numpy array binary mask loaded however needed
maskShape = rasterio.features.shapes(raster_array.astype('uint8'))
mypoly=[]
for vec in maskShape:
    mypoly.append(vec[0])

So far I have gone through different tutorials and questions on GIS SE but I am stuck without any solution. Also not so strong programming knowledge and understanding of these libraries so maybe the answer was already in the above-mentioned links but I need a bit of help with what I am doing wrong here.

1 Answer 1

0

I hope this link might help you.

Converting raster to polygon after excluding no-data values

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.