项目作者: yannforget

项目描述 :
Fetch OpenStreetMap features and export them as GeoJSON.
高级语言: Python
项目地址: git://github.com/yannforget/OSMxtract.git
创建时间: 2018-06-12T18:36:21Z
项目社区:https://github.com/yannforget/OSMxtract

开源协议:MIT License

下载


OSMxtract

Description

Note: in development, do not use in production.

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:

  1. 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.
  1. Usage: osmxtract [OPTIONS] OUTPUT
  2. Extract GeoJSON features from OSM with the Overpass API.
  3. Options:
  4. --fromfile PATH Bounding box from input file.
  5. --latlon FLOAT... Space-separated lat/lon coordinates.
  6. --address TEXT Address to geocode.
  7. --buffer INTEGER Buffer size in meters around lat/lon or
  8. address.
  9. --tag TEXT OSM tag of interest (ex: "highway").
  10. --values TEXT Comma-separated list of possible values (ex:
  11. "tertiary,primary").
  12. --case-insensitive Make the first character of each value case
  13. insensitive.
  14. --geom [point|linestring|polygon|multipolygon]
  15. Output geometry type.
  16. --help Show this message and exit.

Examples

  1. # buildings around the "Université Libre de Bruxelles" as polygons
  2. # save features in the file `buildings.geojson`. since no values
  3. # are provided, all non-null values are accepted for the tag
  4. # "highway" are accepted.
  5. osmxtract --address "Université Libre de Bruxelles" --buffer 5000 \
  6. --tag building --geom polygon buildings.geojson
  7. # primary, secondary and tertiary roads based on the extent
  8. # of an existing raster. save the result as linestrings in the
  9. # `major_roads.geojson` file. we use the `--case-insensitive`
  10. # flag to get roads tagged as "primary" as well as "Primary".
  11. osmxtract --fromfile map.tif --tag highway \
  12. --values "primary,secondary,tertiary" \
  13. --case-insensitive --geom linestring \
  14. major_roads.geojson
  15. # cafes and bars near "Atomium, Brussels"
  16. osmxtract --address "atomium, brussels" --buffer 1000 \
  17. --tag amenity --values "cafe,bar" --geom point \
  18. cafes_and_bars.geojson

API

  1. import json
  2. from osmxtract import overpass, location
  3. import geopandas as gpd
  4. # Get bounding box coordinates from a 2km buffer
  5. # around the Atomium in Brussels
  6. lat, lon = location.geocode('Atomium, Brussels')
  7. bounds = location.from_buffer(lat, lon, buffer_size=2000)
  8. # Build an overpass QL query and get the JSON response
  9. query = overpass.ql_query(bounds, tag='amenity', values=['cafe', 'bar'])
  10. response = overpass.request(query)
  11. # Process response manually...
  12. for elem in response['elements']:
  13. print(elem['tags'].get('name'))
  14. # Output:
  15. # Au Bon Coin
  16. # Aux 4 Coins du Monde
  17. # Excelsior
  18. # Welcome II
  19. # Heymbos
  20. # Games Café
  21. # Stadium
  22. # Le Beau Rivage
  23. # The Corner
  24. # None
  25. # Expo
  26. # Koning
  27. # Centrum
  28. # St. Amands
  29. # Bij Manu
  30. # ...or parse them as GeoJSON
  31. feature_collection = overpass.as_geojson(response, 'point')
  32. # Write as GeoJSON
  33. with open('cafes_and_bars.geojson', 'w') as f:
  34. json.dump(feature_collection, f)
  35. # To GeoPandas GeoDataFrame:
  36. geodataframe = gpd.GeoDataFrame.from_features(feature_collection)