GithubHelp home page GithubHelp logo

aaronlidman / osm-and-geojson Goto Github PK

View Code? Open in Web Editor NEW
89.0 89.0 29.0 233 KB

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

JavaScript 100.00%

osm-and-geojson's People

Contributors

aaronlidman avatar mikemorris avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

osm-and-geojson's Issues

The large osm files to geojson?

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

Single node is not parsed

<?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

Cannot call method 'getElementsByTagName' of undefined

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.

basic help

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.

LineString and Nodes map

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
	};
}

...

};
`

Some geojson polygons can cause a crash

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]]);
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.