mediasuitenz / overpass2geojson
将 Overpass JSON 转换为 GeoJSON
v2.1.1
2021-01-10 23:45 UTC
Requires (Dev)
- phpunit/phpunit: ~4.5.0
This package is not auto-updated.
Last update: 2024-09-28 17:17:57 UTC
README
composer require mediasuitenz/Overpass2Geojson
PHP 模块,用于将 Overpass API JSON 输出转换为 GeoJSON 格式
注意:目前此工具仅将 OSM 路段及其节点转换为具有 LineString 几何特征的 FeatureCollection 特征集合,或将 OSM 节点转换为具有 Point 特征的 FeatureCollection 特征集合。
在转换路段时间时,如果任何路段引用了不在输入中的节点,则这些节点将被忽略。如果任何路段的节点少于 2 个,则该路段不会生成特征,因为 LineStrings 必须有超过 2 个坐标。
要将多边形转换为 LineString,可以在调用 Overpass2Geojson::convertWays 时传递第三个参数 true,或者在使用实用程序之前设置 Overpass2Geojson::polygon = true;。
示例
Overpass 查询
[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);
示例输入(来自 overpass)
{
"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"
}
}
]
}
Overpass2Geojson::convertWays 的示例输出
{
"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"
}
}
]
}
Overpass2Geojson::convertNodes 的示例输出
{
"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
]
}
}
]
}