2017 © Pedro Peláez
 

library overpass2geojson

Convert overpass JSON to GeoJSON

image

mediasuitenz/overpass2geojson

Convert overpass JSON to GeoJSON

  • Thursday, September 29, 2016
  • by matt-in-a-hat
  • Repository
  • 11 Watchers
  • 4 Stars
  • 554 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 4 % Grown

The README.md

Overpass 2 GeoJSON

Media Suite, (*1)

Build Status, (*2)

composer require mediasuitenz/Overpass2Geojson, (*3)

PHP modules to convert Overpass API JSON output to GeoJSON format, (*4)

Note: this currently only converts either OSM ways and their nodes into a FeatureCollection of Features that have LineString geometries, or OSM nodes into a FeatureCollection of Point features., (*5)

With the ways conversion, if any ways reference nodes that aren't also in the input, those nodes will be ignored. If there are less than 2 nodes for any way, that way will not produce a Feature as LineStrings must have more than 2 coordinates., (*6)

To create a Polygon instead of a LineString you can pass true as the 3rd argument to Overpass2Geojson::convertWays, or set Overpass2Geojson::polygon = true; before using the utility., (*7)

Example

Overpass query

[out:json][timeout:25];
// gather results
(
  // all with highway tag
  way["highway"]
  // within bounding box
  (-43.5594542,172.6998653,-43.5548322,172.708076);
  // recursively get all nodes for the resultant ways (contains the lat/lon whereas ways don't)
  node(w)->.x;
);
out;

PHP

// Example from above
$url = 'http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json][timeout:25];%20(%20way[%22highway%22]%20(-43.5594542,172.6998653,-43.5548322,172.708076);%20node(w)-%3E.x;%20);%20out;';

// cURL the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$osmJsonData = curl_exec($ch);

// convert accepts JSON string or array e.g. from json_decode($osmJsonData, true);

$geojson = Overpass2Geojson::convertWays($osmJsonData); // Returns JSON encoded string
$geojson = Overpass2Geojson::convertWays($osmJsonData, false); // Returns array with GeoJSON structure
$geojson = Overpass2Geojson::convertNodes($osmJsonData);

Example input (from overpass), (*8)

{
  "elements": [
    {
      "type": "node",
      "id": 31064347,
      "lat": -43.5309816,
      "lon": 172.6420391
    },
    {
      "type": "node",
      "id": 31813685,
      "lat": -43.5309652,
      "lon": 172.6396892,
      "tags": {
        "highway": "traffic_signals",
      }
    },
    {
      "type": "way",
      "id": 4830778,
      "nodes": [
        31064347,
        31813685
      ],
      "tags": {
        "highway": "residential",
        "maxspeed": "50",
        "name": "Worcester Street"
      }
    }
  ]
}

Example output from Overpass2Geojson::convertWays, (*9)

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            172.6420391,
            -43.5309816
          ],
          [
            172.6396892,
            -43.5309652
          ]
        ]
      },
      "properties": {
        "highway": "residential",
        "maxspeed": "50",
        "name": "Worcester Street"
      }
    }
  ]
}

Example output from Overpass2Geojson::convertNodes, (*10)

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": [],
      "geometry": {
        "type": "Point",
        "coordinates": [
          172.6420391,
          -43.5309816
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "highway": "traffic_signals"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          172.6396892,
          -43.5309652
        ]
      }
    }
  ]
}

The Versions

29/09 2016

dev-master

9999999-dev

Convert overpass JSON to GeoJSON

  Sources   Download

MIT

The Development Requires

29/09 2016

v2.0.0

2.0.0.0

Convert overpass JSON to GeoJSON

  Sources   Download

MIT

The Development Requires

22/05 2015

v1.1.0

1.1.0.0

Convert overpass JSON to GeoJSON

  Sources   Download

MIT

The Development Requires

18/03 2015

v1.0.0

1.0.0.0

Convert overpass JSON to GeoJSON

  Sources   Download

MIT

The Development Requires