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 withpygris(thetigerextra), 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 underlyingReply/Paginator, with the parsed body on.dataand 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
- Documentation: https://henryspatialanalysis.github.io/closecity-python/
- Interactive API: https://api.close.city/docs
- Machine-readable contract: https://api.close.city/openapi.json
Development
pip install -e '.[dev]'
pytest # unit tests, no network (httpx MockTransport)
ruff check src tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86b069a3d7980a494da34a9ab17d5e50f02ace0553348438f62439edf03226eb
|
|
| MD5 |
69613f3823b77cbc39676f35269431ed
|
|
| BLAKE2b-256 |
3ab7211c650a6f7d9755d18a224cd9a05437b934c4d5ace532a4b5c89ee25fa1
|
Provenance
The following attestation bundles were made for closecity-1.5.0.tar.gz:
Publisher:
publish.yml on henryspatialanalysis/closecity-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
closecity-1.5.0.tar.gz -
Subject digest:
86b069a3d7980a494da34a9ab17d5e50f02ace0553348438f62439edf03226eb - Sigstore transparency entry: 2224394048
- Sigstore integration time:
-
Permalink:
henryspatialanalysis/closecity-python@560831abdbbe5c6207b283220dae0f5850b870a2 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/henryspatialanalysis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@560831abdbbe5c6207b283220dae0f5850b870a2 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1311431c7628ea3851379b4c37810ed6a962260dc4f296db35df9071f995349c
|
|
| MD5 |
81a0a4d0df8f40b9d4607b019f72e005
|
|
| BLAKE2b-256 |
f3017873dc8e792c1d9e343ff8459f5a7ebd51bb03c414fe5c6f70bf5cdf3577
|
Provenance
The following attestation bundles were made for closecity-1.5.0-py3-none-any.whl:
Publisher:
publish.yml on henryspatialanalysis/closecity-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
closecity-1.5.0-py3-none-any.whl -
Subject digest:
1311431c7628ea3851379b4c37810ed6a962260dc4f296db35df9071f995349c - Sigstore transparency entry: 2224394707
- Sigstore integration time:
-
Permalink:
henryspatialanalysis/closecity-python@560831abdbbe5c6207b283220dae0f5850b870a2 -
Branch / Tag:
refs/tags/v1.5.0 - Owner: https://github.com/henryspatialanalysis
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@560831abdbbe5c6207b283220dae0f5850b870a2 -
Trigger Event:
release
-
Statement type: