Repository navigation
Polygon for blocked areas - #1306
Conversation
|
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I implemented a new Polygon class because:
- My class has the
GROWN_FACTORconstant, which is used to increase bounds ofPolygon - The existing
Polygonclass doesn't implementShapeinterface, so I cannot pass it tofindEdgesInShapemethod. - 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
Thanks! What is this method call: GeometryFactory.createPointFromInternalCoord ?
There was a problem hiding this comment.
It's to transform Coordinate into Geometry (Point) to be abble to pass into contains() method
There was a problem hiding this comment.
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 :))
|
@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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Here it should throw an UnsupportedException and not return a potentially wrong result, same for contains and calculateArea
| } | ||
| } | ||
|
|
||
| return new Polygon(lats, lons); |
There was a problem hiding this comment.
@boldtrn do you think "lat,lon,lat2,lon2,..." is a good format? At least we should check that the count is dividable by 2
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Why do we return isPolygon here?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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
|
Really nice approach @andreylh, thanks for providing this PR. Would you mind to add a test that shows that block Polygon works? |
|
@boldtrn thank you very much. Tests added. |
|
@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]; |
There was a problem hiding this comment.
@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]); |
There was a problem hiding this comment.
here j/2+1 makes more sense as index into the lons array IMO
There was a problem hiding this comment.
@karussell Can you give an example of a j where it is clear how it would make more sense?
There was a problem hiding this comment.
Oh, probably I misunderstood this index, sorry for the bubble
|
@andreylh I've added two comments (regarding if arr.length%2==1 and |
This PR allows block areas to support polygons (discuss here and here)
How this works (it's in Portuguese, "Centro" means "Center"):
