public static Polygon parsePoints(String pointsStr, double growFactor) { String[] arr = pointsStr.split(","); if (arr.length % 2 == 1) throw new IllegalArgumentException("incorrect polygon specified"); double[] lats = new double[arr.length /2]; double[] lons = new double[arr.length /2]; for (int j = 0; j < arr.length; j++) { if (j % 2 == 0) { lats[j / 2] = Double.parseDouble(arr[j]); } else { lons[(j - 1) / 2] = Double.parseDouble(arr[j]); } } return new Polygon(lats, lons, growFactor); } }
private Polygon parsePolygonString(String polygonString) { String[] germanPolygonArr = polygonString.split("\\],\\["); double[] lats = new double[germanPolygonArr.length]; double[] lons = new double[germanPolygonArr.length]; for (int i = 0; i < germanPolygonArr.length; i++) { String temp = germanPolygonArr[i]; temp = temp.replaceAll("\\[", ""); temp = temp.replaceAll("\\]", ""); String[] coords = temp.split(","); lats[i] = Double.parseDouble(coords[1]); lons[i] = Double.parseDouble(coords[0]); } return new Polygon(lats, lons); }
/** * Lossy conversion to a GraphHopper Polygon. */ public static Polygon create(org.locationtech.jts.geom.Polygon polygon) { double[] lats = new double[polygon.getNumPoints()]; double[] lons = new double[polygon.getNumPoints()]; for (int i = 0; i < polygon.getNumPoints(); i++) { lats[i] = polygon.getCoordinates()[i].y; lons[i] = polygon.getCoordinates()[i].x; } return new Polygon(lats, lons); }
@Test public void testExactAdjacentBorder() { List<SpatialRule> spatialRules = new ArrayList<>(); // Two rules that divide the tile in half spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 1.5, 1.5}, new double[]{1, 2, 2, 1}), "top")); spatialRules.add(getSpatialRule(new Polygon(new double[]{1.5, 1.5, 2, 2}, new double[]{1, 2, 2, 1}), "bottom")); SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(spatialRules, 1, true, new BBox(1, 4, 1, 4)); assertEquals("top", spatialRuleLookup.lookupRule(1.4, 1.5).getId()); assertEquals("bottom", spatialRuleLookup.lookupRule(1.6, 1.5).getId()); }
@Test public void testSmallBoundsBigPolygon() { List<SpatialRule> spatialRules = new ArrayList<>(); spatialRules.add(getSpatialRule(new Polygon(new double[]{-100, -100, 100, 100}, new double[]{-100, 100, 100, -100}), "big")); SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(spatialRules, 1, true, new BBox(1, 2, 1, 2)); assertEquals("big", spatialRuleLookup.lookupRule(1.5, 1.5).getId()); }
@Test public void testSmallScenario() { List<SpatialRule> spatialRules = new ArrayList<>(); spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 2, 2}, new double[]{1, 2, 2, 1}), "1")); spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 3.6, 3.6}, new double[]{3, 4, 4, 3}), "2")); SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(spatialRules, 1, false, new BBox(1, 4, 1, 4)); Assert.assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 3.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); assertEquals(AccessValue.ACCESSIBLE, spatialRuleLookup.lookupRule(2.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); }
@Test public void testExact() { List<SpatialRule> spatialRules = new ArrayList<>(); spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 2, 2}, new double[]{1, 2, 2, 1}), "1")); spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 3.6, 3.6}, new double[]{3, 4, 4, 3}), "2")); spatialRules.add(getSpatialRule(new Polygon(new double[]{1, 1, 2, 2}, new double[]{-1, 0, 0, -1}), "3")); SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(spatialRules, 1, true, new BBox(-1, 4, 1, 4)); assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 3.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); // Not in the second Polygon anymore assertEquals(AccessValue.ACCESSIBLE, spatialRuleLookup.lookupRule(3.9, 3.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); assertEquals(AccessValue.ACCESSIBLE, spatialRuleLookup.lookupRule(2.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE)); // Get the EmptySpatialRule in a BorderTile #1077 assertEquals(SpatialRule.EMPTY.getId(), spatialRuleLookup.lookupRule(0.9, 0.9).getId()); }
@Test public void testSpatialLookup() { List<SpatialRule> spatialRules = new ArrayList<>(); SpatialRule germany = new DefaultSpatialRule() { @Override public String getId() { return "DEU"; } }.addBorder(new Polygon(new double[]{1, 1, 2, 2}, new double[]{1, 2, 2, 1})); spatialRules.add(germany); SpatialRule austria = new DefaultSpatialRule() { @Override public String getId() { return "AUT"; } }.addBorder(new Polygon(new double[]{5, 5, 6, 6}, new double[]{5, 6, 6, 5})); spatialRules.add(austria); // create lookup with bbox just for DEU (for space reduction) SpatialRuleLookupArray lookup = new SpatialRuleLookupArray(spatialRules, 1, false, new BBox(1, 2, 1, 2)); SpatialRule rule = lookup.lookupRule(1.5, 1.5); assertEquals(germany, rule); assertEquals("DEU", rule.getId()); int id = lookup.getSpatialId(rule); assertTrue(id > 0); assertEquals(rule, lookup.getSpatialRule(id)); }
@Test public void testSpatialId() { final GermanySpatialRule germany = new GermanySpatialRule(); germany.setBorders(Collections.singletonList(new Polygon(new double[]{0, 0, 1, 1}, new double[]{0, 1, 1, 0})));
public static Polygon parsePoints(String pointsStr, double growFactor) { String[] arr = pointsStr.split(","); if (arr.length % 2 == 1) throw new IllegalArgumentException("incorrect polygon specified"); double[] lats = new double[arr.length /2]; double[] lons = new double[arr.length /2]; for (int j = 0; j < arr.length; j++) { if (j % 2 == 0) { lats[j / 2] = Double.parseDouble(arr[j]); } else { lons[(j - 1) / 2] = Double.parseDouble(arr[j]); } } return new Polygon(lats, lons, growFactor); } }
/** * Lossy conversion to a GraphHopper Polygon. */ public static Polygon create(org.locationtech.jts.geom.Polygon polygon) { double[] lats = new double[polygon.getNumPoints()]; double[] lons = new double[polygon.getNumPoints()]; for (int i = 0; i < polygon.getNumPoints(); i++) { lats[i] = polygon.getCoordinates()[i].y; lons[i] = polygon.getCoordinates()[i].x; } return new Polygon(lats, lons); }