Java Code Examples for com.esri.core.geometry.ogc.OGCGeometry#fromText()

The following examples show how to use com.esri.core.geometry.ogc.OGCGeometry#fromText() . 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: TestOGC.java    From geometry-api-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiPolygonUnion() {
	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))");
	OGCGeometry g2 = OGCGeometry
			.fromText("POLYGON((90 90, 110 90, 110 110, 90 110, 90 90))");
	OGCGeometry u = g.union(g2);
	assertTrue(u.geometryType().equals("MultiPolygon"));
	assertTrue(!u.contains(OGCGeometry.fromText("POINT(0 0)")));
	assertTrue(u.contains(OGCGeometry.fromText("POINT(9 9)")));
	assertTrue(!u.contains(OGCGeometry.fromText("POINT(-20 1)")));
	assertTrue(u.disjoint(OGCGeometry.fromText("POINT(0 0)")));
	assertTrue(!u.disjoint(OGCGeometry.fromText("POINT(9 9)")));
	assertTrue(u.disjoint(OGCGeometry.fromText("POINT(-20 1)")));
	assertTrue(u.contains(OGCGeometry.fromText("POINT(100 100)")));
}
 
Example 2
Source File: HiveGeometryOIHelper.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
private OGCGeometry getGeometry(DeferredObject arg) {
	Object writable;
	try {
		writable = oi.getPrimitiveWritableObject(arg.get());
	} catch (HiveException e) {
		LOG.error("Failed to get writable", e);
		return null;
	}
	
	if (writable == null) {
		return null;
	}
	
	switch (oi.getPrimitiveCategory()) {
		case BINARY: return getGeometryFromBytes((BytesWritable)writable);
		case STRING: return OGCGeometry.fromText(((Text)writable).toString());
		default: return null;
	}
}
 
Example 3
Source File: ST_MultiPoint.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public BytesWritable evaluate(Text wkwrap) throws UDFArgumentException {
	String wkt = wkwrap.toString();
	try {
		OGCGeometry ogcObj = OGCGeometry.fromText(wkt);
		ogcObj.setSpatialReference(null);
		if (ogcObj.geometryType().equals("MultiPoint")) {
			return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
		} else {
			LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_MULTIPOINT, GeometryUtils.OGCType.UNKNOWN);
			return null;
		}
	} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LogUtils.Log_InvalidText(LOG, wkt);
			return null;
	}
}
 
Example 4
Source File: ST_GeomFromText.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public BytesWritable evaluate(Text wkwrap, int wkid) throws UDFArgumentException {

		String wkt = wkwrap.toString();
		try {
			SpatialReference spatialReference = null;
			if (wkid != GeometryUtils.WKID_UNKNOWN) {
				spatialReference = SpatialReference.create(wkid);
			}
			OGCGeometry ogcObj = OGCGeometry.fromText(wkt);
			ogcObj.setSpatialReference(spatialReference);
			return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
		} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LogUtils.Log_InvalidText(LOG, wkt);
			return null;
		}
	}
 
Example 5
Source File: TestOGCDisjoint.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
private void assertNotDisjoint(String wkt, String otherWkt, String intersectionWkt)
{
    OGCGeometry geometry = OGCGeometry.fromText(wkt);
    OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);
    assertFalse(geometry.disjoint(otherGeometry));
    assertTrue(geometry.intersects(otherGeometry));
    assertEquals(intersectionWkt, geometry.intersection(otherGeometry).asText());

    assertFalse(otherGeometry.disjoint(geometry));
    assertTrue(otherGeometry.intersects(geometry));
    assertEquals(intersectionWkt, otherGeometry.intersection(geometry).asText());
}
 
Example 6
Source File: TestOGC.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlattened() {
	OGCConcreteGeometryCollection ogcGeometry = (OGCConcreteGeometryCollection)OGCGeometry.fromText("GEOMETRYCOLLECTION (MULTILINESTRING ((1 2, 3 4)), MULTIPOLYGON (((1 2, 3 4, 5 6, 1 2))), MULTIPOINT (1 1))");
	assertFalse(ogcGeometry.isFlattened());
	ogcGeometry = (OGCConcreteGeometryCollection)OGCGeometry.fromText("GEOMETRYCOLLECTION (MULTIPOINT (1 1), MULTILINESTRING ((1 2, 3 4)), MULTIPOLYGON (((1 2, 3 4, 5 6, 1 2))))");
	assertTrue(ogcGeometry.isFlattened());
}
 
Example 7
Source File: TestOGCDisjoint.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
private void assertDisjoint(String wkt, String otherWkt)
{
    OGCGeometry geometry = OGCGeometry.fromText(wkt);
    OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);
    assertTrue(geometry.disjoint(otherGeometry));
    assertFalse(geometry.intersects(otherGeometry));
    assertTrue(geometry.intersection(otherGeometry).isEmpty());

    assertTrue(otherGeometry.disjoint(geometry));
    assertFalse(otherGeometry.intersects(geometry));
    assertTrue(otherGeometry.intersection(geometry).isEmpty());
}
 
Example 8
Source File: TestOGCContains.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
private void assertContains(String wkt, String otherWkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);

	assertTrue(geometry.contains(otherGeometry));
	assertTrue(otherGeometry.within(geometry));
	assertFalse(geometry.disjoint(otherGeometry));
}
 
Example 9
Source File: TestOGC.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testDistanceOnGeometryCollection() {
	OGCGeometry ogcGeometry = OGCGeometry.fromText("GEOMETRYCOLLECTION (POINT (1 1))");
	assertTrue(ogcGeometry.distance(OGCGeometry.fromText("POINT (1 1)")) == 0);
	
	//distance to empty is NAN
	ogcGeometry = OGCGeometry.fromText("GEOMETRYCOLLECTION (POINT (1 1))");
	assertTrue(Double.isNaN(ogcGeometry.distance(OGCGeometry.fromText("POINT EMPTY"))));
}
 
Example 10
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
private static OGCGeometry geometryFromText(Slice input)
{
    OGCGeometry geometry;
    try {
        geometry = OGCGeometry.fromText(input.toStringUtf8());
    }
    catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid WKT: " + input.toStringUtf8(), e);
    }
    geometry.setSpatialReference(null);
    return geometry;
}
 
Example 11
Source File: TestConvexHull.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testHullIssueGithub194() {
	{
		//empty
		OGCGeometry geom = OGCGeometry.fromText("GEOMETRYCOLLECTION(POLYGON EMPTY, POLYGON((0 0, 1 0, 1 1, 0 0)), POLYGON((-10 -10, 10 -10, 10 10, -10 10, -10 -10), (-5 -5, -5 5, 5 5, 5 -5, -5 -5)))");
		OGCGeometry result = geom.convexHull();
		String text = result.asText();
		assertTrue(text.compareTo("POLYGON ((-10 -10, 10 -10, 10 10, -10 10, -10 -10))") == 0);
	}
}
 
Example 12
Source File: TestGeometrySerialization.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void testJtsSerialization(String wkt)
{
    Geometry jtsGeometry = createJtsGeometry(wkt);
    OGCGeometry esriGeometry = OGCGeometry.fromText(wkt);

    Slice jtsSerialized = JtsGeometrySerde.serialize(jtsGeometry);
    Slice esriSerialized = GeometrySerde.serialize(esriGeometry);
    assertEquals(jtsSerialized, esriSerialized);

    Geometry jtsDeserialized = JtsGeometrySerde.deserialize(jtsSerialized);
    assertGeometryEquals(jtsDeserialized, jtsGeometry);

    OGCGeometry esriDeserialized = GeometrySerde.deserialize(esriSerialized);
    assertGeometryEquals(esriDeserialized, esriGeometry);
}
 
Example 13
Source File: AbstractTestGeoAggregationFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
protected void assertAggregatedGeometries(String testDescription, String expectedWkt, String... wkts)
{
    List<Slice> geometrySlices = Arrays.stream(wkts)
            .map(text -> text == null ? null : OGCGeometry.fromText(text))
            .map(input -> input == null ? null : GeometrySerde.serialize(input))
            .collect(Collectors.toList());

    // Add a custom equality assertion because the resulting geometry may have
    // its constituent points in a different order
    BiFunction<Object, Object, Boolean> equalityFunction = (left, right) -> {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            return false;
        }
        OGCGeometry leftGeometry = OGCGeometry.fromText(left.toString());
        OGCGeometry rightGeometry = OGCGeometry.fromText(right.toString());
        // Check for equality by getting the difference
        return leftGeometry.difference(rightGeometry).isEmpty() &&
                rightGeometry.difference(leftGeometry).isEmpty();
    };
    // Test in forward and reverse order to verify that ordering doesn't affect the output
    assertAggregation(function, equalityFunction, testDescription,
            new Page(BlockAssertions.createSlicesBlock(geometrySlices)), expectedWkt);
    Collections.reverse(geometrySlices);
    assertAggregation(function, equalityFunction, testDescription,
            new Page(BlockAssertions.createSlicesBlock(geometrySlices)), expectedWkt);
}
 
Example 14
Source File: TestOGC.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testGeometryCollection() throws Exception {
	OGCGeometry g = OGCGeometry
			.fromText("GEOMETRYCOLLECTION(POLYGON EMPTY, POINT(1 1), LINESTRING EMPTY, MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY)");
	assertTrue(g.geometryType().equals("GeometryCollection"));
	OGCConcreteGeometryCollection gc = (OGCConcreteGeometryCollection) g;
	assertTrue(gc.numGeometries() == 5);
	assertTrue(gc.geometryN(0).geometryType().equals("Polygon"));
	assertTrue(gc.geometryN(1).geometryType().equals("Point"));
	assertTrue(gc.geometryN(2).geometryType().equals("LineString"));
	assertTrue(gc.geometryN(3).geometryType().equals("MultiPolygon"));
	assertTrue(gc.geometryN(4).geometryType().equals("MultiLineString"));

	g = OGCGeometry
			.fromText("GEOMETRYCOLLECTION(POLYGON EMPTY, POINT(1 1), GEOMETRYCOLLECTION EMPTY, LINESTRING EMPTY, GEOMETRYCOLLECTION(POLYGON EMPTY, POINT(1 1), LINESTRING EMPTY, MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY, MULTIPOINT EMPTY), MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY)");
	assertTrue(g.geometryType().equals("GeometryCollection"));
	gc = (OGCConcreteGeometryCollection) g;
	assertTrue(gc.numGeometries() == 7);
	assertTrue(gc.geometryN(0).geometryType().equals("Polygon"));
	assertTrue(gc.geometryN(1).geometryType().equals("Point"));
	assertTrue(gc.geometryN(2).geometryType().equals("GeometryCollection"));
	assertTrue(gc.geometryN(3).geometryType().equals("LineString"));
	assertTrue(gc.geometryN(4).geometryType().equals("GeometryCollection"));
	assertTrue(gc.geometryN(5).geometryType().equals("MultiPolygon"));
	assertTrue(gc.geometryN(6).geometryType().equals("MultiLineString"));

	OGCConcreteGeometryCollection gc2 = (OGCConcreteGeometryCollection) gc
			.geometryN(4);
	assertTrue(gc2.numGeometries() == 6);
	assertTrue(gc2.geometryN(0).geometryType().equals("Polygon"));
	assertTrue(gc2.geometryN(1).geometryType().equals("Point"));
	assertTrue(gc2.geometryN(2).geometryType().equals("LineString"));
	assertTrue(gc2.geometryN(3).geometryType().equals("MultiPolygon"));
	assertTrue(gc2.geometryN(4).geometryType().equals("MultiLineString"));
	assertTrue(gc2.geometryN(5).geometryType().equals("MultiPoint"));

	ByteBuffer wkbBuffer = g.asBinary();
	g = OGCGeometry.fromBinary(wkbBuffer);

	assertTrue(g.geometryType().equals("GeometryCollection"));
	gc = (OGCConcreteGeometryCollection) g;
	assertTrue(gc.numGeometries() == 7);
	assertTrue(gc.geometryN(0).geometryType().equals("Polygon"));
	assertTrue(gc.geometryN(1).geometryType().equals("Point"));
	assertTrue(gc.geometryN(2).geometryType().equals("GeometryCollection"));
	assertTrue(gc.geometryN(3).geometryType().equals("LineString"));
	assertTrue(gc.geometryN(4).geometryType().equals("GeometryCollection"));
	assertTrue(gc.geometryN(5).geometryType().equals("MultiPolygon"));
	assertTrue(gc.geometryN(6).geometryType().equals("MultiLineString"));

	gc2 = (OGCConcreteGeometryCollection) gc.geometryN(4);
	assertTrue(gc2.numGeometries() == 6);
	assertTrue(gc2.geometryN(0).geometryType().equals("Polygon"));
	assertTrue(gc2.geometryN(1).geometryType().equals("Point"));
	assertTrue(gc2.geometryN(2).geometryType().equals("LineString"));
	assertTrue(gc2.geometryN(3).geometryType().equals("MultiPolygon"));
	assertTrue(gc2.geometryN(4).geometryType().equals("MultiLineString"));
	assertTrue(gc2.geometryN(5).geometryType().equals("MultiPoint"));

	String wktString = g.asText();
	assertTrue(wktString
			.equals("GEOMETRYCOLLECTION (POLYGON EMPTY, POINT (1 1), GEOMETRYCOLLECTION EMPTY, LINESTRING EMPTY, GEOMETRYCOLLECTION (POLYGON EMPTY, POINT (1 1), LINESTRING EMPTY, MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY, MULTIPOINT EMPTY), MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY)"));

	g = OGCGeometry
			.fromGeoJson("{\"type\" : \"GeometryCollection\", \"geometries\" : [{\"type\" : \"Polygon\", \"coordinates\" : []}, {\"type\" : \"Point\", \"coordinates\" : [1, 1]}, {\"type\" : \"GeometryCollection\", \"geometries\" : []}, {\"type\" : \"LineString\", \"coordinates\" : []}, {\"type\" : \"GeometryCollection\", \"geometries\" : [{\"type\": \"Polygon\", \"coordinates\" : []}, {\"type\" : \"Point\", \"coordinates\" : [1,1]}, {\"type\" : \"LineString\", \"coordinates\" : []}, {\"type\" : \"MultiPolygon\", \"coordinates\" : []}, {\"type\" : \"MultiLineString\", \"coordinates\" : []}, {\"type\" : \"MultiPoint\", \"coordinates\" : []}]}, {\"type\" : \"MultiPolygon\", \"coordinates\" : []}, {\"type\" : \"MultiLineString\", \"coordinates\" : []} ] }");

	wktString = g.asText();
	assertTrue(wktString
			.equals("GEOMETRYCOLLECTION (POLYGON EMPTY, POINT (1 1), GEOMETRYCOLLECTION EMPTY, LINESTRING EMPTY, GEOMETRYCOLLECTION (POLYGON EMPTY, POINT (1 1), LINESTRING EMPTY, MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY, MULTIPOINT EMPTY), MULTIPOLYGON EMPTY, MULTILINESTRING EMPTY)"));
	
	assertTrue(g.equals((Object)OGCGeometry.fromText(wktString)));
	
	assertTrue(g.hashCode() == OGCGeometry.fromText(wktString).hashCode());

}
 
Example 15
Source File: TestOGCGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private void assertContains(String wkt, String otherWkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);
	Assert.assertTrue(geometry.contains(otherGeometry));
	Assert.assertTrue(otherGeometry.within(geometry));
}
 
Example 16
Source File: TestEstimateMemorySize.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private static OGCGeometry parseWkt(String wkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	geometry.setSpatialReference(null);
	return geometry;
}
 
Example 17
Source File: TestOGCGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private void assertUnion(String wkt, String otherWkt, String expectedWkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);
	Assert.assertEquals(expectedWkt, geometry.union(otherGeometry).asText());
	Assert.assertEquals(expectedWkt, otherGeometry.union(geometry).asText());
}
 
Example 18
Source File: TestOGC.java    From geometry-api-java with 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 19
Source File: TestOGCContains.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private void assertNotContains(String wkt, String otherWkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry otherGeometry = OGCGeometry.fromText(otherWkt);
	assertFalse(geometry.contains(otherGeometry));
	assertFalse(otherGeometry.within(geometry));
}
 
Example 20
Source File: TestOGCCentroid.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private static void assertEmptyCentroid(String wkt) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry centroid = geometry.centroid();
	Assert.assertEquals(centroid, new OGCPoint(new Point(), geometry.getEsriSpatialReference()));
}