Java Code Examples for org.locationtech.jts.geom.Geometry#getLength()

The following examples show how to use org.locationtech.jts.geom.Geometry#getLength() . 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 a float between 0 and 1 representing the location of the closest point on the LineString to the given Point, as a fraction of total 2d line length.")
@ScalarFunction("line_locate_point")
@SqlType(DOUBLE)
public static Double lineLocatePoint(@SqlType(GEOMETRY_TYPE_NAME) Slice lineSlice, @SqlType(GEOMETRY_TYPE_NAME) Slice pointSlice)
{
    Geometry line = JtsGeometrySerde.deserialize(lineSlice);
    Geometry point = JtsGeometrySerde.deserialize(pointSlice);

    if (line.isEmpty() || point.isEmpty()) {
        return null;
    }

    GeometryType lineType = GeometryType.getForJtsGeometryType(line.getGeometryType());
    if (lineType != GeometryType.LINE_STRING && lineType != GeometryType.MULTI_LINE_STRING) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("First argument to line_locate_point must be a LineString or a MultiLineString. Got: %s", line.getGeometryType()));
    }

    GeometryType pointType = GeometryType.getForJtsGeometryType(point.getGeometryType());
    if (pointType != GeometryType.POINT) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Second argument to line_locate_point must be a Point. Got: %s", point.getGeometryType()));
    }

    return new LengthIndexedLine(line).indexOf(point.getCoordinate()) / line.getLength();
}
 
Example 2
Source File: FeatureElevationComparer.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param newGeometry new geometry to insert.
 */
public void substituteGeometry( Geometry newGeometry ) {
    if (toRemove) {
        return;
    }
    if (newGeometry.getLength() < lengthThreshold) {
        feature = null;
        geometry = null;
        toRemove = true;
        return;
    }

    Object[] attributes = feature.getAttributes().toArray();
    Object[] newAttributes = new Object[attributes.length];
    System.arraycopy(attributes, 0, newAttributes, 0, attributes.length);
    newAttributes[0] = newGeometry;

    SimpleFeatureType featureType = feature.getFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
    builder.addAll(newAttributes);
    feature = builder.buildFeature(feature.getID());
    geometry = newGeometry;
    bufferPolygon = geometry.buffer(buffer);
}
 
Example 3
Source File: TestGeometryUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public void testPolygonToUnitScaler() throws Exception {
    WKTReader reader = new WKTReader();
    Geometry geometry = reader.read(IRREGULAR_POLYGON);
    Geometry scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry);
    double area = scaled.getArea();
    assertEquals(1.0, area, DELTA);

    geometry = reader.read(TWO_BALLS);
    scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry);
    area = scaled.getArea();
    assertEquals(1.0, area, DELTA);

    geometry = reader.read(RECTANGLE);
    scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry);
    area = scaled.getArea();
    assertEquals(1.0, area, DELTA);
    double perim = scaled.getLength();
    assertEquals(4.0, perim, DELTA);

    // hexa
    geometry = reader.read(HEXAGON);
    scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry);
    area = scaled.getArea();
    assertEquals(1.0, area, DELTA);
    perim = scaled.getLength();
    assertEquals(3.72241943, perim, DELTA);

    // triangle
    geometry = reader.read(EQUILATERAL_TRIANGLE);
    scaled = GeometryUtilities.scaleToUnitaryArea((Polygon) geometry);
    area = scaled.getArea();
    assertEquals(1.0, area, DELTA);
    perim = scaled.getLength();
    assertEquals(4.55901411, perim, DELTA);

}
 
Example 4
Source File: PolygonAreaCalculator.java    From geowave with Apache License 2.0 5 votes vote down vote up
public double getAreaDensify(final Geometry polygon) throws Exception {
  final Point centroid = polygon.getCentroid();
  final CoordinateReferenceSystem equalAreaCRS = lookupUtmCrs(centroid.getY(), centroid.getX());

  final double vertexSpacing = polygon.getLength() / densifyVertexCount;
  final Geometry densePolygon = Densifier.densify(polygon, vertexSpacing);

  final MathTransform transform =
      CRS.findMathTransform(DefaultGeographicCRS.WGS84, equalAreaCRS, true);

  final Geometry transformedPolygon = JTS.transform(densePolygon, transform);

  return transformedPolygon.getArea() * SQM_2_SQKM;
}
 
Example 5
Source File: VectorTileEncoder.java    From java-vector-tile with Apache License 2.0 4 votes vote down vote up
/**
 * Add a feature with layer name (typically feature type name), some attributes
 * and a Geometry. The Geometry must be in "pixel" space 0,0 upper left and
 * 256,256 lower right.
 * <p>
 * For optimization, geometries will be clipped and simplified. Features with
 * geometries outside of the tile will be skipped.
 *
 * @param layerName
 * @param attributes
 * @param geometry
 * @param id
 */
public void addFeature(String layerName, Map<String, ?> attributes, Geometry geometry, long id) {

    // skip small Polygon/LineString.
    if (geometry instanceof MultiPolygon && geometry.getArea() < minimumArea) {
        return;
    }
    if (geometry instanceof Polygon && geometry.getArea() < minimumArea) {
        return;
    }
    if (geometry instanceof LineString && geometry.getLength() < minimumLength) {
        return;
    }

    // special handling of GeometryCollection. subclasses are not handled here.
    if (geometry.getClass().equals(GeometryCollection.class)) {
        for (int i = 0; i < geometry.getNumGeometries(); i++) {
            Geometry subGeometry = geometry.getGeometryN(i);
            // keeping the id. any better suggestion?
            addFeature(layerName, attributes, subGeometry, id);
        }
        return;
    }

    // clip geometry
    if (geometry instanceof Point) {
        if (!clipCovers(geometry)) {
            return;
        }
    } else {
        geometry = clipGeometry(geometry);
    }
    
    // simplify non-points
    if (simplificationDistanceTolerance > 0.0 && !(geometry instanceof Point)) {
        geometry = TopologyPreservingSimplifier.simplify(geometry, simplificationDistanceTolerance);
    }

    // no need to add empty geometry
    if (geometry.isEmpty()) {
        return;
    }

    Layer layer = layers.get(layerName);
    if (layer == null) {
        layer = new Layer();
        layers.put(layerName, layer);
    }

    Feature feature = new Feature();
    feature.geometry = geometry;
    feature.id = id;
    this.autoincrement = Math.max(this.autoincrement, id + 1);

    for (Map.Entry<String, ?> e : attributes.entrySet()) {
        // skip attribute without value
        if (e.getValue() == null) {
            continue;
        }
        feature.tags.add(layer.key(e.getKey()));
        feature.tags.add(layer.value(e.getValue()));
    }

    layer.features.add(feature);
}