Last active
January 7, 2018 06:02
-
-
Save hiiamyes/b83dd3e6f89f30c495c18642a4088b9e to your computer and use it in GitHub Desktop.
osm-ways-to-geojson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const querystring = require("querystring"); | |
const _ = require("lodash"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const osmWays = [ | |
{ | |
// 三六九到黑森林入口 | |
id: 464086142, | |
start: 1518876742, | |
end: 1518875824 | |
}, | |
{ | |
// 黑森林 | |
id: 464085687 | |
}, | |
{ | |
// 黑森林出口到圈谷底部岔路 | |
id: 464085679 | |
} | |
]; | |
async function transform() { | |
try { | |
let coordinates = []; | |
for (let i = 0; i < osmWays.length; i += 1) { | |
const way = osmWays[i]; | |
// get nodes of way from OSM by overpass api | |
const query = ` | |
[out:json]; | |
way(${way.id}); | |
(._;>;); | |
out; | |
`; | |
const { data: { elements } } = await axios.post( | |
"http://overpass-api.de/api/interpreter", | |
querystring.stringify({ data: query }) | |
); | |
const nodeIds = _.last(elements).nodes; | |
// find the start and end node index of way by node id | |
const startIndex = way.start | |
? nodeIds.findIndex(nodeId => nodeId === way.start) | |
: 0; | |
const endIndex = way.end | |
? nodeIds.findIndex(nodeId => nodeId === way.end) | |
: nodeIds.length - 1; | |
// concatenate nodes into the coordinates of geoJSON | |
coordinates = coordinates.concat( | |
nodeIds.slice(startIndex, endIndex + 1).map(nodeId => { | |
const node = elements.find(e => e.id === nodeId); | |
return [node.lon, node.lat]; | |
}) | |
); | |
} | |
// create and save the geoJSON | |
const geoJSON = { | |
type: "LineString", | |
coordinates: coordinates | |
}; | |
fs.writeFileSync( | |
path.resolve(__dirname, "demo.json"), | |
JSON.stringify(geoJSON) | |
); | |
console.log("done"); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
transform(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type":"LineString","coordinates":[[121.2547261,24.392336],[121.2546085,24.392463],[121.2545623,24.3925245],[121.2544607,24.3926065],[121.2544167,24.392652],[121.2543148,24.3927777],[121.2542084,24.3929043],[121.2539094,24.3932588],[121.2536411,24.3934628],[121.2533581,24.3936374],[121.253381,24.3934787],[121.2534775,24.3932148],[121.2534573,24.3930733],[121.2533833,24.3931192],[121.2532527,24.3932312],[121.2531908,24.3932995],[121.253116,24.3933501],[121.2529954,24.3933757],[121.2529963,24.3931125],[121.2529837,24.3930357],[121.2529526,24.3929556],[121.2525988,24.3932808],[121.2526037,24.3931855],[121.2526142,24.3931005],[121.2525924,24.3930331],[121.2525382,24.3928469],[121.2522931,24.39312],[121.2522336,24.3932119],[121.2521934,24.3932534],[121.2521338,24.3932979],[121.2519025,24.3933662],[121.2517405,24.3934356],[121.2515785,24.3935373],[121.2513913,24.393712],[121.2514119,24.3935082],[121.2514175,24.3933528],[121.251115,24.393628],[121.251115,24.393628],[121.2508919,24.3935861],[121.2507524,24.3936643],[121.2503555,24.3935959],[121.2501087,24.3935764],[121.2497824,24.3936839],[121.2493148,24.3937132],[121.2490036,24.3937718],[121.2487676,24.3938206],[121.2486496,24.3937522],[121.2483814,24.393762],[121.248156,24.3939477],[121.2476303,24.3940161],[121.2472763,24.3940649],[121.2471583,24.3942408],[121.2468793,24.3942506],[121.2465682,24.3942408],[121.2464931,24.3943678],[121.2461068,24.3942897],[121.2458386,24.3942994],[121.2457742,24.3944851],[121.2453558,24.3945535],[121.2448945,24.3945242],[121.2447228,24.3944362],[121.2444439,24.3942506],[121.2442186,24.3941431],[121.2440469,24.3939868],[121.243768,24.3939574],[121.2436821,24.39385],[121.2435856,24.3937132],[121.2432852,24.3935764],[121.2431457,24.3934591],[121.2428667,24.3933126],[121.2426736,24.3931562],[121.2426522,24.393039],[121.2425019,24.3929803],[121.2423303,24.393039],[121.2422659,24.3929022],[121.2421157,24.3927361],[121.2419655,24.3926677],[121.2417509,24.3927654],[121.2417402,24.3926481],[121.2416329,24.3925015],[121.2415042,24.392355],[121.2413432,24.3920423],[121.2411823,24.3919934],[121.2410536,24.3917785],[121.2408712,24.3916124],[121.2407961,24.3914658],[121.2405815,24.3914658],[121.2405708,24.3912997],[121.2402704,24.3911922],[121.2402489,24.3910652],[121.2401201,24.3909382],[121.2399378,24.3907134],[121.2399056,24.3905375],[121.2397446,24.3904496],[121.2397339,24.3901955],[121.2394871,24.3903128],[121.2393048,24.3902932],[121.239455,24.390088],[121.2392404,24.3900783],[121.2392404,24.3898731],[121.239412,24.389619],[121.2393369,24.3895506],[121.2390365,24.3895311],[121.2388756,24.3894529],[121.2385967,24.3895115],[121.2384035,24.3893845],[121.2381782,24.3893357],[121.2379744,24.3893747],[121.2378885,24.3891793],[121.2377062,24.3891207],[121.2374701,24.3890425],[121.2372019,24.3889741],[121.2372019,24.3889741],[121.2370732,24.3889057],[121.2367406,24.3886907],[121.2364723,24.3885637],[121.2364433,24.3884581],[121.2364187,24.3883683],[121.2363329,24.3881631],[121.2363216,24.3880767]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment