Skip to content

Commit 07806d6

Browse files
committed
feat: add config flag to use country node tags from PBF
1 parent 8a81a86 commit 07806d6

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/reader/borders/CountryBordersReader.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ public class CountryBordersReader {
3333
public static final String INTERNATIONAL_NAME = "INTERNATIONAL";
3434
public static final String INTERNATIONAL_ID = "-1";
3535
public static final String KEY_PROPERTIES = "properties";
36+
private static final String nameField = "name";
37+
private static final String hierarchyIdField = "hierarchy";
3638

3739
private final String borderFile;
38-
private final String nameField;
39-
private final String hierarchyIdField;
40-
4140
private final String idsPath;
4241
private final String openPath;
4342

@@ -55,8 +54,6 @@ public class CountryBordersReader {
5554
*/
5655
public CountryBordersReader() {
5756
borderFile = "";
58-
nameField = "name";
59-
hierarchyIdField = "hierarchy";
6057
idsPath = "";
6158
openPath = "";
6259

@@ -72,17 +69,16 @@ public CountryBordersReader() {
7269
*/
7370
public CountryBordersReader(String filepath, String idsPath, String openPath) throws IOException {
7471
borderFile = filepath;
75-
nameField = "name";
76-
hierarchyIdField = "hierarchy";
77-
7872
this.idsPath = idsPath;
7973
this.openPath = openPath;
8074

8175
try {
82-
JSONObject data = readBordersData();
83-
LOGGER.info("Border geometries read");
76+
if (!"".equals(borderFile)) {
77+
JSONObject data = readBordersData();
78+
LOGGER.info("Border geometries read");
8479

85-
createGeometries(data);
80+
createGeometries(data);
81+
}
8682

8783
readIds();
8884
LOGGER.info("Border ids data read");

ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/storages/builders/BordersGraphStorageBuilder.java

+51-42
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder {
4040
static final Logger LOGGER = Logger.getLogger(BordersGraphStorageBuilder.class.getName());
4141

42+
private static final String PARAM_KEY_IDS = "ids";
4243
private static final String PARAM_KEY_BOUNDARIES = "boundaries";
4344
private static final String PARAM_KEY_OPEN_BORDERS = "openborders";
4445
private static final String TAG_KEY_COUNTRY1 = "country1";
@@ -49,6 +50,7 @@ public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder {
4950

5051
private BordersGraphStorage storage;
5152
private CountryBordersReader cbReader;
53+
private boolean preprocessed;
5254

5355
private final GeometryFactory gf;
5456

@@ -79,6 +81,8 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception {
7981
String countryIdsFile = "";
8082
String openBordersFile = "";
8183

84+
if (parameters.containsKey("preprocessed"))
85+
preprocessed = true;
8286
if (parameters.containsKey(PARAM_KEY_BOUNDARIES))
8387
bordersFile = parameters.get(PARAM_KEY_BOUNDARIES);
8488
else {
@@ -87,10 +91,10 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception {
8791
throw new MissingResourceException("A boundary geometry file is needed to use the borders extended storage!", BordersGraphStorage.class.getName(), PARAM_KEY_BOUNDARIES);
8892
}
8993

90-
if (parameters.containsKey("ids"))
91-
countryIdsFile = parameters.get("ids");
94+
if (parameters.containsKey(PARAM_KEY_IDS))
95+
countryIdsFile = parameters.get(PARAM_KEY_IDS);
9296
else
93-
ErrorLoggingUtility.logMissingConfigParameter(BordersGraphStorageBuilder.class, "ids");
97+
ErrorLoggingUtility.logMissingConfigParameter(BordersGraphStorageBuilder.class, PARAM_KEY_IDS);
9498

9599
if (parameters.containsKey(PARAM_KEY_OPEN_BORDERS))
96100
openBordersFile = parameters.get(PARAM_KEY_OPEN_BORDERS);
@@ -131,7 +135,10 @@ public void processWay(ReaderWay way) {
131135
public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<String, String>> nodeTags) {
132136
// Process the way using the geometry provided
133137
// if we don't have the reader object, then we can't do anything
134-
if (cbReader != null) {
138+
if (cbReader == null)
139+
return;
140+
141+
if (!preprocessed) {
135142
String[] countries = findBorderCrossing(coords);
136143
// If we find that the length of countries is more than one, then it does cross a border
137144
if (countries.length > 1 && !countries[0].equals(countries[1])) {
@@ -144,16 +151,17 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<Stri
144151
//DEBUG OUTPUT
145152
if (countries.length > 0)
146153
System.out.println(way.getId() + ": " + String.join(",", countries));
147-
}
148-
149-
wayNodeTags = new HashMap<>();
150-
if (nodeTags != null) {
151-
for (Integer internalNodeId : nodeTags.keySet()) {
152-
int nodeId = convertTowerNodeId(internalNodeId);
153-
if (nodeId == EMPTY_NODE)// skip non-tower nodes
154-
continue;
155-
Map<String, String> tagPairs = nodeTags.get(internalNodeId);
156-
wayNodeTags.put(nodeId, tagPairs.get("country"));
154+
} else {
155+
wayNodeTags = new HashMap<>();
156+
if (nodeTags != null) {
157+
for (Map.Entry<Integer, Map<String, String>> entry : nodeTags.entrySet()) {
158+
int internalNodeId = entry.getKey();
159+
int nodeId = convertTowerNodeId(internalNodeId);
160+
if (nodeId == EMPTY_NODE)// skip non-tower nodes
161+
continue;
162+
Map<String, String> tagPairs = entry.getValue();
163+
wayNodeTags.put(nodeId, tagPairs.get("country"));
164+
}
157165
}
158166
}
159167
}
@@ -179,39 +187,40 @@ public void processEdge(ReaderWay way, EdgeIteratorState edge) {
179187
// Make sure we actually have the storage initialised - if there were errors accessing the data then this could be the case
180188
if (storage != null) {
181189
// If there is no border crossing then we set the edge value to be 0
182-
183-
// First get the start and end countries - if they are equal, then there is no crossing
184-
String startVal = way.getTag(TAG_KEY_COUNTRY1);
185-
String endVal = way.getTag(TAG_KEY_COUNTRY2);
186190
short type = BordersGraphStorage.NO_BORDER;
187191
short start = 0;
188192
short end = 0;
189-
try {
190-
start = Short.parseShort(cbReader.getId(startVal));
191-
end = Short.parseShort(cbReader.getId(endVal));
192-
} catch (Exception ignore) {
193-
// do nothing
194-
} finally {
195-
if (start != end) {
196-
type = (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1;
193+
if (!preprocessed) {
194+
// First get the start and end countries - if they are equal, then there is no crossing
195+
String startVal = way.getTag(TAG_KEY_COUNTRY1);
196+
String endVal = way.getTag(TAG_KEY_COUNTRY2);
197+
try {
198+
start = Short.parseShort(cbReader.getId(startVal));
199+
end = Short.parseShort(cbReader.getId(endVal));
200+
} catch (Exception ignore) {
201+
// do nothing
202+
} finally {
203+
if (start != end) {
204+
type = (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1;
205+
}
206+
storage.setEdgeValue(edge.getEdge(), type, start, end);
197207
}
198-
storage.setEdgeValue(edge.getEdge(), type, start, end);
199-
}
200-
201-
int egdeId1 = edge.getBaseNode();
202-
int edgeId2 = edge.getAdjNode();
203-
String countryCode1 = wayNodeTags.get(egdeId1);
204-
String countryCode2 = wayNodeTags.get(edgeId2);
205-
try {
206-
start = cbReader.getCountryIdByISOCode(countryCode1);
207-
end = cbReader.getCountryIdByISOCode(countryCode2);
208-
} catch (Exception ignore) {
209-
// do nothing
210-
} finally {
211-
if (start != end) {
212-
type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1;
208+
} else {
209+
int egdeId1 = edge.getBaseNode();
210+
int edgeId2 = edge.getAdjNode();
211+
String countryCode1 = wayNodeTags.getOrDefault(egdeId1, "");
212+
String countryCode2 = wayNodeTags.getOrDefault(edgeId2, "");
213+
try {
214+
start = CountryBordersReader.getCountryIdByISOCode(countryCode1);
215+
end = CountryBordersReader.getCountryIdByISOCode(countryCode2);
216+
} catch (Exception ignore) {
217+
// do nothing
218+
} finally {
219+
if (start != end) {
220+
type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1;
221+
}
222+
storage.setEdgeValue(edge.getEdge(), type, start, end);
213223
}
214-
storage.setEdgeValue(edge.getEdge(), type, start, end);
215224
}
216225
}
217226
}

0 commit comments

Comments
 (0)