39
39
public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder {
40
40
static final Logger LOGGER = Logger .getLogger (BordersGraphStorageBuilder .class .getName ());
41
41
42
+ private static final String PARAM_KEY_IDS = "ids" ;
42
43
private static final String PARAM_KEY_BOUNDARIES = "boundaries" ;
43
44
private static final String PARAM_KEY_OPEN_BORDERS = "openborders" ;
44
45
private static final String TAG_KEY_COUNTRY1 = "country1" ;
@@ -49,6 +50,7 @@ public class BordersGraphStorageBuilder extends AbstractGraphStorageBuilder {
49
50
50
51
private BordersGraphStorage storage ;
51
52
private CountryBordersReader cbReader ;
53
+ private boolean preprocessed ;
52
54
53
55
private final GeometryFactory gf ;
54
56
@@ -79,6 +81,8 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception {
79
81
String countryIdsFile = "" ;
80
82
String openBordersFile = "" ;
81
83
84
+ if (parameters .containsKey ("preprocessed" ))
85
+ preprocessed = true ;
82
86
if (parameters .containsKey (PARAM_KEY_BOUNDARIES ))
83
87
bordersFile = parameters .get (PARAM_KEY_BOUNDARIES );
84
88
else {
@@ -87,10 +91,10 @@ public GraphExtension init(GraphHopper graphhopper) throws Exception {
87
91
throw new MissingResourceException ("A boundary geometry file is needed to use the borders extended storage!" , BordersGraphStorage .class .getName (), PARAM_KEY_BOUNDARIES );
88
92
}
89
93
90
- if (parameters .containsKey ("ids" ))
91
- countryIdsFile = parameters .get ("ids" );
94
+ if (parameters .containsKey (PARAM_KEY_IDS ))
95
+ countryIdsFile = parameters .get (PARAM_KEY_IDS );
92
96
else
93
- ErrorLoggingUtility .logMissingConfigParameter (BordersGraphStorageBuilder .class , "ids" );
97
+ ErrorLoggingUtility .logMissingConfigParameter (BordersGraphStorageBuilder .class , PARAM_KEY_IDS );
94
98
95
99
if (parameters .containsKey (PARAM_KEY_OPEN_BORDERS ))
96
100
openBordersFile = parameters .get (PARAM_KEY_OPEN_BORDERS );
@@ -131,7 +135,10 @@ public void processWay(ReaderWay way) {
131
135
public void processWay (ReaderWay way , Coordinate [] coords , Map <Integer , Map <String , String >> nodeTags ) {
132
136
// Process the way using the geometry provided
133
137
// 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 ) {
135
142
String [] countries = findBorderCrossing (coords );
136
143
// If we find that the length of countries is more than one, then it does cross a border
137
144
if (countries .length > 1 && !countries [0 ].equals (countries [1 ])) {
@@ -144,16 +151,17 @@ public void processWay(ReaderWay way, Coordinate[] coords, Map<Integer, Map<Stri
144
151
//DEBUG OUTPUT
145
152
if (countries .length > 0 )
146
153
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
+ }
157
165
}
158
166
}
159
167
}
@@ -179,39 +187,40 @@ public void processEdge(ReaderWay way, EdgeIteratorState edge) {
179
187
// Make sure we actually have the storage initialised - if there were errors accessing the data then this could be the case
180
188
if (storage != null ) {
181
189
// 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 );
186
190
short type = BordersGraphStorage .NO_BORDER ;
187
191
short start = 0 ;
188
192
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 );
197
207
}
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 ) ;
213
223
}
214
- storage .setEdgeValue (edge .getEdge (), type , start , end );
215
224
}
216
225
}
217
226
}
0 commit comments