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

The following examples show how to use com.esri.core.geometry.ogc.OGCGeometry#getEsriGeometry() . 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: TestStGeomFromShape.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeomFromPolylineShape() throws UDFArgumentException {
	Polyline line = createPolyline();
	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 2
Source File: TestStGeomFromShape.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns the great-circle distance in meters between two SphericalGeography points.")
@ScalarFunction("ST_Distance")
@SqlType(DOUBLE)
public static Double stSphericalDistance(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice left, @SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice right)
{
    OGCGeometry leftGeometry = deserialize(left);
    OGCGeometry rightGeometry = deserialize(right);
    if (leftGeometry.isEmpty() || rightGeometry.isEmpty()) {
        return null;
    }

    // TODO: support more SphericalGeography types.
    validateSphericalType("ST_Distance", leftGeometry, EnumSet.of(POINT));
    validateSphericalType("ST_Distance", rightGeometry, EnumSet.of(POINT));
    Point leftPoint = (Point) leftGeometry.getEsriGeometry();
    Point rightPoint = (Point) rightGeometry.getEsriGeometry();

    // greatCircleDistance returns distance in KM.
    return greatCircleDistance(leftPoint.getY(), leftPoint.getX(), rightPoint.getY(), rightPoint.getX()) * 1000;
}
 
Example 4
Source File: TestStGeomFromShape.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeomFromPolygonShape() throws UDFArgumentException {
	Polygon polygon = createPolygon();
	byte[] esriShape = GeometryEngine.geometryToEsriShape(polygon);
	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(polygon, esriGeometry, SpatialReference.create(wkid)));
}
 
Example 5
Source File: GeometrySerde.java    From presto with 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 6
Source File: ST_AsJson.java    From spatial-framework-for-hadoop with 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 7
Source File: ST_AsShape.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
public BytesWritable 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 {
		// Get Esri shape representation
		Geometry esriGeometry = ogcGeometry.getEsriGeometry();
		byte[] esriShape = GeometryEngine.geometryToEsriShape(esriGeometry);
		return new BytesWritable(esriShape);
	} catch (Exception e){
		LOG.error(e.getMessage());
		return null;
	}
}
 
Example 8
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the great-circle length in meters of a linestring or multi-linestring on Earth's surface")
@ScalarFunction("ST_Length")
@SqlType(DOUBLE)
public static Double stSphericalLength(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }

    validateSphericalType("ST_Length", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    MultiPath lineString = (MultiPath) geometry.getEsriGeometry();

    double sum = 0;

    // sum up paths on (multi)linestring
    for (int path = 0; path < lineString.getPathCount(); path++) {
        if (lineString.getPathSize(path) < 2) {
            continue;
        }

        // sum up distances between adjacent points on this path
        int pathStart = lineString.getPathStart(path);
        Point previous = lineString.getPoint(pathStart);
        for (int i = pathStart + 1; i < lineString.getPathEnd(path); i++) {
            Point next = lineString.getPoint(i);
            sum += greatCircleDistance(previous.getY(), previous.getX(), next.getY(), next.getX());
            previous = next;
        }
    }

    return sum * 1000;
}
 
Example 9
Source File: ST_StartPoint.java    From spatial-framework-for-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Return the first point of the ST_Linestring.
 * @param geomref hive geometry bytes
 * @return byte-reference of the first ST_Point
 */
public BytesWritable 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_LINESTRING) {
		MultiPath lines = (MultiPath)(ogcGeometry.getEsriGeometry());
		int wkid = GeometryUtils.getWKID(geomref);
		SpatialReference spatialReference = null;
		if (wkid != GeometryUtils.WKID_UNKNOWN) {
			spatialReference = SpatialReference.create(wkid);
		}
		return GeometryUtils.geometryToEsriShapeBytesWritable(OGCGeometry.createFromEsriGeometry(lines.getPoint(0),
																								 spatialReference));
	} else {
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_LINESTRING, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example 10
Source File: ST_EndPoint.java    From spatial-framework-for-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Return the last point of the ST_Linestring.
 * @param geomref hive geometry bytes
 * @return byte-reference of the last ST_Point
 */
public BytesWritable 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_LINESTRING) {
		MultiPath lines = (MultiPath)(ogcGeometry.getEsriGeometry());
		int wkid = GeometryUtils.getWKID(geomref);
		SpatialReference spatialReference = null;
		if (wkid != GeometryUtils.WKID_UNKNOWN) {
			spatialReference = SpatialReference.create(wkid);
		}
		return GeometryUtils.geometryToEsriShapeBytesWritable(OGCGeometry.createFromEsriGeometry(lines.getPoint(lines.getPointCount()-1),
																								 spatialReference));
	} else {
		LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_LINESTRING, GeometryUtils.getType(geomref));
		return null;
	}
}
 
Example 11
Source File: ST_NumPoints.java    From spatial-framework-for-hadoop with Apache License 2.0 5 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;
	}

	Geometry esriGeom = ogcGeometry.getEsriGeometry();
	switch(esriGeom.getType()) {
	case Point:
		resultInt.set(esriGeom.isEmpty() ? 0 : 1);
		break;
	case MultiPoint:
		resultInt.set(((MultiPoint)(esriGeom)).getPointCount());
		break;
	case Polygon:
		Polygon polygon = (Polygon)(esriGeom);
	    resultInt.set(polygon.getPointCount() + polygon.getPathCount());
		break;
	default:
		resultInt.set(((MultiPath)(esriGeom)).getPointCount());
		break;
	}
	return resultInt;
}
 
Example 12
Source File: ST_IsClosed.java    From spatial-framework-for-hadoop with Apache License 2.0 5 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 {

		switch(GeometryUtils.getType(geomref)) {
		case ST_LINESTRING:
		case ST_MULTILINESTRING:
			MultiPath lines = (MultiPath)(ogcGeometry.getEsriGeometry());
			int nPaths = lines.getPathCount();
			boolean rslt = true;
			for (int ix = 0; rslt && ix < nPaths; ix++) {
				Point p0 = lines.getPoint(lines.getPathStart(ix));
				Point pf = lines.getPoint(lines.getPathEnd(ix)-1);
				rslt = rslt && pf.equals(p0);  // no tolerance - OGC
			}
			resultBoolean.set(rslt);
			return resultBoolean;
		default:  // ST_IsClosed gives ERROR on Point or Polygon, on Postgres/Oracle
			LogUtils.Log_InvalidType(LOG, GeometryUtils.OGCType.ST_LINESTRING, GeometryUtils.getType(geomref));
			return null;
		}

	} catch (Exception e) {
	    LogUtils.Log_InternalError(LOG, "ST_IsClosed" + e);
		return null;
	}

}
 
Example 13
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 14
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the area of a geometry on the Earth's surface using spherical model")
@ScalarFunction("ST_Area")
@SqlType(DOUBLE)
public static Double stSphericalArea(@SqlType(SPHERICAL_GEOGRAPHY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    if (geometry.isEmpty()) {
        return null;
    }

    validateSphericalType("ST_Area", geometry, EnumSet.of(POLYGON, MULTI_POLYGON));

    Polygon polygon = (Polygon) geometry.getEsriGeometry();

    // See https://www.movable-type.co.uk/scripts/latlong.html
    // and http://osgeo-org.1560.x6.nabble.com/Area-of-a-spherical-polygon-td3841625.html
    // and https://www.element84.com/blog/determining-if-a-spherical-polygon-contains-a-pole
    // for the underlying Maths

    double sphericalExcess = 0.0;

    int numPaths = polygon.getPathCount();
    for (int i = 0; i < numPaths; i++) {
        double sign = polygon.isExteriorRing(i) ? 1.0 : -1.0;
        sphericalExcess += sign * Math.abs(computeSphericalExcess(polygon, polygon.getPathStart(i), polygon.getPathEnd(i)));
    }

    // Math.abs is required here because for Polygons with a 2D area of 0
    // isExteriorRing returns false for the exterior ring
    return Math.abs(sphericalExcess * EARTH_RADIUS_M * EARTH_RADIUS_M);
}
 
Example 15
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the last point of a LINESTRING geometry as a Point")
@ScalarFunction("ST_EndPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stEndPoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_EndPoint", geometry, EnumSet.of(LINE_STRING));
    if (geometry.isEmpty()) {
        return null;
    }
    MultiPath lines = (MultiPath) geometry.getEsriGeometry();
    SpatialReference reference = geometry.getEsriSpatialReference();
    return serialize(createFromEsriGeometry(lines.getPoint(lines.getPointCount() - 1), reference));
}
 
Example 16
Source File: GeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@SqlNullable
@Description("Returns the first point of a LINESTRING geometry as a Point")
@ScalarFunction("ST_StartPoint")
@SqlType(GEOMETRY_TYPE_NAME)
public static Slice stStartPoint(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_StartPoint", geometry, EnumSet.of(LINE_STRING));
    if (geometry.isEmpty()) {
        return null;
    }
    MultiPath lines = (MultiPath) geometry.getEsriGeometry();
    SpatialReference reference = geometry.getEsriSpatialReference();
    return serialize(createFromEsriGeometry(lines.getPoint(0), reference));
}
 
Example 17
Source File: ST_GeodesicLengthWGS84.java    From spatial-framework-for-hadoop with Apache License 2.0 4 votes vote down vote up
public DoubleWritable evaluate(BytesWritable geomref) {
	if (geomref == null || geomref.getLength() == 0) {
		LogUtils.Log_ArgumentsNull(LOG);
		return null;
	}

	int WGS84 = 4326;
	if (GeometryUtils.getWKID(geomref) != WGS84) {
	    LogUtils.Log_SRIDMismatch(LOG, geomref, WGS84);
		return null;
	}

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

	Geometry esriGeom = ogcGeometry.getEsriGeometry();
	switch(esriGeom.getType()) {
	case Point:
	case MultiPoint:
		resultDouble.set(0.);
		break;
	default:
		MultiPath lines = (MultiPath)(esriGeom);
		int nPath = lines.getPathCount();
		double length = 0.;
		for (int ix = 0; ix < nPath; ix++) {
			int curPt = lines.getPathStart(ix);
			int pastPt = lines.getPathEnd(ix);
			Point fromPt = lines.getPoint(curPt);
			Point toPt = null;
			for (int vx = curPt+1; vx < pastPt; vx++) {
				toPt = lines.getPoint(vx);
				length += GeometryEngine.geodesicDistanceOnWGS84(fromPt, toPt);
				fromPt = toPt;
			}
		}
		resultDouble.set(length);
		break;
	}

	return resultDouble;
}
 
Example 18
Source File: ST_Union.java    From spatial-framework-for-hadoop with Apache License 2.0 4 votes vote down vote up
public BytesWritable evaluate (BytesWritable ... geomrefs){
	// validate arguments
	if (geomrefs == null || geomrefs.length < 2){
		LogUtils.Log_VariableArgumentLength(LOG);
	}

	int firstWKID = 0;

	SpatialReference spatialRef = null;

	// validate spatial references and geometries first
	for (int i=0;i<geomrefs.length; i++){
		
		BytesWritable geomref = geomrefs[i];
		
		if (geomref == null || geomref.getLength() == 0){
			LogUtils.Log_ArgumentsNull(LOG);
			return null;
		}

		if (i==0){
			firstWKID = GeometryUtils.getWKID(geomref);
			if (firstWKID != GeometryUtils.WKID_UNKNOWN) {
				spatialRef = SpatialReference.create(firstWKID);
			}
		} else if (firstWKID != GeometryUtils.getWKID(geomref)){
			LogUtils.Log_SRIDMismatch(LOG, geomrefs[0], geomref);
			return null;
		}
	}
	
	// now build geometry array to pass to GeometryEngine.union
	Geometry [] geomsToUnion = new Geometry[geomrefs.length];
	
	for (int i=0;i<geomrefs.length;i++){
		//HiveGeometry hiveGeometry = GeometryUtils.geometryFromEsriShape(geomrefs[i]);
		OGCGeometry ogcGeometry = GeometryUtils.geometryFromEsriShape(geomrefs[i]);
		
		// if (i==0){   // get from ogcGeometry rather than re-create above?
		// 	spatialRef = hiveGeometry.spatialReference;
		// }
		
		if (ogcGeometry == null){
			LogUtils.Log_ArgumentsNull(LOG);
			return null;
		}
		
		geomsToUnion[i] = ogcGeometry.getEsriGeometry();
	}
	
	try {
		Geometry unioned = GeometryEngine.union(geomsToUnion, spatialRef);

		// we have to infer the type of the differenced geometry because we don't know
		// if it's going to end up as a single or multi-part geometry
		OGCType inferredType = GeometryUtils.getInferredOGCType(unioned);
		
		return GeometryUtils.geometryToEsriShapeBytesWritable(unioned, firstWKID, inferredType);
	} catch (Exception e){
		LogUtils.Log_ExceptionThrown(LOG, "GeometryEngine.union", e);
		return null;
	}
}
 
Example 19
Source File: ST_ConvexHull.java    From spatial-framework-for-hadoop with Apache License 2.0 4 votes vote down vote up
public BytesWritable evaluate (BytesWritable ... geomrefs){

		// validate arguments
		if (geomrefs == null || geomrefs.length < 1){
			// LogUtils.Log_VariableArgumentLength(LOG);
			return null;
		}

		int firstWKID = 0;
		
		// validate spatial references and geometries first
		for (int i=0;i<geomrefs.length; i++){
			
			BytesWritable geomref = geomrefs[i];
			
			if (geomref == null || geomref.getLength() == 0){
				LogUtils.Log_ArgumentsNull(LOG);
				return null;
			}
			
			if (i==0){
				firstWKID = GeometryUtils.getWKID(geomref);
			} else if (firstWKID != GeometryUtils.getWKID(geomref)){
				LogUtils.Log_SRIDMismatch(LOG, geomrefs[0], geomref);
				return null;
			}
		}
		
		// now build geometry array to pass to GeometryEngine.union
		Geometry [] geomsToProcess = new Geometry[geomrefs.length];
		
		for (int i=0;i<geomrefs.length;i++){
			//HiveGeometry hiveGeometry = GeometryUtils.geometryFromEsriShape(geomrefs[i]);
			OGCGeometry ogcGeometry = GeometryUtils.geometryFromEsriShape(geomrefs[i]);
			
			if (ogcGeometry == null){
				LogUtils.Log_ArgumentsNull(LOG);
				return null;
			}
			
			geomsToProcess[i] = ogcGeometry.getEsriGeometry();
		}
		
		try {

  		    Geometry [] geomResult = GeometryEngine.convexHull(geomsToProcess, true);

  		    if (geomResult.length != 1){
  		    	return null;
  		    }
  		    
  		    Geometry merged = geomResult[0];
  		    
			// we have to infer the type of the differenced geometry because we don't know
			// if it's going to end up as a single or multi-part geometry
			OGCType inferredType = GeometryUtils.getInferredOGCType(merged);
			
			return GeometryUtils.geometryToEsriShapeBytesWritable(merged, firstWKID, inferredType);
		} catch (Exception e){
			LogUtils.Log_ExceptionThrown(LOG, "GeometryEngine.convexHull", e);
			return null;
		}
	}
 
Example 20
Source File: GeoFunctions.java    From presto with Apache License 2.0 4 votes vote down vote up
private static List<Point> interpolatePoints(OGCGeometry geometry, double fractionStep, boolean repeated)
{
    validateType("line_interpolate_point", geometry, EnumSet.of(LINE_STRING));
    if (fractionStep < 0 || fractionStep > 1) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "fraction must be between 0 and 1");
    }

    MultiPath path = (MultiPath) geometry.getEsriGeometry();

    if (fractionStep == 0) {
        return Collections.singletonList(path.getPoint(0));
    }
    if (fractionStep == 1) {
        return Collections.singletonList((path.getPoint(path.getPointCount() - 1)));
    }

    int pointCount = repeated ? (int) Math.floor(1 / fractionStep) : 1;
    List<Point> interpolatedPoints = new ArrayList<>(pointCount);

    double lineStringLength = path.calculateLength2D();
    Point previous = path.getPoint(0);
    double fractionConsumed = 0.0;
    double fractionIncrement = fractionStep;

    for (int i = 1; i < path.getPointCount() && interpolatedPoints.size() < pointCount; i++) {
        Point current = path.getPoint(i);
        double segmentLengthFraction = GeometryEngine.distance(previous, current, null) / lineStringLength;

        while (fractionStep < fractionConsumed + segmentLengthFraction && interpolatedPoints.size() < pointCount) {
            double segmentFraction = (fractionStep - fractionConsumed) / segmentLengthFraction;
            Point point = new Point();
            point.setX(previous.getX() + (current.getX() - previous.getX()) * segmentFraction);
            point.setY(previous.getY() + (current.getY() - previous.getY()) * segmentFraction);
            interpolatedPoints.add(point);
            fractionStep += fractionIncrement;
        }

        fractionConsumed += segmentLengthFraction;
        previous = current;
    }

    if (interpolatedPoints.size() < pointCount) {
        interpolatedPoints.add(path.getPoint(path.getPointCount() - 1));
    }

    return interpolatedPoints;
}