Rasterize Geometry Example not working

38
August 10, 2018, at 1:50 PM

I am trying to recreate this rasterio example:

import numpy as np
import rasterio
from rasterio.features import rasterize
from rasterio.transform import IDENTITY
rows = cols = 10
geometry = {
    'type': 'Polygon',
    'coordinates': [[(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]]
}

with rasterio.Env():
    result = rasterize([geometry], out_shape=(rows, cols))
    with rasterio.open(
            "test.tif",
            'w',
            driver='GTiff',
            width=cols,
            height=rows,
            count=1,
            dtype=np.uint8,
            nodata=0,
            transform=IDENTITY,
            crs={'init': "EPSG:4326"}) as out:
        out.write(result.astype(np.uint8), indexes=1)

I have checked the result of rasterize (print(result)) which seems normal and sould result in a 2x2 pixel square:

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 1 1 0 0 0 0 0 0]
 [0 0 1 1 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

But I am getting a completely black image (nodata image).

Answer 1

Although I cannot define the reason for the black .tif image, I concluded that the process works as intended and we can see a visual result using matplolib.pyplot.

The example must be modified as follows to use pyplot:

import rasterio
from matplotlib import pyplot
from rasterio.features import rasterize
from rasterio.transform import IDENTITY
rows = cols = 10
geometry = {
    'type': 'Polygon',
    'coordinates': [[(2, 2), (2, 4.25), (4.25, 4.25), (4.25, 2), (2, 2)]]
}

with rasterio.Env():
    result = rasterize([geometry], out_shape=(rows, cols))
    with rasterio.open(
            "test.tif",
            'w+', # Open the file in read/write mode
            driver='GTiff',
            width=cols,
            height=rows,
            count=1,
            dtype=rasterio.uint8,
            nodata=0,
            transform=IDENTITY,
            crs={'init': "EPSG:4326"}) as out:
        out.write(result.astype(rasterio.uint8), indexes=1)
        # Plot the image.
        pyplot.imshow(out.read(1))
        pyplot.show()
READ ALSO
Assiging a rank to each group in pandas

Assiging a rank to each group in pandas

I have a dataframe and would like to assign a rank to each row in a groupFor example,

54
The correct way to shorten long for statement to a maximum of 79 columns

The correct way to shorten long for statement to a maximum of 79 columns

My question is what's the most conventional way of making the following line of code:

51
Understanding output from statsmodels grangercausalitytests

Understanding output from statsmodels grangercausalitytests

I'm new to Granger Causality and would appreciate any advice on understanding/interpreting the results of the python statsmodels outputI've constructed two data sets (sine functions shifted in time with noise added)

52