diff --git a/src/RoutableTilesToGeoJSON.js b/src/RoutableTilesToGeoJSON.js index 5561aae3..8b48e34e 100644 --- a/src/RoutableTilesToGeoJSON.js +++ b/src/RoutableTilesToGeoJSON.js @@ -74,6 +74,80 @@ var extractWays = function (json, nodes, feats) { return nodes[nodeId]; }); }); + + return json["@graph"] + .filter((item) => { + return item["@type"] === "osm:Way" && !item["osm:building"]; + }) + .map((item) => { + //Transform osm:hasNodes to a linestring style thing + if (!item["osm:hasNodes"]) { + item["osm:hasNodes"] = []; + } else if (typeof item["osm:hasNodes"] === "string") { + item["osm:hasNodes"] = [item["osm:hasNodes"]]; + } + item["osm:hasNodes"] = item["osm:hasNodes"].map( + (nodeId, index, nodeIds) => { + const node = feats.get(nodeId); + if ( + (index === 0 || index === nodeIds.length - 1) && + node["osm:hasTag"]?.find((tag) => tag.startsWith("entrance=")) + ) { + let angle; + let xy, xy2; + if (index === 0) { + xy = mercatorProject(nodes[nodeId]); + xy2 = mercatorProject(nodes[nodeIds[index + 1]]); + } else { + xy = mercatorProject(nodes[nodeId]); + xy2 = mercatorProject(nodes[nodeIds[index - 1]]); + } + angle = Math.atan2(xy[0] - xy2[0], -(xy[1] - xy2[1])); // atan2(lat', -lon') + angle = (angle / Math.PI) * 180 - 90; // rotate 0 from right to top + if ( + item["osm:hasTag"]?.find((tag) => tag.startsWith("indoor=")) || + item["osm:hasTag"]?.find((tag) => tag === "highway=corridor") + ) { + angle = angle + 180; // flip indoor angle to get ordinary outdoor angle + } + const offset = 0.5; + node.properties = { + entrance: node["osm:hasTag"] + ?.find((tag) => tag.startsWith("entrance=")) + ?.substring(9), + offset: [ + Math.cos((angle / 180) * Math.PI) * offset, + Math.sin((angle / 180) * Math.PI) * offset, + ], + rotate: angle - 90, + ref: node["osm:hasTag"] + ?.find((tag) => tag.startsWith("ref=")) + ?.substring(4), + "addr:unit": node["osm:hasTag"] + ?.find((tag) => tag.startsWith("addr:unit=")) + ?.substring(10), + }; + // console.log(node); + } + + return nodes[nodeId]; + } + ); + let geometry = { + type: "LineString", + coordinates: item["osm:hasNodes"], + }; + return { + id: item["@id"], + //layer: item['osm:highway'], + type: "Feature", + properties: { + highway: item["osm:highway"], + name: item["rdfs:label"] ? item["rdfs:label"] : "", + }, + geometry: geometry, + }; + }); }; const entranceToLabel = function (node) {