use of org.opengis.geometry.coordinate.LineString in project geotoolkit by Geomatys.
the class GeometryParser method readLinearRingText.
/**
* Creates a <code>Curve</code> using the next token in the stream.
*
* @param tokenizer tokenizer over a stream of text in Well-known Text
* format. The next tokens must form a <LineString Text>.
* @return a <code>Curve</code> specified by the next
* token in the stream
* @throws IOException if an I/O error occurs
* @throws ParseException if the coordinates used to create the <code>Curve</code>
* do not form a closed linestring, or if an unexpected token was
* encountered
*/
private Curve readLinearRingText(final StreamTokenizer tokenizer) throws IOException, ParseException {
List<Position> coordList = getCoordinates(tokenizer);
LineString lineString = geometryFactory.createLineString(coordList);
List<CurveSegment> curveSegmentList = Collections.singletonList((CurveSegment) lineString);
return primitiveFactory.createCurve(curveSegmentList);
}
use of org.opengis.geometry.coordinate.LineString in project geotoolkit by Geomatys.
the class JTSUtils method toISO.
/**
* Creates a 19107 primitive geometry from the given JTS geometry.
*/
public static Geometry toISO(final org.locationtech.jts.geom.Geometry jtsGeom, CoordinateReferenceSystem crs) {
if (jtsGeom == null) {
return null;
}
if (crs == null) {
// try to extract the crs from the srid
final int srid = jtsGeom.getSRID();
if (srid != 0) {
final String strCRS = SRIDGenerator.toSRS(srid, SRIDGenerator.Version.V1);
try {
crs = CRS.forCode(strCRS);
} catch (FactoryException ex) {
Logger.getLogger("org.geotoolkit.geometry.isoonjts").log(Level.SEVERE, null, ex);
}
}
}
// TODO use factory finder when primitive factory and geometry factory are ready.
// FactoryFinder.getPrimitiveFactory(hints);
final PrimitiveFactory pf = new JTSPrimitiveFactory(crs);
// FactoryFinder.getGeometryFactory(hints);
final GeometryFactory gf = new JTSGeometryFactory(crs);
if (jtsGeom instanceof org.locationtech.jts.geom.Point) {
org.locationtech.jts.geom.Point candidate = (org.locationtech.jts.geom.Point) jtsGeom;
DirectPosition dp = pointToDirectPosition(candidate, crs);
return pf.createPoint(dp);
} else if (jtsGeom instanceof org.locationtech.jts.geom.LineString) {
org.locationtech.jts.geom.LineString candidate = (org.locationtech.jts.geom.LineString) jtsGeom;
LineString ls = gf.createLineString(new ArrayList<Position>());
PointArray pointList = ls.getControlPoints();
for (int i = 0, n = candidate.getNumPoints(); i < n; i++) {
pointList.add(coordinateToDirectPosition(candidate.getCoordinateN(i), crs));
}
return (JTSLineString) ls;
} else if (jtsGeom instanceof org.locationtech.jts.geom.LinearRing) {
return linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsGeom, crs);
} else if (jtsGeom instanceof org.locationtech.jts.geom.Polygon) {
org.locationtech.jts.geom.Polygon jtsPolygon = (org.locationtech.jts.geom.Polygon) jtsGeom;
Ring externalRing = linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsPolygon.getExteriorRing(), crs);
ArrayList internalRings = new ArrayList();
for (int i = 0, n = jtsPolygon.getNumInteriorRing(); i < n; i++) {
internalRings.add(linearRingToRing((org.locationtech.jts.geom.LinearRing) jtsPolygon.getInteriorRingN(i), crs));
}
SurfaceBoundary boundary = pf.createSurfaceBoundary(externalRing, internalRings);
Polygon polygon = gf.createPolygon(boundary);
return (JTSPolygon) polygon;
/*ArrayList<Polygon> patches = new ArrayList<Polygon>();
patches.add(polygon);
PolyhedralSurface result = gf.createPolyhedralSurface(patches);
return result;*/
} else if (jtsGeom instanceof GeometryCollection) {
org.locationtech.jts.geom.GeometryCollection jtsCollection = (org.locationtech.jts.geom.GeometryCollection) jtsGeom;
boolean multiPoint = jtsGeom instanceof MultiPoint;
boolean multiCurve = jtsGeom instanceof MultiLineString;
boolean multiSurface = jtsGeom instanceof MultiPolygon;
// determine it by analyzing its content.
if (!(multiPoint || multiCurve || multiSurface || jtsGeom.isEmpty())) {
multiPoint = multiCurve = multiSurface = true;
for (int i = 0, n = jtsCollection.getNumGeometries(); i < n && (multiPoint || multiCurve || multiSurface); i++) {
if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.Point)) {
multiPoint = false;
}
if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.LineString)) {
multiCurve = false;
}
if (!(jtsCollection.getGeometryN(i) instanceof org.locationtech.jts.geom.Polygon)) {
multiSurface = false;
}
}
}
AbstractJTSAggregate result;
if (multiPoint) {
result = new JTSMultiPoint(crs);
Set elements = result.getElements();
for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
// result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
elements.add(toISO(jtsCollection.getGeometryN(i), crs));
}
} else if (multiCurve) {
result = new JTSMultiCurve(crs);
Set elements = result.getElements();
for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
// result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
Geometry element = toISO(jtsCollection.getGeometryN(i), crs);
if (element instanceof JTSLineString) {
JTSCurve curve = new JTSCurve(crs);
curve.getSegments().add((JTSLineString) element);
element = curve;
}
elements.add(element);
}
} else if (multiSurface) {
result = new JTSMultiSurface(crs);
Set elements = result.getElements();
for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
// result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
elements.add(toISO(jtsCollection.getGeometryN(i), crs));
}
} else {
result = new JTSMultiPrimitive();
Set elements = result.getElements();
for (int i = 0, n = jtsCollection.getNumGeometries(); i < n; i++) {
// result.getElements().add(jtsToGo1(jtsCollection.getGeometryN(i), crs));
elements.add(toISO(jtsCollection.getGeometryN(i), crs));
}
}
return result;
} else {
throw new IllegalArgumentException("Unsupported geometry type: " + jtsGeom.getGeometryType());
}
}
use of org.opengis.geometry.coordinate.LineString in project geotoolkit by Geomatys.
the class GMLUtilities method getGMLFromISO.
/**
* @param geometry The ISO geometry to convert.
* @return A GML 3.1.1 geometry matching given geometry definition.
* @deprecated This method should not be used for multiple reasons:
* <ol>
* <li>OpenGIS definition is based on an obsolete ISO-19107 draft</li>
* <li>The GML version is fixed and obsolete</li>
* <li>Only partial management of geometries</li>
* </ol>
*/
public static AbstractGeometryType getGMLFromISO(final org.opengis.geometry.Geometry geometry) {
if (geometry instanceof Point) {
Point point = (Point) geometry;
PointType gmlPoint = new PointType(null, point.getDirectPosition());
return gmlPoint;
} else if (geometry instanceof OrientableSurface) {
OrientableSurface surface = (OrientableSurface) geometry;
SurfaceBoundary boundary = surface.getBoundary();
Ring exterior = boundary.getExterior();
List<CurvePropertyType> curves = new ArrayList<CurvePropertyType>();
for (Primitive p : exterior.getElements()) {
curves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlExterior = new RingType();
gmlExterior.getCurveMember().addAll(curves);
List<Ring> interiors = boundary.getInteriors();
List<RingType> gmlInteriors = new ArrayList<RingType>();
for (Ring interior : interiors) {
List<CurvePropertyType> intcurves = new ArrayList<CurvePropertyType>();
for (Primitive p : interior.getElements()) {
intcurves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlinterior = new RingType();
gmlinterior.getCurveMember().addAll(intcurves);
gmlInteriors.add(gmlinterior);
}
PolygonType poly = new PolygonType(gmlExterior, gmlInteriors);
return poly;
} else if (geometry instanceof MultiSurface) {
MultiSurface multiPrim = (MultiSurface) geometry;
List<PolygonPropertyType> geometries = new ArrayList<PolygonPropertyType>();
for (Geometry prim : multiPrim.getElements()) {
PolygonType element = (PolygonType) getGMLFromISO(prim);
PolygonPropertyType gp = new PolygonPropertyType((PolygonType) element);
geometries.add(gp);
}
MultiPolygonType gmlMulti = new MultiPolygonType(null, geometries);
return gmlMulti;
} else if (geometry instanceof MultiCurve) {
MultiCurve multiPrim = (MultiCurve) geometry;
List<CurvePropertyType> geometries = new ArrayList<CurvePropertyType>();
for (OrientableCurve prim : multiPrim.getElements()) {
AbstractCurveType element = (AbstractCurveType) getGMLFromISO(prim);
CurvePropertyType gp = new CurvePropertyType((AbstractCurveType) element);
geometries.add(gp);
}
MultiCurveType gmlMulti = new MultiCurveType(geometries);
return gmlMulti;
} else if (geometry instanceof MultiPoint) {
MultiPoint multiPrim = (MultiPoint) geometry;
List<PointPropertyType> geometries = new ArrayList<PointPropertyType>();
for (Point prim : multiPrim.getElements()) {
PointType element = (PointType) getGMLFromISO(prim);
PointPropertyType gp = new PointPropertyType((PointType) element);
geometries.add(gp);
}
MultiPointType gmlMulti = new MultiPointType(null, geometries);
return gmlMulti;
} else if (geometry instanceof MultiPrimitive) {
MultiPrimitive multiPrim = (MultiPrimitive) geometry;
List<GeometryPropertyType> geometries = new ArrayList<GeometryPropertyType>();
for (Primitive prim : multiPrim.getElements()) {
AbstractGMLType element = getGMLFromISO(prim);
GeometryPropertyType gp = new GeometryPropertyType((AbstractGeometryType) element);
geometries.add(gp);
}
MultiGeometryType gmlMulti = new MultiGeometryType(geometries);
return gmlMulti;
} else if (geometry instanceof Curve) {
Curve curve = (Curve) geometry;
List<? extends CurveSegment> segments = curve.getSegments();
List<LineStringSegmentType> gmlSegments = new ArrayList<LineStringSegmentType>();
for (CurveSegment segment : segments) {
CurveInterpolationType interpolation = CurveInterpolationType.fromValue(segment.getInterpolation().identifier());
PointArray array = GeometricUtilities.getSamplePoints(segment);
List<DirectPosition> positions = new ArrayList<DirectPosition>();
for (int i = 0; i < array.size(); i++) {
positions.add(array.getDirectPosition(i, null));
}
LineStringSegmentType gmlSegment = new LineStringSegmentType(segment.getNumDerivativesAtStart(), segment.getNumDerivativesAtEnd(), segment.getNumDerivativesInterior(), interpolation, positions);
gmlSegments.add(gmlSegment);
}
CurveType gmlCurve = new CurveType(gmlSegments);
return gmlCurve;
} else if (geometry instanceof LineString) {
LineString line = (LineString) geometry;
PointArray array = GeometricUtilities.getSamplePoints(line);
List<DirectPosition> positions = new ArrayList<DirectPosition>();
for (int i = 0; i < array.size(); i++) {
positions.add(array.getDirectPosition(i, null));
}
LineStringType gmlLine = new LineStringType(positions);
return gmlLine;
} else if (geometry instanceof Polygon) {
Polygon polygon = (Polygon) geometry;
SurfaceBoundary boundary = polygon.getBoundary();
Ring exterior = boundary.getExterior();
List<CurvePropertyType> curves = new ArrayList<CurvePropertyType>();
for (Primitive p : exterior.getElements()) {
curves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlExterior = new RingType();
gmlExterior.getCurveMember().addAll(curves);
List<Ring> interiors = boundary.getInteriors();
List<RingType> gmlInteriors = new ArrayList<RingType>();
for (Ring interior : interiors) {
List<CurvePropertyType> intcurves = new ArrayList<CurvePropertyType>();
for (Primitive p : interior.getElements()) {
intcurves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlinterior = new RingType();
gmlinterior.getCurveMember().addAll(intcurves);
gmlInteriors.add(gmlinterior);
}
PolygonType gmlPolygon = new PolygonType(gmlExterior, gmlInteriors);
return gmlPolygon;
} else if (geometry instanceof PolyhedralSurface) {
PolyhedralSurface polySurface = (PolyhedralSurface) geometry;
List<PolygonPatchType> gmlPatches = new ArrayList<PolygonPatchType>();
List<? extends Polygon> patches = polySurface.getPatches();
for (Polygon polygon : patches) {
SurfaceInterpolationType interpolation = SurfaceInterpolationType.fromValue(polygon.getInterpolation().identifier());
SurfaceBoundary boundary = polygon.getBoundary();
Ring exterior = boundary.getExterior();
List<CurvePropertyType> curves = new ArrayList<CurvePropertyType>();
for (Primitive p : exterior.getElements()) {
curves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlExterior = new RingType();
gmlExterior.getCurveMember().addAll(curves);
List<Ring> interiors = boundary.getInteriors();
List<RingType> gmlInteriors = new ArrayList<RingType>();
for (Ring interior : interiors) {
List<CurvePropertyType> intcurves = new ArrayList<CurvePropertyType>();
for (Primitive p : interior.getElements()) {
intcurves.add(new CurvePropertyType((CurveType) getGMLFromISO(p)));
}
RingType gmlinterior = new RingType();
gmlinterior.getCurveMember().addAll(intcurves);
gmlInteriors.add(gmlinterior);
}
PolygonPatchType patche = new PolygonPatchType(interpolation, gmlExterior, gmlInteriors);
gmlPatches.add(patche);
}
PolygonPatchArrayPropertyType pathArray = new PolygonPatchArrayPropertyType(gmlPatches);
PolyhedralSurfaceType gmlPolySurface = new PolyhedralSurfaceType(pathArray);
return gmlPolySurface;
} else {
System.out.println("unexpected iso geometry type:" + geometry.getClass().getName());
}
return null;
}
use of org.opengis.geometry.coordinate.LineString in project geotoolkit by Geomatys.
the class GeometryParser method readLineStringText.
/**
* Creates a <code>LineString</code> using the next token in the stream.
*
* @param tokenizer tokenizer over a stream of text in Well-known Text
* format. The next tokens must form a <LineString Text>.
* @return a <code>LineString</code> specified by the next
* token in the stream
* @throws IOException if an I/O error occurs
* @throws ParseException if an unexpected token was encountered
*/
private Curve readLineStringText(final StreamTokenizer tokenizer) throws IOException, ParseException {
final List<Position> coordList = getCoordinates(tokenizer);
final LineString lineString = geometryFactory.createLineString(coordList);
final List<CurveSegment> curveSegmentList = Collections.singletonList((CurveSegment) lineString);
return primitiveFactory.createCurve(curveSegmentList);
}
use of org.opengis.geometry.coordinate.LineString in project geotoolkit by Geomatys.
the class GeometryUtils method getLineCharSequences.
/**
* Recursively populates the specified List with LineCharSequences corresponding
* to the primitive elements of the specified CompositeCurve.
* Returns null if any element of the CompositeCurve cannot be converted
* to a LineString.
* @param cc The CompositeCurve of interest
* @param lsList The ArrayList to be populated
* @return The populated List, or null if not valid
*/
private static ArrayList getLineCharSequences(final CompositeCurve cc, final ArrayList lsList) {
// Cast below can be removed when Types will be allowed to abandon Java 1.4 support.
List elements = (List) cc.getGenerators();
boolean valid = true;
if (!elements.isEmpty()) {
Iterator it = elements.iterator();
LineString ls = null;
while (it.hasNext() && valid) {
Object element = it.next();
if (element instanceof CompositeCurve) {
valid = getLineCharSequences((CompositeCurve) element, lsList) != null;
} else if (element instanceof Curve) {
// PENDING(NL): When we have arc geometries implemented,
// make provision to pass in real parameters for spacing and offset.
// What we have below essentially just returns start and end points
// if it's not a LineString
ls = ((Curve) element).asLineString(Double.MAX_VALUE, Double.MAX_VALUE);
if (ls != null) {
lsList.add(ls);
} else {
valid = false;
}
} else {
valid = false;
}
}
}
if (valid) {
return null;
}
return lsList;
}
Aggregations