I am trying to clip my tiff file either using a shape or geojson file in python. The code for clipping the image is -
from datetime import date
import geopandas as gpd
import rasterio
import rasterio.features
import rasterio.warp
from shapely.geometry import MultiPolygon, Polygon
import subprocess
import matplotlib.pyplot as plt
import geopandas as gpd
from rasterio.mask import mask
nReserve = gpd.read_file('poly.shp')
# the polygon GeoJSON geometry
nReserve_proj = nReserve.to_crs({'init': 'epsg:32643'})
with rasterio.open("RGBNew.tiff") as src:
out_image, out_transform = rasterio.mask.mask(src, nReserve_proj.geometry,crop=True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
with rasterio.open("RGB_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)
The code to create the above used shape file is -
import pandas as pd
from shapely.geometry import Polygon
def bbox(lat,lng, margin):
#return (Polygon([[19.386508042365318,72.83352971076965],[19.386163944316234,72.83352971076965],[19.387256959134895,72.83352971076965],[19.38746948894201,72.83352971076965],[19.386508042365318,72.83352971076965]]))
return (Polygon([[72.83352971076965,19.386508042365318],[72.83352971076965,19.386163944316234],[72.83352971076965,19.387256959134895],[72.83352971076965,19.38746948894201],[72.83352971076965,19.386508042365318]]))
gpd.GeoDataFrame(crs = {'init':'epsg:32643'},geometry = [bbox(10,10, 0.25)]).to_file('poly.shp')
But I am getting the error-
File "tryWithNewEPSG.py", line 24, in out_image, out_transform = mask(src, geoms, crop=True) File "/home/ubuntu/.local/lib/python2.7/site-packages/rasterio/mask.py", line 181, in mask pad=pad) File "/home/ubuntu/.local/lib/python2.7/site-packages/rasterio/mask.py", line 87, in raster_geometry_mask raise ValueError('Input shapes do not overlap raster.') ValueError: Input shapes do not overlap raster.
I am currently checking the epsg using the code-
import rasterio
with rasterio.open('NDVI.tif') as src:
print (src.crs)
and have confirmed it is the same; I have even tried by changing the epsg of both to 4326; but still did not work.
out_image, out_transform = rasterio.mask.mask(src, nReserve_proj.geometry,crop=True)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],