osm_flex.clip

This file is part of OSM-flex. Copyright (C) 2023 OSM-flex contributors listed in AUTHORS. OSM-flex is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. OSM-flex is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. —– clipping functions

Module Contents

Functions

get_admin1_shapes(country)

Provide Natural Earth registry info and shape files for countries

get_country_shape(country)

Provide Natural Earth registry info and shape files for admin1 regions

_simplify_shapelist(geom_list[, thres])

remove tiny shapes and simplify outlines to save on file size for

_shapely2poly(geom_list, filename)

Convert list of shapely (multi)polygon(s) into .poly files needed for

_build_osmconvert_cmd(shape, osmpbf_clip_from, ...)

builds osmconvert command for clipping

_osmconvert_clip(shape, osmpbf_clip_from, osmpbf_output)

_build_osmosis_cmd(shape, osmpbf_clip_from, osmpbf_output)

builds osmosis command for clipping

_osmosis_clip(shape, osmpbf_clip_from, osmpbf_output)

Runs the command line tool osmosis to cut out all map info within

clip_from_bbox(bbox, osmpbf_clip_from, osmpbf_output)

get OSM raw data from abounding-box, which is extracted

clip_from_poly(poly_file, osmpbf_clip_from, osmpbf_output)

get OSM raw data from a custom shape defined in .poly file which is clipped

clip_from_shapes(shape_list, osmpbf_clip_from, ...[, ...])

get OSM raw data from a custom shape defined by a list of polygons

Attributes

LOGGER

osm_flex.clip.LOGGER[source]
osm_flex.clip.get_admin1_shapes(country)[source]

Provide Natural Earth registry info and shape files for countries

Parameters

countrystr

string of ISO 3166 code

Returns

country_shapesdict

Shapes (according to Natural Earth) of admin1 regions of country with name as keys

osm_flex.clip.get_country_shape(country)[source]

Provide Natural Earth registry info and shape files for admin1 regions of chosen country

Parameters

countrystr

string of ISO 3166 code

Returns

country_shape(multi-)polygon

Shape of the country according to Natural Earth.

osm_flex.clip._simplify_shapelist(geom_list, thres=None)[source]

remove tiny shapes and simplify outlines to save on file size for .poly files

osm_flex.clip._shapely2poly(geom_list, filename)[source]

Convert list of shapely (multi)polygon(s) into .poly files needed for osmosis to generate cut-outs from bigger osm.pbf files.

Parameters

geom_listlist

list of polygon, polygons or multipolygons containing a (complex) shape to be cut out of a bigger file

filenamepathlib.Path or str

output filename including directory path.

Returns

None

Note

For more info on what .poly files are (incl. several tools for creating them), see https://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format

osm_flex.clip._build_osmconvert_cmd(shape, osmpbf_clip_from, osmpbf_output)[source]

builds osmconvert command for clipping

Parameters

shapelist or str or pathlib.Path

list containing [xmin, ymin, xmax, ymax] for a bounding box or a string/Path to the .poly file path delimiting the bounds.

osmpbf_clip_from: str or pathlib.Path

file path to planet.osm.pbf or other osm.pbf file to clip

osmpbf_outputstr or pathlib.Path

file path (incl. name & ending) under which extract will be stored

Note

If cross-border multi-polygons or lines should be retained, comment out below options ‘–complete-ways’ and ‘–complete-multipolygons’. This only works with parent files < 2GB.

osm_flex.clip._osmconvert_clip(shape, osmpbf_clip_from, osmpbf_output, overwrite=False)[source]
osm_flex.clip._build_osmosis_cmd(shape, osmpbf_clip_from, osmpbf_output)[source]

builds osmosis command for clipping

Parameters

shapelist or str or pathlib.Path

list containing [xmin, ymin, xmax, ymax] for a bounding box or a string/Path to the .poly file path delimiting the bounds.

osmpbf_clip_from: str or pathlib.Path

file path to planet.osm.pbf or other osm.pbf file to clip

osmpbf_outputstr or pathlib.Path

file path (incl. name & ending) under which extract will be stored

osm_flex.clip._osmosis_clip(shape, osmpbf_clip_from, osmpbf_output, overwrite=False)[source]

Runs the command line tool osmosis to cut out all map info within shape (bounding box or poygon(s)), from a bigger parent file.

If your device doesn’t have osmosis yet, see installation instructions: https://wiki.openstreetmap.org/wiki/Osmosis/Installation

Parameters

shapelist or str or pathlib.Path

list containing [xmin, ymin, xmax, ymax] for a bounding box or a string/Path to the .poly file path delimiting the bounds.

osmpbf_clip_from: str or pathlib.Path

file path to planet.osm.pbf or other osm.pbf file to clip

osmpbf_outputstr or pathlib.Path

file path (incl. name & ending) under which extract will be stored

overwritebool

default is False. Whether to overwrite files if they already exist.

Returns

None or subprocess

osm_flex.clip.clip_from_bbox(bbox, osmpbf_clip_from, osmpbf_output, overwrite=False, kernel='osmosis')[source]

get OSM raw data from abounding-box, which is extracted from a bigger (e.g. the planet) file.

Parameters

bboxlist

bounding box [xmin, ymin, xmax, ymax]

osmpbf_clip_from: str or pathlib.Path

file path to planet.osm.pbf or other osm.pbf file to clip

osmpbf_outputstr or pathlib.Path

file path (incl. name & ending) under which extract will be stored

overwritebool

default is False. Whether to overwrite files if they already exist.

kernelstr

name of the clipping kernel: ‘osmconvert’ or ‘osmosis’ Default is ‘osmosis’.

Note

This function uses the command line tool osmosis to cut out new osm.pbf files from the original ones. Installation instructions (windows, linux, apple) - see https://wiki.openstreetmap.org/wiki/Osmosis/Installation

osm_flex.clip.clip_from_poly(poly_file, osmpbf_clip_from, osmpbf_output, overwrite=False, kernel='osmosis')[source]

get OSM raw data from a custom shape defined in .poly file which is clipped from a .osm.pbf file.

Parameters

poly_filestr

file path to a .poly file

osmpbf_outputstr or pathlib.Path

file path (incl. name & ending) under which extract will be stored

clip_from_filestr or pathlib.Path

file path to planet-latest.osm.pbf. Will download & store it as indicated, if doesn`t yet exist. Default is DATA_DIR/planet-latest.osm.pbf

overwritebool

default is False. Whether to overwrite files if they already exist.

kernelstr

name of the clipping kernel: ‘osmconvert’ or ‘osmosis’ Default is ‘osmosis’.

Note

This function uses the command line tool osmosis or osmconvert to clip new osm.pbf files from the original ones. Installation instructions (windows, linux, apple) - see https://wiki.openstreetmap.org/wiki/Osmosis/Installation or https://wiki.openstreetmap.org/wiki/Osmconvert

osm_flex.clip.clip_from_shapes(shape_list, osmpbf_clip_from, osmpbf_output, overwrite=False, kernel='osmosis')[source]

get OSM raw data from a custom shape defined by a list of polygons which is extracted from the entire OSM planet file. The list of shapes first needs to be converted to a .poly file and then passed back to the function (under the hood, a temporary file is created and deleted upon completion again).

Parameters

shape_listlist

list of (Multi-)Polygon(s) that define the shape which should be cut, as e.g. obtained

osmpbf_outputstr

Full file path under which the clipped data will be stored.

osmpbf_clip_fromstr or pathlib.Path

file path (including filename) to the *.osm.pbf file to clip from.

overwritebool

default is False. Whether to overwrite files if they already exist.

kernelstr

name of the clipping kernel: ‘osmconvert’ or ‘osmosis’ Default is ‘osmosis’.

Note

This function uses the command line tool osmosis or osmconvert to clip new osm.pbf files from the original ones. Installation instructions (windows, linux, apple) - see https://wiki.openstreetmap.org/wiki/Osmosis/Installation or https://wiki.openstreetmap.org/wiki/Osmconvert