Skip to content

Commit a69c908

Browse files
committed
fix: handle country ids as shorts rather than strings
1 parent 2c0c8d9 commit a69c908

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

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

+19-14
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public class CountryBordersReader {
4343

4444
private final HashMap<String, CountryInfo> ids = new HashMap<>();
4545
private final HashMap<String, ArrayList<String>> openBorders = new HashMap<>();
46-
private final HashMap<String, Integer> isoCodes = new HashMap<>();
47-
46+
private final Map<String, Short> isoCodes = new HashMap<>();
47+
private Map<Short, String> names = new HashMap<>();
4848
private final HashMap<Long, CountryBordersHierarchy> hierarchies = new HashMap<>();
4949

5050
// Package scoped for testing purposes
@@ -108,8 +108,8 @@ public void addHierarchy(Long id, CountryBordersHierarchy hierarchy) {
108108
public void addId(String id, String localName, String englishName, String cca2, String cca3) {
109109
if (!ids.containsKey(localName)) {
110110
ids.put(localName, new CountryInfo(id, localName, englishName));
111-
isoCodes.put(cca2.trim().toUpperCase(), Integer.parseInt(id));
112-
isoCodes.put(cca3.trim().toUpperCase(), Integer.parseInt(id));
111+
isoCodes.put(cca2.trim().toUpperCase(), Short.parseShort(id));
112+
isoCodes.put(cca3.trim().toUpperCase(), Short.parseShort(id));
113113
}
114114
}
115115

@@ -346,6 +346,10 @@ public String getEngName(String name) {
346346
return "";
347347
}
348348

349+
public String getName(short id) {
350+
return names.get(id);
351+
}
352+
349353
/**
350354
* Get whether a border between two specified countries is open or closed
351355
*
@@ -370,8 +374,8 @@ public boolean isOpen(String c1, String c2) {
370374
* @param code The code to look up
371375
* @return The ID of the country or 0 if not found
372376
*/
373-
public static int getCountryIdByISOCode(String code) {
374-
return currentInstance != null ? currentInstance.isoCodes.getOrDefault(code.toUpperCase(), 0) : 0;
377+
public static short getCountryIdByISOCode(String code) {
378+
return currentInstance != null ? currentInstance.isoCodes.getOrDefault(code.toUpperCase(), (short) 0) : 0;
375379
}
376380

377381
/**
@@ -388,23 +392,24 @@ private void readIds() {
388392
int isoCCA2 = 0;
389393
int isoCCA3 = 0;
390394
for (List<String> col : data) {
391-
if (col.size() >= 3) {
392-
ids.put(col.get(1), new CountryInfo(col.get(0), col.get(1), col.get(2)));
393-
countries++;
394-
}
395-
int intID = 0;
395+
short shortID = 0;
396396
try {
397-
intID = Integer.parseInt(col.get(0));
397+
shortID = Short.parseShort(col.get(0));
398398
} catch (NumberFormatException e) {
399399
LOGGER.error("Invalid country ID " + col.get(0));
400400
continue;
401401
}
402+
if (col.size() >= 3) {
403+
ids.put(col.get(1), new CountryInfo(col.get(0), col.get(1), col.get(2)));
404+
names.put(shortID, col.get(2));
405+
countries++;
406+
}
402407
if (col.size() >= 4 && !col.get(3).trim().isEmpty()) {
403-
isoCodes.put(col.get(3).trim().toUpperCase(), intID);
408+
isoCodes.put(col.get(3).trim().toUpperCase(), shortID);
404409
isoCCA2++;
405410
}
406411
if (col.size() == 5 && !col.get(4).trim().isEmpty()) {
407-
isoCodes.put(col.get(4).trim().toUpperCase(), intID);
412+
isoCodes.put(col.get(4).trim().toUpperCase(), shortID);
408413
isoCCA3++;
409414
}
410415
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<Stri
141141
way.setTag(TAG_KEY_COUNTRY1, countries[0]);
142142
way.setTag(TAG_KEY_COUNTRY2, countries[0]);
143143
}
144+
//DEBUG OUTPUT
145+
if (countries.length > 0)
146+
System.out.println(way.getId() + ": " + String.join(",", countries));
144147
}
145148

146149
wayNodeTags = new HashMap<>();
@@ -200,13 +203,13 @@ public void processEdge(ReaderWay way, EdgeIteratorState edge) {
200203
String countryCode1 = wayNodeTags.get(egdeId1);
201204
String countryCode2 = wayNodeTags.get(edgeId2);
202205
try {
203-
start = Short.parseShort(String.valueOf(cbReader.getCountryIdByISOCode(countryCode1)));
204-
end = Short.parseShort(String.valueOf(cbReader.getCountryIdByISOCode(countryCode2)));
206+
start = cbReader.getCountryIdByISOCode(countryCode1);
207+
end = cbReader.getCountryIdByISOCode(countryCode2);
205208
} catch (Exception ignore) {
206209
// do nothing
207210
} finally {
208211
if (start != end) {
209-
type = (short) 1;//FIXME (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1;
212+
type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1;
210213
}
211214
storage.setEdgeValue(edge.getEdge(), type, start, end);
212215
}

0 commit comments

Comments
 (0)