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

The following examples show how to use com.esri.core.geometry.ogc.OGCGeometry. 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
public BytesWritable evaluate(Text wkwrap) throws UDFArgumentException {
	String wkt = wkwrap.toString();
	try {
		OGCGeometry ogcObj = OGCGeometry.fromText(wkt);
		ogcObj.setSpatialReference(null);
		if (ogcObj.geometryType().equals("LineString")) {
			return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
		} else {
			LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_LINESTRING, GeometryUtils.OGCType.UNKNOWN);
			return null;
		}

	} catch (Exception e) {  // IllegalArgumentException, GeometryException
		LogUtils.Log_InvalidText(LOG, wkt);
		return null;
	}
}
 
Example 2
public BytesWritable evaluate(BytesWritable wkb, int wkid) throws UDFArgumentException {

		try {
			SpatialReference spatialReference = null;
			if (wkid != GeometryUtils.WKID_UNKNOWN) {
				spatialReference = SpatialReference.create(wkid);
			}
			byte [] byteArr = wkb.getBytes();
            ByteBuffer byteBuf = ByteBuffer.allocate(byteArr.length);
			byteBuf.put(byteArr);
			OGCGeometry ogcObj = OGCGeometry.fromBinary(byteBuf);
			ogcObj.setSpatialReference(spatialReference);
			String gType = ogcObj.geometryType();
			if (gType.equals("MultiPolygon") || gType.equals("Polygon")) {
				return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
			} else {
				LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_MULTIPOLYGON, GeometryUtils.OGCType.UNKNOWN);
				return null;
			}
		} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LOG.error(e.getMessage());
			return null;
		}
	}
 
Example 3
Source Project: spatial-framework-for-hadoop   Source File: ST_Buffer.java    License: Apache License 2.0 6 votes vote down vote up
public BytesWritable evaluate(BytesWritable geometryref1, DoubleWritable distance)
{
	if (geometryref1 == null || geometryref1.getLength() == 0 || distance == null) {
		return null;
	}

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

	OGCGeometry bufferedGeometry = ogcGeometry.buffer(distance.get());
	// TODO persist type information (polygon vs multipolygon)
	return GeometryUtils.geometryToEsriShapeBytesWritable(bufferedGeometry);
}
 
Example 4
Source Project: spatial-framework-for-hadoop   Source File: ST_AsJson.java    License: Apache License 2.0 6 votes vote down vote up
public Text 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;
	}

	Geometry esriGeom = ogcGeometry.getEsriGeometry();
	int wkid = GeometryUtils.getWKID(geomref);
	return new Text(GeometryEngine.geometryToJson(wkid, esriGeom));
}
 
Example 5
Source Project: geometry-api-java   Source File: TestOGC.java    License: 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 6
Source Project: spatial-framework-for-hadoop   Source File: ST_AsText.java    License: Apache License 2.0 6 votes vote down vote up
public Text 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;
	}
	
	int wktExportFlag = getWktExportFlag(GeometryUtils.getType(geomref));
	
	try {
		// mind: GeometryType with ST_AsText(ST_GeomFromText('MultiLineString((0 80, 0.03 80.04))'))
		// return new Text(ogcGeometry.asText());
		return new Text(GeometryEngine.geometryToWkt(ogcGeometry.getEsriGeometry(), wktExportFlag));
	} catch (Exception e){
		LOG.error(e.getMessage());
		return null;
	}
}
 
Example 7
Source Project: presto   Source File: GeoFunctions.java    License: Apache License 2.0 6 votes vote down vote up
@Description("Returns a Geometry type object from Spatial Framework for Hadoop representation")
@ScalarFunction("geometry_from_hadoop_shape")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice geometryFromHadoopShape(@SqlType(VARBINARY) Slice input)
{
    requireNonNull(input, "input is null");

    try {
        OGCGeometry geometry = OGCGeometry.fromEsriShape(getShapeByteBuffer(input));
        String wkt = geometryToWkt(geometry.getEsriGeometry(), getWktExportFlags(input));
        return serialize(OGCGeometry.fromText(wkt));
    }
    catch (IndexOutOfBoundsException | UnsupportedOperationException | IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid Hadoop shape", e);
    }
}
 
Example 8
Source Project: spatial-framework-for-hadoop   Source File: ST_M.java    License: 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 Project: bboxdb   Source File: UserDefinedGeoJsonSpatialFilter.java    License: 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 10
public BytesWritable evaluate(Text wkwrap, int wkid) throws UDFArgumentException {

		String wkt = wkwrap.toString();

		try {
			Geometry geomObj = GeometryEngine.geometryFromWkt(wkt,
															  0,
															  Geometry.Type.Unknown);
			SpatialReference spatialReference = null;  // Idea: OGCGeometry.setSpatialReference after .fromText
			if (wkid != GeometryUtils.WKID_UNKNOWN) {
				spatialReference = SpatialReference.create(wkid);
			}
			OGCGeometry ogcObj = OGCGeometry.createFromEsriGeometry(geomObj, spatialReference);
			return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
		} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LogUtils.Log_InvalidText(LOG, wkt);
			return null;
		}
	}
 
Example 11
public BytesWritable evaluate(DoubleWritable ... xyPairs) throws UDFArgumentLengthException{

		if (xyPairs == null || xyPairs.length == 0 ||  xyPairs.length%2 != 0) {
			LogUtils.Log_VariableArgumentLengthXY(LOG);
			return null;
		}

		try {
			MultiPoint mPoint = new MultiPoint();

			for (int i=0;i<xyPairs.length;i+=2){
				mPoint.add(xyPairs[i].get(), xyPairs[i+1].get());
			}

			return GeometryUtils.geometryToEsriShapeBytesWritable(OGCGeometry.createFromEsriGeometry(mPoint, null, true));
		} catch (Exception e) {
		    LogUtils.Log_InternalError(LOG, "ST_MultiPoint: " + e);
		    return null;
		}
	}
 
Example 12
@Test
public void testGeomFromLineShape() throws UDFArgumentException {
	Polyline line = createLine();
	byte[] esriShape = GeometryEngine.geometryToEsriShape(line);
	assertNotNull("The shape must not be null!", esriShape);

	BytesWritable shapeAsWritable = new BytesWritable(esriShape);
	assertNotNull("The shape writable must not be null!", shapeAsWritable);

	final int wkid = 4326;
	ST_GeomFromShape fromShape = new ST_GeomFromShape();
	BytesWritable geometryAsWritable = fromShape.evaluate(shapeAsWritable, wkid);
	assertNotNull("The geometry writable must not be null!", geometryAsWritable);

	OGCGeometry ogcGeometry = GeometryUtils.geometryFromEsriShape(geometryAsWritable);
	assertNotNull("The OGC geometry must not be null!", ogcGeometry);

	Geometry esriGeometry = ogcGeometry.getEsriGeometry();
	assertNotNull("The Esri geometry must not be null!", esriGeometry);
	assertTrue("The geometries are different!",
			GeometryEngine.equals(line, esriGeometry, SpatialReference.create(wkid)));
}
 
Example 13
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
	
	OGCGeometry geom1 = geomHelper1.getGeometry(args);
	OGCGeometry geom2 = geomHelper2.getGeometry(args);
	
	if (geom1 == null || geom2 == null) {
		return false;
	}

	if (firstRun && geomHelper1.isConstant()) {
		
		// accelerate geometry 1 for quick relation operations since it is constant
		geom1IsAccelerated = opSimpleRelation.accelerateGeometry(geom1.getEsriGeometry(), 
				geom1.getEsriSpatialReference(), GeometryAccelerationDegree.enumMedium);
	}

	firstRun = false;
	
	return opSimpleRelation.execute(geom1.getEsriGeometry(), geom2.getEsriGeometry(), geom1.getEsriSpatialReference(), null);
}
 
Example 14
Source Project: presto   Source 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 15
public BytesWritable evaluate(BytesWritable wkb, int wkid) throws UDFArgumentException {

		try {
			SpatialReference spatialReference = null;
			if (wkid != GeometryUtils.WKID_UNKNOWN) {
				spatialReference = SpatialReference.create(wkid);
			}
			byte [] byteArr = wkb.getBytes();
            ByteBuffer byteBuf = ByteBuffer.allocate(byteArr.length);
			byteBuf.put(byteArr);
			OGCGeometry ogcObj = OGCGeometry.fromBinary(byteBuf);
			ogcObj.setSpatialReference(spatialReference);
			String gType = ogcObj.geometryType();
			if (gType.equals("MultiLineString") || gType.equals("LineString")) {
				return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
			} else {
				LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_MULTILINESTRING, GeometryUtils.OGCType.UNKNOWN);
				return null;
			}
		} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LOG.error(e.getMessage());
			return null;
		}
	}
 
Example 16
Source Project: geometry-api-java   Source 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 17
Source Project: geometry-api-java   Source File: TestEstimateMemorySize.java    License: Apache License 2.0 6 votes vote down vote up
private void testAcceleratedGeometry(OGCGeometry geometry) {
	long initialSize = geometry.estimateMemorySize();
	assertTrue(initialSize > 0);

	Envelope envelope = new Envelope();
	geometry.getEsriGeometry().queryEnvelope(envelope);

	long withEnvelopeSize = geometry.estimateMemorySize();
	assertTrue(withEnvelopeSize > initialSize);

	accelerate(geometry, GeometryAccelerationDegree.enumMild);
	long mildAcceleratedSize = geometry.estimateMemorySize();
	assertTrue(mildAcceleratedSize > withEnvelopeSize);

	accelerate(geometry, GeometryAccelerationDegree.enumMedium);
	long mediumAcceleratedSize = geometry.estimateMemorySize();
	assertTrue(mediumAcceleratedSize > mildAcceleratedSize);

	accelerate(geometry, GeometryAccelerationDegree.enumHot);
	long hotAcceleratedSize = geometry.estimateMemorySize();
	assertTrue(hotAcceleratedSize > mediumAcceleratedSize);
}
 
Example 18
Source Project: spatial-framework-for-hadoop   Source File: ST_IsSimple.java    License: Apache License 2.0 6 votes vote down vote up
public BooleanWritable 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;
	}

	try {
		resultBoolean.set(ogcGeometry.isSimple());
	} catch (Exception e) {
	    LogUtils.Log_InternalError(LOG, "ST_IsSimple" + e);
		return null;
	}
	return resultBoolean;
}
 
Example 19
public BytesWritable evaluate(BytesWritable wkb, int wkid) throws UDFArgumentException {

		try {
			SpatialReference spatialReference = null;
			if (wkid != GeometryUtils.WKID_UNKNOWN) {
				spatialReference = SpatialReference.create(wkid);
			}
			byte [] byteArr = wkb.getBytes();
            ByteBuffer byteBuf = ByteBuffer.allocate(byteArr.length);
			byteBuf.put(byteArr);
			OGCGeometry ogcObj = OGCGeometry.fromBinary(byteBuf);
			ogcObj.setSpatialReference(spatialReference);
			if (ogcObj.geometryType().equals("Point")) {
				return GeometryUtils.geometryToEsriShapeBytesWritable(ogcObj);
			} else {
				LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.OGCType.UNKNOWN);
				return null;
			}
		} catch (Exception e) {  // IllegalArgumentException, GeometryException
			LOG.error(e.getMessage());
			return null;
		}
	}
 
Example 20
Source Project: presto   Source File: GeometrySerde.java    License: Apache License 2.0 6 votes vote down vote up
private static void writePoint(DynamicSliceOutput output, OGCGeometry geometry)
{
    Geometry esriGeometry = geometry.getEsriGeometry();
    verify(esriGeometry instanceof Point, "geometry is expected to be an instance of Point");
    Point point = (Point) esriGeometry;
    verify(!point.hasAttribute(VertexDescription.Semantics.Z) &&
                    !point.hasAttribute(VertexDescription.Semantics.M) &&
                    !point.hasAttribute(VertexDescription.Semantics.ID),
            "Only 2D points with no ID nor M attribute are supported");
    output.appendByte(GeometrySerializationType.POINT.code());
    if (!point.isEmpty()) {
        output.appendDouble(point.getX());
        output.appendDouble(point.getY());
    }
    else {
        output.appendDouble(NaN);
        output.appendDouble(NaN);
    }
}
 
Example 21
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 22
Source Project: geometry-api-java   Source File: TestConvexHull.java    License: 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 23
public BooleanWritable 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;
	}

	resultBoolean.set(ogcGeometry.isMeasured());
	return resultBoolean;
}
 
Example 24
Source Project: presto   Source File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@Description("Returns a Geometry type Polygon object from Well-Known Text representation (WKT)")
@ScalarFunction("ST_Polygon")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stPolygon(@SqlType(VARCHAR) Slice input)
{
    OGCGeometry geometry = geometryFromText(input);
    validateType("ST_Polygon", geometry, EnumSet.of(POLYGON));
    return serialize(geometry);
}
 
Example 25
Source Project: spatial-framework-for-hadoop   Source File: ST_Distance.java    License: Apache License 2.0 5 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geometryref1, BytesWritable geometryref2) {
	if (geometryref1 == null || geometryref2 == null ||
	    geometryref1.getLength() == 0 || geometryref2.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}
	if (!GeometryUtils.compareSpatialReferences(geometryref1, geometryref2)) {
		LogUtils.Log_SRIDMismatch(LOG, geometryref1, geometryref2);
		return null;
	}

	OGCGeometry ogcGeom1 = GeometryUtils.geometryFromEsriShape(geometryref1);
	OGCGeometry ogcGeom2 = GeometryUtils.geometryFromEsriShape(geometryref2);
	if (ogcGeom1 == null || ogcGeom2 == null){
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

	try {
	    resultDouble.set(ogcGeom1.distance(ogcGeom2));
	    return resultDouble;
	} catch (Exception e) {
	    LogUtils.Log_InternalError(LOG, "ST_Distance: " + e);
	    return null;
	}

}
 
Example 26
Source Project: presto   Source File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@Description("Returns the length of a LineString or Multi-LineString using Euclidean measurement on a 2D plane (based on spatial ref) in projected units")
@ScalarFunction("ST_Length")
@SqlType(DOUBLE)
public static double stLength(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_Length", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    return geometry.getEsriGeometry().calculateLength2D();
}
 
Example 27
private String getSpatialPartitioning(Rectangle extent, List<OGCGeometry> geometries, int partitionCount)
{
    ImmutableList.Builder<Rectangle> rectangles = ImmutableList.builder();
    for (OGCGeometry geometry : geometries) {
        Envelope envelope = new Envelope();
        geometry.getEsriGeometry().queryEnvelope(envelope);
        rectangles.add(new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax()));
    }

    return KdbTreeUtils.toJson(buildKdbTree(roundToInt(geometries.size() * 1.0 / partitionCount, CEILING), extent, rectangles.build()));
}
 
Example 28
public BytesWritable evaluate(ArrayList<BytesWritable> points) throws UDFArgumentException {
	if (null == points || points.size() == 0) {
		return null;
	}

	try {		
		Polyline linestring = new Polyline();
	
		for (int ix = 0; ix < points.size(); ++ix) {
			BytesWritable geomref = points.get(ix);
			OGCGeometry gcur = GeometryUtils.geometryFromEsriShape(geomref);
			if (gcur == null || GeometryUtils.getType(geomref) != GeometryUtils.OGCType.ST_POINT) {
				if (gcur == null)
					LogUtils.Log_ArgumentsNull(LOG);
				else
					LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_POINT, GeometryUtils.getType(geomref));
				return null;
			}
			if (ix == 0) {
				linestring.startPath((Point)gcur.getEsriGeometry());
			} else {
				linestring.lineTo((Point)gcur.getEsriGeometry());
			}
		}
	
		return GeometryUtils.geometryToEsriShapeBytesWritable(OGCGeometry.createFromEsriGeometry(linestring, null));
	} catch (Exception e) {
	    LogUtils.Log_InternalError(LOG, "ST_LineString: " + e);
	    return null;
	}
}
 
Example 29
Source Project: presto   Source File: GeoFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the vertex of a linestring at the specified index (indices started with 1) ")
@ScalarFunction("ST_PointN")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stPointN(@SqlType(GEOMETRY_TYPE_NAME) Slice input, @SqlType(INTEGER) long index)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_PointN", geometry, EnumSet.of(LINE_STRING));

    OGCLineString linestring = (OGCLineString) geometry;
    if (index < 1 || index > linestring.numPoints()) {
        return null;
    }
    return serialize(linestring.pointN(toIntExact(index) - 1));
}
 
Example 30
Source Project: presto   Source 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);
}