Skip to main content

OSMxtract

Description

OSMxtract is a simple Python package that uses the Overpass API to fetch OpenStreetMap features and export them in a GeoJSON file.

Installation

Using pip:

pip install osmxtract

Command-line interface

Usage

OSMxtract can guess the extent of your query based on three different options:

  • --fromfile: use the bounds from the input vector or raster file ;
  • --latlon and --buffer: use the bounds of a buffer around a given point ;
  • --address and --buffer: use the bounds of a buffer around a geocoded address.
Usage: osmxtract [OPTIONS] OUTPUT

  Extract GeoJSON features from OSM with the Overpass API.

Options:
  --fromfile PATH                 Bounding box from input file.
  --latlon FLOAT...               Space-separated lat/lon coordinates.
  --address TEXT                  Address to geocode.
  --buffer INTEGER                Buffer size in meters around lat/lon or
                                  address.
  --tag TEXT                      OSM tag of interest (ex: "highway").
  --values TEXT                   Comma-separated list of possible values (ex:
                                  "tertiary,primary").
  --case-insensitive              Make the first character of each value case
                                  insensitive.
  --geom [point|linestring|polygon|multipolygon]
                                  Output geometry type.
  --help                          Show this message and exit.

Examples

# buildings around the "Université Libre de Bruxelles" as polygons
# save features in the file `buildings.geojson`. since no values
# are provided, all non-null values are accepted for the tag
# "highway" are accepted.
osmxtract --address "Université Libre de Bruxelles" --buffer 5000 \
          --tag building --geom polygon buildings.geojson

# primary, secondary and tertiary roads based on the extent
# of an existing raster. save the result as linestrings in the
# `major_roads.geojson` file. we use the `--case-insensitive`
# flag to get roads tagged as "primary" as well as "Primary".
osmxtract --fromfile map.tif --tag highway \
          --values "primary,secondary,tertiary" \
          --case-insensitive --geom linestring \
          major_roads.geojson

# cafes and bars near "Atomium, Brussels" 
osmxtract --address "atomium, brussels" --buffer 1000 \
          --tag amenity --values "cafe,bar" --geom point \
          cafes_and_bars.geojson

API

import json
from osmxtract import overpass, location
import geopandas as gpd

# Get bounding box coordinates from a 2km buffer
# around the Atomium in Brussels
lat, lon = location.geocode('Atomium, Brussels')
bounds = location.from_buffer(lat, lon, buffer_size=2000)

# Build an overpass QL query and get the JSON response
query = overpass.ql_query(bounds, tag='amenity', values=['cafe', 'bar'])
response = overpass.request(query)

# Process response manually...
for elem in response['elements']:
    print(elem['tags'].get('name'))

# Output:
# Au Bon Coin
# Aux 4 Coins du Monde
# Excelsior
# Welcome II
# Heymbos
# Games Café
# Stadium
# Le Beau Rivage
# The Corner
# None
# Expo
# Koning
# Centrum
# St. Amands
# Bij Manu

# ...or parse them as GeoJSON
feature_collection = overpass.as_geojson(response, 'point')

# Write as GeoJSON
with open('cafes_and_bars.geojson', 'w') as f:
    json.dump(feature_collection, f)

# To GeoPandas GeoDataFrame:
geodataframe = gpd.GeoDataFrame.from_features(feature_collection)

Release files for osmxtract 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for osmxtract 0.0.1
File Size Uploaded
osmxtract-0.0.1.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for osmxtract 0.0.1
File Interpreter ABI Platform
osmxtract-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / osmxtract-0.0.1.tar.gz

Download URL osmxtract-0.0.1.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1d81d413e06079500d29587fdff0c0e2ad3e30ef5333ea3db03517f1e7d14bba
BLAKE2b-256 checksum
How to use checksums
0830b230da48e6674d3f5bb6e171f3e1e947e4635fdf5b83b3cfbbdf84631509
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / osmxtract-0.0.1-py3-none-any.whl

Download URL osmxtract-0.0.1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a813c475377fadcf4a62ec48052d44bc56b5ffb0c056b026a59ff7c94f1feef
BLAKE2b-256 checksum
How to use checksums
0b6862ca532b56a4548eeb78f9be4396e58a8c5d927d8aedba7711159e419211
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page