Repository navigation
Description
We currently create EncodedValue objects in two places: The TagParsers and in DefaultEncodedValueFactory. The reason is probably that we have two scenarios:
-
We generate new graph data from an OSM file. For this we define
TagParsers that handle different OSM tags and write to according encoded values. TheTagParsers also create theseEncodedValues, e.g.OSMMaxSpeedParsercreates theMaxSpeedencoded value that it writes to. -
We load existing graph data. In this case we don't need
TagParsers, because we are not parsing any tags. We still have to create theEncodedValueobjects though. This is whereEncodedValueFactorycomes in. We use it to create theEncodedValueobjects given the strings that were written to the properties file during the import here:? EncodingManager.create(emBuilder, encodedValueFactory, flagEncoderFactory, properties)
The obvious downside of EncodedValues being created in not one, but two places is that they have to be kept in sync. And, to no surprise, they currently aren't.
This test fails:
@Test
public void lanesEV() {
{
GraphHopperConfig config = new GraphHopperConfig();
config.putObject("graph.location", GH_LOCATION);
config.putObject("datareader.file", BAYREUTH);
config.putObject("graph.encoded_values", "lanes");
config.setProfiles(Collections.singletonList(
new Profile("car").setVehicle("car").setWeighting("fastest")
));
GraphHopper hopper = new GraphHopper();
hopper.init(config);
hopper.importOrLoad();
}
{
GraphHopper hopper = new GraphHopper()
.setGraphHopperLocation(GH_LOCATION)
.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest"));
hopper.importOrLoad();
// error: java.lang.IllegalArgumentException: DefaultEncodedValueFactory cannot find EncodedValue lanes
}
}It fails, because we have an OSMLanesParser that is listed in DefaultTagParserFactory, but the Lanes EV is not listed in DefaultEncodedValueFactory. But here is the catch: The test only fails, because we use the GraphHopper's 'setter' initialization when we load the graph data. If we used init instead it would work, because in this case, even though there is nothing to import anymore, we do create the TagParsers, which then create the encoded values (and not the EncodedValueFactory). Only when we skip the init() call encodingManager will be null in the load method and we try to create the encoded values from the graph.encoded_values string that was written to the properties file.
So we only ever use EncodedValueFactory when we load existing graph data and initialize the GraphHopper class using the setters (do not call init()).
We have a few options:
-
Just remember that we have to keep the
EncodedValueFactoryin sync with theTagParserFactoryin case we loadGraphHopperwithout callinginit(). This is what we do currently. Really ugly (see above). -
Remove
EncodedValueFactoryand only create encoded values viaTagParsers. Maybe a bit better, becauseEncodedValueFactoryis already only used in one specific case and it is already broken/out of sync. Still ugly though, because this means we need to create tag parser objects also when loading an existing graph. But 'parsing tags' is something that has nothing to do with 'loading existing graph data'. The fact thatGraphHopper(as well asGraphHopperStorage,DataAccess,FlagEncoder,EncodingManageretc.) to a large part use the same code to 'load' as they use to 'import' is a huge technical debt if you ask me. The root of many evils, so to say. However, since we already useTagParsersto create the encoded values when we useload()afterinit()anyway, it actually might be worth to removeEncodedValueFactoryfirst and only introduce something similar when we removeTagParser#createEncodedValues. -
Always create encoded values via
EncodedValueFactory(sounds right at least) and stop creating encoded values inTagParsers. For example I wonder what if we created all encoded values first and then passed them to each tag parser. Maybe this would also allow some new use cases, like modifying an encoded value from different tag parsers, or setting encoded values based on others ('aggregated encoded values').
Activity
easbar commentedon Oct 21, 2021
I also did a quick comparison of
DefaultEncodedValueFactoryandDefaultTagParserFactory:lanes,mtb_rating,hike_rating,horse_rating,countryget_off_bike,bike/foot networkand thesubnetworkEVeasbar commentedon Apr 5, 2022
I went with option 2) in #2551.
This is next.