Clip vector by raster¶
In [3]:
Copied!
from rasvec import clip_vector_by_raster
from geopandas import gpd
from rasterio.plot import show
import rasterio as rio
import matplotlib.pyplot as plt
from rasvec import clip_vector_by_raster
from geopandas import gpd
from rasterio.plot import show
import rasterio as rio
import matplotlib.pyplot as plt
In [4]:
Copied!
ras_path = 'sample_data/raster/ras.tif'
vec_path = 'sample_data/vector/vec/vec.shp'
output='output.shp'
ras_path = 'sample_data/raster/ras.tif'
vec_path = 'sample_data/vector/vec/vec.shp'
output='output.shp'
For this to work both ras and vec should be converted to the same crs.
In [5]:
Copied!
vec = gpd.read_file(vec_path).to_crs(3857)
ras = rio.open(ras_path)
vec = gpd.read_file(vec_path).to_crs(3857)
ras = rio.open(ras_path)
In [6]:
Copied!
fig, ax = plt.subplots()
show(ras, ax=ax)
vec.boundary.plot(ax=ax, color='red')
fig, ax = plt.subplots()
show(ras, ax=ax)
vec.boundary.plot(ax=ax, color='red')
Out[6]:
<Axes: >
In [ ]:
Copied!
clipped_vec = clip_vec_by_ras(
ras_path=ras_path,
vec_path=vec_path,
output=output
)
clipped_vec = clip_vec_by_ras(
ras_path=ras_path,
vec_path=vec_path,
output=output
)
In [ ]:
Copied!
fig, ax = plt.subplots()
show(ras, ax=ax)
clipped_vec.boundary.plot(ax=ax, color='red')
fig, ax = plt.subplots()
show(ras, ax=ax)
clipped_vec.boundary.plot(ax=ax, color='red')
Out[ ]:
<Axes: >