1

I already asked this in StackOverflow, but maybe it's better here. I have a ndvi raster that I binarized (I put my threshold at 0.35) with a script python and I wanted to polygonize this raster, in order to get ndvi into polygones, to store them after in a database. I found and used this code :

import rasterio
import rasterio.env
from rasterio.features import shapes
import fiona
mask = None
with rasterio.Env():
    with rasterio.open('binarized_ndvi.tif') as src:
        image = src.read()

    results = (
    {'properties': {'raster_val': v}, 'geometry': s}
    for i, (s, v)
    in enumerate(
    shapes(image, mask=mask, transform=src.transform)))

    with fiona.open(
    'ndvi_shapefile.shp', 'w',
    driver='Shapefile',
    crs=src.crs,
    schema={'properties': [('raster_val', 'int')],
    'geometry': 'Polygon'}) as dst:
        dst.writerecords(results)

But all I get is a shapefile, with only one polygon whose shape is the same as my raster, like if only the first elment of the raster was taken into account ...

I can't send you my raster, it's too big.

  • What is the value of raster_val? Maybe it results from the conversion of the initial (binary) value. – Christine Jun 27 '17 at 7:29
  • raster_val is 0 if the value of the raster was <= 0.35, 1 if > 0.35 – hort_98663 Jun 27 '17 at 7:32
  • I want to mean : what is the value of the attribute of the shapefile created? – Christine Jun 27 '17 at 7:41
  • in the shapefile, there is one attribut raster_val=0 – hort_98663 Jun 27 '17 at 8:14
  • 1
    Is this a float raster by any chance? GDAL's polygonizer is intended for thematic data, not fields of continuous values, and you'll end up with one feature for every pixel, and maybe run out of memory in the process. – sgillies Jun 30 '17 at 6:50
1

I've used this pattern, based on this and this. I'm not familiar with rasterio.Env - a quick look at the Docs suggests it's to access AWS. Main differences with your script are creating a mask and and having everything from results = statement tabbed in so it's happening with the open raster.

import fiona
import rasterio
from rasterio import features

with rasterio.open('path_to_your_raster') as src:
    image = src.read(1)
    #Make a mask
    mask = image != 1
    #iterate over shapes.
    results = (
        {'properties': {'raster_val': v}, 'geometry': s}        
        for i, (s, v) in enumerate(
            shapes = features.shapes(image, mask=mask, transform = src.transform)))

    with fiona.open(
            'ndvi_shapefile.shp', 'w',
            driver='Shapefile',
            crs=src.crs,
            schema={'properties': [('raster_val', 'int')],
            'geometry': 'Polygon'}) as dst:
        dst.writerecords(results)
  • It's not working, a file is created, but it's empty and the script keeps running, I have to close the cmd to stop it ... – hort_98663 Jun 27 '17 at 12:34
  • Any chance there is a way to easily flag that we do not want to coalesce neighboring like pixels and instead preserve the shape of each pixel when exporting? – kuanb Jul 13 '17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.