import numpy as npGeo
Geoprocessing utilities: bounding boxes, coordinate conversions.
get_bbox
def get_bbox(
df:pandas.DataFrame, # DataFrame with coordinate columns
coord_cols:tuple[str, str]=('LON', 'LAT'), # Column names for (longitude, latitude)
)->shapely.geometry.base.BaseGeometry: # Envelope: a Polygon, or a Point when all coordinates are equalBounding box of a DataFrame’s coordinates as a shapely envelope
get_bbox returns the envelope of all the points. Its bounds are (min lon, min lat, max lon, max lat):
df = pd.DataFrame({'LON': np.linspace(-10, 5, 20), 'LAT': np.linspace(40, 50, 20)})
bbox = get_bbox(df)
test_eq(bbox.bounds, (-10.0, 40.0, 5.0, 50.0))The envelope is a shapely Polygon:
test_eq(bbox.wkt, 'POLYGON ((-10 40, 5 40, 5 50, -10 50, -10 40))')When all the points are equal, the envelope is a Point:
bbox = get_bbox(pd.DataFrame({'LON': [0, 0], 'LAT': [1, 1]}))
test_eq(bbox.geom_type, 'Point')
test_eq(bbox.bounds, (0.0, 1.0, 0.0, 1.0))coord_cols names other coordinate columns:
bbox = get_bbox(pd.DataFrame({'X': [10, 20], 'Y': [30, 40]}), coord_cols=('X', 'Y'))
test_eq(bbox.bounds, (10.0, 30.0, 20.0, 40.0))ddmm_to_dd
def ddmm_to_dd(
ddmmmm:float, # Coord in DDMM.MMMM format (e.g. 45.34 → 45°34')
)->float: # Same coord in decimal degreesConvert DDMM.MMMM to decimal degrees
ddmm_to_dd converts degrees and decimal minutes, such as 45°34’ written as 45.34, to decimal degrees. A negative coordinate stays negative:
test_close(ddmm_to_dd(45.34), 45.566667)
test_close(ddmm_to_dd(0.0), 0.0)
test_close(ddmm_to_dd(0.30), 0.5)
test_close(ddmm_to_dd(-45.34), -45.566667)
test_close(ddmm_to_dd(180.00), 180.0)