Merge pull request #11074 from osmandapp/FixDownloadButton

Fix algorithm to check one region is inside another
This commit is contained in:
Vitaliy 2021-03-09 10:21:20 +02:00 committed by GitHub
commit bd2e50ec01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 6 deletions

View file

@ -436,7 +436,7 @@ public class OsmandRegions {
cx /= object.getPointsLength(); cx /= object.getPointsLength();
cy /= object.getPointsLength(); cy /= object.getPointsLength();
rd.regionCenter = new LatLon(MapUtils.get31LatitudeY((int) cy), MapUtils.get31LongitudeX((int) cx)); rd.regionCenter = new LatLon(MapUtils.get31LatitudeY((int) cy), MapUtils.get31LongitudeX((int) cx));
rd.boundingBox = findBoundingBox(object); findBoundaries(rd, object);
} }
rd.regionParentFullName = mapIndexFields.get(mapIndexFields.parentFullName, object); rd.regionParentFullName = mapIndexFields.get(mapIndexFields.parentFullName, object);
@ -462,13 +462,15 @@ public class OsmandRegions {
return rd; return rd;
} }
private QuadRect findBoundingBox(BinaryMapDataObject object) { private void findBoundaries(WorldRegion rd, BinaryMapDataObject object) {
if (object.getPointsLength() == 0) { if (object.getPointsLength() == 0) {
return new QuadRect(0, 0, 0, 0); return;
} }
List<LatLon> polygon = new ArrayList<>();
double currentX = object.getPoint31XTile(0); double currentX = object.getPoint31XTile(0);
double currentY = object.getPoint31YTile(0); double currentY = object.getPoint31YTile(0);
polygon.add(new LatLon(currentX, currentY));
double minX = currentX; double minX = currentX;
double maxX = currentX; double maxX = currentX;
double minY = currentY; double minY = currentY;
@ -488,6 +490,7 @@ public class OsmandRegions {
} else if (currentY < minY) { } else if (currentY < minY) {
minY = currentY; minY = currentY;
} }
polygon.add(new LatLon(currentX, currentY));
} }
} }
@ -496,7 +499,8 @@ public class OsmandRegions {
double revertedMinY = MapUtils.get31LatitudeY((int) maxY); double revertedMinY = MapUtils.get31LatitudeY((int) maxY);
double revertedMaxY = MapUtils.get31LatitudeY((int) minY); double revertedMaxY = MapUtils.get31LatitudeY((int) minY);
return new QuadRect(minX, revertedMinY, maxX, revertedMaxY); rd.boundingBox = new QuadRect(minX, revertedMinY, maxX, revertedMaxY);
rd.polygon = polygon;
} }
private String getSearchIndex(BinaryMapDataObject object) { private String getSearchIndex(BinaryMapDataObject object) {

View file

@ -42,6 +42,7 @@ public class WorldRegion implements Serializable {
protected boolean regionMapDownload; protected boolean regionMapDownload;
protected LatLon regionCenter; protected LatLon regionCenter;
protected QuadRect boundingBox; protected QuadRect boundingBox;
protected List<LatLon> polygon;
public static class RegionParams { public static class RegionParams {
protected String regionLeftHandDriving; protected String regionLeftHandDriving;
@ -186,12 +187,23 @@ public class WorldRegion implements Serializable {
} }
public boolean containsRegion(WorldRegion region) { public boolean containsRegion(WorldRegion region) {
if (this.boundingBox != null && region.boundingBox != null) { if (containsBoundingBox(region.boundingBox)) {
return this.boundingBox.contains(region.boundingBox); // check polygon only if bounding box match
return containsPolygon(region.polygon);
} }
return false; return false;
} }
private boolean containsBoundingBox(QuadRect rectangle) {
return (boundingBox != null && rectangle != null) &&
boundingBox.contains(rectangle);
}
private boolean containsPolygon(List<LatLon> another) {
return (polygon != null && another != null) &&
Algorithms.isFirstPolygonInsideSecond(another, polygon);
}
public boolean isContinent() { public boolean isContinent() {
if (superregion != null) { if (superregion != null) {
String superRegionId = superregion.getRegionId(); String superRegionId = superregion.getRegionId();

View file

@ -2,6 +2,7 @@ package net.osmand.util;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -117,6 +118,35 @@ public class Algorithms {
} }
return def; return def;
} }
public static boolean isFirstPolygonInsideSecond(List<LatLon> firstPolygon,
List<LatLon> secondPolygon) {
for (LatLon point : firstPolygon) {
if (!isPointInsidePolygon(point, secondPolygon)) {
// if at least one point is not inside the boundary, return false
return false;
}
}
return true;
}
public static boolean isPointInsidePolygon(LatLon point,
List<LatLon> polygon) {
double pointX = point.getLongitude();
double pointY = point.getLatitude();
boolean result = false;
for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
double x1 = polygon.get(i).getLongitude();
double y1 = polygon.get(i).getLatitude();
double x2 = polygon.get(j).getLongitude();
double y2 = polygon.get(j).getLatitude();
if ((y1 > pointY) != (y2 > pointY)
&& (pointX < (x2 - x1) * (pointY - y1) / (y2-y1) + x1)) {
result = !result;
}
}
return result;
}
public static int parseIntSilently(String input, int def) { public static int parseIntSilently(String input, int def) {
if (input != null && input.length() > 0) { if (input != null && input.length() > 0) {