Skip to main content

Python client for the Close API (api.close.city). Travel times to points of interest for every US census block.

Project description

closecity

This is the Python software development kit for the Close.City API. It returns travel times from every US census block to nearby places, on foot, by bike, and by public transit. The data behind close.city is served over the Close API.

Documentation: https://henryspatialanalysis.github.io/closecity-python/

Install

Will soon be on PyPI; install from GitHub until then:

pip install git+https://github.com/henryspatialanalysis/closecity-python.git
# add the census-block boundary downloader:
pip install "closecity[tiger] @ git+https://github.com/henryspatialanalysis/closecity-python.git"

This pulls in httpx, pandas, and geopandas, so results come back as data frames out of the box: a GeoDataFrame where geometry applies, a plain DataFrame otherwise.

A first call

You make requests through a client. Routes with geometry come back as GeoDataFrames, so you can map them right away.

from closecity import Client, close_map

# The key (ck_live_) comes from https://account.close.city (5,000 free tokens
# on signup, no card). You can also set the CLOSECITY_KEY environment variable
# and call Client() with no argument.
close = Client("ck_live_your_key")   # use your own key here

# Supermarkets within a 1.5 km walk of a point (type 30 is grocery stores):
supermarkets = close.pois_search(lat = 41.823, lon = -71.412, radius_m = 1500, type = 30)
close_map(supermarkets, color = "#e8590c")   # interactive map, bright hoverable points

Catalog and lookup routes are free, need no key, and come back as data frames:

close = Client()
close.modes()                  # walk, bike, transit
close.places("Providence")     # a city name to its GEOID and centre

Key terms

A few terms come up throughout the API:

  • Census block. The smallest area the Census Bureau publishes. Each one has a 15-digit id called a GEOID.
  • Destination type. A category of place, such as grocery stores or libraries. Each type has a numeric id. Look them up with close.destination_types().
  • Mode. How someone travels: walk, bike, or transit.
  • Isochrone or catchment: the area you can reach starting from a point within a time limit, by a selected travel mode.

Choose an output

Set output on the client, or per call:

  • output = "spatial" (the default) returns a GeoDataFrame for inherently spatial data and a DataFrame otherwise. Block routes join census-block boundaries with pygris (the tiger extra), downloaded once and cached.
  • output = "tabular" returns a plain DataFrame for every route and never downloads boundaries. Reach for it when you only want the numbers.
  • output = "raw" returns the underlying Reply / Paginator, with the parsed body on .data and the token counts alongside.
close = Client("ck_live_your_key", output = "raw")   # use your own key here
for poi in close.pois_search(lat = 41.823, lon = -71.412, radius_m = 1500):
    print(poi["name"])

Handling errors

Problem responses become typed exceptions. Catch a specific one, or the CloseAPIError base.

from closecity import TokensExhaustedError, CloseAPIError

try:
    close.block_summary("000000000000000")
except TokensExhaustedError:
    ...
except CloseAPIError as err:
    print(err.status, err.slug)

The client does not retry automatically. On a RateLimitedError or ServiceUnavailableError, wait err.retry_after seconds (from the Retry-After header) and retry the request yourself.

Reference

Development

pip install -e '.[dev]'
pytest        # unit tests, no network (httpx MockTransport)
ruff check src tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

closecity-1.5.0.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

closecity-1.5.0-py3-none-any.whl (23.9 kB view details)

Uploaded Python 3

File details

Details for the file closecity-1.5.0.tar.gz.

File metadata

  • Download URL: closecity-1.5.0.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for closecity-1.5.0.tar.gz
Algorithm Hash digest
SHA256 86b069a3d7980a494da34a9ab17d5e50f02ace0553348438f62439edf03226eb
MD5 69613f3823b77cbc39676f35269431ed
BLAKE2b-256 3ab7211c650a6f7d9755d18a224cd9a05437b934c4d5ace532a4b5c89ee25fa1

See more details on using hashes here.

Provenance

The following attestation bundles were made for closecity-1.5.0.tar.gz:

Publisher: publish.yml on henryspatialanalysis/closecity-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file closecity-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: closecity-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 23.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for closecity-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1311431c7628ea3851379b4c37810ed6a962260dc4f296db35df9071f995349c
MD5 81a0a4d0df8f40b9d4607b019f72e005
BLAKE2b-256 f3017873dc8e792c1d9e343ff8459f5a7ebd51bb03c414fe5c6f70bf5cdf3577

See more details on using hashes here.

Provenance

The following attestation bundles were made for closecity-1.5.0-py3-none-any.whl:

Publisher: publish.yml on henryspatialanalysis/closecity-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page