Configuration¶
IduEdu keeps its runtime settings in one global object, iduedu.config. Downloaders and builders read it on
every call, so a change applies to everything that runs after it in the same process. Most settings can also
be given as environment variables, which are read once when iduedu is imported.
from iduedu import config
config.to_dict() # a snapshot of every current setting
Overpass endpoint and requests¶
from iduedu import config
config.set_overpass_url("https://overpass-api.de/api/interpreter")
config.set_timeout(120)
config.set_rate_limit(min_interval=2.0, max_retries=5, backoff_base=2.0)
set_overpass_urlaccepts anyhttp://orhttps://Overpass interpreter, for example a self-hosted instance or a mirror.set_timeoutsets the request timeout in seconds. The same value is sent to Overpass as[timeout:...]in every query.set_rate_limitspaces requests at leastmin_intervalseconds apart. Before each attempt IduEdu asks the instance’s/statusendpoint for a free slot and waits for it. Network errors and HTTP 429, 502, 503 and 504 responses are retried up tomax_retriestimes with an exponentially growing delay based onbackoff_base; a 429 response honours the server’sRetry-Afterheader.config.user_agentis sent with every request. Set it to something that identifies your project:config.user_agent = "my-project/1.0 (me@example.org)".config.proxies(arequests-style mapping such as{"https": "http://proxy:3128"}) andconfig.verify_sslare passed to every request.
The public Overpass instance is shared by many users. Keep the default pacing unless you run your own instance.
Overpass cache¶
IduEdu caches raw Overpass JSON responses on disk: boundaries, street-network queries, route relations and
their members. The cache is enabled by default and lives in .iduedu_cache relative to the current working
directory.
from iduedu import config
config.set_overpass_cache(enabled=False) # ignore the cache completely
config.set_overpass_cache(cache_dir="/tmp/overpass_cache", enabled=True)
The cache stores responses only, never processed graphs. Clear the directory or disable the cache to force fresh downloads, for example after OSM data you depend on has been edited.
Historical snapshots¶
Overpass can answer a query as OpenStreetMap stood at a given moment:
from iduedu import config
config.set_overpass_date(date="2020-01-01") # 2020-01-01T00:00:00Z
config.set_overpass_date(date="2020-01-01T12:34:56Z")
config.set_overpass_date(year=2020, month=5) # 2020-05-01T00:00:00Z
config.set_overpass_date() # back to current data
While a date is set, subway routes are queried as plain route relations: stop areas, stations and entrances are skipped because Overpass does not reliably support those queries at arbitrary timestamps, and a warning is logged.
Local OSM files¶
from iduedu import config
config.set_osm_source("pbf", path="city.osm.pbf") # every builder reads the file
config.set_osm_source("overpass") # back to the default
In pbf mode the Overpass cache is not used and overpass_date is ignored. Data sources describes
use_pbf, downloads from Geofabrik, per-city files and the differences from Overpass.
Progress bars and logging¶
from iduedu import config
config.set_enable_tqdm(False) # hide progress bars while public-transport routes are parsed
config.configure_logging(level="INFO")
config.configure_logging(level="DEBUG", to_file="iduedu.log")
IduEdu logs through loguru, available as config.logger. In a fresh
interpreter IduEdu replaces the default loguru sink on import, so that its own messages appear from LOG_LEVEL
(WARNING by default) and the messages of other packages keep the default level and format. An application that
set up loguru before importing IduEdu keeps its sinks untouched and receives the IduEdu messages there; it can
mute them with logger.disable("iduedu"). configure_logging replaces only the sinks it added before and the
default loguru sink, then writes IduEdu messages of level and above to standard error and, with to_file, to a
file rotated at 20 MB and kept for 14 days. json=True writes the bare message without time and level, for
collectors that add their own.
Progress bars are shown when standard error is a terminal or the code runs in Jupyter, and hidden in services
and scheduled jobs; ENABLE_TQDM or set_enable_tqdm overrides this.
Environment variables¶
Variable |
Default |
Equivalent setting |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unset |
|
|
|
|
|
|
|
|
on in a terminal or Jupyter; |
|
|
|
|
|
|
|
|
unset |
|
export OVERPASS_URL="https://overpass.example.org/api/interpreter"
export OVERPASS_CACHE_DIR="/tmp/overpass_cache"
export LOG_LEVEL="INFO"
An invalid OVERPASS_DATE is ignored with a warning, and the import goes on.
API reference¶
- class iduedu.config.Config[source]¶
Global runtime settings for the OSM data source, Overpass, caching, logging and graph builders.
- configure_logging(level='INFO', *, to_file=None, json=False)[source]¶
Write IduEdu messages of
leveland above to standard error and, withto_file, to a file.Only the sinks added by an earlier call and the default sink of loguru are replaced; sinks added by the application stay. When the default sink is replaced, messages of other packages keep its level and format.
- set_overpass_url(url)[source]¶
Set active Overpass URL (validated), keeps the pool intact.
- Parameters:
url (str)
- set_overpass_cache(*, enabled=None, cache_dir=None)[source]¶
Managing the file cache of Overpass requests. enabled=True -> read/write cache enabled=False -> ignore cache completely
- set_timeout(timeout)[source]¶
Set the default Overpass request timeout in seconds.
- Parameters:
timeout (
int) – Positive timeout value in seconds.- Raises:
ValueError – If
timeoutis not positive.
- set_overpass_date(date=None, *, year=None, month=None, day=None)[source]¶
Set the snapshot date for Overpass queries.
The date is stored in ISO 8601 UTC format:
YYYY-MM-DDTHH:MM:SSZand used in the Overpass header as[date:"..."]. Calling this method allows you to fix the OSM data to a specific historical snapshot or reset back to the “current” data.- Usage examples:
set_overpass_date()orset_overpass_date(None)- reset the date (no[date:...]in queries).set_overpass_date(date="2020-01-01")->2020-01-01T00:00:00Z.set_overpass_date(date="2020-01-01T12:34:56Z")-> stores the normalized value of this timestamp.set_overpass_date(year=2020)->2020-01-01T00:00:00Z.set_overpass_date(year=2020, month=5)->2020-05-01T00:00:00Z.set_overpass_date(year=2020, month=5, day=10)->2020-05-10T00:00:00Z.
- Parameters:
date (
str|None) – A date string inYYYY-MM-DDorYYYY-MM-DDTHH:MM:SSZformat. If provided,year,monthanddaymust all beNone.year (
int|None) – Year component of the date. If provided withoutdate, at least the year must be set;monthanddayare optional and default to January 1 if omitted.month (
int|None) – Month component of the date (1-12). Optional when used withyear. Defaults to 1 (January) if not provided.day (
int|None) – Day component of the date (1-31). Optional when used withyearandmonth. Defaults to 1 if not provided.
- Raises:
ValueError – If both
dateand any ofyear,month,dayare provided, if the date string is not in a supported format, or if the constructed date is invalid.
- set_osm_source(source, *, path=None)[source]¶
Choose where OpenStreetMap data comes from.
"overpass"(the default) sends queries to the Overpass API."pbf"answers the same queries from a local.osm.pbffile, read by IduEdu’s Rust core; the Overpass cache andoverpass_dateare not used then.- Parameters:
- Raises:
ValueError – For an unknown source, or
"pbf"without a file.FileNotFoundError – If the file does not exist.
- use_pbf(path=None, *, osm_id=None, territory=None, region=None, **download_kwargs)[source]¶
Temporarily read OSM data from a local file; the previous source is restored on exit.
The file is either given as
pathor downloaded for a territory:osm_id,territory,regionand any other keyword argument go unchanged todownload_osm_pbf(), which picks the Geofabrik extract, caches it and cuts the territory out. A file that is already cached is reused without any download. Also available asiduedu.use_pbf.- Parameters:
osm_id (
int|None) – OSM relation id of the territory to download, instead ofpath.territory (
Polygon|MultiPolygon|GeoDataFrame|GeoSeries|None) – Territory polygon in EPSG:4326, or a GeoDataFrame/GeoSeries, instead ofpath.region (
str|None) – Geofabrik region id to cut the territory from; chosen automatically when omitted.**download_kwargs – Further arguments of
download_osm_pbf()(update,out_dir,name, …).
- Raises:
ValueError – If both
pathand download arguments are given, or neither.- Return type:
Example
>>> with config.use_pbf("berlin.osm.pbf"): ... walk = get_walk_graph(territory=polygon) >>> with iduedu.use_pbf(osm_id=1114252, region="northwestern-fed-district"): ... graph = get_intermodal_graph(osm_id=1114252)
- build_overpass_header(*, timeout=None, date=None)[source]¶
Build the Overpass query header for JSON requests.
- Parameters:
- Return type:
- Returns:
Header string such as
[out:json][timeout:120];or[out:json][timeout:120][date:"2020-01-01T00:00:00Z"];.- Raises:
ValueError – If the effective timeout is not positive.
- set_enable_tqdm(enable)[source]¶
Enable or disable progress bars for long-running operations.
- Parameters:
enable (bool)
- set_transport_useful_edges_attr(attr)[source]¶
Set OSM edge tags retained by public-transport graph builders.
- set_transport_route_attr(attr)[source]¶
Set the tags of OSM route relations kept in the
routestable of public-transport graphs.
- set_rate_limit(*, min_interval=None, max_retries=None, backoff_base=None)[source]¶
Configure Overpass request pacing and retry behavior.
- Parameters:
- Raises:
ValueError – If any provided value is outside its accepted range.