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 9be5ad3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ public class CountryBordersReader {
public static final String INTERNATIONAL_NAME = "INTERNATIONAL";
public static final String INTERNATIONAL_ID = "-1";
public static final String KEY_PROPERTIES = "properties";
private static final String NAME_FIELD = "name";
private static final String HIERARCHY_ID_FIELD = "hierarchy";

private final String borderFile;
private final String nameField;
private final String hierarchyIdField;

private final String idsPath;
private final String openPath;

Expand All @@ -55,8 +54,6 @@ public class CountryBordersReader {
*/
public CountryBordersReader() {
borderFile = "";
nameField = "name";
hierarchyIdField = "hierarchy";
idsPath = "";
openPath = "";

Expand All @@ -72,17 +69,16 @@ public CountryBordersReader() {
*/
public CountryBordersReader(String filepath, String idsPath, String openPath) throws IOException {
borderFile = filepath;
nameField = "name";
hierarchyIdField = "hierarchy";

this.idsPath = idsPath;
this.openPath = openPath;

try {
JSONObject data = readBordersData();
LOGGER.info("Border geometries read");
if (!"".equals(borderFile)) {
JSONObject data = readBordersData();
LOGGER.info("Border geometries read");

createGeometries(data);
createGeometries(data);
}

readIds();
LOGGER.info("Border ids data read");
Expand Down Expand Up @@ -228,13 +224,13 @@ private void createGeometries(JSONObject json) {
Geometry geom = GeometryJSON.parse(obj.getJSONObject("geometry"));

// Also need the id of the country and its hierarchy id
String id = obj.getJSONObject(KEY_PROPERTIES).getString(nameField);
String id = obj.getJSONObject(KEY_PROPERTIES).getString(NAME_FIELD);

Long hId = -1L;

// If there is no hierarchy info, then we set the id of the hierarchy to be a default of 1
if (obj.getJSONObject(KEY_PROPERTIES).has(hierarchyIdField))
hId = obj.getJSONObject(KEY_PROPERTIES).getLong(hierarchyIdField);
if (obj.getJSONObject(KEY_PROPERTIES).has(HIERARCHY_ID_FIELD))
hId = obj.getJSONObject(KEY_PROPERTIES).getLong(HIERARCHY_ID_FIELD);

// Create the borders object
CountryBordersPolygon c = new CountryBordersPolygon(id, geom);
Expand Down
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_IDS = "ids";
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,6 +81,8 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception {
String countryIdsFile = "";
String openBordersFile = "";

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

if (parameters.containsKey("ids"))
countryIdsFile = parameters.get("ids");
if (parameters.containsKey(PARAM_KEY_IDS))
countryIdsFile = parameters.get(PARAM_KEY_IDS);
else
ErrorLoggingUtility.logMissingConfigParameter(BordersGraphStorageBuilder.class, "ids");
ErrorLoggingUtility.logMissingConfigParameter(BordersGraphStorageBuilder.class, PARAM_KEY_IDS);

if (parameters.containsKey(PARAM_KEY_OPEN_BORDERS))
openBordersFile = parameters.get(PARAM_KEY_OPEN_BORDERS);
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.getOrDefault(egdeId1, "");
String countryCode2 = wayNodeTags.getOrDefault(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 9be5ad3

Please sign in to comment.