Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 134 characters in body; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Convert the Converting Raster to Shapefile using filtered values of the raster

I have a bunch ofmultiple 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

p.s: I need some pythona Python solution cozbecause there are too many raster files and doing it with qgisQGIS or arcArcGIS is not a viable solution.

Atempt1Attempt 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, thanks tofollowing (Make shapefile from raster-bounds in Python)Make shapefile from raster-bounds in Python

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

So far I have gone through different tutorials and questions on the stackGIS 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.

Any and all help is appreciated.

Convert the Raster to Shapefile using filtered values of the raster

I have a bunch of 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

p.s: I need some python solution coz there are too many raster files and doing it with qgis or arc is not a viable solution.

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

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

So far I have gone through different tutorials and questions on the stack 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.

Any and all help is appreciated.

Converting Raster to Shapefile using filtered values of raster

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

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

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

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

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.

Source Link

Convert the Raster to Shapefile using filtered values of the raster

I have a bunch of 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

p.s: I need some python solution coz there are too many raster files and doing it with qgis or arc is not a viable solution.

Raster Sample: Raster file

Atempt1: So far I am only able to get the bounding box of the whole raster which is not the ideal solution but it works, thanks to (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 here: 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 the stack 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.

Any and all help is appreciated.