Skip to content

Allow overlapping SpatialRules with priorities - #1903

Merged
karussell merged 10 commits into
graphhopper:masterfrom
otbutz:ruleset
Apr 14, 2020
Merged

karussell merged 10 commits into
graphhopper:masterfrom
otbutz:ruleset

Conversation

@otbutz

@otbutz otbutz commented Feb 12, 2020 •

Copy link
Copy Markdown
Contributor

This PR builds on top of #1880 and restructures the SpatialRuleLookup interface to return SpatialRuleSets which cover multiple rules.

@otbutz

otbutz commented Mar 9, 2020

Copy link
Copy Markdown
Contributor Author

@karussell any feedback so far?

@karussell karussell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Did you do some performance tests how this affects speed if spatial rules are present? Probably we should add this to our benchmark to see changes.

@otbutz

otbutz commented Mar 11, 2020

Copy link
Copy Markdown
Contributor Author

Thanks! Did you do some performance tests how this affects speed if spatial rules are present?

I'll test it with an import but the SpatialRuleSet.EMPTY should already avoid a lot of unnecessary object creation. The sorting overhead in lookupRules() shouldn't be that bad considering that the number of overlapping rules for a certain point is going to be very low.

The next step after this PR will be to store the active rule IDs in the graph properties in order to drop the Country enum. That should remove the last obstacle for external rule files :)

@karussell

Copy link
Copy Markdown
Member

I'll test it with an import

Thanks!

The next step after this PR will be to store the active rule IDs in the graph properties in order to drop the Country enum. That should remove the last obstacle for external rule files :)

But why a special handling for the country? E.g. for road_class we also use the enum for storage. This sounds a bit strange but as we also want enums for a nicer/simpler Java API there is IMO no better solution.

@otbutz

otbutz commented Mar 12, 2020

Copy link
Copy Markdown
Contributor Author

but as we also want enums for a nicer/simpler Java API there is IMO no better solution.

The user will be able to pass arbitrary rules with custom IDs later. I cannot use enums for this.

@karussell

Copy link
Copy Markdown
Member

Ok. I think we have two requirements here: one is a set of countries (Country enum) and the other are custom areas where we could use integer IDs or even the StringIndex. So even if we make custom areas possible without code we cannot remove the Country enum.

@otbutz

otbutz commented Mar 12, 2020 •

Copy link
Copy Markdown
Contributor Author

Is there a reason why we should treat country rules differently? We could move them to external files later and use the same mechanism.

@karussell

Copy link
Copy Markdown
Member

Have not a strong opinion about this but "country" is a distinct set of values similar to what we have for road_class.

@otbutz

otbutz commented Mar 16, 2020

Copy link
Copy Markdown
Contributor Author

I've tested the import with a small dataset(Bavaria) and didn't really notice a difference. But i'm going to run the import using the Europe pbf just to be sure.

@otbutz
otbutz requested a review from karussell April 1, 2020 10:56
@otbutz

otbutz commented Apr 7, 2020

Copy link
Copy Markdown
Contributor Author

@karussell anything missing?

@karussell

Copy link
Copy Markdown
Member

Sorry for the delay and yes, time is missing ;)

Have already asked @boldtrn to try this out in real world, so we'll have some progress here within the next 3 weeks or so :)

@boldtrn

boldtrn commented Apr 14, 2020

Copy link
Copy Markdown
Member

Have not a strong opinion about this but "country" is a distinct set of values similar to what we have for road_class.

I think the biggest advantage of using ids is that it's easy to extend and customize. For example one might use countries most of the time, but in the US, we might use states, because rules can differ a lot between states (for example - I have not idea if this is true). Enums are a bit unflexible and hard to change. But with IDs I don't care, I can simply add them.

@boldtrn

boldtrn commented Apr 14, 2020 •

Copy link
Copy Markdown
Member

I had a look at the code and I missed a test case for the overlapping, so I wrote one for SpatialRuleLookupJTSTest to also better understand the internals, maybe we could add it (or anything similar) to the PR?

    @Test
    public void testOverlapWitPriority() {
        // Let the two polygons overlap
        Polygon deBorder = FAC.createPolygon(new Coordinate[] { new Coordinate(1, 1), new Coordinate(2, 1), new Coordinate(2, 2), new Coordinate(1, 2), new Coordinate(1, 1) });
        List<SpatialRule> spatialRules = new ArrayList<>();
        SpatialRule germany = new AbstractSpatialRule(deBorder) {
            @Override
            public String getId() {
                return "DEU";
            }
        };
        spatialRules.add(germany);

        Polygon atBorder = FAC.createPolygon(new Coordinate[] { new Coordinate(0.5, 1), new Coordinate(1.5, 1), new Coordinate(1.5, 2), new Coordinate(0.5, 2), new Coordinate(0.5, 1) });
        SpatialRule austria = new AbstractSpatialRule(atBorder) {
            @Override
            public String getId() {
                return "AUT";
            }

            @Override
            public int getPriority(){
                return 500;
            }
        };
        spatialRules.add(austria);

        SpatialRuleLookupJTS lookup = new SpatialRuleLookupJTS(spatialRules, new Envelope(0, 3, 0, 3));
        SpatialRuleSet set = lookup.lookupRules(1.5, 1.25);
        assertEquals(2, set.getRules().size());
        // Since Austria is priority 500, Germany should be the first, Austria the second
        assertEquals(germany.getId(), set.getRules().get(0).getId());
        assertEquals(austria.getId(), set.getRules().get(1).getId());
        assertEquals(1, lookup.lookupRules(1.5, 0.99).getRules().size());
        assertEquals(2, lookup.lookupRules(1.5, 1.00).getRules().size());
        assertEquals(2, lookup.lookupRules(1.5, 1.50).getRules().size());
        assertEquals(1, lookup.lookupRules(1.5, 1.51).getRules().size());
    }

@otbutz

otbutz commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

@boldtrn Thank you! I have added your test case and listed you as commit author.

@boldtrn

boldtrn commented Apr 14, 2020

Copy link
Copy Markdown
Member

Thanks! The author wouldn't have been necessary :)

@boldtrn

boldtrn commented Apr 14, 2020

Copy link
Copy Markdown
Member

I also did a manual test by changing the polygons of Germany and Austria, so that they massively overlap and checked if the priorities and overlapping is resolved correctly. The results look good to me.

Screenshot from 2020-04-14 16-57-26
Screenshot from 2020-04-14 16-57-09

@boldtrn boldtrn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me. Thanks!

This seems to work perfect.

The only thing I haven't compared is the performance.

A minor thing I missed was to specify the priority for example through the geojson, but I think this could be easily added in the future, if that's a desired change in the future.

@otbutz

otbutz commented Apr 14, 2020

Copy link
Copy Markdown
Contributor Author

A minor thing I missed was to specify the priority for example through the geojson, but I think this could be easily added in the future, if that's a desired change in the future.

This is going to be one of the next steps:

  • storing IDs as part of the graph properties
  • defining an external rule file

@boldtrn

boldtrn commented Apr 14, 2020

Copy link
Copy Markdown
Member

This is going to be one of the next steps

I thought so :). So I think this should be good to merge.

@karussell

Copy link
Copy Markdown
Member

Thanks @otbutz and @boldtrn !

@karussell
karussell merged commit fb34ac0 into graphhopper:master Apr 14, 2020
@karussell karussell added this to the 1.0 milestone Apr 14, 2020
SpatialRule ruleAdj = ruleLookup.lookupRules(nodeAccess.getLatitude(adjNode), nodeAccess.getLongitude(adjNode)).getRules().get(0);

int baseNode = allEdgesIterator.getBaseNode();
SpatialRule ruleBase = ruleLookup.lookupRule(nodeAccess.getLatitude(baseNode), nodeAccess.getLongitude(baseNode));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@otbutz if the first SpatialRule is always available should we provide a convenient method for this? Or will it be rather rare use case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not always available per se. The landmark use case is a bit special in that respect.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so if no rule is available the list could be empty (unlike before where you got SpatialRule.EMPTY?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. If no rules apply, an empty List is returned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a problem with this code: #2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants