From 5769eb05ca5d3ac60746fb5a9bc4f4a760d92e0b Mon Sep 17 00:00:00 2001 From: aoles Date: Tue, 26 Mar 2024 01:47:16 +0100 Subject: [PATCH] feat: add config flag to use country node tags from PBF --- .../builders/BordersGraphStorageBuilder.java | 89 ++++++++++--------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/storages/builders/BordersGraphStorageBuilder.java b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/storages/builders/BordersGraphStorageBuilder.java index 226c3b9585..91943ba234 100644 --- a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/storages/builders/BordersGraphStorageBuilder.java +++ b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/storages/builders/BordersGraphStorageBuilder.java @@ -39,6 +39,7 @@ public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder { static final Logger LOGGER = Logger.getLogger(BordersGraphStorageBuilder.class.getName()); + private static final String PARAM_KEY_PREPROCESSED = "preprocessed"; private static final String PARAM_KEY_BOUNDARIES = "boundaries"; private static final String PARAM_KEY_OPEN_BORDERS = "openborders"; private static final String TAG_KEY_COUNTRY1 = "country1"; @@ -49,6 +50,7 @@ public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder { private BordersGraphStorage storage; private CountryBordersReader cbReader; + private boolean preprocessed; private final GeometryFactory gf; @@ -79,7 +81,9 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception { String countryIdsFile = ""; String openBordersFile = ""; - if (parameters.containsKey(PARAM_KEY_BOUNDARIES)) + if (parameters.containsKey(PARAM_KEY_PREPROCESSED)) + preprocessed = true; + else if (parameters.containsKey(PARAM_KEY_BOUNDARIES)) bordersFile = parameters.get(PARAM_KEY_BOUNDARIES); else { ErrorLoggingUtility.logMissingConfigParameter(BordersGraphStorageBuilder.class, PARAM_KEY_BOUNDARIES); @@ -131,7 +135,10 @@ public void processWay(ReaderWay way) { public void processWay(ReaderWay way, Coordinate[] coords, Map> nodeTags) { // Process the way using the geometry provided // if we don't have the reader object, then we can't do anything - if (cbReader != null) { + if (cbReader == null) + return; + + if (!preprocessed) { String[] countries = findBorderCrossing(coords); // If we find that the length of countries is more than one, then it does cross a border if (countries.length > 1 && !countries[0].equals(countries[1])) { @@ -144,16 +151,17 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map 0) System.out.println(way.getId() + ": " + String.join(",", countries)); - } - - wayNodeTags = new HashMap<>(); - if (nodeTags != null) { - for (Integer internalNodeId : nodeTags.keySet()) { - int nodeId = convertTowerNodeId(internalNodeId); - if (nodeId == EMPTY_NODE)// skip non-tower nodes - continue; - Map tagPairs = nodeTags.get(internalNodeId); - wayNodeTags.put(nodeId, tagPairs.get("country")); + } else { + wayNodeTags = new HashMap<>(); + if (nodeTags != null) { + for (Map.Entry> entry : nodeTags.entrySet()) { + int internalNodeId = entry.getKey(); + int nodeId = convertTowerNodeId(internalNodeId); + if (nodeId == EMPTY_NODE)// skip non-tower nodes + continue; + Map tagPairs = entry.getValue(); + wayNodeTags.put(nodeId, tagPairs.get("country")); + } } } } @@ -179,39 +187,40 @@ public void processEdge(ReaderWay way, EdgeIteratorState edge) { // Make sure we actually have the storage initialised - if there were errors accessing the data then this could be the case if (storage != null) { // If there is no border crossing then we set the edge value to be 0 - - // First get the start and end countries - if they are equal, then there is no crossing - String startVal = way.getTag(TAG_KEY_COUNTRY1); - String endVal = way.getTag(TAG_KEY_COUNTRY2); short type = BordersGraphStorage.NO_BORDER; short start = 0; short end = 0; - try { - start = Short.parseShort(cbReader.getId(startVal)); - end = Short.parseShort(cbReader.getId(endVal)); - } catch (Exception ignore) { - // do nothing - } finally { - if (start != end) { - type = (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1; + if (!preprocessed) { + // First get the start and end countries - if they are equal, then there is no crossing + String startVal = way.getTag(TAG_KEY_COUNTRY1); + String endVal = way.getTag(TAG_KEY_COUNTRY2); + try { + start = Short.parseShort(cbReader.getId(startVal)); + end = Short.parseShort(cbReader.getId(endVal)); + } catch (Exception ignore) { + // do nothing + } finally { + if (start != end) { + type = (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1; + } + storage.setEdgeValue(edge.getEdge(), type, start, end); } - storage.setEdgeValue(edge.getEdge(), type, start, end); - } - - int egdeId1 = edge.getBaseNode(); - int edgeId2 = edge.getAdjNode(); - String countryCode1 = wayNodeTags.get(egdeId1); - String countryCode2 = wayNodeTags.get(edgeId2); - try { - start = cbReader.getCountryIdByISOCode(countryCode1); - end = cbReader.getCountryIdByISOCode(countryCode2); - } catch (Exception ignore) { - // do nothing - } finally { - if (start != end) { - type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1; + } else { + int egdeId1 = edge.getBaseNode(); + int edgeId2 = edge.getAdjNode(); + String countryCode1 = wayNodeTags.get(egdeId1); + String countryCode2 = wayNodeTags.get(edgeId2); + try { + start = CountryBordersReader.getCountryIdByISOCode(countryCode1); + end = CountryBordersReader.getCountryIdByISOCode(countryCode2); + } catch (Exception ignore) { + // do nothing + } finally { + if (start != end) { + type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1; + } + storage.setEdgeValue(edge.getEdge(), type, start, end); } - storage.setEdgeValue(edge.getEdge(), type, start, end); } } }