Search in sources :

Example 1 with Geometries

use of org.geotools.geometry.jts.Geometries in project coastal-hazards by USGS-CIDA.

the class CRSUtils method getLinesFromFeatureCollection.

/**
 * Since we are now supporting points, we need to join points together into
 * lines as well as break multilines into lines
 *
 * @param collection feature collection to split up
 * @return
 */
public static MultiLineString getLinesFromFeatureCollection(SimpleFeatureCollection collection) {
    List<LineString> lines = new LinkedList<>();
    FeatureIterator<SimpleFeature> features = null;
    try {
        if (collection == null) {
            throw new IllegalArgumentException("Must include feature collection to convert");
        }
        features = collection.features();
        SimpleFeature feature = null;
        while (features.hasNext()) {
            feature = features.next();
            Geometry geometry = (Geometry) feature.getDefaultGeometry();
            Geometries geomType = Geometries.get(geometry);
            switch(geomType) {
                case LINESTRING:
                case MULTILINESTRING:
                    List<LineString> separatedLines = breakLinesIntoLineList(feature);
                    lines.addAll(separatedLines);
                    break;
                case POINT:
                case MULTIPOINT:
                    List<LineString> gatheredLines = gatherPointsIntoLineList(feature, features);
                    lines.addAll(gatheredLines);
                    break;
                case POLYGON:
                case MULTIPOLYGON:
                    throw new UnsupportedFeatureTypeException(geomType.getSimpleName() + " features not supported");
                default:
                    throw new UnsupportedFeatureTypeException("Only line type supported");
            }
        }
    } finally {
        if (null != features) {
            features.close();
        }
    }
    LineString[] linesArr = new LineString[lines.size()];
    lines.toArray(linesArr);
    return geometryFactory.createMultiLineString(linesArr);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Geometries(org.geotools.geometry.jts.Geometries) LinkedList(java.util.LinkedList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) UnsupportedFeatureTypeException(gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException)

Example 2 with Geometries

use of org.geotools.geometry.jts.Geometries in project coastal-hazards by USGS-CIDA.

the class CRSUtils method breakLinesIntoLineList.

private static List<LineString> breakLinesIntoLineList(SimpleFeature feature) {
    List<LineString> lines = new LinkedList<>();
    Geometry geometry = (Geometry) feature.getDefaultGeometry();
    Geometries geomType = Geometries.get(geometry);
    LineString lineString = null;
    switch(geomType) {
        case LINESTRING:
            lineString = (LineString) geometry;
            lines.add(lineString);
            break;
        case MULTILINESTRING:
            MultiLineString multiLineString = (MultiLineString) geometry;
            for (int i = 0; i < multiLineString.getNumGeometries(); i++) {
                lineString = (LineString) multiLineString.getGeometryN(i);
                lines.add(lineString);
            }
            break;
        default:
            throw new IllegalStateException("Only line types should end up here");
    }
    return lines;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Geometries(org.geotools.geometry.jts.Geometries) LinkedList(java.util.LinkedList) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 3 with Geometries

use of org.geotools.geometry.jts.Geometries in project coastal-hazards by USGS-CIDA.

the class CRSUtils method gatherPointsIntoLineList.

private static List<LineString> gatherPointsIntoLineList(SimpleFeature start, FeatureIterator<SimpleFeature> rest) {
    List<LineString> lines = new LinkedList<>();
    SimpleFeatureType featureType = start.getFeatureType();
    AttributeGetter getter = new AttributeGetter(featureType);
    SimpleFeature previous = null;
    SimpleFeature current = start;
    List<Coordinate> currentLine = new LinkedList<>();
    while (current != null) {
        Geometry geometry = (Geometry) current.getDefaultGeometry();
        Geometries geomType = Geometries.get(geometry);
        switch(geomType) {
            case LINESTRING:
            case MULTILINESTRING:
                // flush currentLine to list before starting new one
                if (currentLine.size() > 0) {
                    lines.add(buildLineString(currentLine));
                    currentLine = new LinkedList<>();
                }
                List<LineString> separatedLines = breakLinesIntoLineList(current);
                lines.addAll(separatedLines);
                break;
            case POINT:
                Point p = (Point) geometry;
                if (isNewLineSegment(previous, current, getter)) {
                    // only create a line if 2 or more points exist
                    if (currentLine.size() > 1) {
                        lines.add(buildLineString(currentLine));
                    } else {
                        // DO nothing right now, signifies a single point segnment
                        LOGGER.warn("Single point feature found and is being ignored, segment_id" + getter.getValue(Constants.SEGMENT_ID_ATTR, current));
                    }
                    currentLine = new LinkedList<>();
                }
                currentLine.add(p.getCoordinate());
                break;
            case MULTIPOINT:
                MultiPoint mp = (MultiPoint) geometry;
                if (isNewLineSegment(previous, current, getter)) {
                    lines.add(buildLineString(currentLine));
                    currentLine = new LinkedList<>();
                }
                for (int i = 0; i < mp.getNumPoints(); i++) {
                    Point pointMember = (Point) mp.getGeometryN(i);
                    currentLine.add(pointMember.getCoordinate());
                }
                break;
            case POLYGON:
            case MULTIPOLYGON:
                throw new UnsupportedFeatureTypeException(geomType.getSimpleName() + " features not supported");
            default:
                throw new UnsupportedFeatureTypeException("Only line type supported");
        }
        if (rest.hasNext()) {
            previous = current;
            current = rest.next();
        } else {
            if (currentLine.size() > 0) {
                lines.add(buildLineString(currentLine));
                currentLine = new LinkedList<>();
            }
            current = null;
        }
    }
    return lines;
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Geometries(org.geotools.geometry.jts.Geometries) AttributeGetter(gov.usgs.cida.utilities.features.AttributeGetter) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) LinkedList(java.util.LinkedList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Geometry(com.vividsolutions.jts.geom.Geometry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) LineString(com.vividsolutions.jts.geom.LineString) MultiLineString(com.vividsolutions.jts.geom.MultiLineString) Coordinate(com.vividsolutions.jts.geom.Coordinate) UnsupportedFeatureTypeException(gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException)

Aggregations

Geometry (com.vividsolutions.jts.geom.Geometry)3 LineString (com.vividsolutions.jts.geom.LineString)3 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)3 LinkedList (java.util.LinkedList)3 Geometries (org.geotools.geometry.jts.Geometries)3 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)2 Point (com.vividsolutions.jts.geom.Point)2 UnsupportedFeatureTypeException (gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 AttributeGetter (gov.usgs.cida.utilities.features.AttributeGetter)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1