I'm using rasterio sample module, list_of_coords are just 3 random points but only 2nd is within the raster bounds:
list_of_coords = [(754.2,4248548.6), (754222.6,4248548.6), (54.9,4248548.4)]
sample = np.array(list(rasterio.sample.sample_gen(raster, list_of_coords))).flatten()
output:
[ 0 896 0]
It works great but as you can see if coordinates are out of raster image it gives value 0. Is there any way to let user know that coords that they put in the list are out of raster bounds? 0 can be also a value for existing within raster bounds point so simple loop:
for idx, element in enumerate(sample):
if element == 0:
print(f"coords {a[idx]} out of raster")
is not a good solution. Here is what I came up with so far:
Knowing basic info about geographic coordinate system and raster bounds we could write down some "rules". With raster.bounds
I got bbox for my raster and I wrote a bit better loop:
for idx, element in enumerate(a):
if element[0] > 0 and band2.bounds.right > 0 and element[0] > band2.bounds.right\
or element[0] > 0 and band2.bounds.left > 0 and element[0] < band2.bounds.left: #more conditions
print(f"coords {a[idx]} out of raster")
output (correct):
coords (754.6, 4248548.6) out of raster
coords (54.6, 4248548.6) out of raster
Problem is - to cover all posibilities I need to write in this loop way more conditions, is tere a better way to let user know that given point is out of raster?