Sunday, 25 August 2019

crop same geo polygon from multiple raster inputs

I am trying to position geotif rasters so i can extract raster data of the same area resulting in same sized output images.

The idea is to be able to create a timelapse of overlapping satelite photos, some of the photos only contain fractions that overlap so i need to align and crop/whatever them.

I am using rasterio currently but i am not locked to using it. I am trying to position the two rasters below:

So they will be aligned in the same coordinate system. The idea is to get fixed size outputs where only the pixels from the input raster that fall within the fixed size area is taken.

Btw the coorsinate system is WGS 84 / UTM zone 32N (EPSG:32632).

install dependencies:

pip install rasterio, numpy, pillow

example:

import rasterio
from shapely.geometry.polygon import Polygon
from rasterio.mask import mask
from rasterio.plot import reshape_as_image
from PIL import Image


coords = [(331500,6171471),(480444,6171471), (480444,6320415),(331500,6320415)]
copenhagen_poly = Polygon(coords)

img_1 = rasterio.open('1_out.tif')
cropped_img_1, out_transform = mask(img_1, shapes=[copenhagen_poly], crop=True)
img_1 = reshape_as_image(img_1)
img_1 = Image.fromarray(img_1)

img_2 = rasterio.open('1_out.tif')
cropped_img_2, out_transform = mask(img_2, shapes=[copenhagen_poly], crop=True)
img_2 = reshape_as_image(img_2)
img_2 = Image.fromarray(img_2)

assert img_1.size, img_2.size == ((1803, 1997), (1948, 2000))

as you can see the sizes don't match which means i failed at extracting the exact same polygon, no matter if it included data or not, into a resulting image of a fixed size.



from crop same geo polygon from multiple raster inputs

No comments:

Post a Comment