Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LineString and Nodes map #17

Open
pecet86 opened this issue Dec 15, 2016 · 1 comment
Open

LineString and Nodes map #17

pecet86 opened this issue Dec 15, 2016 · 1 comment

Comments

@pecet86
Copy link

pecet86 commented Dec 15, 2016

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

...

};
`

@vignesh882
Copy link

vignesh882 commented Sep 2, 2021

for loop in lines function should be

for (var j = 0; j < geo.coordinates.length; j++) {

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants