Search in sources :

Example 1 with AttributeGetter

use of gov.usgs.cida.utilities.features.AttributeGetter 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

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