GraphHopper.com | Forum | GitHub | Maps | Blog

Retrieve OSM node and way ID


#1

hi,
i saw several discussion on this topics and compiled from them this:

public class MyGraphHopper extends GraphHopper {
	private DataAccess edgeMapping;
	private DataAccess nodeMapping;
	private BitUtil bitUtil;
	private Directory dir;
	private OSMReaderOSM reader;

	@Override
	public boolean load(String graphHopperFolder) {
		boolean loaded = super.load(graphHopperFolder);
		dir = getGraphHopperStorage().getDirectory();
		bitUtil = BitUtil.get(dir.getByteOrder());
		edgeMapping = dir.find("edge_mapping");
		nodeMapping = dir.find("node_mapping");

		if (loaded) {
			edgeMapping.loadExisting();
			nodeMapping.loadExisting();
		}

		return loaded;
	}

	@Override
	protected DataReader createReader(GraphHopperStorage ghStorage) {
		this.reader = new OSMReaderOSM(ghStorage, this.dir);
		return initDataReader(reader);
	}

	public long getOSMWay(int internalEdgeId) {
		long pointer = 8L * internalEdgeId;
		return bitUtil.combineIntsToLong(edgeMapping.getInt(pointer), edgeMapping.getInt(pointer + 4L));
	}

	@Override
	public List<Path> calcPaths(GHRequest request, GHResponse rsp) {
		return super.calcPaths(request, rsp);
	}

	public long getOSMNode(int internalNodeId) {
		if (internalNodeId >= getGraphHopperStorage().getNodes()) {
			return -1;
		}
		long pointer = 8L * internalNodeId;
		long res = bitUtil.combineIntsToLong(nodeMapping.getInt(pointer), nodeMapping.getInt(pointer + 4L));
		return res;
	}
}

and the extended OSMReaderOSM:

.....

	@Override
	boolean addNode(ReaderNode node) {
		boolean result = super.addNode(node);

		if (result) {
			int internalNodeId = this.getNodeMap().get(node.getId());
			storeOsmNodeID(internalNodeId, node.getId());
		}
		return result;
	}

	protected void storeOsmNodeID(int nodeId, long osmNodeId) {
		if (nodeId > 0) {
			long pointer = 8L * nodeId;
			nodeMapping.ensureCapacity(pointer + 8L);

			nodeMapping.setInt(pointer, bitUtil.getIntLow(osmNodeId));
			nodeMapping.setInt(pointer + 4, bitUtil.getIntHigh(osmNodeId));
		}
	}

	@Override
	protected void storeOsmWayID(int edgeId, long osmWayId) {
		super.storeOsmWayID(edgeId, osmWayId);

		long pointer = 8L * edgeId;
		edgeMapping.ensureCapacity(pointer + 8L);

		edgeMapping.setInt(pointer, bitUtil.getIntLow(osmWayId));
		edgeMapping.setInt(pointer + 4, bitUtil.getIntHigh(osmWayId));
	}
....

Is this the way to do it?


#2

actually i found out an easier way for node IDs? Instead of overwriting addNode(), just overwrite addTowerNode() method:

@Override
	int addTowerNode(long osmId, double lat, double lon, double ele) {
		if (nodeAccess.is3D())
			nodeAccess.setNode(nextTowerId, lat, lon, ele);
		else
			nodeAccess.setNode(nextTowerId, lat, lon);

		int id = -(nextTowerId + 3);
		getNodeMap().put(osmId, id);
		storeOsmNodeID(id, osmId);
		nextTowerId++;
		return id;
	}

#3

I haven’t read every line, but overall I would have done it the same way.

actually i found out an easier way for node IDs? Instead of overwriting addNode(), just overwrite addTowerNode() method:

If you only want to do this for tower nodes, you can do this.


#4

OK, thanks for review. That i have to post a question into the matching discussion, cos when i translate the matching result to OSM nodes and ways, i get very weirg results :slight_smile:


#5

See my comment here