pyrosm.get_data_by_area#
- pyrosm.get_data_by_area(area, crop=True, update=False, directory=None, output_path=None, headers=None, timeout=60, opener=None, strategy='single', must_cover=None, workers=None, progress=None)#
Download the OSM data for
area: the smallest single extract that contains it, or a smaller set of extracts merged into one file.Compares Geofabrik extracts, BBBike city extracts and Movisda administrative areas and 1°/10° grid tiles, keeps those that contain the whole area (or
must_coverwhen given), and downloads the one with the smallest file (the first row offind_extracts(area, contains_only=True, must_cover=must_cover), which shows the choice without downloading). Withstrategy="smallest_total"it may instead download several extracts that together cover the area and merge them into one file (seestrategy). A download that fails with a network error or HTTP status 408, 425, 429 or 5xx is tried up to three times, waiting 1 s, 2 s or the server’sRetry-Afterin between; when it still fails, the choice is made again without it. By default the file is then cropped to the area’s bounding box, or withcrop="polygon"to the area itself.Movisda cuts its extracts exactly at their edges, so features crossing the edge of a Movisda extract are clipped or missing there: open ways such as streets are cut at the edge and kept. A grid tile can leave out a whole closed way that crosses its edge, such as a building or a land-use area, including its part inside the area. To keep a grid tile’s edges at least N metres outside the area, pass the area buffered by N metres (in a local metric CRS) as
must_cover. In a merged set the Movisda extract goes last, so where another extract holds a complete copy of such a feature, that copy is kept.- Parameters:
area (shapely geometry | GeoDataFrame | GeoSeries | list | tuple | numpy.ndarray) – The area of interest in lon/lat: a (Multi)Polygon, a GeoDataFrame/GeoSeries (its geometries are combined) or
[minx, miny, maxx, maxy].crop (bool or "polygon") – When
True(default), crop the extract to the area’s bounding box and return the cropped file, namedbbox_<minx>_<miny>_<maxx>_<maxy>.osm.pbf. With"polygon", crop it to the area itself, which must be a (Multi)Polygon: a node is kept when it lies inside the area or on its boundary, and a way that has a kept node is kept whole. The file is namedarea_<hash of the area>.osm.pbf. A cropped file of these names is used again instead of cropping anew while it and the extracts it was cropped from are unchanged. WhenFalse, return the full extract.update (bool) – When
True, re-download the extract, refresh the provider indexes and sizes, and crop again.directory (str, optional) – Directory for the downloads, the provider indexes and the cropped file.
None(default) uses a pyrosm temp directory.output_path (str, optional) – Path for the file pyrosm writes (overrides the automatic name), cropped anew on every call: the cropped file, or with
crop=Falsea copy of the extract (a hard link where the file system allows one) or the merged file of a"smallest_total"set.headers – Network options for the index, size and download requests, as for
find_extracts().timeout – Network options for the index, size and download requests, as for
find_extracts().opener – Network options for the index, size and download requests, as for
find_extracts().strategy (str) –
"single"(default): the smallest extract that contains the area."smallest_total": when extracts that together cover the area are smaller in total than that extract (or its size is unknown), download those and merge them withpyrosm.merge_pbf(). The set is found greedily (lowest size per area newly covered), so it is small but not guaranteed to be the smallest possible. Geofabrik, BBBike and Movisda administrative extracts may be combined, at most one of them from Movisda, which goes last in the merge; Movisda grid tiles are never combined. Withcrop=Falsethe merged file is namedmerged_<hash of the URLs>.osm.pbf.must_cover (shapely geometry | GeoDataFrame | GeoSeries, optional) – What the download must cover, in place of the whole area, e.g. the transit stops that routing needs, so sea or unserved edges of the area do not force a larger extract. Points and lines are covered exactly, polygons except for less than 1 m². To also require the streets around each stop, pass the stops buffered in a local metric CRS, e.g.
stops.to_crs(stops.estimate_utm_crs()).buffer(300), or a hull of them to require the streets between stops as well. A GeoDataFrame or GeoSeries in another CRS is reprojected to lon/lat. The crop still followsarea, so parts ofmust_coveroutside it are cropped away.workers (int | str) – Number of threads that crop (and merge) the downloaded file. By default (
None, as"auto") the count is chosen as an out-of-core read does for the file’s size;1runs sequentially. More workers are used only for a file with at least2 * workersdata blocks, and the written file is the same for every value. Withcrop=Falseand a single extract nothing is cropped, so it has no effect. WithPYROSM_DECODE_BACKEND=processesthey are worker processes, one by default; on macOS and Windows each starts by importing the script, so a script that passesworkers="auto"orworkers > 1then needs theif __name__ == "__main__":guard; without it, or for a script read from stdin, the work runs in one process with a warning.progress (bool | callable | None) –
None(default) followspyrosm.enable_logging(), which isTrueunless set.Trueshows the download as a bar on stderr, a widget in Jupyter when ipywidgets is installed (pip install "pyrosm[notebook]"); without a terminal, only a line naming the file is printed.Falseshows nothing. A callable is called asprogress(written, total)after each chunk of each download, with the bytes written so far and the file size,Nonewhen the server does not give it. The search for the extracts and the crop or merge show their progress the same way; a callable is then called with the percentage of the search (seefind_extracts()) or of the crop or merge done (seepyrosm.merge_pbf()).
- Returns:
The file path and the extract it came from; usable as a path.
- Return type:
AreaExtract
- Raises:
ValueError – If the area is empty, or has no width or no height, or
must_coveris empty, orcropis a string other than"polygon", orcrop="polygon"and the area is not a (Multi)Polygon, orworkersis neither an integer of at least 1 nor"auto".pyrosm.exceptions.ExtractNotFoundError – If no extract (or set of extracts) contains the whole area, or
must_coverwhen given (aValueErrorsubclass).pyrosm.exceptions.ExtractDownloadError – If every extract that contains the area failed to download; its
errorshold theDownloadErrorof each extract tried.