pycancensus.get_intersecting_geometries

pycancensus.get_intersecting_geometries(dataset: str, level: str, geometry: GeoDataFrame | GeoSeries | Point | Polygon | MultiPolygon, simplified: bool = False, use_cache: bool = True, quiet: bool = False, api_key: str | None = None) List[str] | Dict[str, List[str]][source]

Get identifiers for census regions intersecting a geometry.

This function returns a list of regions that intersect a given geometry input. This list of regions can be used directly to query census when one is interested in census data for a particular geographic region that does not coincide with defined census geometries.

Parameters:
  • dataset (str) – A CensusMapper dataset identifier.

  • level (str) – The census aggregation level to retrieve. One of “Regions”, “PR”, “CMA”, “CD”, “CSD”, “CT”, “DA”, “EA” (for 1996), or “DB” (for 2001-2021).

  • geometry (gpd.GeoDataFrame, gpd.GeoSeries, Point, Polygon, or MultiPolygon) – A valid geometry object. Any projection is accepted - objects will be reprojected to WGS84 (EPSG:4326) for server-side intersections.

  • simplified (bool, default False) – If True, returns a list of region IDs. If False, returns a dictionary compatible with get_census() regions parameter.

  • use_cache (bool, default True) – Whether to use cached data if available.

  • quiet (bool, default False) – Whether to suppress messages and warnings.

  • api_key (str, optional) – API key for CensusMapper API. If None, uses environment variable or previously set key.

Returns:

If simplified=True, returns a list of region identifiers. If simplified=False, returns a dictionary with level as key and list of region IDs as value, suitable for use with get_census().

Return type:

List[str] or Dict[str, List[str]]

Examples

>>> import pycancensus as pc
>>> from shapely.geometry import Point
>>>
>>> # Example using a Point from lat/lon coordinates
>>> point_geo = Point(-123.25149, 49.27026)
>>> regions = pc.get_intersecting_geometries(
...     dataset='CA21',
...     level='CT',
...     geometry=point_geo
... )
>>>
>>> # Use regions to get census data
>>> census_data = pc.get_census(
...     dataset='CA21',
...     regions=regions,
...     vectors=['v_CA21_1', 'v_CA21_2'],
...     level='CT'
... )