Skip to content

Use JTS to determine applicable spatial rules - #1829

Merged
boldtrn merged 10 commits into
graphhopper:masterfrom
otbutz:JTS_index
Jan 29, 2020
Merged

boldtrn merged 10 commits into
graphhopper:masterfrom
otbutz:JTS_index

Conversation

@otbutz

@otbutz otbutz commented Dec 18, 2019

Copy link
Copy Markdown
Contributor

Builds on top of #1827 and uses JTS classes to handle border geometries and SpatialLookup.

This should also solve #1680 as JTS polygons are able to deal with holes.

@karussell

Copy link
Copy Markdown
Member

Cool, thanks! Probably after this we should get rid of our own Polygon class and also use the JTS version elsewhere.

@otbutz

otbutz commented Dec 19, 2019

Copy link
Copy Markdown
Contributor Author

Cool, thanks! Probably after this we should get rid of our own Polygon class and also use the JTS version elsewhere.

BBox would also be a good candidate for removal.

@boldtrn

boldtrn commented Jan 2, 2020

Copy link
Copy Markdown
Member

Really nice, thanks for the great work. Did you have a chance to compare the performance of the JTS lookup vs. the array lookup?

@otbutz

otbutz commented Jan 3, 2020

Copy link
Copy Markdown
Contributor Author

Really nice, thanks for the great work. Did you have a chance to compare the performance of the JTS lookup vs. the array lookup?

Just did a small benchmark with a dummy rule for each country in our geojson file on my machine with an i7-4790 and OpenJDK 11. Looking up the rules for 1 million random points in central europe takes about 1.5 seconds.

If i change the underlying index from STRtree to QuadTree the execution time goes up to about 2 seconds.
Screenshot_2020-01-03 Bounding Box Tool Metadata Enrichment for Catalogue Records by Visually Selecting Geographic Coordina

SpatialRuleLookup spatialRuleLookup = SpatialRuleLookupBuilder.buildIndex(Collections.singletonList(Jackson.newObjectMapper().readValue(reader, JsonFeatureCollection.class)), "ISO_A3", rulePerCountryFactory);

List<GHPoint> randomPoints = new ArrayList<>();
for (int i = 0; i < 1_000_000; i++) {
    double lat = 46d + Math.random() * 7d;
    double lon = 6d + Math.random() * 21d;
    randomPoints.add(new GHPoint(lat, lon));
}

long start = System.nanoTime();
for (GHPoint point : randomPoints) {
    spatialRuleLookup.lookupRule(point);
}
long duration = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
System.out.println("Took: " + duration + "ms");

I ran the same test against the array lookup but had to limit the number of countries in order for them to fit into the index. With the first 100 countries the same benchmark takes about 0.5 seconds but i noticed that building the index is rather slow with 50 seconds. The JTS approach builds the complete world index with 255 countries in about 30ms.

I than reran the benchmark using the same 100 countries and the JTS index finished in around 0.9 seconds with an index build time of around 20ms.

Considering the startup costs the array lookup would need to process about 80 million points for the startup delay to amortize.

@otbutz

otbutz commented Jan 7, 2020

Copy link
Copy Markdown
Contributor Author

SpatialRuleLookupBuilderTest.testSpeed:246 » OutOfMemory Java heap space

What is the limit for the test builds?

@otbutz
otbutz marked this pull request as ready for review January 7, 2020 12:51
@karussell

Copy link
Copy Markdown
Member

We limit it explicitly to 100MB to detect memory issues very early: https://github.com/graphhopper/graphhopper/blob/master/pom.xml#L118. How much more would be required?

@otbutz

otbutz commented Jan 8, 2020

Copy link
Copy Markdown
Contributor Author

@karussell i've lowered the number of points for the test from 5 to 1 million.

@boldtrn

boldtrn commented Jan 8, 2020

Copy link
Copy Markdown
Member

With the first 100 countries the same benchmark takes about 0.5 seconds but i noticed that building the index is rather slow with 50 seconds.

Yes, that's somewhat expected. The lookup is currently optimized for look up performance. For me building the index was not that time critical. This is only done during the import, where time (adding a few seconds to minutes) plays only a minor role, but we might need to lookup > 200.000.000 edges for world wide imports. Also if we ever want to do the lookup during the route calculation.

Thanks for providing some benchmark data and this nice PR 👍

@otbutz

otbutz commented Jan 8, 2020

Copy link
Copy Markdown
Contributor Author

but we might need to lookup > 200.000.000 edges for world wide imports.

The JTS approach should be fast enough to be used during the import. The benchmark is a bit pessimistic as i measured it with one rule for every country on the planet which results in an index with ~2.6k entries.

Also if we ever want to do the lookup during the route calculation.

There is still room for improvement. The hit-rate of the spatial index seems to be a good candidate for optimization. Just take a look at the bounding box of Austria which covers half of Bavaria:

Screenshot_2020-01-08 Bounding Box Tool Metadata Enrichment for Catalogue Records by Visually Selecting Geographic Coordina

Each point in the southern region of Bavaria needs to be tested against both polygons as the fast index/envelope shortcut doesn't work here.

We could split bigger polygons in order to keep their Envelope small and therefor closer to the actual borders.

@otbutz

otbutz commented Jan 8, 2020

Copy link
Copy Markdown
Contributor Author

Did a bit of profiling using VisualVM while recording roughly 2 minutes of execution time:

Whole world index (2.6k polygons)

Screenshot_20200108_172850

Central europe index (54 polygons)

Screenshot_20200108_182351

Benchmark results

Whole world Europe
1 million points 1.5s 1s
5 million points 6.2s 4.5s

Seems like the size of the index isn't that crucial which means that we would need to lower the amount unnecessary PIP checks in order to speed things up. Note that the number of invocations of SpatialRuleContainer.containsProperly(Point) is roughly twice the amount of rule lookups.

@otbutz

otbutz commented Jan 9, 2020

Copy link
Copy Markdown
Contributor Author

With the last change the lookup of 5 million points is down to 3.5s

@otbutz

otbutz commented Jan 21, 2020

Copy link
Copy Markdown
Contributor Author

@karussell anything missing?

@karussell

Copy link
Copy Markdown
Member

As this is based on #1827 should we close #1827 when merging here? Or should we merge #1827 first?

@otbutz

otbutz commented Jan 27, 2020

Copy link
Copy Markdown
Contributor Author

We can close #1827 when we merge this branch. I just thought it would be better to break my changes into smaller units.

@boldtrn

boldtrn commented Jan 28, 2020

Copy link
Copy Markdown
Member

Since we change the config value to: spatial_rules.borders_directory, maybe we should add this to the changelog.txt? Otherwise people might wonder if SpatialRules are suddenly not loaded anymore?

@otbutz

otbutz commented Jan 28, 2020

Copy link
Copy Markdown
Contributor Author

Since we change the config value to: spatial_rules.borders_directory, maybe we should add this to the changelog.txt? Otherwise people might wonder if SpatialRules are suddenly not loaded anymore?

Thanks! Completely forgot to cherry-pick this commit: 9708e45

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.

Since this would work out of the box now and since we completely change the implementation, wouldn't it make sense to break the interface? Or maybe deprecate the lookupRule method and create a new lookupRules method. lookupRule could fall back to returning only the first result.

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.

The interfaces still need a bit of love but that will be the focus of the following PRs. The next step is going to be adding support for multiple overlapping rules with priorities.

#1808 describes the roadmap

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 sure, that makes sense :)

@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.

Overall this PR looks really good to me. It's great that we are now using JTS. I gave this PR a bit of live testing as well and it seems like everything is working fine. Also the import time for the area "Alps" seems to be more or less equally fast compared to master.

I added a few notes, but nothing critical. I think we can merge this PR.

@otbutz

otbutz commented Jan 28, 2020

Copy link
Copy Markdown
Contributor Author

@boldtrn i've addressed the issues you mentioned and rebased on the current master

@boldtrn

boldtrn commented Jan 28, 2020

Copy link
Copy Markdown
Member

i've addressed the issues you mentioned

Thanks a lot. I added a lost minute note, sorry :).

@karussell karussell added this to the 1.0 milestone Jan 29, 2020
@boldtrn
boldtrn merged commit 7c52b8a into graphhopper:master Jan 29, 2020
@boldtrn

boldtrn commented Jan 29, 2020

Copy link
Copy Markdown
Member

Thanks for the nice improvement @otbutz. I just merged this PR.

@karussell

Copy link
Copy Markdown
Member

Thanks @otbutz! And @boldtrn for the review.

@karussell

Copy link
Copy Markdown
Member

On appveyor we get:

SpatialRuleLookupBuilderTest.testSpeed:256 Benchmark must be finished in less than 10000ms

Can we roughly half the complexity of that speed test to make it pass also on slightly weaker servers?

@otbutz

otbutz commented Jan 29, 2020

Copy link
Copy Markdown
Contributor Author

On appveyor we get:

SpatialRuleLookupBuilderTest.testSpeed:256 Benchmark must be finished in less than 10000ms

Can we roughly half the complexity of that speed test to make it pass also on slightly weaker servers?

I can also remove the assert and keep it only for debugging

@karussell

Copy link
Copy Markdown
Member

But then probably removing the entire test would make more sense. We could move it into the Measurement class instead?

@otbutz

otbutz commented Jan 29, 2020

Copy link
Copy Markdown
Contributor Author

I wasn't aware of that class. Seems like a good fit.

@otbutz
otbutz deleted the JTS_index branch January 29, 2020 16:38
@michaz

michaz commented Jan 30, 2020

Copy link
Copy Markdown
Member

Excellent, thanks! I was wondering, @otbutz, how much performance do we gain by the tiling optimization in SpatialRuleContainer? Is there a tradeoff, do you think there could be Polygons where performance gets worse?

@otbutz

otbutz commented Jan 30, 2020

Copy link
Copy Markdown
Contributor Author

I was wondering, @otbutz, how much performance do we gain by the tiling optimization in SpatialRuleContainer?

About 10-15% as far as i can remember but don't pin me down on this.

Is there a tradeoff, do you think there could be Polygons where performance gets worse?

I tested a few optimizations to see if i can shove off a bit of execution time by using simpler checks to avoid the whole point-in-polygon logic wherever possible. Ideally you would want to find the biggest envelope which can fit entirely into the polygon without intersecting/covering any holes.

As the country polygons are rather complex, finding this ideal inner envelope is quite hard(NP-complete?) and would require some kind of optimization algorithm. Breaking the polygon up into tiles is rather quick and servers as a good approximation.

A small grid won't yield any benefits as most tiles will intersect with the border. If the grid is too big, the costs of the tile checks outweigh the regular point-in-polygon costs.

I don't think that the tiling approach will cause problems for certain polygons. Either we have a high hitrate which allows us to dodge most of the pip logic or we don't have a lot/any tiles which eliminates the envelope checks.

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.

4 participants