Search in sources :

Example 6 with ObservationStream

use of org.n52.shetland.ogc.om.ObservationStream in project arctic-sea by 52North.

the class AqdEncoder method encodeGetObservationResponse.

private XmlObject encodeGetObservationResponse(GetObservationResponse response, EncodingContext ctx) throws EncodingException {
    try {
        FeatureCollection featureCollection = getFeatureCollection(response);
        // TODO get FLOW from response
        EReportingHeader eReportingHeader = getEReportingHeader(getReportObligationType(response));
        featureCollection.addMember(eReportingHeader);
        TimePeriod timePeriod = new TimePeriod();
        TimeInstant resultTime = new TimeInstant(new DateTime(DateTimeZone.UTC));
        int counter = 1;
        ObservationStream observationCollection = response.getObservationCollection();
        while (observationCollection.hasNext()) {
            OmObservation observation = observationCollection.next();
            if (observation.getValue() instanceof ObservationStream) {
                ObservationStream value = (ObservationStream) observation.getValue();
                if (value instanceof StreamingValue) {
                    value = value.merge();
                }
                while (value.hasNext()) {
                    processObservation(value.next(), timePeriod, resultTime, featureCollection, eReportingHeader, counter++);
                }
            } else {
                processObservation(observation, timePeriod, resultTime, featureCollection, eReportingHeader, counter++);
            }
        }
        if (!timePeriod.isEmpty()) {
            eReportingHeader.setReportingPeriod(Referenceable.of((Time) timePeriod));
        }
        return encodeObjectToXml(GmlConstants.NS_GML_32, featureCollection, ctx.with(XmlEncoderFlags.ENCODE_NAMESPACE, OmConstants.NS_OM_2).with(XmlBeansEncodingFlags.DOCUMENT));
    } catch (OwsExceptionReport ex) {
        throw new EncodingException(ex);
    }
}
Also used : FeatureCollection(org.n52.shetland.ogc.om.features.FeatureCollection) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) StreamingValue(org.n52.shetland.ogc.om.StreamingValue) EncodingException(org.n52.svalbard.encode.exception.EncodingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AbstractEReportingHeader(org.n52.shetland.aqd.AbstractEReportingHeader) EReportingHeader(org.n52.shetland.aqd.EReportingHeader) TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) OmObservation(org.n52.shetland.ogc.om.OmObservation) Time(org.n52.shetland.ogc.gml.time.Time) DateTime(org.joda.time.DateTime) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant) DateTime(org.joda.time.DateTime)

Example 7 with ObservationStream

use of org.n52.shetland.ogc.om.ObservationStream in project arctic-sea by 52North.

the class AqdGetObservationResponseXmlStreamWriter method writeFeatureCollectionDoc.

private void writeFeatureCollectionDoc() throws XMLStreamException, EncodingException {
    start(GmlConstants.QN_FEATURE_COLLECTION_32);
    addNamespaces();
    addSchemaLocations();
    FeatureCollection featureCollection = getElement();
    addGmlId(featureCollection.getGmlId());
    TimeInstant resultTime = new TimeInstant(new DateTime(DateTimeZone.UTC));
    for (AbstractFeature abstractFeature : featureCollection.getMembers().values()) {
        long start = System.currentTimeMillis();
        Encoder<XmlObject, AbstractFeature> encoder = getEncoder(abstractFeature);
        if (abstractFeature instanceof OmObservation) {
            OmObservation observation = (OmObservation) abstractFeature;
            if (observation.getValue() instanceof ObservationStream) {
                try {
                    // start the timer task to write blank strings to avoid
                    // connection closing
                    startTimer();
                    ObservationStream mergeObservation = ((ObservationStream) observation.getValue()).merge();
                    LOGGER.debug("Observation processing requires {} ms", System.currentTimeMillis() - start);
                    int count = 0;
                    while (mergeObservation.hasNext()) {
                        OmObservation omObservation = mergeObservation.next();
                        if (abstractFeature.isSetGmlID()) {
                            if (count == 0) {
                                omObservation.setGmlId(abstractFeature.getGmlId());
                            } else {
                                omObservation.setGmlId(abstractFeature.getGmlId() + "_" + count);
                            }
                            count++;
                        }
                        omObservation.setResultTime(resultTime);
                        String xmlTextObservation = prepareObservation(omObservation, encoder);
                        // stop the timer task
                        stopTimer();
                        writeMember(xmlTextObservation);
                    }
                } catch (OwsExceptionReport ex) {
                    throw new EncodingException(ex);
                }
            } else {
                if (encoder instanceof XmlStreamWriter) {
                    encoder.encode(abstractFeature, getContext());
                } else {
                    writeMember(abstractFeature, encoder);
                }
            }
        } else {
            writeMember(abstractFeature, encoder);
        }
        LOGGER.debug("Writing member requires {} ms", System.currentTimeMillis() - start);
    }
    end(GmlConstants.QN_FEATURE_COLLECTION_32);
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) OmObservation(org.n52.shetland.ogc.om.OmObservation) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) DateTime(org.joda.time.DateTime) FeatureCollection(org.n52.shetland.ogc.om.features.FeatureCollection) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) XmlObject(org.apache.xmlbeans.XmlObject) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 8 with ObservationStream

use of org.n52.shetland.ogc.om.ObservationStream in project arctic-sea by 52North.

the class GetObservationResponseXmlStreamWriter method writeGetObservationResponseDoc.

private void writeGetObservationResponseDoc() throws XMLStreamException, EncodingException {
    start(Sos2StreamingConstants.GET_OBSERVATION_RESPONSE);
    namespace(W3CConstants.NS_XLINK_PREFIX, W3CConstants.NS_XLINK);
    namespace(SosConstants.NS_SOS_PREFIX, Sos2Constants.NS_SOS_20);
    GetObservationResponse response = getElement();
    // get observation encoder
    ObservationEncoder<XmlObject, OmObservation> encoder = findObservationEncoder(response.getResponseFormat());
    // write schemaLocation
    schemaLocation(getSchemaLocation(encoder));
    EncodingContext ctx = getContext().with(XmlEncoderFlags.ENCODE_NAMESPACE, response.getResponseFormat()).with(XmlBeansEncodingFlags.DOCUMENT).with(StreamingEncoderFlags.EMBEDDED).without(XmlBeansEncodingFlags.PROPERTY_TYPE).without(XmlBeansEncodingFlags.TYPE);
    try {
        ObservationStream stream = response.getObservationCollection();
        if (encoder.shouldObservationsWithSameXBeMerged()) {
            stream = stream.merge();
        }
        while (stream.hasNext()) {
            OmObservation o = stream.next();
            if (o.getValue() instanceof ObservationStream) {
                ObservationStream value = (ObservationStream) o.getValue();
                if (encoder.supportsResultStreamingForMergedValues()) {
                    writeObservationData(ctx, o, encoder);
                } else {
                    while (value.hasNext()) {
                        writeObservationData(ctx, value.next(), encoder);
                    }
                }
            } else {
                writeObservationData(ctx, o, encoder);
            }
        }
    } catch (OwsExceptionReport owse) {
        throw new EncodingException(owse);
    }
    end(Sos2StreamingConstants.GET_OBSERVATION_RESPONSE);
}
Also used : EncodingContext(org.n52.svalbard.encode.EncodingContext) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) EncodingException(org.n52.svalbard.encode.exception.EncodingException) GetObservationResponse(org.n52.shetland.ogc.sos.response.GetObservationResponse) OmObservation(org.n52.shetland.ogc.om.OmObservation) XmlObject(org.apache.xmlbeans.XmlObject) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport)

Example 9 with ObservationStream

use of org.n52.shetland.ogc.om.ObservationStream in project arctic-sea by 52North.

the class AbstractWmlEncoderv20 method createWmlGetObservationResponse.

/**
 * Encodes a SOS GetObservationResponse to a single WaterML 2.0 observation
 * or to a WaterML 1.0 ObservationCollection
 *
 * @param getObservationResonse
 *            SOS GetObservationResponse
 * @return Encoded response
 * @throws EncodingException
 *             If an error occurs
 */
protected XmlObject createWmlGetObservationResponse(GetObservationResponse getObservationResonse) throws EncodingException {
    // TODO: set schemaLocation if final
    Map<CodeWithAuthority, String> gmlID4sfIdentifier = Maps.newHashMap();
    int sfIdCounter = 1;
    try {
        if (getObservationResonse.getObservationCollection() != null && !getObservationResonse.getObservationCollection().hasNext()) {
            ObservationStream observations = getObservationResonse.getObservationCollection();
            OmObservation observation = observations.next();
            if (!observations.hasNext()) {
                OMObservationDocument omObservationDoc = OMObservationDocument.Factory.newInstance(getXmlOptions());
                omObservationDoc.setOMObservation(encodeObservation(observation, gmlID4sfIdentifier, sfIdCounter));
                sfIdCounter++;
                return omObservationDoc;
            } else {
                CollectionDocument xmlCollectionDoc = CollectionDocument.Factory.newInstance(getXmlOptions());
                CollectionType wmlCollection = xmlCollectionDoc.addNewCollection();
                wmlCollection.addNewObservationMember().setOMObservation(encodeObservation(observation, gmlID4sfIdentifier, sfIdCounter));
                sfIdCounter++;
                while (observations.hasNext()) {
                    wmlCollection.addNewObservationMember().setOMObservation(encodeObservation(observations.next(), gmlID4sfIdentifier, sfIdCounter));
                    sfIdCounter++;
                }
                return xmlCollectionDoc;
            }
        } else {
            // TODO: HydrologieProfile-Exception
            throw new EncodingException("Combination does not exists!");
        }
    } catch (NoSuchElementException | OwsExceptionReport e) {
        throw new EncodingException(e);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) CollectionDocument(net.opengis.waterml.x20.CollectionDocument) OmObservation(org.n52.shetland.ogc.om.OmObservation) OMObservationDocument(net.opengis.om.x20.OMObservationDocument) WmlMonitoringPoint(org.n52.shetland.ogc.om.series.wml.WmlMonitoringPoint) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) CollectionType(net.opengis.waterml.x20.CollectionType) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

EncodingException (org.n52.svalbard.encode.exception.EncodingException)9 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)8 ObservationStream (org.n52.shetland.ogc.om.ObservationStream)7 OmObservation (org.n52.shetland.ogc.om.OmObservation)7 XmlObject (org.apache.xmlbeans.XmlObject)3 DateTime (org.joda.time.DateTime)3 CodeWithAuthority (org.n52.shetland.ogc.gml.CodeWithAuthority)2 TimeInstant (org.n52.shetland.ogc.gml.time.TimeInstant)2 TimePeriod (org.n52.shetland.ogc.gml.time.TimePeriod)2 StreamingValue (org.n52.shetland.ogc.om.StreamingValue)2 FeatureCollection (org.n52.shetland.ogc.om.features.FeatureCollection)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 NoSuchElementException (java.util.NoSuchElementException)1