| Forum | GitHub | Maps | Blog

Retrieve OSM node and way ID


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;

	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) {

		return loaded;

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

	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:


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

	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?


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

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

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


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.


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:


See my comment here