Python open osm file

Accessing OSM Data in Python#

OpenStreetMap (OSM) is a global collaborative (crowd-sourced) dataset and project that aims at creating a free editable map of the world containing a lot of information about our environment 1. It contains data for example about streets, buildings, different services, and landuse to mention a few. You can view the map at www.openstreetmap.org. You can also sign up as a contributor if you want to edit the map. More details about OpenStreetMap and its contents are available in the OpenStreetMap Wiki.

OSMnx#

This week we will explore a Python module called OSMnx that can be used to retrieve, construct, analyze, and visualize street networks from OpenStreetMap, and also retrieve data about Points of Interest such as restaurants, schools, and lots of different kind of services. It is also easy to conduct network routing based on walking, cycling or driving by combining OSMnx functionalities with a package called NetworkX.

To get an overview of the capabilities of the package, see an introductory video given by the lead developer of the package, Prof. Geoff Boeing: “Meet the developer: Introduction to OSMnx package by Geoff Boeing”.

Читайте также:  Commons lang java jar

Download and visualize OpenStreetMap data with OSMnx#

One the most useful features that OSMnx provides is an easy-to-use way of retrieving OpenStreetMap data (using OverPass API).

In this tutorial, we will learn how to download and visualize OSM data covering a specified area of interest: the neighborhood of Edgewood in Washington DC USA.

# Specify the name that is used to seach for the data place_name = "Edgewood Washington, DC, USA" 

OSM Location Boundary#

Let’s also plot the Polygon that represents the boundary of our area of interest (Washington DC). We can retrieve the Polygon geometry using the ox.geocode_to_gdf [docs](https://osmnx.readthedocs.io/en/stable/osmnx.html?highlight=geocode_to_gdf(#osmnx.geocoder.geocode_to_gdf) function.

# import osmnx import osmnx as ox import geopandas as gpd # Get place boundary related to the place name as a geodataframe area = ox.geocode_to_gdf(place_name) 

As the name of the function already tells us, gdf_from_place() returns a GeoDataFrame based on the specified place name query.

Let’s still verify the data type:

# Check the data type type(area) 

OSM Building footprints#

It is also possible to retrieve other types of OSM data features with OSMnx such as buildings or points of interest (POIs). Let’s download the buildings with ox.geometries_from_place docs function and plot them on top of our street network in Kamppi.

When fetching spesific types of geometries from OpenStreetMap using OSMnx geometries_from_place we also need to specify the correct tags. For getting all types of buildings, we can use the tag building=yes .

# List key-value pairs for tags tags = 'building': True> buildings = ox.geometries_from_place(place_name, tags) buildings.head() 

We can plot the footprints quickly.

# Plot footprints buildings.plot() 

OSM Write Features to .shp#

Now let’s assume we want to access this data outside of python, or have a permanent copy of our building footprints for Edgewood.

Since these objects are already geopandas.GeoDataFrame it’s easy to save them to disk. We simply use gpd.to_file docs.

We can’t write OSM GeoDataFrames directly to disk because they contain field types (like lists) that can’t be saved in .shp or .geojsons etc. Instead lets isolate only the attributes we are interested in, including geometry which is required.

We need to isolate just the attributes we are interested in:

buildings = buildings.loc[:,buildings.columns.str.contains('addr:|geometry')] 

OSM data often contains multiple feature types like mixing points with polygons. This is a problem when we try to write it to disk.

We also need to isolate the feature type we are looking for [e.g. Multipolygon, Polygon, Point]. Since here we want building footprints we are going to keep only polygons.

buildings = buildings.loc[buildings.geometry.type=='Polygon'] 

Now, finally, we can write it to disk.

# Save footprints buildings.to_file('../temp/edgewood_buildings.shp') # Or save in a more open source format #buildings.to_file('../temp/edgewood_buildings.geojson', driver='GeoJSON') 

Window Operations with Rasterio and GeoWombat

Accessing Census and ACS Data in Python

By Michael Mann, Steven Chao, Jordan Graesser, Nina Feldman

Источник

Pyrosm¶

Pyrosm is a Python library for reading OpenStreetMap from Protocolbuffer Binary Format -files ( * .osm.pbf) into Geopandas GeoDataFrames. Pyrosm makes it easy to extract various datasets from OpenStreetMap pbf-dumps including e.g. road networks, buildings, Points of Interest (POI), landuse, natural elements, administrative boundaries and much more. Fully customized queries are supported which makes it possible to parse any kind of data from OSM, even with more specific filters.

Pyrosm is easy to use and it provides a somewhat similar user interface as OSMnx. The main difference between pyrosm and OSMnx is that OSMnx reads the data using an OverPass API, whereas pyrosm reads the data from local OSM data dumps that are downloaded from the PBF data providers (Geofabrik, BBBike). This makes it possible to parse OSM data faster and make it more feasible to extract data covering large regions.

For instance, parsing all roads from the state of New York (USA) with a “basic” work laptop (16GB memory, SSD, and Intel Core i5 CPU), takes less than 3 minutes and parsing the buildings from the same region takes less than 4 minutes (see benchmarks for details):

_images/NY_roads_and_buildings.PNG

Current features¶

  • download PBF data easily from hundreds of locations across the world
  • read street networks (separately for driving, cycling, walking and all-combined)
  • read buildings from PBF
  • read Points of Interest (POI) from PBF
  • read landuse from PBF
  • read “natural” from PBF
  • read boundaries from PBF (such as administrative borders)
  • read any other data from PBF by using a custom user-defined filter
  • filter data based on bounding box
  • export networks as a directed graph to igraph , networkx and pandana

When should I use Pyrosm?¶

Pyrosm can of course be used whenever you need to parse data from OSM into geopandas GeoDataFrames. However, pyrosm is better suited for situations where you want to fetch data for whole city or larger regions (even whole country).

If you are interested to fetch OSM data for smaller areas such as neighborhoods, or search data around a specific location/address, we recommend using OSMnx which is more flexible in terms of specifying the area of interest. That being said, it is also possible to extract neighborhood level information with pyrosm and filter data based on a bounding box (see docs).

License¶

Pyrosm is licensed under MIT (see license).

Getting started¶

  • Installation
    • Troubleshooting
    • Protobuf file: What is it and how to get one?
      • Available datasets
      • What to do if you cannot find the data for your area of interest?
      • Controlling which OSM attributes are parsed into columns
      • Notes about Pyrosm graph building
      • Read nodes and edges (first step)
      • Export to iGraph
      • Export to NetworkX / OSMnx
      • Export to Pandana
      • to_graph parameters explained
      • Constructing a custom filter
        • Different kind of filters
          • Filter A
          • Filter B
          • Filter C
          • Controlling which OSM element types are returned
          • keep vs exclude data with custom filters
          • Find shortest path with Pyrosm + NetworkX + OSMnx
            • Calculate shortest paths
            • Find nearest X number of POIs for each network node
            • Calculate the job accessibility
            • Parsing large datasets
            • Eight Steps for Contributing
              • 1. Fork the pyrosm git repository
              • 2. Create a development environment
              • 3. Installing Dependencies
              • 4. Making a development build
              • 5. Making changes and writing tests
                • Writing tests
                • Running the test suite
                • Updating User Guide
                • Python (PEP8 / black)
                • Reference to All Attributes and Methods
                • Changelog
                  • v0.6.1 (Oct 11, 2021)
                  • v0.6.0 (Nov 18, 2020)
                  • v0.5.3 (Sep 13, 2020)
                  • v0.5.1/2 (May 11, 2020)
                  • v0.5.0 (May 7, 2020)
                  • v0.4.3 (April 27, 2020)
                  • v0.4.2 (April 23, 2020)
                  • v0.4.1 (April 17, 2020)
                  • v0.4.0 (April 16, 2020)
                  • v0.3.1 (April 15, 2020)
                  • v0.2.0 (April 13, 2020)
                  • v0.1.8 (April 8, 2020)
                  • v0.1.0 (April 7, 2020)

                  Источник

                  Saved searches

                  Use saved searches to filter your results more quickly

                  You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

                  Simple library for reading OpenStreetMap XML and PBF data files

                  dezhin/osmread

                  This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

                  Name already in use

                  A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

                  Sign In Required

                  Please sign in to use Codespaces.

                  Launching GitHub Desktop

                  If nothing happens, download GitHub Desktop and try again.

                  Launching GitHub Desktop

                  If nothing happens, download GitHub Desktop and try again.

                  Launching Xcode

                  If nothing happens, download Xcode and try again.

                  Launching Visual Studio Code

                  Your codespace will open once ready.

                  There was a problem preparing your codespace, please try again.

                  Latest commit

                  Git stats

                  Files

                  Failed to load latest commit information.

                  README.md

                  Osmread is a simple library for reading OpenStreetMap data files in Python. It supports XML and PBF file formats as inputs. It is not designed for fast processing of big planet dumps but can be used for simple processing of smaller files such as regional extracts.

                  If you need fast processing of large files look at imposm.parser library.

                  Simply run setup.py or use pip or easy_install .

                  from osmread import parse_file, Way highway_count = 0 for entity in parse_file('foo.osm.bz2'): if isinstance(entity, Way) and 'highway' in entity.tags: highway_count += 1 print("%d highways found" % highway_count) 

                  All Node , Way and Relation instances have id , version , changeset , timestamp , uid and tags attributes. Node coordinates stored in lon and lat . Way nodes stored in nodes , relation members in members .

                  Relation members are array of tuples ( role , class , id ), where class is Node , Way or Relation .

                  This library uses very slow protobuf implementation — pure python protobuf library. As a result parsing PBF files is slower than XML. This may change in future versions.

                  About

                  Simple library for reading OpenStreetMap XML and PBF data files

                  Источник

Оцените статью