Java Code Examples for com.esri.core.geometry.ogc.OGCPolygon

The following examples show how to use com.esri.core.geometry.ogc.OGCPolygon. These examples are extracted from open source projects. 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 Project: presto   Author: prestosql   File: GeoFunctions.java    License: Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns an array of interior rings of a polygon")
@ScalarFunction("ST_InteriorRings")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stInteriorRings(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_InteriorRings", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }

    OGCPolygon polygon = (OGCPolygon) geometry;
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, polygon.numInteriorRing());
    for (int i = 0; i < polygon.numInteriorRing(); i++) {
        GEOMETRY.writeSlice(blockBuilder, serialize(polygon.interiorRingN(i)));
    }
    return blockBuilder.build();
}
 
Example #2
Source Project: spatial-framework-for-hadoop   Author: Esri   File: ST_InteriorRingN.java    License: Apache License 2.0 6 votes vote down vote up
public BytesWritable evaluate(BytesWritable geomref, IntWritable index) {
	if (geomref == null || geomref.getLength() == 0 || index == null) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

	OGCGeometry ogcGeometry = GeometryUtils.geometryFromEsriShape(geomref);
	if (ogcGeometry == null){
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

	int idx = index.get() - 1;  // 1-based UI, 0-based engine
	if (GeometryUtils.getType(geomref) == GeometryUtils.OGCType.ST_POLYGON) {
		try {
			OGCLineString hole = ((OGCPolygon)(ogcGeometry)).interiorRingN(idx);
			return GeometryUtils.geometryToEsriShapeBytesWritable(hole);
		} catch (Exception e) {
			LogUtils.Log_InternalError(LOG, "ST_InteriorRingN: " + e);
			return null;
		}
	} else {
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POLYGON, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #3
Source Project: spatial-framework-for-hadoop   Author: Esri   File: ST_NumInteriorRing.java    License: Apache License 2.0 6 votes vote down vote up
public IntWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

	OGCGeometry ogcGeometry = GeometryUtils.geometryFromEsriShape(geomref);
	if (ogcGeometry == null){
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}
	if (GeometryUtils.getType(geomref) == GeometryUtils.OGCType.ST_POLYGON) {
		try {
			resultInt.set(((OGCPolygon)(ogcGeometry)).numInteriorRing());
			return resultInt;
		} catch (Exception e) {
			LogUtils.Log_InternalError(LOG, "ST_NumInteriorRing: " + e);
			return null;
		}
	} else {
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POLYGON, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #4
Source Project: geometry-api-java   Author: Esri   File: TestOGC.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPoint() {
	OGCGeometry g = OGCGeometry.fromText("POINT(1 2)");
	assertTrue(g.geometryType().equals("Point"));
	OGCPoint p = (OGCPoint) g;
	assertTrue(p.X() == 1);
	assertTrue(p.Y() == 2);
	assertTrue(g.equals(OGCGeometry.fromText("POINT(1 2)")));
	assertTrue(!g.equals(OGCGeometry.fromText("POINT(1 3)")));
	assertTrue(g.equals((Object)OGCGeometry.fromText("POINT(1 2)")));
	assertTrue(!g.equals((Object)OGCGeometry.fromText("POINT(1 3)")));
	OGCGeometry buf = g.buffer(10);
	assertTrue(buf.geometryType().equals("Polygon"));
	OGCPolygon poly = (OGCPolygon) buf.envelope();
	double a = poly.area();
	assertTrue(Math.abs(a - 400) < 1e-1);
}
 
Example #5
Source Project: geometry-api-java   Author: Esri   File: TestOGC.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testFirstPointOfPolygon() {
	OGCGeometry g = OGCGeometry
			.fromText("POLYGON((-10 -10, 10 -10, 10 10, -10 10, -10 -10), (-5 -5, -5 5, 5 5, 5 -5, -5 -5))");
	assertTrue(g.geometryType().equals("Polygon"));
	OGCPolygon p = (OGCPolygon) g;
	assertTrue(p.numInteriorRing() == 1);
	OGCLineString ls = p.exteriorRing();
	OGCPoint p1 = ls.pointN(1);
	assertTrue(ls.pointN(1).equals(OGCGeometry.fromText("POINT(10 -10)")));
	OGCPoint p2 = ls.pointN(3);
	assertTrue(ls.pointN(3).equals(OGCGeometry.fromText("POINT(-10 10)")));
	OGCPoint p0 = ls.pointN(0);
	assertTrue(ls.pointN(0).equals(OGCGeometry.fromText("POINT(-10 -10)")));
	String ms = g.convertToMulti().asText();
	assertTrue(ms.equals("MULTIPOLYGON (((-10 -10, 10 -10, 10 10, -10 10, -10 -10), (-5 -5, -5 5, 5 5, 5 -5, -5 -5)))"));

}
 
Example #6
Source Project: geometry-api-java   Author: Esri   File: TestGeomToGeoJson.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOGCPolygonWithHole() {
	Polygon p = new Polygon();

	p.startPath(100.0, 0.0);
	p.lineTo(100.0, 1.0);
	p.lineTo(101.0, 1.0);
	p.lineTo(101.0, 0.0);
	p.closePathWithLine();

	p.startPath(100.2, 0.2);
	p.lineTo(100.8, 0.2);
	p.lineTo(100.8, 0.8);
	p.lineTo(100.2, 0.8);
	p.closePathWithLine();

	OGCPolygon ogcPolygon = new OGCPolygon(p, null);
	String result = ogcPolygon.asGeoJson();
	assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100,0],[101,0],[101,1],[100,1],[100,0]],[[100.2,0.2],[100.2,0.8],[100.8,0.8],[100.8,0.2],[100.2,0.2]]],\"crs\":null}", result);
}
 
Example #7
Source Project: presto   Author: prestosql   File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the cardinality of the collection of interior rings of a polygon")
@ScalarFunction("ST_NumInteriorRing")
@SqlType(BIGINT)
public static Long stNumInteriorRings(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_NumInteriorRing", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }
    return Long.valueOf(((OGCPolygon) geometry).numInteriorRing());
}
 
Example #8
Source Project: presto   Author: prestosql   File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the interior ring element at the specified index (indices start at 1)")
@ScalarFunction("ST_InteriorRingN")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stInteriorRingN(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(INTEGER) long index)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_InteriorRingN", geometry, EnumSet.of(POLYGON));
    OGCPolygon polygon = (OGCPolygon) geometry;
    if (index < 1 || index > polygon.numInteriorRing()) {
        return null;
    }
    OGCGeometry interiorRing = polygon.interiorRingN(toIntExact(index) - 1);
    return serialize(interiorRing);
}
 
Example #9
Source Project: presto   Author: prestosql   File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns a line string representing the exterior ring of the POLYGON")
@ScalarFunction("ST_ExteriorRing")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stExteriorRing(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_ExteriorRing", geometry, EnumSet.of(POLYGON));
    if (geometry.isEmpty()) {
        return null;
    }
    return serialize(((OGCPolygon) geometry).exteriorRing());
}
 
Example #10
Source Project: presto   Author: prestosql   File: GeometryUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isPointOrRectangle(OGCGeometry ogcGeometry, Envelope envelope)
{
    if (ogcGeometry instanceof OGCPoint) {
        return true;
    }

    if (!(ogcGeometry instanceof OGCPolygon)) {
        return false;
    }

    Polygon polygon = (Polygon) ogcGeometry.getEsriGeometry();
    if (polygon.getPathCount() > 1) {
        return false;
    }

    if (polygon.getPointCount() != 4) {
        return false;
    }

    Set<Point> corners = new HashSet<>();
    corners.add(new Point(envelope.getXMin(), envelope.getYMin()));
    corners.add(new Point(envelope.getXMin(), envelope.getYMax()));
    corners.add(new Point(envelope.getXMax(), envelope.getYMin()));
    corners.add(new Point(envelope.getXMax(), envelope.getYMax()));

    for (int i = 0; i < 4; i++) {
        Point point = polygon.getPoint(i);
        if (!corners.contains(point)) {
            return false;
        }
    }

    return true;
}
 
Example #11
Source Project: presto   Author: prestosql   File: GeometrySerde.java    License: Apache License 2.0 5 votes vote down vote up
private static OGCGeometry createFromEsriGeometry(Geometry geometry, boolean multiType)
{
    Geometry.Type type = geometry.getType();
    switch (type) {
        case Polygon: {
            if (!multiType && ((Polygon) geometry).getExteriorRingCount() <= 1) {
                return new OGCPolygon((Polygon) geometry, null);
            }
            return new OGCMultiPolygon((Polygon) geometry, null);
        }
        case Polyline: {
            if (!multiType && ((Polyline) geometry).getPathCount() <= 1) {
                return new OGCLineString((Polyline) geometry, 0, null);
            }
            return new OGCMultiLineString((Polyline) geometry, null);
        }
        case MultiPoint: {
            if (!multiType && ((MultiPoint) geometry).getPointCount() <= 1) {
                if (geometry.isEmpty()) {
                    return new OGCPoint(new Point(), null);
                }
                return new OGCPoint(((MultiPoint) geometry).getPoint(0), null);
            }
            return new OGCMultiPoint((MultiPoint) geometry, null);
        }
        case Point: {
            if (!multiType) {
                return new OGCPoint((Point) geometry, null);
            }
            return new OGCMultiPoint((Point) geometry, null);
        }
        case Envelope: {
            Polygon polygon = new Polygon();
            polygon.addEnvelope((Envelope) geometry, false);
            return new OGCPolygon(polygon, null);
        }
        default:
            throw new IllegalArgumentException("Unexpected geometry type: " + type);
    }
}
 
Example #12
Source Project: geometry-api-java   Author: Esri   File: TestEstimateMemorySize.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInstanceSizes() {
	assertEquals(getInstanceSize(AttributeStreamOfFloat.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_FLOAT);
	assertEquals(getInstanceSize(AttributeStreamOfDbl.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_DBL);
	assertEquals(getInstanceSize(AttributeStreamOfInt8.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_INT8);
	assertEquals(getInstanceSize(AttributeStreamOfInt16.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_INT16);
	assertEquals(getInstanceSize(AttributeStreamOfInt32.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_INT32);
	assertEquals(getInstanceSize(AttributeStreamOfInt64.class), SizeOf.SIZE_OF_ATTRIBUTE_STREAM_OF_INT64);
	assertEquals(getInstanceSize(Envelope.class), SizeOf.SIZE_OF_ENVELOPE);
	assertEquals(getInstanceSize(Envelope2D.class), SizeOf.SIZE_OF_ENVELOPE2D);
	assertEquals(getInstanceSize(Line.class), SizeOf.SIZE_OF_LINE);
	assertEquals(getInstanceSize(MultiPath.class), SizeOf.SIZE_OF_MULTI_PATH);
	assertEquals(getInstanceSize(MultiPathImpl.class), SizeOf.SIZE_OF_MULTI_PATH_IMPL);
	assertEquals(getInstanceSize(MultiPoint.class), SizeOf.SIZE_OF_MULTI_POINT);
	assertEquals(getInstanceSize(MultiPointImpl.class), SizeOf.SIZE_OF_MULTI_POINT_IMPL);
	assertEquals(getInstanceSize(Point.class), SizeOf.SIZE_OF_POINT);
	assertEquals(getInstanceSize(Polygon.class), SizeOf.SIZE_OF_POLYGON);
	assertEquals(getInstanceSize(Polyline.class), SizeOf.SIZE_OF_POLYLINE);
	assertEquals(getInstanceSize(OGCConcreteGeometryCollection.class),
			SizeOf.SIZE_OF_OGC_CONCRETE_GEOMETRY_COLLECTION);
	assertEquals(getInstanceSize(OGCLineString.class), SizeOf.SIZE_OF_OGC_LINE_STRING);
	assertEquals(getInstanceSize(OGCMultiLineString.class), SizeOf.SIZE_OF_OGC_MULTI_LINE_STRING);
	assertEquals(getInstanceSize(OGCMultiPoint.class), SizeOf.SIZE_OF_OGC_MULTI_POINT);
	assertEquals(getInstanceSize(OGCMultiPolygon.class), SizeOf.SIZE_OF_OGC_MULTI_POLYGON);
	assertEquals(getInstanceSize(OGCPoint.class), SizeOf.SIZE_OF_OGC_POINT);
	assertEquals(getInstanceSize(OGCPolygon.class), SizeOf.SIZE_OF_OGC_POLYGON);
	assertEquals(getInstanceSize(RasterizedGeometry2DImpl.class), SizeOf.SIZE_OF_RASTERIZED_GEOMETRY_2D_IMPL);
	assertEquals(getInstanceSize(RasterizedGeometry2DImpl.ScanCallbackImpl.class), SizeOf.SIZE_OF_SCAN_CALLBACK_IMPL);
	assertEquals(getInstanceSize(Transformation2D.class), SizeOf.SIZE_OF_TRANSFORMATION_2D);
	assertEquals(getInstanceSize(SimpleRasterizer.class), SizeOf.SIZE_OF_SIMPLE_RASTERIZER);
	assertEquals(getInstanceSize(SimpleRasterizer.Edge.class), SizeOf.SIZE_OF_EDGE);
	assertEquals(getInstanceSize(QuadTreeImpl.class), SizeOf.SIZE_OF_QUAD_TREE_IMPL);
	assertEquals(getInstanceSize(QuadTreeImpl.Data.class), SizeOf.SIZE_OF_DATA);
	assertEquals(getInstanceSize(StridedIndexTypeCollection.class), SizeOf.SIZE_OF_STRIDED_INDEX_TYPE_COLLECTION);
}
 
Example #13
Source Project: geometry-api-java   Author: Esri   File: TestGeomToGeoJson.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testOGCPolygon() {
	Polygon p = new Polygon();
	p.startPath(100.0, 0.0);
	p.lineTo(101.0, 0.0);
	p.lineTo(101.0, 1.0);
	p.lineTo(100.0, 1.0);
	p.closePathWithLine();
	OGCPolygon ogcPolygon = new OGCPolygon(p, null);
	String result = ogcPolygon.asGeoJson();
	assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[[100,0],[100,1],[101,1],[101,0],[100,0]]],\"crs\":null}", result);
}
 
Example #14
Source Project: geometry-api-java   Author: Esri   File: TestOGC.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testPolygon() throws Exception {
	OGCGeometry g = OGCGeometry
			.fromText("POLYGON((-10 -10, 10 -10, 10 10, -10 10, -10 -10), (-5 -5, -5 5, 5 5, 5 -5, -5 -5))");
	assertTrue(g.geometryType().equals("Polygon"));
	OGCPolygon p = (OGCPolygon) g;
	assertTrue(p.numInteriorRing() == 1);
	OGCLineString ls = p.exteriorRing();
	// assertTrue(ls.pointN(1).equals(OGCGeometry.fromText("POINT(10 -10)")));
	boolean b = ls
			.Equals(OGCGeometry
					.fromText("LINESTRING(-10 -10, 10 -10, 10 10, -10 10, -10 -10)"));
	assertTrue(b);
	OGCLineString lsi = p.interiorRingN(0);
	b = lsi.Equals(OGCGeometry
			.fromText("LINESTRING(-5 -5, -5 5, 5 5, 5 -5, -5 -5)"));
	assertTrue(b);
	b = lsi.equals((Object)OGCGeometry
			.fromText("LINESTRING(-5 -5, -5 5, 5 5, 5 -5, -5 -5)"));
	assertTrue(!lsi.Equals(ls));
	OGCMultiCurve boundary = p.boundary();
	String s = boundary.asText();
	assertTrue(s.equals("MULTILINESTRING ((-10 -10, 10 -10, 10 10, -10 10, -10 -10), (-5 -5, -5 5, 5 5, 5 -5, -5 -5))"));

	{
		OGCGeometry g2 = OGCGeometry.fromGeoJson(
				"{\"type\": \"Polygon\", \"coordinates\": [[[1.00000001,1.00000001], [4.00000001,1.00000001], [4.00000001,4.00000001], [1.00000001,4.00000001]]]}");
		OGCGeometry
				.fromGeoJson(
						"{\"type\": \"LineString\", \"coordinates\": [[1.00000001,1.00000001], [7.00000001,8.00000001]]}")
				.intersects(g2);
		OGCGeometry
				.fromGeoJson(
						"{\"type\": \"LineString\", \"coordinates\": [[2.449,4.865], [7.00000001,8.00000001]]}")
				.intersects(g2);

		OGCGeometry g3 = OGCGeometry.fromGeoJson(
				"{\"type\": \"Polygon\", \"coordinates\": [[[1.00000001,1.00000001], [4.00000001,1.00000001], [4.00000001,4.00000001], [1.00000001,4.00000001]]]}");
		boolean bb = g2.equals((Object) g3);
		assertTrue(bb);
	}
}
 
Example #15
Source Project: geometry-api-java   Author: Esri   File: TestGeomToGeoJson.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testGeometryCollection() {
	SpatialReference sr = SpatialReference.create(4326);

	StringBuilder geometrySb = new StringBuilder();
	geometrySb
			.append("{\"type\" : \"GeometryCollection\", \"geometries\" : [");

	OGCPoint point = new OGCPoint(new Point(1.0, 1.0), sr);
	assertEquals("{\"x\":1,\"y\":1,\"spatialReference\":{\"wkid\":4326}}",
			point.asJson());
	assertEquals(
			"{\"type\":\"Point\",\"coordinates\":[1,1],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}",
			point.asGeoJson());
	geometrySb.append(point.asGeoJson()).append(", ");

	OGCLineString line = new OGCLineString(new Polyline(
			new Point(1.0, 1.0), new Point(2.0, 2.0)), 0, sr);
	assertEquals(
			"{\"paths\":[[[1,1],[2,2]]],\"spatialReference\":{\"wkid\":4326}}",
			line.asJson());
	assertEquals(
			"{\"type\":\"LineString\",\"coordinates\":[[1,1],[2,2]],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}",
			line.asGeoJson());
	geometrySb.append(line.asGeoJson()).append(", ");

	Polygon p = new Polygon();
	p.startPath(1.0, 1.0);
	p.lineTo(2.0, 2.0);
	p.lineTo(3.0, 1.0);
	p.lineTo(2.0, 0.0);

	OGCPolygon polygon = new OGCPolygon(p, sr);
	assertEquals(
			"{\"rings\":[[[1,1],[2,2],[3,1],[2,0],[1,1]]],\"spatialReference\":{\"wkid\":4326}}",
			polygon.asJson());
	assertEquals(
			"{\"type\":\"Polygon\",\"coordinates\":[[[1,1],[2,0],[3,1],[2,2],[1,1]]],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}",
			polygon.asGeoJson());
	geometrySb.append(polygon.asGeoJson()).append("]}");

	List<OGCGeometry> geoms = new ArrayList<OGCGeometry>(3);
	geoms.add(point);
	geoms.add(line);
	geoms.add(polygon);
	OGCConcreteGeometryCollection collection = new OGCConcreteGeometryCollection(
			geoms, sr);
	String s2 = collection.asGeoJson();
	
	assertEquals("{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[1,1]},{\"type\":\"LineString\",\"coordinates\":[[1,1],[2,2]]},{\"type\":\"Polygon\",\"coordinates\":[[[1,1],[2,0],[3,1],[2,2],[1,1]]]}],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}", collection.asGeoJson());
}