Search in sources :

Example 1 with UnsupportedFeatureTypeException

use of gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException 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 UnsupportedFeatureTypeException

use of gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException 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)2 LineString (com.vividsolutions.jts.geom.LineString)2 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)2 UnsupportedFeatureTypeException (gov.usgs.cida.coastalhazards.exceptions.UnsupportedFeatureTypeException)2 LinkedList (java.util.LinkedList)2 Geometries (org.geotools.geometry.jts.Geometries)2 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)1 Point (com.vividsolutions.jts.geom.Point)1 AttributeGetter (gov.usgs.cida.utilities.features.AttributeGetter)1 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)1