/** * Takes a two dimensional linestring and creates a three dimensional polygon from it. Also closes the polygon if needed. * @param lineString 2d JsonArray * @return 3d double array with the polygon */ public static @Nonnull JsonArray lineStringToPolygon(@Nonnull JsonArray lineString) { if(!lineString.first().equals(lineString.last())) { JsonElement e = lineString.first().deepClone(); lineString.add(e); } return array(lineString); }
public JsonElementAssert last() { isNotEmpty(); return new JsonElementAssert(actual.first()); }
public JsonElementAssert first() { isNotEmpty(); return new JsonElementAssert(actual.first()); }
/** * GeoJson specifies the order of coordinates as x,y or in geo terms longitude followed by latitude. * Most data sources stick to latitude, longitude though. This function, swaps the two for any dimension array. * * * @param array 2d array with latitude, longitude pairs * @return swapped array 2d array with longitude, latitude pairs */ public static @Nonnull JsonArray swapLatLon(@Nonnull JsonArray array) { if(array.isNotEmpty() && array.first().isArray()) { for(JsonElement e: array) { swapLatLon(e.asArray()); } } else { if(array.size() < 2) { throw new IllegalArgumentException("need at least two coordinates"); } JsonElement first = array.get(0); JsonElement second = array.get(1); array.set(0, second); array.set(1, first); } return array; }
public void shouldSwapPoints() { JsonArray ls = toJsonJLineString(lineString1); JsonArray swapped = swapLatLon(ls); assertThat(swapped.first().asArray(), is(array(2.0,1.0))); }
public void shouldConvertLineString() { JsonArray ls = toJsonJLineString(lineString1); assertThat(ls.first().asArray(), is(toJsonJPoint(point1))); assertThat(ls.last().asArray(), is(toJsonJPoint(point3))); }
public void shouldConvertLineStringToPolygon() { JsonArray polygon = GeoJsonSupport.lineStringToPolygon(lineString1); assertThat(polygon.first().asArray().last().asArray(), is(toJsonJPoint(point1))); } }
@Test(expectedExceptions=IllegalStateException.class) public void shouldNotAllowMutationsOnElement() { JsonArray list = array(array(1)).immutableClone(); JsonArray l2=list.first().asArray(); l2.add(2); }