Skip to content

Commit

Permalink
fix: handle country ids as shorts rather than strings
Browse files Browse the repository at this point in the history
  • Loading branch information
aoles committed Mar 26, 2024
1 parent 2c0c8d9 commit a69c908
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class CountryBordersReader {

private final HashMap<String, CountryInfo> ids = new HashMap<>();
private final HashMap<String, ArrayList<String>> openBorders = new HashMap<>();
private final HashMap<String, Integer> isoCodes = new HashMap<>();

private final Map<String, Short> isoCodes = new HashMap<>();
private Map<Short, String> names = new HashMap<>();
private final HashMap<Long, CountryBordersHierarchy> hierarchies = new HashMap<>();

// Package scoped for testing purposes
Expand Down Expand Up @@ -108,8 +108,8 @@ public void addHierarchy(Long id, CountryBordersHierarchy hierarchy) {
public void addId(String id, String localName, String englishName, String cca2, String cca3) {
if (!ids.containsKey(localName)) {
ids.put(localName, new CountryInfo(id, localName, englishName));
isoCodes.put(cca2.trim().toUpperCase(), Integer.parseInt(id));
isoCodes.put(cca3.trim().toUpperCase(), Integer.parseInt(id));
isoCodes.put(cca2.trim().toUpperCase(), Short.parseShort(id));
isoCodes.put(cca3.trim().toUpperCase(), Short.parseShort(id));
}
}

Expand Down Expand Up @@ -346,6 +346,10 @@ public String getEngName(String name) {
return "";
}

public String getName(short id) {
return names.get(id);
}

/**
* Get whether a border between two specified countries is open or closed
*
Expand All @@ -370,8 +374,8 @@ public boolean isOpen(String c1, String c2) {
* @param code The code to look up
* @return The ID of the country or 0 if not found
*/
public static int getCountryIdByISOCode(String code) {
return currentInstance != null ? currentInstance.isoCodes.getOrDefault(code.toUpperCase(), 0) : 0;
public static short getCountryIdByISOCode(String code) {
return currentInstance != null ? currentInstance.isoCodes.getOrDefault(code.toUpperCase(), (short) 0) : 0;
}

/**
Expand All @@ -388,23 +392,24 @@ private void readIds() {
int isoCCA2 = 0;
int isoCCA3 = 0;
for (List<String> col : data) {
if (col.size() >= 3) {
ids.put(col.get(1), new CountryInfo(col.get(0), col.get(1), col.get(2)));
countries++;
}
int intID = 0;
short shortID = 0;
try {
intID = Integer.parseInt(col.get(0));
shortID = Short.parseShort(col.get(0));
} catch (NumberFormatException e) {
LOGGER.error("Invalid country ID " + col.get(0));
continue;
}
if (col.size() >= 3) {
ids.put(col.get(1), new CountryInfo(col.get(0), col.get(1), col.get(2)));
names.put(shortID, col.get(2));
countries++;
}
if (col.size() >= 4 && !col.get(3).trim().isEmpty()) {
isoCodes.put(col.get(3).trim().toUpperCase(), intID);
isoCodes.put(col.get(3).trim().toUpperCase(), shortID);
isoCCA2++;
}
if (col.size() == 5 && !col.get(4).trim().isEmpty()) {
isoCodes.put(col.get(4).trim().toUpperCase(), intID);
isoCodes.put(col.get(4).trim().toUpperCase(), shortID);
isoCCA3++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<Stri
way.setTag(TAG_KEY_COUNTRY1, countries[0]);
way.setTag(TAG_KEY_COUNTRY2, countries[0]);
}
//DEBUG OUTPUT
if (countries.length > 0)
System.out.println(way.getId() + ": " + String.join(",", countries));
}

wayNodeTags = new HashMap<>();
Expand Down Expand Up @@ -200,13 +203,13 @@ public void processEdge(ReaderWay way, EdgeIteratorState edge) {
String countryCode1 = wayNodeTags.get(egdeId1);
String countryCode2 = wayNodeTags.get(edgeId2);
try {
start = Short.parseShort(String.valueOf(cbReader.getCountryIdByISOCode(countryCode1)));
end = Short.parseShort(String.valueOf(cbReader.getCountryIdByISOCode(countryCode2)));
start = cbReader.getCountryIdByISOCode(countryCode1);
end = cbReader.getCountryIdByISOCode(countryCode2);
} catch (Exception ignore) {
// do nothing
} finally {
if (start != end) {
type = (short) 1;//FIXME (cbReader.isOpen(cbReader.getEngName(startVal), cbReader.getEngName(endVal))) ? (short) 2 : (short) 1;
type = cbReader.isOpen(cbReader.getName(start), cbReader.getName(end)) ? (short) 2 : (short) 1;
}
storage.setEdgeValue(edge.getEdge(), type, start, end);
}
Expand Down

0 comments on commit a69c908

Please sign in to comment.