Skip to content

Commit

Permalink
feat: add config flag to use country node tags from PBF
Browse files Browse the repository at this point in the history
  • Loading branch information
aoles committed Apr 5, 2024
1 parent 8a81a86 commit 5769eb0
Showing 1 changed file with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -49,6 +50,7 @@ public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder {

private BordersGraphStorage storage;
private CountryBordersReader cbReader;
private boolean preprocessed;

private final GeometryFactory gf;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -131,7 +135,10 @@ public void processWay(ReaderWay way) {
public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<String, String>> 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])) {
Expand All @@ -144,16 +151,17 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<Stri
//DEBUG OUTPUT
if (countries.length > 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<String, String> tagPairs = nodeTags.get(internalNodeId);
wayNodeTags.put(nodeId, tagPairs.get("country"));
} else {
wayNodeTags = new HashMap<>();
if (nodeTags != null) {
for (Map.Entry<Integer, Map<String, String>> entry : nodeTags.entrySet()) {
int internalNodeId = entry.getKey();
int nodeId = convertTowerNodeId(internalNodeId);
if (nodeId == EMPTY_NODE)// skip non-tower nodes
continue;
Map<String, String> tagPairs = entry.getValue();
wayNodeTags.put(nodeId, tagPairs.get("country"));
}
}
}
}
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 5769eb0

Please sign in to comment.