Search in sources :

Example 11 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class GmlEncoderv321 method createPosition.

private XmlObject createPosition(Geometry geom, EncodingContext ctx) throws EncodingException {
    String foiId = ctx.<String>get(XmlBeansEncodingFlags.GMLID).orElse(null);
    if (geom instanceof Point) {
        PointType xbPoint = PointType.Factory.newInstance(getXmlOptions());
        xbPoint.setId(getGmlID(geom, foiId));
        createPointFromJtsGeometry((Point) geom, xbPoint);
        if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
            PointDocument xbPointDoc = PointDocument.Factory.newInstance(getXmlOptions());
            xbPointDoc.setPoint(xbPoint);
            return xbPointDoc;
        } else if (ctx.has(XmlBeansEncodingFlags.PROPERTY_TYPE)) {
            GeometryPropertyType geometryPropertyType = GeometryPropertyType.Factory.newInstance(getXmlOptions());
            geometryPropertyType.setAbstractGeometry(xbPoint);
            geometryPropertyType.getAbstractGeometry().substitute(GmlConstants.QN_POINT_32, PointType.type);
            return geometryPropertyType;
        }
        return xbPoint;
    } else if (geom instanceof LineString) {
        LineStringType xbLineString = LineStringType.Factory.newInstance(getXmlOptions());
        xbLineString.setId(getGmlID(geom, foiId));
        createLineStringFromJtsGeometry((LineString) geom, xbLineString);
        if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
            LineStringDocument xbLineStringDoc = LineStringDocument.Factory.newInstance(getXmlOptions());
            xbLineStringDoc.setLineString(xbLineString);
            return xbLineStringDoc;
        } else if (ctx.has(XmlBeansEncodingFlags.PROPERTY_TYPE)) {
            GeometryPropertyType geometryPropertyType = GeometryPropertyType.Factory.newInstance(getXmlOptions());
            geometryPropertyType.setAbstractGeometry(xbLineString);
            geometryPropertyType.getAbstractGeometry().substitute(GmlConstants.QN_LINESTRING_32, LineStringType.type);
            return geometryPropertyType;
        }
        return xbLineString;
    } else if (geom instanceof MultiLineString) {
        MultiCurveType xbMultiCurve = MultiCurveType.Factory.newInstance(getXmlOptions());
        xbMultiCurve.setId(getGmlID(geom, foiId));
        xbMultiCurve.setSrsName(getSrsName(geom));
        for (int i = 0; i < geom.getNumGeometries(); ++i) {
            Geometry lineString = geom.getGeometryN(i);
            LineStringType xbLineString = LineStringType.Factory.newInstance(getXmlOptions());
            xbLineString.setId(getGmlID(geom, foiId));
            xbLineString.addNewPosList().setStringValue(JTSHelper.getCoordinatesString(lineString));
            CurvePropertyType xbCurveMember = xbMultiCurve.addNewCurveMember();
            xbCurveMember.addNewAbstractCurve().set(xbLineString);
            XmlHelper.substituteElement(xbCurveMember.getAbstractCurve(), xbLineString);
        }
        if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
            MultiCurveDocument xbMultiCurveDoc = MultiCurveDocument.Factory.newInstance(getXmlOptions());
            xbMultiCurveDoc.setMultiCurve(xbMultiCurve);
            return xbMultiCurveDoc;
        } else if (ctx.has(XmlBeansEncodingFlags.PROPERTY_TYPE)) {
            GeometryPropertyType xbGeometryProperty = GeometryPropertyType.Factory.newInstance(getXmlOptions());
            xbGeometryProperty.addNewAbstractGeometry().set(xbMultiCurve);
            XmlHelper.substituteElement(xbGeometryProperty.getAbstractGeometry(), xbMultiCurve);
            return xbGeometryProperty;
        } else {
            return xbMultiCurve;
        }
    } else if (geom instanceof Polygon) {
        PolygonType xbPolygon = PolygonType.Factory.newInstance(getXmlOptions());
        xbPolygon.setId(getGmlID(geom, foiId));
        createPolygonFromJtsGeometry((Polygon) geom, xbPolygon);
        if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
            PolygonDocument xbPolygonDoc = PolygonDocument.Factory.newInstance(getXmlOptions());
            xbPolygonDoc.setPolygon(xbPolygon);
            return xbPolygonDoc;
        } else if (ctx.has(XmlBeansEncodingFlags.PROPERTY_TYPE)) {
            GeometryPropertyType geometryPropertyType = GeometryPropertyType.Factory.newInstance(getXmlOptions());
            geometryPropertyType.setAbstractGeometry(xbPolygon);
            geometryPropertyType.getAbstractGeometry().substitute(GmlConstants.QN_POLYGON_32, PolygonType.type);
            return geometryPropertyType;
        }
        return xbPolygon;
    } else if (geom instanceof MultiPoint) {
        MultiPointType xbMultiPoint = MultiPointType.Factory.newInstance(getXmlOptions());
        String id = getGmlID(geom, foiId);
        xbMultiPoint.setId(id);
        createMultiPointFromJtsGeometry((MultiPoint) geom, xbMultiPoint, id);
        if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
            MultiPointDocument xbMultiPointDoc = MultiPointDocument.Factory.newInstance(getXmlOptions());
            xbMultiPointDoc.setMultiPoint(xbMultiPoint);
            return xbMultiPointDoc;
        } else if (ctx.has(XmlBeansEncodingFlags.PROPERTY_TYPE)) {
            GeometryPropertyType geometryPropertyType = GeometryPropertyType.Factory.newInstance(getXmlOptions());
            geometryPropertyType.setAbstractGeometry(xbMultiPoint);
            geometryPropertyType.getAbstractGeometry().substitute(GmlConstants.QN_MULTI_POINT_32, PolygonType.type);
            return geometryPropertyType;
        }
        return xbMultiPoint;
    } else {
        throw new UnsupportedEncoderInputException(this, geom);
    }
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) MultiLineString(org.locationtech.jts.geom.MultiLineString) PolygonDocument(net.opengis.gml.x32.PolygonDocument) MultiPointDocument(net.opengis.gml.x32.MultiPointDocument) PointDocument(net.opengis.gml.x32.PointDocument) MultiCurveType(net.opengis.gml.x32.MultiCurveType) MultiCurveDocument(net.opengis.gml.x32.MultiCurveDocument) PolygonType(net.opengis.gml.x32.PolygonType) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) LineStringType(net.opengis.gml.x32.LineStringType) MultiPointType(net.opengis.gml.x32.MultiPointType) MultiPointDocument(net.opengis.gml.x32.MultiPointDocument) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException) EnvelopeOrGeometry(org.n52.shetland.util.EnvelopeOrGeometry) Geometry(org.locationtech.jts.geom.Geometry) AbstractGeometry(org.n52.shetland.ogc.gml.AbstractGeometry) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) LineStringDocument(net.opengis.gml.x32.LineStringDocument) MultiPointType(net.opengis.gml.x32.MultiPointType) PointType(net.opengis.gml.x32.PointType) CurvePropertyType(net.opengis.gml.x32.CurvePropertyType) Polygon(org.locationtech.jts.geom.Polygon) GeometryPropertyType(net.opengis.gml.x32.GeometryPropertyType)

Example 12 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class GmlEncoderv321 method createGenericMetaData.

private XmlObject createGenericMetaData(GenericMetaData element, EncodingContext ctx) throws EncodingException {
    GenericMetaDataDocument gmdd = GenericMetaDataDocument.Factory.newInstance(getXmlOptions());
    GenericMetaDataType gmdt = gmdd.addNewGenericMetaData();
    if (element.getContent() instanceof HasDefaultEncoding && ((HasDefaultEncoding<?>) element.getContent()).isSetDefaultElementEncoding()) {
        // TODO check
        gmdt.set(encodeObjectToXml(((HasDefaultEncoding<?>) element.getContent()).getDefaultElementEncoding(), element.getContent(), new EncodingContext().with(XmlBeansEncodingFlags.PROPERTY_TYPE, true)));
    }
    if (ctx.has(XmlBeansEncodingFlags.DOCUMENT)) {
        return gmdd;
    }
    return gmdt;
}
Also used : GenericMetaDataDocument(net.opengis.gml.x32.GenericMetaDataDocument) GenericMetaDataType(net.opengis.gml.x32.GenericMetaDataType) HasDefaultEncoding(org.n52.shetland.ogc.HasDefaultEncoding)

Example 13 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class GetDataAvailabilityXmlEncoder method create.

@Override
protected XmlObject create(GetDataAvailabilityResponse response) throws EncodingException {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EncodingContext ctx = EncodingContext.empty().with(EncoderFlags.ENCODER_REPOSITORY, getEncoderRepository()).with(XmlEncoderFlags.XML_OPTIONS, (Supplier<XmlOptions>) this::getXmlOptions);
        if (GetDataAvailabilityConstants.NS_GDA.equals(response.getResponseFormat())) {
            new GetDataAvailabilityStreamWriter(ctx, baos, response.getDataAvailabilities()).write();
        } else if (GetDataAvailabilityConstants.NS_GDA_20.equals(response.getResponseFormat())) {
            new GetDataAvailabilityV20StreamWriter(ctx, baos, response.getDataAvailabilities()).write();
        }
        XmlObject encodedObject = XmlObject.Factory.parse(baos.toString("UTF8"));
        XmlHelper.validateDocument(encodedObject, EncodingException::new);
        return encodedObject;
    } catch (XMLStreamException | XmlException | UnsupportedEncodingException ex) {
        throw new EncodingException("Error encoding response", ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EncodingException(org.n52.svalbard.encode.exception.EncodingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XmlException(org.apache.xmlbeans.XmlException) GetDataAvailabilityV20StreamWriter(org.n52.svalbard.write.GetDataAvailabilityV20StreamWriter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Supplier(java.util.function.Supplier) XmlObject(org.apache.xmlbeans.XmlObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GetDataAvailabilityStreamWriter(org.n52.svalbard.write.GetDataAvailabilityStreamWriter)

Example 14 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class GetObservationByIdResponseEncoder method createResponse.

@Override
protected XmlObject createResponse(ObservationEncoder<XmlObject, OmObservation> encoder, GetObservationByIdResponse response) throws EncodingException {
    GetObservationByIdResponseDocument doc = GetObservationByIdResponseDocument.Factory.newInstance(getXmlOptions());
    GetObservationByIdResponseType xbResponse = doc.addNewGetObservationByIdResponse();
    ObservationStream observations = getObservationsAndCheckForStreaming(response, encoder);
    HashMap<CodeWithAuthority, String> gmlID4sfIdentifier = new HashMap<>();
    try {
        while (observations.hasNext()) {
            OmObservation observation = observations.next();
            EncodingContext codingContext = EncodingContext.empty();
            CodeWithAuthority foiId = observation.getObservationConstellation().getFeatureOfInterest().getIdentifierCodeWithAuthority();
            if (gmlID4sfIdentifier.containsKey(foiId)) {
                codingContext = codingContext.with(XmlBeansEncodingFlags.EXIST_FOI_IN_DOC, true);
            } else {
                gmlID4sfIdentifier.put(foiId, GML_ID);
                codingContext = codingContext.with(XmlBeansEncodingFlags.EXIST_FOI_IN_DOC, false);
            }
            codingContext = codingContext.with(XmlBeansEncodingFlags.GMLID, gmlID4sfIdentifier.get(foiId));
            xbResponse.addNewObservation().addNewOMObservation().set(encoder.encode(observation, codingContext));
        }
    } catch (OwsExceptionReport ex) {
        throw new EncodingException(ex);
    }
    XmlHelper.makeGmlIdsUnique(xbResponse.getDomNode());
    return doc;
}
Also used : ObservationStream(org.n52.shetland.ogc.om.ObservationStream) HashMap(java.util.HashMap) EncodingException(org.n52.svalbard.encode.exception.EncodingException) OmObservation(org.n52.shetland.ogc.om.OmObservation) GetObservationByIdResponseDocument(net.opengis.sos.x20.GetObservationByIdResponseDocument) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) GetObservationByIdResponseType(net.opengis.sos.x20.GetObservationByIdResponseType)

Example 15 with EncodingContext

use of org.n52.svalbard.encode.EncodingContext in project arctic-sea by 52North.

the class GetObservationResponseEncoder method createResponse.

@Override
protected void createResponse(ObservationEncoder<XmlObject, OmObservation> encoder, GetObservationResponse response, OutputStream outputStream, EncodingContext ctx) throws EncodingException {
    try {
        EncodingContext context = ctx.with(EncoderFlags.ENCODER_REPOSITORY, getEncoderRepository()).with(XmlEncoderFlags.XML_OPTIONS, (Supplier<XmlOptions>) this::getXmlOptions).with(StreamingEncoderFlags.ENCODER, this);
        new GetObservationResponseXmlStreamWriter(context, outputStream, response).write();
    } catch (XMLStreamException xmlse) {
        throw new EncodingException(xmlse);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EncodingException(org.n52.svalbard.encode.exception.EncodingException) XmlOptions(org.apache.xmlbeans.XmlOptions) GetObservationResponseXmlStreamWriter(org.n52.svalbard.write.GetObservationResponseXmlStreamWriter)

Aggregations

EncodingException (org.n52.svalbard.encode.exception.EncodingException)26 XmlObject (org.apache.xmlbeans.XmlObject)24 UnsupportedEncoderInputException (org.n52.svalbard.encode.exception.UnsupportedEncoderInputException)15 XmlException (org.apache.xmlbeans.XmlException)9 XMLStreamException (javax.xml.stream.XMLStreamException)6 OmObservation (org.n52.shetland.ogc.om.OmObservation)6 Supplier (java.util.function.Supplier)5 Time (org.n52.shetland.ogc.gml.time.Time)5 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AbstractCRSType (net.opengis.gml.x32.AbstractCRSType)4 CodeType (net.opengis.gml.x32.CodeType)4 FeaturePropertyType (net.opengis.gml.x32.FeaturePropertyType)4 DateTime (org.joda.time.DateTime)4 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)4 TimePeriod (org.n52.shetland.ogc.gml.time.TimePeriod)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 AbstractGeometryType (net.opengis.gml.x32.AbstractGeometryType)3 GeometryPropertyType (net.opengis.gml.x32.GeometryPropertyType)3