org.geojson.Polygon Java Examples

The following examples show how to use org.geojson.Polygon. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GeoTests.java    From FROST-Server with GNU Lesser General Public License v3.0 7 votes vote down vote up
private static void createLocation4() throws ServiceFailureException {
    // Locations 4
    Polygon gjo = new Polygon(
            new LngLatAlt(8, 53),
            new LngLatAlt(7, 52),
            new LngLatAlt(7, 53),
            new LngLatAlt(8, 53));
    Location location = new Location("Location 4", "Location of Thing 4.", "application/vnd.geo+json", gjo);
    location.getThings().add(THINGS.get(3));
    service.create(location);
    LOCATIONS.add(location);

    FeatureOfInterest featureOfInterest = new FeatureOfInterest("FoI 4", "This should be FoI #4.", "application/geo+json", gjo);
    service.create(featureOfInterest);
    FEATURESOFINTEREST.add(featureOfInterest);
}
 
Example #2
Source File: GeoHelper.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Polygon parsePolygon(String value) {
    Matcher matcher = GeoHelper.WKT_POLYGON_PATTERN.matcher(value.trim());
    if (matcher.matches()) {
        // definition of GeoJson Polygon:
        // First parameter is exterior ring, all others are interior rings
        String group = matcher.group(2);
        String[] rings = group.trim().substring(1, group.length() - 1).split("[)]\\s*,\\s*[(]");
        Polygon result = new Polygon(stringListToPoints(rings[0]));
        for (int i = 1; i < rings.length; i++) {
            // add interior rings
            result.addInteriorRing(stringListToPoints(rings[i]));
        }
        return result;
    } else {
        throw new IllegalArgumentException("'" + value + DOES_NOT_MATCH_PATTERN + GeoHelper.WKT_POLYGON_PATTERN.pattern() + "'");
    }
}
 
Example #3
Source File: PolygonConstantTest.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testParseFromString2DWithInteriorRing() {
    String text = "POLYGON ((30 10, 10 30, 40 40), (29 29, 29 30, 30 29))";
    PolygonConstant result = new PolygonConstant(text);
    Polygon polygon = TestHelper.getPolygon(
            2,
            30, 10,
            10, 30,
            40, 40);
    polygon.addInteriorRing(TestHelper.getPointList(
            2,
            29, 29,
            29, 30,
            30, 29));
    Assert.assertEquals(polygon, result.getValue());
}
 
Example #4
Source File: PolygonConstantTest.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testParseFromString2DWithMultpleInteriorRings() {
    String text = "POLYGON ((30 10, 10 30, 40 40), (29 29, 29 30, 30 29), (21 21, 21 22, 22 21))";
    PolygonConstant result = new PolygonConstant(text);
    Polygon polygon = TestHelper.getPolygon(
            2,
            30, 10,
            10, 30,
            40, 40);
    polygon.addInteriorRing(TestHelper.getPointList(
            2,
            29, 29,
            29, 30,
            30, 29));
    polygon.addInteriorRing(TestHelper.getPointList(
            2,
            21, 21,
            21, 22,
            22, 21));
    Assert.assertEquals(polygon, result.getValue());
}
 
Example #5
Source File: PolygonConstantTest.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testParseFromString3DWithInteriorRing() {
    String text = "POLYGON ((30 10 1, 10 30 1, 40 40 1), (29 29 1, 29 30 1, 30 29 1))";
    PolygonConstant result = new PolygonConstant(text);
    Polygon polygon = TestHelper.getPolygon(
            3,
            30, 10, 1,
            10, 30, 1,
            40, 40, 1);
    polygon.addInteriorRing(TestHelper.getPointList(
            3,
            29, 29, 1,
            29, 30, 1,
            30, 29, 1));
    Assert.assertEquals(polygon, result.getValue());
}
 
Example #6
Source File: PolygonConstantTest.java    From FROST-Server with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testParseFromString3DWithMultpleInteriorRings() {
    String text = "POLYGON ((30 10 1, 10 30 1, 40 40 1), (29 29 1, 29 30 1, 30 29 1), (21 21 1, 21 22 1, 22 21 1))";
    PolygonConstant result = new PolygonConstant(text);
    Polygon polygon = TestHelper.getPolygon(
            3,
            30, 10, 1,
            10, 30, 1,
            40, 40, 1);
    polygon.addInteriorRing(TestHelper.getPointList(
            3,
            29, 29, 1,
            29, 30, 1,
            30, 29, 1));
    polygon.addInteriorRing(TestHelper.getPointList(
            3,
            21, 21, 1,
            21, 22, 1,
            22, 21, 1));
    Assert.assertEquals(polygon, result.getValue());
}
 
Example #7
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldSerialize() throws Exception {
	Polygon polygon = new Polygon(MockData.EXTERNAL);
	assertEquals("{\"type\":\"Polygon\",\"coordinates\":"
			+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}",
			mapper.writeValueAsString(polygon));
}
 
Example #8
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldSerializeWithHole() throws Exception {
	Polygon polygon = new Polygon(MockData.EXTERNAL);
	polygon.addInteriorRing(MockData.INTERNAL);
	assertEquals("{\"type\":\"Polygon\",\"coordinates\":"
			+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
			+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", mapper.writeValueAsString(polygon));
}
 
Example #9
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldDeserialize() throws Exception {
	Polygon polygon = mapper.readValue("{\"type\":\"Polygon\",\"coordinates\":"
			+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
			+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", Polygon.class);
	assertListEquals(MockData.EXTERNAL, polygon.getExteriorRing());
	assertListEquals(MockData.INTERNAL, polygon.getInteriorRing(0));
	assertListEquals(MockData.INTERNAL, polygon.getInteriorRings().get(0));
}
 
Example #10
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldReplaceExteriorRing() throws Exception {
	Polygon polygon = new Polygon(Arrays.asList(
				new LngLatAlt(0, 0), new LngLatAlt(1, 0), new LngLatAlt(1, 1), new LngLatAlt(0, 1), new LngLatAlt(0, 0)));
	polygon.setExteriorRing(MockData.EXTERNAL);
	assertEquals(MockData.EXTERNAL, polygon.getExteriorRing());
	assertEquals(0, polygon.getInteriorRings().size());
}
 
Example #11
Source File: MultiPoligonTest.java    From geojson-jackson with Apache License 2.0 5 votes vote down vote up
@Test
public void itShouldSerialize() throws Exception {
	MultiPolygon multiPolygon = new MultiPolygon();
	multiPolygon.add(new Polygon(new LngLatAlt(102, 2), new LngLatAlt(103, 2), new LngLatAlt(103, 3),
			new LngLatAlt(102, 3), new LngLatAlt(102, 2)));
	Polygon polygon = new Polygon(MockData.EXTERNAL);
	polygon.addInteriorRing(MockData.INTERNAL);
	multiPolygon.add(polygon);
	assertEquals(
			"{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]],"
					+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
					+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}",
			mapper.writeValueAsString(multiPolygon));
}
 
Example #12
Source File: PolygonConstant.java    From FROST-Server with GNU Lesser General Public License v3.0 4 votes vote down vote up
public PolygonConstant(Polygon value) {
    super(value);
}
 
Example #13
Source File: PolygonConstant.java    From FROST-Server with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected Polygon parse(String value) {
    return GeoHelper.parsePolygon(value);
}
 
Example #14
Source File: TestHelper.java    From FROST-Server with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static <T extends Number> Polygon getPolygon(int dimensions, T... values) {
    return new Polygon(getPointList(dimensions, values));
}
 
Example #15
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 4 votes vote down vote up
@Test(expected = RuntimeException.class)
public void itShouldFailOnAddInteriorRingWithoutExteriorRing() throws Exception {
	Polygon polygon = new Polygon();
	polygon.addInteriorRing(MockData.EXTERNAL);
}
 
Example #16
Source File: PolygonTest.java    From geojson-jackson with Apache License 2.0 4 votes vote down vote up
@Test
public void itShouldSetExteriorRing() throws Exception {
	Polygon polygon = new Polygon();
	polygon.setExteriorRing(MockData.EXTERNAL);
	assertEquals(MockData.EXTERNAL, polygon.getExteriorRing());
}