-
-
Save francbartoli/7301008 to your computer and use it in GitHub Desktop.
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
| function osm2geojson(xml) { | |
| var parser = new DOMParser(); | |
| var doc = parser.parseFromString(xml, 'text/xml'); | |
| var points = {} | |
| d3.select(doc).selectAll('node').each(function() { | |
| var n = d3.select(this) | |
| points[n.attr('id')] = [+n.attr('lon'), +n.attr('lat')] | |
| }) | |
| var features = []; | |
| d3.select(doc).selectAll('way').each(function() { | |
| var type = "LineString" | |
| var way = d3.select(this) | |
| var refs = [] | |
| var properties = {} | |
| way.selectAll('nd').each(function() { refs.push(d3.select(this).attr('ref')) }) | |
| way.selectAll('tag').each(function() { | |
| var t = d3.select(this); | |
| properties[t.attr('k')] = t.attr('v'); | |
| }) | |
| var coordinates = refs.map(function(r) { return points[r]; }) | |
| if (refs[0] === refs[refs.length - 1]) { | |
| type = "Polygon" | |
| coordinates = [ coordinates ] | |
| // poygon | |
| } | |
| if( coordinates && coordinates.length) { | |
| features.push({ | |
| type: "Feature", | |
| geometry: { | |
| type: type, | |
| coordinates: coordinates | |
| }, | |
| properties: properties | |
| }) | |
| } | |
| }) | |
| return features; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment