Search in sources :

Example 1 with PolygonType

use of org.geotoolkit.internal.jaxb.PolygonType 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);
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Attribute(org.opengis.feature.Attribute) FactoryException(org.opengis.util.FactoryException) JAXBException(javax.xml.bind.JAXBException) PolygonType(org.geotoolkit.internal.jaxb.PolygonType) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) JAXBElement(javax.xml.bind.JAXBElement) JTSMultiCurve(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve) LineStringPosListType(org.geotoolkit.internal.jaxb.LineStringPosListType) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) JTSGeometry(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.JTSGeometry) Geometry(org.locationtech.jts.geom.Geometry) XMLStreamException(javax.xml.stream.XMLStreamException) AttributeType(org.opengis.feature.AttributeType) Unmarshaller(javax.xml.bind.Unmarshaller) JTSGeometry(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.JTSGeometry)

Example 2 with PolygonType

use of org.geotoolkit.internal.jaxb.PolygonType in project geotoolkit by Geomatys.

the class JTSGeometryBindingTest method PolygonUnmarshalingTest.

/**
 * Test Ring Unmarshalling.
 */
@Test
public void PolygonUnmarshalingTest() throws Exception {
    CoordinateReferenceSystem crs = CRS.forCode("urn:ogc:def:crs:epsg::27572");
    assertTrue(crs != null);
    // EXTERIOR
    JTSRing exterior1 = new JTSRing(crs);
    JTSCurve c1 = new JTSCurve(crs);
    JTSLineString c1l1 = new JTSLineString(crs);
    DirectPosition c1l1p1 = new GeneralDirectPosition(crs);
    c1l1p1.setOrdinate(0, 401500);
    c1l1p1.setOrdinate(1, 3334500);
    DirectPosition c1l1p2 = new GeneralDirectPosition(crs);
    c1l1p2.setOrdinate(0, 401700);
    c1l1p2.setOrdinate(1, 3334850);
    DirectPosition c1l1p3 = new GeneralDirectPosition(crs);
    c1l1p3.setOrdinate(0, 402200);
    c1l1p3.setOrdinate(1, 3335200);
    c1l1.getControlPoints().add(c1l1p1);
    c1l1.getControlPoints().add(c1l1p2);
    c1l1.getControlPoints().add(c1l1p3);
    DirectPosition c1l2p1 = new GeneralDirectPosition(crs);
    c1l2p1.setOrdinate(0, 402320);
    c1l2p1.setOrdinate(1, 3334850);
    DirectPosition c1l2p2 = new GeneralDirectPosition(crs);
    c1l2p2.setOrdinate(0, 402200);
    c1l2p2.setOrdinate(1, 3335200);
    c1l1.getControlPoints().add(c1l2p1);
    c1l1.getControlPoints().add(c1l2p2);
    c1.getSegments().add(c1l1);
    exterior1.getElements().add(c1);
    // INTERIOR
    Ring[] interiors1 = new Ring[1];
    JTSRing interior1 = new JTSRing(crs);
    JTSCurve c2 = new JTSCurve(crs);
    JTSLineString c2l1 = new JTSLineString(crs);
    DirectPosition c2l1p1 = new GeneralDirectPosition(crs);
    c2l1p1.setOrdinate(0, 401500);
    c2l1p1.setOrdinate(1, 3334500);
    DirectPosition c2l1p2 = new GeneralDirectPosition(crs);
    c2l1p2.setOrdinate(0, 401700);
    c2l1p2.setOrdinate(1, 3334850);
    DirectPosition c2l1p3 = new GeneralDirectPosition(crs);
    c2l1p3.setOrdinate(0, 402200);
    c2l1p3.setOrdinate(1, 3335200);
    c2l1.getControlPoints().add(c2l1p1);
    c2l1.getControlPoints().add(c2l1p2);
    c2l1.getControlPoints().add(c2l1p3);
    c2.getSegments().add(c2l1);
    interior1.getElements().add(c2);
    interiors1[0] = interior1;
    JTSSurfaceBoundary bound1 = new JTSSurfaceBoundary(crs, exterior1, interiors1);
    JTSPolygon expResult = new JTSPolygon(bound1);
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + '\n' + "<gml:Polygon srsName=\"urn:ogc:def:crs:epsg::27572\" xmlns:gml=\"http://www.opengis.net/gml\">" + '\n' + "  <gml:exterior>" + '\n' + "    <gml:LinearRing>" + '\n' + "      <gml:posList>401500.0 3334500.0 401700.0 3334850.0 402200.0 3335200.0 402320.0 3334850.0 402200.0 3335200.0</gml:posList>" + '\n' + "    </gml:LinearRing>" + '\n' + "  </gml:exterior>" + '\n' + "  <gml:interior>" + '\n' + "    <gml:LinearRing>" + '\n' + "      <gml:posList>401500.0 3334500.0 401700.0 3334850.0 402200.0 3335200.0</gml:posList>" + '\n' + "    </gml:LinearRing>" + '\n' + "  </gml:interior>" + '\n' + "</gml:Polygon>" + '\n';
    PolygonType temp = (PolygonType) ((JAXBElement) un.unmarshal(new StringReader(xml))).getValue();
    JTSPolygon result = temp.getJTSPolygon();
    assertEquals(expResult, result);
    xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + '\n' + "<gml:Polygon srsName=\"urn:ogc:def:crs:epsg::27572\" xmlns:gml=\"http://www.opengis.net/gml\">" + '\n' + "  <gml:outerBoundaryIs>" + '\n' + "    <gml:LinearRing>" + '\n' + "      <gml:posList>401500.0 3334500.0 401700.0 3334850.0 402200.0 3335200.0 402320.0 3334850.0 402200.0 3335200.0</gml:posList>" + '\n' + "    </gml:LinearRing>" + '\n' + "  </gml:outerBoundaryIs>" + '\n' + "  <gml:innerBoundaryIs>" + '\n' + "    <gml:LinearRing>" + '\n' + "      <gml:posList>401500.0 3334500.0 401700.0 3334850.0 402200.0 3335200.0</gml:posList>" + '\n' + "    </gml:LinearRing>" + '\n' + "  </gml:innerBoundaryIs>" + '\n' + "</gml:Polygon>" + '\n';
    temp = (PolygonType) ((JAXBElement) un.unmarshal(new StringReader(xml))).getValue();
    result = temp.getJTSPolygon();
    assertEquals(expResult, result);
}
Also used : GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) DirectPosition(org.opengis.geometry.DirectPosition) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) JTSRing(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSRing) JTSSurfaceBoundary(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSSurfaceBoundary) JTSPolygon(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSPolygon) PolygonType(org.geotoolkit.internal.jaxb.PolygonType) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) JAXBElement(javax.xml.bind.JAXBElement) JTSCurve(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSCurve) JTSLineString(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString) JTSRing(org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSRing) Ring(org.opengis.geometry.primitive.Ring) StringReader(java.io.StringReader) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)2 JTSLineString (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSLineString)2 PolygonType (org.geotoolkit.internal.jaxb.PolygonType)2 StringReader (java.io.StringReader)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 GeneralDirectPosition (org.apache.sis.geometry.GeneralDirectPosition)1 JTSGeometry (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.JTSGeometry)1 JTSMultiCurve (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.aggregate.JTSMultiCurve)1 JTSPolygon (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.geometry.JTSPolygon)1 JTSCurve (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSCurve)1 JTSRing (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSRing)1 JTSSurfaceBoundary (org.geotoolkit.geometry.isoonjts.spatialschema.geometry.primitive.JTSSurfaceBoundary)1 AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)1 LineStringPosListType (org.geotoolkit.internal.jaxb.LineStringPosListType)1 Geometry (org.locationtech.jts.geom.Geometry)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 MultiPoint (org.locationtech.jts.geom.MultiPoint)1