I have this command:

gdal_rasterize -l training -a "MC_ID" -ts 3473 4075 -te 42.4132272 -85.3704013 42.41419 -85.3692901 -ot Float32 -of GTiff "training.shp" "training_rasterized2.tif"

What I am trying to do is to rasterize a polygon layer with two classes (1 and 2). The attribute continuing the classes is "MC_ID". The extent, however, contains areas outside the polygons that I want to keep. I gained a raster layer. However, while I loaded it to python, I found only the nodata parts.

Here is the python code and result.

import numpy as np

roi_ds = gdal.Open('/training_rasterized2.tif', gdal.GA_ReadOnly)

roi = roi_ds.GetRasterBand(1).ReadAsArray()

# How many pixels are in each class?
classes = np.unique(roi)
# Iterate over all class labels in the ROI image, printing out some information
for c in classes:
    print('Class {c} contains {n} pixels'.format(c=c,
                                             n=(roi == c).sum()))

I received Class 0.0 contains 14152475 pixels

How can I change the gdadl_rasterize command to include polygon classes 1 and 2 in the raster result?

Your Answer

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

Browse other questions tagged or ask your own question.