Example #1
0
			jsonfile.readFile(jsonFileName, function(err, jsonObj) {
  			if (!err) {
					console.log('\n\n\n\jxon Test - JSON ==> XML');
					console.log('\n\n\n\JSON');
  				console.log(jsonObj);
					var xml = jxon.jsToString(jsonObj);
					console.log('\n\n\n\Transformed XML');
					console.log(xml);
				}
				done();
			});
Example #2
0
function geojsontoosm(geojson, lastNodeId, lastWayId, lastRelationId) {
    var features = geojson.features || (geojson.length>0 ? geojson : [geojson])

    var nodes = [], nodesIndex = {},
        ways = [],
        relations = [];

    features.forEach(function(feature) { // feature can also be a pure GeoJSON geometry object
        // todo: GeometryCollection?
        var properties = feature.properties || {},
            geometry = feature.geometry || feature
        // todo: validity check
        // todo: ids if (feature.id && feature.id.match(/^(node|way|relation)\/(\d+)$/)) id = …
        switch (geometry.type) {
        case "Point":
            processPoint(geometry.coordinates, properties, nodes, nodesIndex)
        break;
        case "LineString":
            processLineString(geometry.coordinates, properties, ways, nodes, nodesIndex)
        break;
        case "Polygon":
            processMultiPolygon([geometry.coordinates], properties, relations, ways, nodes, nodesIndex)
        break;
        case "Multipolygon":
            processMultiPolygon(geometry.coordinates, properties, relations, ways, nodes, nodesIndex)
        break;
        default:
            console.error("unknown or unsupported geometry type:", geometry.type);
        }
    });

    //console.log(nodes, ways, relations)
    function jxonTags(tags) {
        var res = []
        for (var k in tags) {
	    if (tags[k]) {
            res.push({
                "@k": k.replace(/gpx_trk.|gpx_wp./gi,""),
                "@v": (k.match(/color|sym/))? tags[k].replace(/\W/g,'') :tags[k]
            })
		}
        }
        return res
    }
    var jxonData = {
        osm: {
            "@version": "0.6",
            "@generator": "geojsontoosm",
            "node": nodes.map(function(node) {
                node.id = lastNodeId--
                return {
                    "@id": node.id,
                    "@visible": "true",
                    "@version": "1",
                    "@lat": node.lat,
                    "@lon": node.lon,
                    // todo: meta
                    "tag": jxonTags(node.tags)
                }
            }),
            "way": ways.map(function(way) {
                way.id = lastWayId--
                return {
                    "@id": way.id,
                    "@visible": "true",
                    "@version": "1",
                    "nd": way.nodes.map(function(nd) { return {"@ref": nd.id} }),
                    "tag": jxonTags(way.tags)
                }
            }),
            "relation": relations.map(function(relation) {
                relation.id = lastRelationId--
                return {
                    "@id": relation.id,
                    "@visible": "true",
                    "@version": "1",
                    "member": relation.members.map(function(member) {
                        return {
                            "@type": member.type,
                            "@ref": member.elem.id,
                            "@role": member.role
                        }
                    }),
                    "tag": jxonTags(relation.tags)
                    // todo: meta
                }
            })
        } 
    }
    // todo: sort by id
    return jxon.jsToString(jxonData)
}