aaronlidman / osm-and-geojson Goto Github PK
View Code? Open in Web Editor NEWConverts between OSM XML and GeoJSON
Home Page: http://aaronlidman.com/osm-and-geojson/
License: Do What The F*ck You Want To Public License
Converts between OSM XML and GeoJSON
Home Page: http://aaronlidman.com/osm-and-geojson/
License: Do What The F*ck You Want To Public License
Hello!
is that possible to convert a large osm file i.e netherland(900mb) to geojson through osm-and-geojson? I'm using a windows7 machine with i3 cpu\8mem\not-ssd
Like so: https://github.com/mapbox/togeojson/blob/gh-pages/togeojson.js#L61-L66 but for parseFromString.
This is handy: https://github.com/mapbox/togeojson/blob/gh-pages/package.json#L24-L26
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<node id="2451704701" version="2" changeset="17750354" lat="5.957788" lon="116.0706755" user="BernardLiew" uid="1728406" visible="true" timestamp="2013-09-09T13:34:57Z"/>
</osm>
results in a zero-length feature collection
It's valid to have a null geometry object (which should be the case here) or to throw a warning, but [undefined]
in a geometry's coordinates is invalid:
I'm trying to parse a 26MB osm file with the following code:
var geo = require('osm-and-geojson');
var xml2js = require('xml2js');
var fs = require('fs');
fs.readFile('./map', function read(err, data) {
if (err) {
throw err;
}
var parser = new xml2js.Parser({validator: true});
parser.parseString(data, function (err, xml) {
var geoJson = geo.osm2geojson(xml, true);
fs.writeFile("./map.json", geoJson, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});
});
but i'm getting the following error:
.../node_modules/osm-and-geojson/osm_geojson.js:451
function getBy(x, y) { return x.getElementsByTagName(y); }
^
TypeError: Cannot call method 'getElementsByTagName' of undefined
Please help.
Current structure is a bit frustrating, leaflet's is cleaner, more comprehensive, much of the work is already done.
https://github.com/Leaflet/Leaflet/blob/master/src/layer/GeoJSON.js
this is a nodejs library. For folks who want to use it as a bbox, how does one invoke the basic
geojson -> osm conversion from the command line without having to put this in a browser or writing additional code.
I added support LineString and mapping nodes (not to repeat with the same coordinates)
check:
`
osm_geojson.geojson2osm = function (geo, changeset, osmChange) {
var nodes_map = {};
function togeojson(geo, properties) {
var nodes = '',
ways = '',
relations = '';
properties = properties || {};
switch (geo.type) {
case 'Point':
var coord = roundCoords([geo.coordinates]);
nodes += '<node id="' + count + '" lat="' + coord[0][1] +
'" lon="' + coord[0][0] + '" changeset="' + changeset + '">';
nodes += propertiesToTags(properties);
nodes += '</node>';
nodes_map[coord[0]] = count;
count--;
break;
case 'MultiPoint':
break;
case 'LineString':
append(lines(geo, properties));
break;
...
}
function lines(geo, properties) {
var nodes = '',
ways = '';
properties = properties || {};
var coords = [];
ways += '<way id="' + count + '" changeset="' + changeset + '">';
count--;
for (var j = 0; j < geo.coordinates.length - 1; j++) {
coords.push([geo.coordinates[j][1], geo.coordinates[j][0]]);
}
coords = createNodes(coords);
nodes += coords.nodes;
ways += coords.nds;
ways += propertiesToTags(properties);
ways += '</way>';
return {
nodes : nodes,
ways : ways,
relations : ''
};
}
...
function arraysEqual(a, b) {
if (a === b)
return true;
if (a == null || b == null)
return false;
if (a.length != b.length)
return false;
// If you don't care about the order of the elements inside
// the array, you should sort both arrays here.
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i])
return false;
}
return true;
}
function createNodes(coords) {
var nds = '',
nodes = '',
length = coords.length;
coords = roundCoords(coords);
var repeatLastND = false;
if(length > 2){
repeatLastND = arraysEqual(coords[0], coords[length-1]);
}
for (var a = 0; a < length; a++) {
var tempCoords = count;
var coordsInMap = coords[a] in nodes_map;
if(coordsInMap){
tempCoords = nodes_map[coords[a]];
}else{
nodes_map[coords[a]] = count;
}
if (repeatLastND && a === 0)
repeatLastND = tempCoords;
nds += '<nd ref="' + tempCoords + '"/>';
if (repeatLastND && a === length - 1) nds += '<nd ref="' + repeatLastND + '"/>';
if(!coordsInMap){
nodes += '<node id="' + tempCoords + '" lat="' + coords[a][0] + '" lon="' + coords[a][1] +'" changeset="' + changeset + '"/>';
count--;
}
}
return {
'nds' : nds,
'nodes' : nodes
};
}
...
};
`
They don't parse https://gist.github.com/aaronlidman/6399931
Object # has no method 'push' - polygon osm_geojson.js:98
You may want to check if geo.coordinates[0] is not undefined before iterating on it...
osm_geojson.js:113-114
Added test:
if( geo.coordinates[0]) {
for (var j = 0; j < geo.coordinates[0].length-1; j++) {
coords.push([geo.coordinates[0][j][1], geo.coordinates[0][j][0]]);
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.