pygeoogc.utils#

Some utilities for PyGeoOGC.

Module Contents#

class pygeoogc.utils.ESRIGeomQuery#

Generate input geometry query for ArcGIS RESTful services.

Parameters
  • geometry (tuple or sgeom.Polygon or sgeom.Point or sgeom.LineString) – The input geometry which can be a point (x, y), a list of points [(x, y), …], bbox (xmin, ymin, xmax, ymax), or a Shapely’s sgeom.Polygon.

  • wkid (int) – The Well-known ID (WKID) of the geometry’s spatial reference e.g., for EPSG:4326, 4326 should be passed. Check ArcGIS for reference.

bbox()#

Query for a bbox.

multipoint()#

Query for a multi-point.

point()#

Query for a point.

polygon()#

Query for a polygon.

polyline()#

Query for a polyline.

class pygeoogc.utils.RetrySession(retries=3, backoff_factor=0.3, status_to_retry=(500, 502, 504), prefixes=('https://',), cache_name=None, expire_after=- 1, disable=False)#

Configures the passed-in session to retry on failed requests.

Notes

The fails can be due to connection errors, specific HTTP response codes and 30X redirections. The code was originally based on: https://github.com/bustawin/retry-requests

Parameters
  • retries (int, optional) – The number of maximum retries before raising an exception, defaults to 5.

  • backoff_factor (float, optional) – A factor used to compute the waiting time between retries, defaults to 0.5.

  • status_to_retry (tuple, optional) – A tuple of status codes that trigger the reply behaviour, defaults to (500, 502, 504).

  • prefixes (tuple, optional) – The prefixes to consider, defaults to (”http://”, “https://”)

  • cache_name (str, optional) – Path to a folder for caching the session, default to None which uses system’s temp directory.

  • expire_after (int, optional) – Expiration time for the cache in seconds, defaults to -1 (never expire).

  • disable (bool, optional) – If True temporarily disable caching requests and get new responses from the server, defaults to False.

get(url, payload=None, headers=None)#

Retrieve data from a url by GET and return the Response.

head(url, data=None, params=None, headers=None)#

Retrieve data from a url by POST and return the Response.

post(url, payload=None, headers=None)#

Retrieve data from a url by POST and return the Response.

pygeoogc.utils.bbox_decompose(bbox, resolution, box_crs=4326, max_px=8000000)#

Split the bounding box vertically for WMS requests.

Parameters
  • bbox (tuple) – A bounding box; (west, south, east, north)

  • resolution (float) – The target resolution for a WMS request in meters.

  • box_crs (str, int, or pyproj.CRS, optional) – The spatial reference of the input bbox, default to epsg:4326.

  • max_px (int, opitonal) – The maximum allowable number of pixels (width x height) for a WMS requests, defaults to 8 million based on some trial-and-error.

Returns

list of tuples – Each tuple includes the following elements:

  • Tuple of px_tot 4 that represents a bounding box (west, south, east, north) of a cell,

  • A label that represents cell ID starting from bottom-left to top-right, for example a 2x2 decomposition has the following labels:

    |---------|---------|
    |         |         |
    |   0_1   |   1_1   |
    |         |         |
    |---------|---------|
    |         |         |
    |   0_0   |   1_0   |
    |         |         |
    |---------|---------|
    
  • Raster width of a cell,

  • Raster height of a cell.

pygeoogc.utils.check_bbox(bbox)#

Check if an input inbox is a tuple of length 4.

pygeoogc.utils.check_response(resp)#

Extract error message from a response, if any.

pygeoogc.utils.match_crs(geom, in_crs, out_crs)#

Reproject a geometry to another CRS.

Parameters
  • geom (list or tuple or geometry) – Input geometry which could be a list of coordinates such as [(x1, y1), ...], a bounding box like so (xmin, ymin, xmax, ymax), or any valid shapely’s geometry such as Polygon, MultiPolygon, etc..

  • in_crs (str, int, or pyproj.CRS) – Spatial reference of the input geometry

  • out_crs (str, int, or pyproj.CRS) – Target spatial reference

Returns

same type as the input geometry – Transformed geometry in the target CRS.

Examples

>>> from pygeoogc.utils import match_crs
>>> from shapely.geometry import Point
>>> point = Point(-7766049.665, 5691929.739)
>>> match_crs(point, "epsg:3857", "epsg:4326").xy
(array('d', [-69.7636111130079]), array('d', [45.44549114818127]))
>>> bbox = (-7766049.665, 5691929.739, -7763049.665, 5696929.739)
>>> match_crs(bbox, "epsg:3857", "epsg:4326")
(-69.7636111130079, 45.44549114818127, -69.73666165448431, 45.47699468552394)
>>> coords = [(-7766049.665, 5691929.739)]
>>> match_crs(coords, "epsg:3857", "epsg:4326")
[(-69.7636111130079, 45.44549114818127)]
pygeoogc.utils.traverse_json(items, ipath)#

Extract an element from a JSON file along a specified ipath.

This function is based on bcmullins.

Parameters
  • items (dict) – The input json dictionary

  • ipath (list) – The ipath to the requested element

Returns

list – The sub_items founds in the JSON

Examples

>>> from pygeoogc.utils import traverse_json
>>> data = [{
...     "employees": [
...         {"name": "Alice", "role": "dev", "nbr": 1},
...         {"name": "Bob", "role": "dev", "nbr": 2}],
...     "firm": {"name": "Charlie's Waffle Emporium", "location": "CA"},
... },]
>>> traverse_json(data, ["employees", "name"])
[['Alice', 'Bob']]
pygeoogc.utils.valid_wms_crs(url)#

Get valid CRSs from a WMS service version 1.3.0.

pygeoogc.utils.validate_crs(crs)#

Validate a CRS.

Parameters

crs (str, int, or pyproj.CRS) – Input CRS.

Returns

str – Validated CRS as a string.