com.esri.core.geometry.ogc.OGCPoint Java Examples

The following examples show how to use com.esri.core.geometry.ogc.OGCPoint. 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 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 #2
Source File: TestOGC.java    From geometry-api-java with 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 #3
Source File: ST_Y.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

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

	switch(GeometryUtils.getType(geomref)) {
	case ST_POINT:
		OGCPoint pt = (OGCPoint)ogcGeometry;
		resultDouble.set(pt.Y());
		return resultDouble;
	default:
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #4
Source File: ST_Bin.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
	double binSize = PrimitiveObjectInspectorUtils.getDouble(args[0].get(), oiBinSize);

	if (!binSizeIsConstant || bins == null) {
		bins = new BinUtils(binSize);
	} 

	OGCPoint point = geomHelper.getPoint(args);

	if (point == null) {
		return null;
	}

	return bins.getId(point.X(), point.Y());
}
 
Example #5
Source File: ST_X.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

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

	switch(GeometryUtils.getType(geomref)) {
	case ST_POINT:
		OGCPoint pt = (OGCPoint)ogcGeometry;
		resultDouble.set(pt.X());
		return resultDouble;
	default:
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #6
Source File: ST_Z.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

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

	switch(GeometryUtils.getType(geomref)) {
	case ST_POINT:
		OGCPoint pt = (OGCPoint)ogcGeometry;
		resultDouble.set(pt.Z());
		return resultDouble;
	default:
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #7
Source File: UserDefinedGeoJsonSpatialFilter.java    From bboxdb with Apache License 2.0 6 votes vote down vote up
/**
 * Perform a real join based on the geometry of the data
 */
@Override
public boolean filterJoinCandidate(final Tuple tuple1, final Tuple tuple2, final byte[] customData) {
	
	final String geoJsonString1 = new String(tuple1.getDataBytes());
	final String geoJsonString2 = new String(tuple2.getDataBytes());

	// Full text search on string (if provided)
	if(customData != null) {
		final String customDataString = new String(customData);
		if(! geoJsonString1.contains(customDataString) && 
				! geoJsonString2.contains(customDataString)) {
			return false;
		}
	}
	
	final OGCGeometry geometry1 = extractGeometry(geoJsonString1);
	final OGCGeometry geometry2 = extractGeometry(geoJsonString2);
	
	if(geometry1 instanceof OGCPoint) {
		final double geometryDistrance = geometry1.distance(geometry2);
		return geometryDistrance < MAX_OVERLAPPING_POINT_DISTANCE;
	} else {
	    return geometry1.intersects(geometry2);
	}
}
 
Example #8
Source File: ST_M.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

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

	switch(GeometryUtils.getType(geomref)) {
	case ST_POINT:
		OGCPoint pt = (OGCPoint)ogcGeometry;
		resultDouble.set(pt.M());
		return resultDouble;
	default:
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example #9
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Returns a LineString from an array of points")
@ScalarFunction("ST_LineString")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stLineString(@SqlType("array(" + GEOMETRY_TYPE_NAME + ")") Block input)
{
    MultiPath multipath = new Polyline();
    OGCPoint previousPoint = null;
    for (int i = 0; i < input.getPositionCount(); i++) {
        Slice slice = GEOMETRY.getSlice(input, i);

        if (slice.getInput().available() == 0) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_LineString: null point at index %s", i + 1));
        }

        OGCGeometry geometry = deserialize(slice);
        if (!(geometry instanceof OGCPoint)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("ST_LineString takes only an array of valid points, %s was passed", geometry.geometryType()));
        }
        OGCPoint point = (OGCPoint) geometry;

        if (point.isEmpty()) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_LineString: empty point at index %s", i + 1));
        }

        if (previousPoint == null) {
            multipath.startPath(point.X(), point.Y());
        }
        else {
            if (point.Equals(previousPoint)) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT,
                        format("Invalid input to ST_LineString: consecutive duplicate points at index %s", i + 1));
            }
            multipath.lineTo(point.X(), point.Y());
        }
        previousPoint = point;
    }
    OGCLineString linestring = new OGCLineString(multipath, 0, null);
    return serialize(linestring);
}
 
Example #10
Source File: TestGeomToGeoJson.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testOGCPoint() {
	Point p = new Point(10.0, 20.0);
	OGCGeometry ogcPoint = new OGCPoint(p, null);
	String result = ogcPoint.asGeoJson();
	assertEquals("{\"type\":\"Point\",\"coordinates\":[10,20],\"crs\":null}", result);
}
 
Example #11
Source File: TestEstimateMemorySize.java    From geometry-api-java with 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 #12
Source File: HiveGeometryOIHelper.java    From spatial-framework-for-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the corresponding geometry from the deferred object list
 * or returns the cached geometry if argument is constant. 
 * 
 * @param args
 * @return OGCPoint or null if not a point
 * @see #getGeometry(DeferredObject[])
 */
public OGCPoint getPoint(DeferredObject[] args) {
	OGCGeometry geometry = getGeometry(args);
	
	if (geometry instanceof OGCPoint) {
		return (OGCPoint)geometry;
	} else {
		return null;
	}
}
 
Example #13
Source File: ST_BinEnvelope.java    From spatial-framework-for-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
	double binSize = PrimitiveObjectInspectorUtils.getDouble(args[0].get(), oiBinSize);
	
	
	if (!binSizeIsConstant || bins == null) {
		bins = new BinUtils(binSize);
	} 
			
	Envelope env = new Envelope();
	
	if (oiBinId != null) {
		// argument 1 is a number, attempt to get the envelope with bin ID
		if (args[1].get() == null) {
			// null bin ID argument usually means the source point was null or failed to parse
			return null; 
		}
		
		long binId = PrimitiveObjectInspectorUtils.getLong(args[1].get(), oiBinId);
		bins.queryEnvelope(binId, env);
	} else {
		// argument 1 is a geometry, attempt to get the envelope with a point
		OGCPoint point = binPoint.getPoint(args);
		
		if (point == null) {
			return null;
		}
		
		bins.queryEnvelope(point.X(), point.Y(), env);
	}

	return GeometryUtils.geometryToEsriShapeBytesWritable(env, 0, OGCType.ST_POLYGON);
}
 
Example #14
Source File: GeometrySerde.java    From presto with 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 #15
Source File: GeometryUtils.java    From presto with 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 #16
Source File: TestSpatialPartitioningInternalAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
private List<OGCGeometry> makeGeometries()
{
    ImmutableList.Builder<OGCGeometry> geometries = ImmutableList.builder();
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            geometries.add(new OGCPoint(new Point(-10 + i, -10 + j), null));
        }
    }

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            geometries.add(new OGCPoint(new Point(-10 + 2 * i, 2 * j), null));
        }
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            geometries.add(new OGCPoint(new Point(2.5 * i, -10 + 2.5 * j), null));
        }
    }

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            geometries.add(new OGCPoint(new Point(5 * i, 5 * j), null));
        }
    }

    return geometries.build();
}
 
Example #17
Source File: BenchmarkSTContains.java    From presto with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup()
        throws IOException
{
    geometry = GeoFunctions.stGeometryFromText(Slices.utf8Slice(loadPolygon("large_polygon.txt")));
    simpleGeometry = GeoFunctions.stGeometryFromText(Slices.utf8Slice("POLYGON ((16.5 54, 16.5 54.1, 16.8 54.1, 16.8 54))"));
    innerPoint = GeoFunctions.stPoint(16.6, 54.0167);
    outerPointInEnvelope = GeoFunctions.stPoint(16.6667, 54.05);
    outerPointNotInEnvelope = GeoFunctions.stPoint(16.6333, 54.2);

    ogcGeometry = deserialize(geometry);
    innerOgcPoint = (OGCPoint) deserialize(innerPoint);
    outerOgcPointInEnvelope = (OGCPoint) deserialize(outerPointInEnvelope);
    outerOgcPointNotInEnvelope = (OGCPoint) deserialize(outerPointNotInEnvelope);
}
 
Example #18
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Return the Y coordinate of the point")
@ScalarFunction("ST_Y")
@SqlType(DOUBLE)
public static Double stY(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Y", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).Y();
}
 
Example #19
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Return the X coordinate of the point")
@ScalarFunction("ST_X")
@SqlType(DOUBLE)
public static Double stX(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_X", geometry, EnumSet.of(POINT));
    if (geometry.isEmpty()) {
        return null;
    }
    return ((OGCPoint) geometry).X();
}
 
Example #20
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns a multi-point geometry formed from input points")
@ScalarFunction("ST_MultiPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stMultiPoint(@SqlType("array(" + GEOMETRY_TYPE_NAME + ")") Block input)
{
    MultiPoint multipoint = new MultiPoint();
    for (int i = 0; i < input.getPositionCount(); i++) {
        if (input.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: null at index %s", i + 1));
        }

        Slice slice = GEOMETRY.getSlice(input, i);
        OGCGeometry geometry = deserialize(slice);
        if (!(geometry instanceof OGCPoint)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: geometry is not a point: %s at index %s", geometry.geometryType(), i + 1));
        }
        OGCPoint point = (OGCPoint) geometry;
        if (point.isEmpty()) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid input to ST_MultiPoint: empty point at index %s", i + 1));
        }

        multipoint.add(point.X(), point.Y());
    }
    if (multipoint.getPointCount() == 0) {
        return null;
    }
    return serialize(createFromEsriGeometry(multipoint, null, true));
}
 
Example #21
Source File: TestGeoFunctions.java    From presto with Apache License 2.0 4 votes vote down vote up
private void assertEnvelopeAsPts(String wkt, Point lowerLeftCorner, Point upperRightCorner)
{
    assertFunction(format("transform(ST_EnvelopeAsPts(ST_GeometryFromText('%s')), x -> ST_AsText(x))", wkt), new ArrayType(VARCHAR), ImmutableList.of(new OGCPoint(lowerLeftCorner, null).asText(), new OGCPoint(upperRightCorner, null).asText()));
}
 
Example #22
Source File: TestOGCCentroid.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
private static void assertCentroid(String wkt, Point expectedCentroid) {
	OGCGeometry geometry = OGCGeometry.fromText(wkt);
	OGCGeometry centroid = geometry.centroid();
	Assert.assertEquals(((OGCPoint)centroid).X(), expectedCentroid.getX(), 1e-13);
	Assert.assertEquals(((OGCPoint)centroid).Y(), expectedCentroid.getY(), 1e-13);
}
 
Example #23
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()));
}
 
Example #24
Source File: PagesRTreeIndex.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of addresses from {@link PagesIndex#valueAddresses} corresponding
 * to rows with matching geometries.
 * <p>
 * The caller is responsible for calling {@link #isJoinPositionEligible(int, int, Page)}
 * for each of these addresses to apply additional join filters.
 */
@Override
public int[] findJoinPositions(int probePosition, Page probe, int probeGeometryChannel, Optional<Integer> probePartitionChannel)
{
    Block probeGeometryBlock = probe.getBlock(probeGeometryChannel);
    if (probeGeometryBlock.isNull(probePosition)) {
        return EMPTY_ADDRESSES;
    }

    int probePartition = probePartitionChannel.map(channel -> toIntExact(INTEGER.getLong(probe.getBlock(channel), probePosition))).orElse(-1);

    Slice slice = probeGeometryBlock.getSlice(probePosition, 0, probeGeometryBlock.getSliceLength(probePosition));
    OGCGeometry probeGeometry = deserialize(slice);
    verifyNotNull(probeGeometry);
    if (probeGeometry.isEmpty()) {
        return EMPTY_ADDRESSES;
    }

    boolean probeIsPoint = probeGeometry instanceof OGCPoint;

    IntArrayList matchingPositions = new IntArrayList();

    Envelope envelope = getEnvelope(probeGeometry);
    rtree.query(envelope, item -> {
        GeometryWithPosition geometryWithPosition = (GeometryWithPosition) item;
        OGCGeometry buildGeometry = geometryWithPosition.getGeometry();
        if (partitions.isEmpty() || (probePartition == geometryWithPosition.getPartition() && (probeIsPoint || (buildGeometry instanceof OGCPoint) || testReferencePoint(envelope, buildGeometry, probePartition)))) {
            if (radiusChannel == -1) {
                if (spatialRelationshipTest.apply(buildGeometry, probeGeometry, OptionalDouble.empty())) {
                    matchingPositions.add(geometryWithPosition.getPosition());
                }
            }
            else {
                if (spatialRelationshipTest.apply(geometryWithPosition.getGeometry(), probeGeometry, OptionalDouble.of(getRadius(geometryWithPosition.getPosition())))) {
                    matchingPositions.add(geometryWithPosition.getPosition());
                }
            }
        }
    });

    return matchingPositions.toIntArray(null);
}
 
Example #25
Source File: TestGeomToGeoJson.java    From geometry-api-java with 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());
}