Skip to content

Instantly share code, notes, and snippets.

@plouvart
Created April 21, 2023 08:58
Show Gist options
  • Save plouvart/a4f3b61caa002b3d750547beba85b109 to your computer and use it in GitHub Desktop.
Save plouvart/a4f3b61caa002b3d750547beba85b109 to your computer and use it in GitHub Desktop.
Maximum Coverage EPSG
import geopandas as gpd
from pyproj.crs.crs import CRS
def maximum_coverage_epsg(
tiles: list[gpd.GeoDataFrame],
roi: gpd.GeoDataFrame,
metric_epsg: str | CRS | None = None,
) -> CRS:
"""Maximum Coverage EPSG
Given a set of tiles (GeoDataFrames with a single feature each)
and a ROI (GeoDataFrame with a single feature), finds out
the EPSG of the tile covering the most area in the ROI.
If an epsg is provided via metric_epsg, it is used to reproject the tiles
and ROI to a common epsg for comparing areas. If not, the epsg of the
ROI is used.
Args:
tiles (list[gpd.GeoDataFrame]): Input tiles.
roi (gpd.GeoDataFrame): Input ROI.
Returns:
CRS: the CRS of the tile covering the most area in the ROI.
"""
return list(
sorted(
tiles,
key = lambda x: -x.to_crs(
metric_epsg or roi.crs,
).intersection(
roi.to_crs(metric_epsg) if metric_epsg else roi,
).area.values[0],
)
)[0].crs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment