use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve 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.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve in project geotoolkit by Geomatys.
the class JTSGeometryBindingTest method MultiCurveUnMarshalingTest.
/**
* Test Composite curve Marshalling.
*/
@Test
public void MultiCurveUnMarshalingTest() throws Exception {
CoordinateReferenceSystem crs = CRS.forCode("urn:ogc:def:crs:epsg::4326");
assertTrue(crs != null);
DirectPosition p1 = new GeneralDirectPosition(crs);
p1.setOrdinate(0, 35.840973);
p1.setOrdinate(1, 0.14967346);
DirectPosition p2 = new GeneralDirectPosition(crs);
p2.setOrdinate(0, 44.11891);
p2.setOrdinate(1, 3.6755037);
JTSLineString l1 = new JTSLineString(crs);
l1.getControlPoints().add(p1);
l1.getControlPoints().add(p2);
JTSCurve c2 = new JTSCurve(crs);
c2.getSegments().add(l1);
JTSCurve c1 = new JTSCurve(crs);
JTSLineString l2 = new JTSLineString(crs);
DirectPosition p21 = new GeneralDirectPosition(crs);
p21.setOrdinate(0, 51.174034);
p21.setOrdinate(1, 12.365124);
DirectPosition p22 = new GeneralDirectPosition(crs);
p22.setOrdinate(0, 55.288635);
p22.setOrdinate(1, 7.583888);
DirectPosition p23 = new GeneralDirectPosition(crs);
p23.setOrdinate(0, 56.534782);
p23.setOrdinate(1, 4.1457024);
l2.getControlPoints().add(p21);
l2.getControlPoints().add(p22);
l2.getControlPoints().add(p23);
c1.getSegments().add(l2);
JTSMultiCurve expResult = new JTSMultiCurve(crs);
expResult.getElements().add(c2);
expResult.getElements().add(c1);
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + '\n' + "<gml:MultiCurve srsName=\"urn:ogc:def:crs:epsg::4326\" xmlns:gml=\"http://www.opengis.net/gml\">" + '\n' + " <gml:curveMember>" + '\n' + " <gml:LineString>" + '\n' + " <gml:posList>35.840973 0.14967346 44.11891 3.6755037</gml:posList>" + '\n' + " </gml:LineString>" + '\n' + " </gml:curveMember>" + '\n' + " <gml:curveMember>" + '\n' + " <gml:LineString>" + '\n' + " <gml:posList>51.174034 12.365124 55.288635 7.583888 56.534782 4.1457024</gml:posList>" + '\n' + " </gml:LineString>" + '\n' + " </gml:curveMember>" + '\n' + "</gml:MultiCurve>" + '\n';
JTSMultiCurve result = (JTSMultiCurve) ((JAXBElement) un.unmarshal(new StringReader(xml))).getValue();
result.applyCRSonChild();
assertEquals(expResult.getElements().iterator().next().getCoordinateReferenceSystem(), result.getElements().iterator().next().getCoordinateReferenceSystem());
assertEquals(((JTSLineString) ((JTSCurve) expResult.getElements().iterator().next()).getSegments().get(0)).getControlPoints().getCoordinateReferenceSystem(), ((JTSLineString) ((JTSCurve) result.getElements().iterator().next()).getSegments().get(0)).getControlPoints().getCoordinateReferenceSystem());
assertEquals(((JTSLineString) ((JTSCurve) expResult.getElements().iterator().next()).getSegments().get(0)).getControlPoints().get(0).getDirectPosition().getCoordinateReferenceSystem(), ((JTSLineString) ((JTSCurve) result.getElements().iterator().next()).getSegments().get(0)).getControlPoints().get(0).getDirectPosition().getCoordinateReferenceSystem());
assertEquals(((JTSLineString) ((JTSCurve) expResult.getElements().iterator().next()).getSegments().get(0)).getControlPoints().get(0), ((JTSLineString) ((JTSCurve) result.getElements().iterator().next()).getSegments().get(0)).getControlPoints().get(0));
assertEquals(((JTSLineString) ((JTSCurve) expResult.getElements().iterator().next()).getSegments().get(0)).getControlPoints(), ((JTSLineString) ((JTSCurve) result.getElements().iterator().next()).getSegments().get(0)).getControlPoints());
assertEquals(((JTSCurve) expResult.getElements().iterator().next()).getSegments().get(0), ((JTSCurve) result.getElements().iterator().next()).getSegments().get(0));
assertEquals(((JTSCurve) expResult.getElements().iterator().next()).getSegments(), ((JTSCurve) result.getElements().iterator().next()).getSegments());
assertEquals(expResult.getElements().iterator().next(), result.getElements().iterator().next());
assertEquals(expResult.getCoordinateReferenceSystem(), result.getCoordinateReferenceSystem());
assertEquals(expResult, result);
}
use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve in project geotoolkit by Geomatys.
the class JTSGeometryBindingTest method MultiCurveMarshalingTest.
/**
* Test Composite curve Marshalling.
*/
@Test
public void MultiCurveMarshalingTest() throws Exception {
CoordinateReferenceSystem crs = CRS.forCode("urn:ogc:def:crs:epsg::4326");
assertTrue(crs != null);
DirectPosition p1 = new GeneralDirectPosition(crs);
p1.setOrdinate(0, 35.840973);
p1.setOrdinate(1, 0.14967346);
DirectPosition p2 = new GeneralDirectPosition(crs);
p2.setOrdinate(0, 44.11891);
p2.setOrdinate(1, 3.6755037);
JTSLineString l1 = new JTSLineString();
l1.getControlPoints().add(p1);
l1.getControlPoints().add(p2);
JTSCurve c2 = new JTSCurve(crs);
c2.getSegments().add(l1);
JTSCurve c1 = new JTSCurve(crs);
JTSLineString l2 = new JTSLineString();
DirectPosition p21 = new GeneralDirectPosition(crs);
p21.setOrdinate(0, 51.174034);
p21.setOrdinate(1, 12.365124);
DirectPosition p22 = new GeneralDirectPosition(crs);
p22.setOrdinate(0, 55.288635);
p22.setOrdinate(1, 7.583888);
DirectPosition p23 = new GeneralDirectPosition(crs);
p23.setOrdinate(0, 56.534782);
p23.setOrdinate(1, 4.1457024);
l2.getControlPoints().add(p21);
l2.getControlPoints().add(p22);
l2.getControlPoints().add(p23);
c1.getSegments().add(l2);
JTSMultiCurve multiCurve = new JTSMultiCurve(crs);
multiCurve.getElements().add(c2);
multiCurve.getElements().add(c1);
StringWriter sw = new StringWriter();
m.marshal(factory.createJTSMultiCurve(multiCurve), sw);
String result = sw.toString();
result = result.replaceAll("(?i)epsg\\:\\d+\\.\\d+\\.?\\d*\\:", "epsg::");
String expResult = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + '\n' + "<gml:MultiCurve xmlns:gml=\"http://www.opengis.net/gml\" srsName=\"urn:ogc:def:crs:epsg::4326\" >" + '\n' + " <gml:curveMember>" + '\n' + " <gml:LineString>" + '\n' + " <gml:posList>35.840973 0.14967346 44.11891 3.6755037</gml:posList>" + '\n' + " </gml:LineString>" + '\n' + " </gml:curveMember>" + '\n' + " <gml:curveMember>" + '\n' + " <gml:LineString>" + '\n' + " <gml:posList>51.174034 12.365124 55.288635 7.583888 56.534782 4.1457024</gml:posList>" + '\n' + " </gml:LineString>" + '\n' + " </gml:curveMember>" + '\n' + "</gml:MultiCurve>" + '\n';
assertXmlEquals(expResult, result, "xmlns:*");
}
use of org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve in project geotoolkit by Geomatys.
the class GeometryMapping method readValue.
@Override
public void readValue(XMLStreamReader reader, GenericName propName, Feature feature) throws XMLStreamException {
final String localName = reader.getLocalName();
if (decorated) {
// check if we are dealing with a link href
String link = reader.getAttributeValue(GMLConvention.XLINK_NAMESPACE, "href");
if (link != null) {
toTagEnd(reader, localName);
Attribute attribute = (Attribute) feature.getProperty(propName.toString());
AttributeType<String> charType = (AttributeType) attribute.getType().characteristics().get(GMLConvention.XLINK_HREF.tip().toString());
Attribute<String> charValue = charType.newInstance();
charValue.setValue(link);
attribute.characteristics().put(GMLConvention.XLINK_HREF.tip().toString(), charValue);
return;
}
}
boolean skipCurrent = decorated;
int event;
Object value;
// backward compatible with incorrect old writings
final String propertyName = propertyType.getName().tip().toString();
if (propertyName.equals(localName)) {
skipCurrent = true;
}
// special case for SurfacePropertyType which may contain a simple polygon
boolean forceMultiPolygon = propertyName.equalsIgnoreCase("multipolygon");
if (skipCurrent) {
event = reader.next();
} else {
event = reader.getEventType();
}
while (event != START_ELEMENT) {
if (event == END_ELEMENT) {
return;
}
event = reader.next();
}
try {
Unmarshaller unmarshaller = pool.acquireUnmarshaller();
final Geometry jtsGeom;
final Object geometry = ((JAXBElement) unmarshaller.unmarshal(reader)).getValue();
if (geometry instanceof JTSGeometry) {
final JTSGeometry isoGeom = (JTSGeometry) geometry;
if (isoGeom instanceof JTSMultiCurve) {
((JTSMultiCurve) isoGeom).applyCRSonChild();
}
jtsGeom = isoGeom.getJTSGeometry();
} else if (geometry instanceof PolygonType) {
final PolygonType polygon = ((PolygonType) geometry);
jtsGeom = polygon.getJTSPolygon().getJTSGeometry();
if (polygon.getCoordinateReferenceSystem() != null) {
JTS.setCRS(jtsGeom, polygon.getCoordinateReferenceSystem());
}
} else if (geometry instanceof LineStringPosListType) {
final JTSLineString line = ((LineStringPosListType) geometry).getJTSLineString();
jtsGeom = line.getJTSGeometry();
if (line.getCoordinateReferenceSystem() != null) {
JTS.setCRS(jtsGeom, line.getCoordinateReferenceSystem());
}
} else if (geometry instanceof AbstractGeometry) {
try {
jtsGeom = GeometrytoJTS.toJTS((AbstractGeometry) geometry, longitudeFirst, forceMultiPolygon);
} catch (FactoryException ex) {
throw new XMLStreamException("Factory Exception while transforming GML object to JTS", ex);
}
} else {
throw new IllegalArgumentException("unexpected geometry type:" + geometry);
}
value = jtsGeom;
value = JTSMapping.convertType(jtsGeom, ((AttributeType) propertyType).getValueClass());
pool.recycle(unmarshaller);
} catch (JAXBException ex) {
String msg = ex.getMessage();
if (msg == null && ex.getLinkedException() != null) {
msg = ex.getLinkedException().getMessage();
}
throw new IllegalArgumentException("JAXB exception while reading the feature geometry: " + msg, ex);
}
JAXPStreamFeatureReader.setValue(feature, propertyType, propName, null, value);
}
Aggregations