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);
}
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;
}
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;
}
Aggregations