use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class XmlFeatureReader method getFeatureFromFOI.
protected final Feature getFeatureFromFOI(final AnyFeature foi) {
if (foi instanceof SamplingFeature) {
final SamplingFeature feature = (SamplingFeature) foi;
final Feature f = type.newInstance();
f.setPropertyValue(AttributeConvention.IDENTIFIER, feature.getId());
final org.opengis.geometry.Geometry isoGeom = feature.getGeometry();
try {
final Geometry geom;
if (isoGeom instanceof AbstractGeometry) {
geom = GeometrytoJTS.toJTS((AbstractGeometry) isoGeom);
} else {
geom = null;
}
if (firstCRS && isoGeom != null) {
// configure crs in the feature type
final CoordinateReferenceSystem crs = ((AbstractGeometry) isoGeom).getCoordinateReferenceSystem(false);
type = FeatureTypeExt.createSubType(type, null, crs);
firstCRS = false;
}
f.setPropertyValue(ATT_DESC.toString(), feature.getDescription());
if (feature.getName() != null) {
f.setPropertyValue(ATT_NAME.toString(), feature.getName().toString());
}
f.setPropertyValue(ATT_POSITION.toString(), geom);
final List<String> sampleds = new ArrayList<>();
for (FeatureProperty featProp : feature.getSampledFeatures()) {
if (featProp.getHref() != null) {
sampleds.add(featProp.getHref());
}
}
f.setPropertyValue(ATT_SAMPLED.toString(), sampleds);
return f;
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "error while transforming GML geometry to JTS", ex);
}
} else {
LOGGER.warning("unable to find a valid feature of interest in the observation");
}
return null;
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class GeometryTransformer method convertMultiSurface.
private MultiPolygon convertMultiSurface(final MultiSurface mp) {
Polygon[] polys;
try {
polys = mp.getSurfaceMember().stream().map(SurfaceProperty::getAbstractSurface).map(surface -> {
if (surface instanceof AbstractGeometry) {
return (AbstractGeometry) surface;
}
throw new UnconvertibleObjectException("We cannot convert a non-geometric object: " + surface.getClass());
}).map(p -> new GeometryTransformer(p, this).get()).map(Polygon.class::cast).toArray(size -> new Polygon[size]);
} catch (ClassCastException e) {
throw new UnconvertibleObjectException("Cannot create a multi-surface, because some of its components are not polygons", e);
}
final MultiPolygon result = GF.createMultiPolygon(polys);
applyCRS(result);
return result;
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JTStoGeometry method toGML.
/**
* Try to convert a JTS GeometryCollection to a GML AbstractGeometricAggregateType
*
* @param jtsGeom {@link GeometryCollection collection}
* @param crs Coordinate Reference System
* @return AbstractGeometricAggregateType
*
* @throws org.opengis.referencing.NoSuchAuthorityCodeException - if {@link CoordinateReferenceSystem crs} can't be
* injected into the {@link AbstractGeometry}.
* @throws org.opengis.util.FactoryException - if {@link CoordinateReferenceSystem crs} can't be injected into the
* {@link AbstractGeometry}.
*/
public static AbstractGeometricAggregate toGML(final String gmlVersion, final GeometryCollection jtsGeom, final CoordinateReferenceSystem crs) throws NoSuchAuthorityCodeException, FactoryException {
// Test if it's a 2D Geometry from CRS
isValideGeometry(crs);
if (jtsGeom.isEmpty()) {
throw new IllegalArgumentException("Cannot write empty geometry for unidentified geometry collection type");
}
final Class buffer = jtsGeom.getGeometryN(0).getClass();
// Get the class of the first geometry in the GeometryCollection
if (!(buffer.isAssignableFrom(Polygon.class) || buffer.isAssignableFrom(Point.class) || buffer.isAssignableFrom(LineString.class))) {
throw new IllegalArgumentException("Writing GML geometry collection works only for Points, LineStrings and polygons");
}
// Verify that we've got a single geometry type
boolean isSupported = true;
final List<Geometry> innerGeometries = new ArrayList<>(jtsGeom.getNumGeometries());
for (int i = 0; i < jtsGeom.getNumGeometries(); i++) {
final Geometry subGeom = jtsGeom.getGeometryN(i);
if (!(subGeom.getClass().isAssignableFrom(buffer))) {
throw new IllegalArgumentException("Writing GML geometry collection containing different geometry types is not supported");
}
innerGeometries.add(subGeom);
}
final GeometryFactory gf = JTS.getFactory();
// Convert to a MultiPoint
if (buffer.equals(Point.class)) {
final MultiPoint mutlPt = gf.createMultiPoint(innerGeometries.toArray(new Point[innerGeometries.size()]));
JTS.setCRS(mutlPt, crs);
return toGML(gmlVersion, mutlPt, crs);
// Convert to a MultiLineString
} else if (buffer.equals(LineString.class)) {
final MultiLineString multLineString = gf.createMultiLineString(innerGeometries.toArray(new LineString[innerGeometries.size()]));
JTS.setCRS(multLineString, crs);
return toGML(gmlVersion, multLineString, crs);
} else if (buffer.equals(Polygon.class)) {
final MultiPolygon multPoly = gf.createMultiPolygon(innerGeometries.toArray(new Polygon[innerGeometries.size()]));
JTS.setCRS(multPoly, crs);
return toGML(gmlVersion, multPoly, crs);
} else {
throw new IllegalArgumentException("Writing GML geometry collection works only for Points, LineStrings and polygons");
}
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JTSISOtransformTest method multiPolygonToISOTest.
@Test
public void multiPolygonToISOTest() throws Exception {
GeometryFactory fact = org.geotoolkit.geometry.jts.JTS.getFactory();
final Coordinate[] coordinates = new Coordinate[5];
coordinates[0] = new Coordinate(0, 0);
coordinates[1] = new Coordinate(0, 1);
coordinates[2] = new Coordinate(1, 1);
coordinates[3] = new Coordinate(1, 0);
coordinates[4] = new Coordinate(0, 0);
LinearRing linear = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates);
Polygon poly = new Polygon(linear, null, fact);
poly.setSRID(2154);
final Coordinate[] coordinates2 = new Coordinate[5];
coordinates2[0] = new Coordinate(10, 10);
coordinates2[1] = new Coordinate(10, 11);
coordinates2[2] = new Coordinate(11, 11);
coordinates2[3] = new Coordinate(11, 10);
coordinates2[4] = new Coordinate(10, 10);
LinearRing linear2 = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates2);
Polygon poly2 = new Polygon(linear2, null, fact);
poly2.setSRID(2154);
Polygon[] polygons = new Polygon[2];
polygons[0] = poly;
polygons[1] = poly2;
MultiPolygon m = new MultiPolygon(polygons, fact);
m.setSRID(2154);
AbstractGeometry gml = JTStoGeometry.toGML("3.2.1", m);
final Geometry geom = GeometrytoJTS.toJTS(gml);
// System.out.println(geom);
System.out.println("SRID:" + geom.getSRID());
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JTSISOtransformTest method polygonToISOTest.
@Test
public void polygonToISOTest() throws Exception {
GeometryFactory fact = org.geotoolkit.geometry.jts.JTS.getFactory();
final Coordinate[] coordinates = new Coordinate[5];
coordinates[0] = new Coordinate(0, 0);
coordinates[1] = new Coordinate(0, 1);
coordinates[2] = new Coordinate(1, 1);
coordinates[3] = new Coordinate(1, 0);
coordinates[4] = new Coordinate(0, 0);
LinearRing linear = org.geotoolkit.geometry.jts.JTS.getFactory().createLinearRing(coordinates);
Polygon poly = new Polygon(linear, null, fact);
poly.setSRID(2154);
AbstractGeometry gml = JTStoGeometry.toGML("3.2.1", poly);
final Geometry geom = GeometrytoJTS.toJTS(gml);
// System.out.println(geom);
System.out.println("SRID:" + geom.getSRID());
}
Aggregations