Skip to content

Polygon for blocked areas - #1306

Merged
karussell merged 11 commits into
graphhopper:masterfrom
andreylh:polygon-blocked-areas
Apr 9, 2018
Merged

karussell merged 11 commits into
graphhopper:masterfrom
andreylh:polygon-blocked-areas

Conversation

@andreylh

@andreylh andreylh commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

This PR allows block areas to support polygons (discuss here and here)

How this works (it's in Portuguese, "Centro" means "Center"):
poligono

@karussell

Copy link
Copy Markdown
Member

Thanks a lot for this! We'll have a look, would you mind to send us your CLA? Please also go ahead and post this PR into the referenced discussion threads.

import java.util.ArrayList;
import java.util.List;

public class Polygon implements Shape {

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.

We should avoid another Polygon class here. The problem here is probably that the current one is in the wrong package? com.graphhopper.routing.util.spatialrules

Or should we use the polygon from JTS?

@andreylh andreylh Mar 9, 2018 •

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.

I implemented a new Polygon class because:

  • My class has the GROWN_FACTOR constant, which is used to increase bounds of Polygon
  • The existing Polygon class doesn't implement Shape interface, so I cannot pass it to findEdgesInShape method.
  • The package com.graphhopper.util.shapes seems to be the best option to me since it already has others geometric shapes.

I'm not sure if I can apply this changes to the existing Polygon class and use. If I can, please let me know and I do the changes.

I don't know if Polygon from JTS library has all features that I need, so I'm going to study the library if I can use it.

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.

I'm going to study the library if I can use it.

This would be the preferred option

If I can, please let me know and I do the changes.

Yes, if JTS is not possible please apply the changes to the existing class and e.g. use a default GROW_FACTOR of 1

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 existing Polygon class doesn't implement Shape interface

I didn't implement the Shape interface because at that point we did not need all the methods supported by the shape interface like intersects etc. If using JTS does not work, feel free to extend the existing class :).

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.

So, I did the changes using JTS library and it works. The problem was the performance. Polygon class is almost 10x faster than JTS.

Maybe I did something wrong, I do not know. Anyway, I'm going to commit the changes using Polygon class.

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.

This is interesting thanks for letting us know. So which in-built methods did you use from JTS or where did the speed loss came from?

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.

btw: applying a (speed or access) change permanently via polgon would be also nice and we currently investigate to make this more streamlined in #1280

@andreylh andreylh Mar 12, 2018 •

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.

@karussell I did not investigate where the performance problem is, just did a test with 484 requests. Using JTS the time was approximately 464 seconds versus 42 seconds of the Polygon class

You can see the changes using JTS solution in my forked repo

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! What is this method call: GeometryFactory.createPointFromInternalCoord ?

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.

It's to transform Coordinate into Geometry (Point) to be abble to pass into contains() method

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

Looks like a really condensed PR - thanks a lot!

Would you mind to have a look if you can utilize one of the two existing Polygon classes already?

Of course you could move the existing Polgon class into shapes and also add new methods if required, but the best would be if we can use more standard classes like the Polygon class from JTS (because e.g. those already can be read from JSON if we ever offer the option to request GH via JSON instead of URL :))

@andreylh

andreylh commented Mar 12, 2018 •

Copy link
Copy Markdown
Contributor Author

@karussell, I did the changes using JTS library and it works. The problem was the performance. Polygon class is almost 10x faster than JTS.

Maybe I did something wrong, I do not know. Anyway, I committed the changes using Polygon class.

changes using JTS solution in my forked repo

minLat -= GROWN_FACTOR;
minLon -= GROWN_FACTOR;
maxLat += GROWN_FACTOR;
maxLon += GROWN_FACTOR;

@karussell karussell Mar 12, 2018 •

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.

Here it would be better if we can keep the default constructor (without touching minLat etc) and create a second one like

public Polygon(double[] lat, double[] lon, double growFactor)


@Override
public boolean intersect(Shape o) {
return false;

@karussell karussell Mar 12, 2018 •

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.

Here it should throw an UnsupportedException and not return a potentially wrong result, same for contains and calculateArea

}
}

return new Polygon(lats, lons);

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.

@boldtrn do you think "lat,lon,lat2,lon2,..." is a good format? At least we should check that the count is dividable by 2

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.

Mhm, good question. My issue with this is that it is really hard to read and write for humans. It is easy for computers though. We should definitely check if the length is a multiple of 2. Maybe a geojson like format would be better? e.g. [[lon,lat],[lon2,lat2]]... But then we don't do this for BBox. Maybe we should do this for BBox and Polygon?

@karussell karussell Mar 16, 2018 •

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.

When I think about this, the only format that makes sense would be like it is proposed here (as we do not use brackets elsewhere and when we don't use brackets we use lat,lon and not lon,lat). But this shows again the ugliness of the URL request approach that we currently have and we should invest more into 1112 to being able to request via JSON where we then could easily use GeoJSON for something like this.

return true;
}
return false;
return isPolygon;

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.

Why do we return isPolygon here?

@andreylh andreylh Mar 19, 2018 •

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.

When the shape is polygon I need to check if is adjacent even if lat/lon is outside of shape bounds. This could happen when polygon is complex. What can I do is return true only if lat/lon is inside of bbox of the polygon, but this is already checked by goFuther(). What do you think?

@karussell karussell Mar 26, 2018 •

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.

I do not fully understand this. Do you mean with "even if lat/lon is outside of shape bounds" that adjacent edges are checked because later edges could be again inside the polygon?

We discussed this with @michaz and instead of this "blurry" workaround that will still fail for certain cases, we should probably implement fetching the edgeIds from the LocationIndexTree instead of from the Graph. Of course this would be out of the scope of this PR but then we should return false here? Or should we keep it as a compromise until the new issue is implemented?

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.

@karussell You're right. Adjacent edges could be outside of the polygon but inside of BBox, this is why I need return true.

At the moment I see three options. We could:

  • Keep returning true;
  • Return false only if the adjacent node is outside of BBox (same as goFuther() method);
  • Increase depth of BFS. But it's difficult to determine a value.

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.

For the moment I would prefer the second option (same as goFurther to reduce the candidates meaningful) and later we need to properly fix this via implementing a new method for LocationIndexTree (tree.traverseEdges(callHookPerEdge, shapeAsFilter)). This way we would also not being stuck in a potential subnetwork (assume 2 islands in a shape then we would traverse only within the 1 island) and we do not need a 'middle' entry point that could be somewhere far away from the road network

@boldtrn

boldtrn commented Mar 16, 2018

Copy link
Copy Markdown
Member

Really nice approach @andreylh, thanks for providing this PR. Would you mind to add a test that shows that block Polygon works?

@andreylh

andreylh commented Mar 19, 2018 •

Copy link
Copy Markdown
Contributor Author

@boldtrn thank you very much. Tests added.

@andreylh

Copy link
Copy Markdown
Contributor Author

@karussell @boldtrn Is there still something I need to change?

public static Polygon parsePoints(String pointsStr, double growFactor) {
String[] arr = pointsStr.split(",");

double[] lats = new double[arr.length /2];

@karussell karussell Mar 26, 2018 •

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.

@andreylh we need a small length check. E.g. if arr.length%2==1 throw IllegalArgumentException("incorrect polygon specified")

if (j % 2 == 0) {
lats[j / 2] = Double.parseDouble(arr[j]);
} else {
lons[(j - 1) / 2] = Double.parseDouble(arr[j]);

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.

here j/2+1 makes more sense as index into the lons array IMO

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.

@karussell Can you give an example of a j where it is clear how it would make more sense?

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.

Oh, probably I misunderstood this index, sorry for the bubble

@karussell

Copy link
Copy Markdown
Member

@andreylh I've added two comments (regarding if arr.length%2==1 and return isPolygon)

@karussell
karussell merged commit 3852b7c into graphhopper:master Apr 9, 2018
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