com.esri.core.geometry.MultiPath Java Examples

The following examples show how to use com.esri.core.geometry.MultiPath. 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: GeoFunctions.java    From presto with Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns TRUE if the LineString or Multi-LineString's start and end points are coincident")
@ScalarFunction("ST_IsClosed")
@SqlType(BOOLEAN)
public static Boolean stIsClosed(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    OGCGeometry geometry = deserialize(input);
    validateType("ST_IsClosed", geometry, EnumSet.of(LINE_STRING, MULTI_LINE_STRING));
    MultiPath lines = (MultiPath) geometry.getEsriGeometry();
    int pathCount = lines.getPathCount();
    for (int i = 0; i < pathCount; i++) {
        Point start = lines.getPoint(lines.getPathStart(i));
        Point end = lines.getPoint(lines.getPathEnd(i) - 1);
        if (!end.equals(start)) {
            return false;
        }
    }
    return true;
}
 
Example #2
Source File: OGCLineString.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
public OGCLineString(MultiPath mp, int pathIndex, SpatialReference sr,
		boolean reversed) {
	multiPath = new Polyline();
	if (!mp.isEmpty())
		multiPath.addPath(mp, pathIndex, !reversed);
	esriSR = sr;
}
 
Example #3
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 #4
Source File: OGCMultiCurve.java    From geometry-api-java with Apache License 2.0 5 votes vote down vote up
public boolean isClosed() {
	MultiPath mp = (MultiPath) getEsriGeometry();
	for (int i = 0, n = mp.getPathCount(); i < n; i++) {
		if (!mp.isClosedPathInXYPlane(i))
			return false;
	}

	return true;
}
 
Example #5
Source File: ESRI.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
     * Merges a sequence of points or paths if the first instance is an implementation of this library.
     *
     * @throws ClassCastException if an element in the iterator is not an ESRI geometry.
     */
    @Override
    final Geometry tryMergePolylines(Object next, final Iterator<?> polylines) {
        if (!(next instanceof MultiPath || next instanceof Point)) {
            return null;
        }
        final Polyline path = new Polyline();
        boolean lineTo = false;
add:    for (;;) {
            if (next instanceof Point) {
                final Point pt = (Point) next;
                if (pt.isEmpty()) {
                    lineTo = false;
                } else {
                    final double x = ((Point) next).getX();
                    final double y = ((Point) next).getY();
                    if (lineTo) {
                        path.lineTo(x, y);
                    } else {
                        path.startPath(x, y);
                        lineTo = true;
                    }
                }
            } else {
                path.add((MultiPath) next, false);
                lineTo = false;
            }
            /*
             * 'polylines.hasNext()' check is conceptually part of 'for' instruction,
             * except that we need to skip this condition during the first iteration.
             */
            do if (!polylines.hasNext()) break add;
            while ((next = polylines.next()) == null);
        }
        return path;
    }
 
Example #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: EncodedPolylineFunctions.java    From presto with Apache License 2.0 4 votes vote down vote up
private static OGCLineString decodePolyline(String polyline)
{
    MultiPath multipath = new Polyline();
    boolean isFirstPoint = true;

    int index = 0;
    int latitude = 0;
    int longitude = 0;

    while (index < polyline.length()) {
        int result = 1;
        int shift = 0;
        int bytes;
        do {
            bytes = polyline.charAt(index++) - 63 - 1;
            result += bytes << shift;
            shift += 5;
        }
        while (bytes >= 0x1f);
        latitude += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        result = 1;
        shift = 0;
        do {
            bytes = polyline.charAt(index++) - 63 - 1;
            result += bytes << shift;
            shift += 5;
        }
        while (bytes >= 0x1f);
        longitude += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);

        if (isFirstPoint) {
            multipath.startPath(longitude * 1e-5, latitude * 1e-5);
            isFirstPoint = false;
        }
        else {
            multipath.lineTo(longitude * 1e-5, latitude * 1e-5);
        }
    }

    return new OGCLineString(multipath, 0, null);
}
 
Example #15
Source File: OGCMultiCurve.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
public int numGeometries() {
	MultiPath mp = (MultiPath) getEsriGeometry();
	return mp.getPathCount();
}
 
Example #16
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;
}
 
Example #17
Source File: OGCLinearRing.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
public OGCLinearRing(MultiPath mp, int pathIndex, SpatialReference sr,
		boolean reversed) {
	super(mp, pathIndex, sr, reversed);
	if (!mp.isClosedPath(0))
		throw new IllegalArgumentException("LinearRing path must be closed");
}
 
Example #18
Source File: OGCConcreteGeometryCollection.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
@Override
public OGCGeometry convexHull() {
	GeometryCursor cursor = OperatorConvexHull.local().execute(
			getEsriGeometryCursor(), false, null);
	MultiPoint mp = new MultiPoint();
	Polygon polygon = new Polygon();
	VertexDescription vd = null;
	for (Geometry geom = cursor.next(); geom != null; geom = cursor.next()) {
		vd = geom.getDescription();
		if (geom.isEmpty())
			continue;

		if (geom.getType() == Geometry.Type.Polygon) {
			polygon.add((MultiPath) geom, false);
		}
		else if (geom.getType() == Geometry.Type.Polyline) {
			mp.add((MultiVertexGeometry) geom, 0, -1);
		}
		else if (geom.getType() == Geometry.Type.Point) {
			mp.add((Point) geom);
		}
		else {
			throw new GeometryException("internal error");
		}
	}

	Geometry resultGeom = null;
	if (!mp.isEmpty()) {
		resultGeom = OperatorConvexHull.local().execute(mp, null);
	}

	if (!polygon.isEmpty()) {
		if (resultGeom != null && !resultGeom.isEmpty()) {
			Geometry[] geoms = { resultGeom, polygon };
			resultGeom = OperatorConvexHull.local().execute(
					new SimpleGeometryCursor(geoms), true, null).next();
		}
		else {
			resultGeom = OperatorConvexHull.local().execute(polygon, null);
		}
	}

	if (resultGeom == null) {
		Point pt = new Point();
		if (vd != null)
			pt.assignVertexDescription(vd);

		return new OGCPoint(pt, getEsriSpatialReference());
	}

	return OGCGeometry.createFromEsriGeometry(resultGeom, getEsriSpatialReference(), false);
}
 
Example #19
Source File: OGCLineString.java    From geometry-api-java with Apache License 2.0 4 votes vote down vote up
public OGCLineString(MultiPath mp, int pathIndex, SpatialReference sr) {
	multiPath = new Polyline();
	if (!mp.isEmpty())
		multiPath.addPath(mp, pathIndex, true);
	esriSR = sr;
}