Hi I am researching to complete a project.

The aim is by given a coordinate I can extract the image amongs the raster files.

How do I check if the coordinate that I have for example (51.3334198, 3.2973934) is in a raster image file - k_01.tif? If this coordinate is indeed in k_01.tif, how do I extract a small part of if i.e NxN window?

My code:

import rasterio
src = rasterio.open('k_01.tif')

src.bound

BoundingBox(left=145000.0, bottom=238000.0, right=162000.0, top=247000.0)

src.crs

CRS.from_epsg(31370)

Any ideas? Thanks in advance.

1

There are 1 answers

0
swatchai On

To check if a location (lat,long) is within the raster image's boundary.

import pyproj
pp = pyproj.Proj(init='epsg:31370')
x,y = pp(3.2973934, 51.3334198)  #long, lat sequence

then, you can use (x,y) to check with the src.bound

src= rasterio.open( your_tiffile )
bnd = src.bounds
if bnd.left < x < bnd.right and bnd.top < y < bnd.bottom:
    print("inside")
else:
    print("outside")

To slice part of the array.

band1 = src.read(1)  #read band-1
# usually we flip the image, uncomment next line if you dont need it
band1_flip =  band1[::-1,:] # reverse height in (height, width, channel)
# extract part of the image array
fr_row, to_row, fr_col, to_col = 0, 100, 0, 100   #you set the values
aslice = band1_flip[fr_row:to_row, fr_col:to_col]