Spatial & Regions#

fetchez.spatial#

Lightweight spatial utilities for parsing region strings and files into standard bounding boxes. Adaptded from CUDEM.

copyright:
  1. 2012 - 2026 CIRES Coastal DEM Team

license:

MIT, see LICENSE for more details.

fetchez.spatial.region_help_msg()[source]#
class fetchez.spatial.Region(w=None, e=None, s=None, n=None, srs=None)[source]#

Bases: object

A geospatial bounding box object.

Behaves like a tuple (xmin, xmax, ymin, ymax) for backward compatibility, but provides methods for manipulation and format conversion.

__init__(w=None, e=None, s=None, n=None, srs=None)[source]#
property w#
property e#
property s#
property n#
property width#
property height#
property xy_region#
valid_p(check_xy=True)[source]#

Check if region is valid.

Parameters:

check_xy (bool, default: True)

Return type:

bool

is_valid(check_xy=True)#

Check if region is valid.

Parameters:

check_xy (bool, default: True)

Return type:

bool

copy()[source]#
buffer(pct=5, x_bv=None, y_bv=None)[source]#

Buffer the region in place.

Parameters:
center()[source]#

Return center (x, y).

chunk(chunk_size=1.0)[source]#

Split into smaller sub-regions.

Parameters:

chunk_size (float, default: 1.0)

Return type:

List[Region]

densify_edges(density=20)[source]#

Generate a list of points along the perimeter of the region.

Parameters:
  • region (Region) – Input region.

  • density (int) – Number of points per edge.

Returns:

list – A list of (x, y) tuples representing the densified perimeter.

to_bbox()[source]#

Export as standard (w, s, e, n) bbox used by many GIS tools.

to_list()[source]#

Export as [w, e, s, n] list.

format(style='gmt')[source]#

String representation.

to_shapely()[source]#
to_wkt()[source]#
to_geojson_geometry()[source]#
srcwin(geo_transform, x_count, y_count, node='grid')[source]#

Output the appropriate GDAL srcwin (xoff, yoff, xsize, ysize).

geo_transform(x_inc=0, y_inc=None, node='grid')[source]#

Return dimensions and a geotransform based on the region and a cellsize.

Returns:

list – [xcount, ycount, geot]

Parameters:
geo_transform_from_count(x_count=0, y_count=0)[source]#
Parameters:
  • x_count (int, default: 0)

  • y_count (int, default: 0)

to_geo_transform(nx, ny)[source]#

Generate a GDAL-style GeoTransform from extent and dimensions.

Args: region (tuple): (min_x, min_y, max_x, max_y) - ‘te’ format. nx (int): Number of columns (x count). ny (int): Number of rows (y count).

Returns: tuple: (top_left_x, x_res, 0, top_left_y, 0, -y_res)

Parameters:
classmethod from_list(r_list)[source]#
classmethod from_string(r_str)[source]#
from_geo_transform(gtl)[source]#

Import a region from a geotransform list and dimensions.

transform_densify(transformer=None, transform_direction='FORWARD')[source]#
transform(transformer=None, transform_direction='FORWARD')[source]#
warp(dst_srs='epsg:4326')[source]#

Transform region horizontally to a new CRS.

fetchez.spatial.region_from_vector(fn)[source]#

Parse the bounding box of any OGR-supported vector file using Fiona.

Parameters:

fn (str)

Return type:

Optional[List[Region]]

fetchez.spatial.region_from_geojson(fn)[source]#

Parse the bounding box(es) of a GeoJSON file.

Parameters:

fn (str)

Return type:

Optional[List[Region]]

fetchez.spatial.region_from_place(query, centered=True)[source]#

Resolve ‘loc:PlaceName’ to a bounding box.

Parameters:
  • query (str)

  • centered (bool, default: True)

Return type:

Optional[Region]

fetchez.spatial.parse_region(input_r)[source]#

Main function to parse region input into a list of Region objects.

Parameters:

input_r (Union[str, List])

Return type:

List[Region]

fetchez.spatial.fix_argparse_region(raw_argv)[source]#

Argument Pre-processing for negative coordinates.

fetchez.spatial.region_valid_p(region, check_xy=True)[source]#

Legacy wrapper for validity check.

fetchez.spatial.region_center(region)[source]#

Calculate the center of a region.

Parameters:

region (Tuple[float, float, float, float])

fetchez.spatial.region_to_shapely(region)[source]#

Convert a fetchez region (xmin, xmax, ymin, ymax) to a shapely box.

fetchez regions are like GMT: (west, east, south, north) while shapely regions are not: (minx, miny, maxx, maxy)

Parameters:

region (Tuple[float, float, float, float])

fetchez.spatial.region_to_wkt(region)[source]#

Convert a fetchez region (xmin, xmax, ymin, ymax) to WKT (via shapely)

Parameters:

region (Tuple[float, float, float, float])

fetchez.spatial.region_to_bbox(region)[source]#

Convert a fetchez region to a bbox

Parameters:

region (Tuple[float, float, float, float])

fetchez.spatial.region_to_geojson_geom(region)[source]#
Parameters:

region (Tuple[float, float, float, float])

fetchez.spatial.region_from_list(r_list)#
fetchez.spatial.region_from_string(r_str)#
fetchez.spatial.chunk_region(r, s=1.0)[source]#
fetchez.spatial.buffer_region(r, p=5)[source]#
fetchez.spatial.regions_reduce(region_a, region_b)[source]#

Combine two regions and find their minimum overlapping region.

Parameters:
Return type:

Region

fetchez.spatial.regions_intersect_p(region_a, region_b)[source]#

Check if two regions intersect.

Parameters:
Return type:

bool

fetchez.spatial.x360(x)[source]#
fetchez.spatial.transform_increment(dst_inc_x, dst_inc_y, transformer, region_center)[source]#

Transform grid increments from Destination SRS to Source SRS.

Parameters:
  • dst_inc_x (float) – X increment in destination units (e.g. 1/3600 for 1s).

  • dst_inc_y (float) – Y increment in destination units.

  • transformer (pyproj.transformer.Transformer) – The pipeline transforming Source -> Dest.

  • region_center (tuple) – (x, y) center of the region in Source CRS.

Returns:

(float, float) – The estimated (src_inc_x, src_inc_y) in source units.