Search in sources :

Example 11 with FeatureCollection

use of org.n52.shetland.ogc.om.features.FeatureCollection 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 12 with FeatureCollection

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

the class GetFeatureOfInterestXmlStreamWriter method writeGetFeatureOfInterestResponseDoc.

private void writeGetFeatureOfInterestResponseDoc() throws XMLStreamException, EncodingException {
    start(Sos2StreamingConstants.QN_GET_FEATURE_OF_INTEREST_RESPONSE);
    namespace(W3CConstants.NS_XLINK_PREFIX, W3CConstants.NS_XLINK);
    namespace(Sos2Constants.NS_SOS_PREFIX, Sos2Constants.NS_SOS_20);
    // write schemaLocation
    schemaLocation(getSchemaLocation());
    AbstractFeature feature = getElement().getAbstractFeature();
    if (feature instanceof FeatureCollection) {
        for (AbstractFeature f : (FeatureCollection) feature) {
            writeFeatureMember(f);
        }
    } else if (feature instanceof AbstractSamplingFeature) {
        writeFeatureMember(feature);
    }
    end(Sos2StreamingConstants.QN_GET_FEATURE_OF_INTEREST_RESPONSE);
}
Also used : AbstractSamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature) FeatureCollection(org.n52.shetland.ogc.om.features.FeatureCollection) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature)

Example 13 with FeatureCollection

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

the class FeatureCollectionTest method should_remove_member_from_feature_collection.

@Test
public final void should_remove_member_from_feature_collection() {
    final FeatureCollection features = new FeatureCollection();
    final String feature1Id = "feature-1";
    final SamplingFeature feature1 = new SamplingFeature(new CodeWithAuthority(feature1Id));
    features.addMember(feature1);
    final String feature2Id = "feature-2";
    final SamplingFeature feature2 = new SamplingFeature(new CodeWithAuthority(feature2Id));
    features.addMember(feature2);
    final SamplingFeature removedFeature = (SamplingFeature) features.removeMember(feature2Id);
    assertThat(removedFeature, is(equalTo(feature2)));
    assertThat(features.getMembers().size(), is(1));
    assertThat(features.getMembers().containsKey(feature2Id), is(FALSE));
    assertThat(features.getMembers().containsValue(feature2), is(FALSE));
    assertThat(features.getMembers().containsKey(feature1Id), is(TRUE));
    assertThat(features.getMembers().containsValue(feature1), is(TRUE));
}
Also used : SamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.SamplingFeature) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority) Test(org.junit.Test)

Example 14 with FeatureCollection

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

the class AbstractWmlEncoderv20 method createMonitoringPoint.

/**
 * Creates a WaterML 2.0 MonitoringPoint XML object from SOS feature object
 *
 * @param absFeature
 *            SOS feature
 * @return WaterML 2.0 MonitoringPoint XML object
 * @throws EncodingException
 *             If an error occurs
 */
protected XmlObject createMonitoringPoint(AbstractFeature absFeature) throws EncodingException {
    if (absFeature instanceof AbstractSamplingFeature) {
        AbstractSamplingFeature sampFeat = (AbstractSamplingFeature) absFeature;
        StringBuilder builder = new StringBuilder();
        builder.append("mp_");
        builder.append(JavaHelper.generateID(absFeature.getIdentifierCodeWithAuthority().getValue()));
        absFeature.setGmlId(builder.toString());
        MonitoringPointDocument monitoringPointDoc = MonitoringPointDocument.Factory.newInstance(getXmlOptions());
        if (sampFeat.isSetXml()) {
            try {
                XmlObject feature = XmlObject.Factory.parse(sampFeat.getXml());
                if (XmlHelper.getNamespace(feature).equals(WaterMLConstants.NS_WML_20)) {
                    if (feature instanceof MonitoringPointDocument) {
                        monitoringPointDoc = (MonitoringPointDocument) feature;
                    } else if (feature instanceof MonitoringPointType) {
                        monitoringPointDoc.setSFSpatialSamplingFeature((MonitoringPointType) feature);
                    }
                    XmlHelper.updateGmlIDs(monitoringPointDoc.getDomNode(), absFeature.getGmlId(), null);
                    return monitoringPointDoc;
                }
            } catch (XmlException xmle) {
                throw new EncodingException("Error while encoding GetFeatureOfInterest response, invalid samplingFeature description!", xmle);
            }
        }
        MonitoringPointType mpt = monitoringPointDoc.addNewMonitoringPoint();
        // set gml:id
        mpt.setId(absFeature.getGmlId());
        if (sampFeat.isSetIdentifier()) {
            XmlObject xmlObject = encodeGML(sampFeat.getIdentifierCodeWithAuthority());
            if (xmlObject != null) {
                mpt.addNewIdentifier().set(xmlObject);
            }
        }
        if (sampFeat.isSetName()) {
            for (CodeType sosName : sampFeat.getName()) {
                mpt.addNewName().set(encodeGML(sosName));
            }
        }
        if (sampFeat.isSetDescription()) {
            if (!mpt.isSetDescription()) {
                mpt.addNewDescription();
            }
            mpt.getDescription().setStringValue(sampFeat.getDescription());
        }
        // TODO: CHECK
        if (sampFeat.getSampledFeatures() != null && !sampFeat.getSampledFeatures().isEmpty()) {
            if (sampFeat.getSampledFeatures().size() == 1) {
                XmlObject encodeObjectToXml = encodeObjectToXml(GmlConstants.NS_GML_32, sampFeat.getSampledFeatures().get(0));
                mpt.addNewSampledFeature().set(encodeObjectToXml);
            } else {
                FeatureCollection featureCollection = new FeatureCollection();
                featureCollection.setGmlId("sampledFeatures_" + absFeature.getGmlId());
                for (AbstractFeature sampledFeature : sampFeat.getSampledFeatures()) {
                    featureCollection.addMember(sampledFeature);
                }
                XmlObject encodeObjectToXml = encodeGML(featureCollection);
                mpt.addNewSampledFeature().set(encodeObjectToXml);
            }
        } else {
            mpt.addNewSampledFeature().setHref(GmlConstants.NIL_UNKNOWN);
        }
        if (sampFeat.isSetParameter()) {
            addParameter(mpt, sampFeat);
        }
        // set position
        ShapeType xbShape = mpt.addNewShape();
        Encoder<XmlObject, Geometry> encoder = getEncoder(getEncoderKey(GmlConstants.NS_GML_32, sampFeat.getGeometry()));
        if (encoder != null) {
            XmlObject xmlObject = encoder.encode(sampFeat.getGeometry(), new EncodingContext().with(XmlBeansEncodingFlags.GMLID, absFeature.getGmlId()));
            xbShape.addNewAbstractGeometry().set(xmlObject);
            XmlHelper.substituteElement(xbShape.getAbstractGeometry(), xmlObject);
        } else {
            throw new EncodingException("Error while encoding geometry for feature, needed encoder is missing!");
        }
        if (absFeature instanceof WmlMonitoringPoint) {
            addMonitoringPointValues(mpt, (WmlMonitoringPoint) absFeature);
        }
        sampFeat.wasEncoded();
        return monitoringPointDoc;
    }
    throw new UnsupportedEncoderInputException(this, absFeature);
}
Also used : MonitoringPointType(net.opengis.waterml.x20.MonitoringPointType) MonitoringPointDocument(net.opengis.waterml.x20.MonitoringPointDocument) AbstractSamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature) EncodingException(org.n52.svalbard.encode.exception.EncodingException) ShapeType(net.opengis.samplingSpatial.x20.ShapeType) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) WmlMonitoringPoint(org.n52.shetland.ogc.om.series.wml.WmlMonitoringPoint) UnsupportedEncoderInputException(org.n52.svalbard.encode.exception.UnsupportedEncoderInputException) Geometry(org.locationtech.jts.geom.Geometry) FeatureCollection(org.n52.shetland.ogc.om.features.FeatureCollection) XmlException(org.apache.xmlbeans.XmlException) CodeType(org.n52.shetland.ogc.gml.CodeType) XmlObject(org.apache.xmlbeans.XmlObject)

Example 15 with FeatureCollection

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

the class AqdEncoder method processObservation.

protected void processObservation(OmObservation observation, TimePeriod timePeriod, TimeInstant resultTime, FeatureCollection featureCollection, AbstractEReportingHeader eReportingHeader, int counter) {
    if (observation.isSetPhenomenonTime()) {
        // generate gml:id
        observation.setGmlId(getObservationId(counter));
        // add xlink:href to eReportingHeader.content
        eReportingHeader.addContent((AbstractFeature) new OmObservation().setIdentifier(new CodeWithAuthority(getObservationXlink(observation.getGmlId()))));
        timePeriod.extendToContain(observation.getPhenomenonTime());
        observation.setResultTime(resultTime);
        featureCollection.addMember(observation);
    }
}
Also used : OmObservation(org.n52.shetland.ogc.om.OmObservation) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority)

Aggregations

FeatureCollection (org.n52.shetland.ogc.om.features.FeatureCollection)14 DateTime (org.joda.time.DateTime)8 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)8 XmlObject (org.apache.xmlbeans.XmlObject)7 OmObservation (org.n52.shetland.ogc.om.OmObservation)6 EncodingException (org.n52.svalbard.encode.exception.EncodingException)5 TimeInstant (org.n52.shetland.ogc.gml.time.TimeInstant)4 TimePeriod (org.n52.shetland.ogc.gml.time.TimePeriod)4 SamplingFeature (org.n52.shetland.ogc.om.features.samplingFeatures.SamplingFeature)4 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)4 EReportingHeader (org.n52.shetland.aqd.EReportingHeader)3 CodeWithAuthority (org.n52.shetland.ogc.gml.CodeWithAuthority)3 Time (org.n52.shetland.ogc.gml.time.Time)3 AbstractSamplingFeature (org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature)3 UnsupportedEncoderInputException (org.n52.svalbard.encode.exception.UnsupportedEncoderInputException)3 FeaturePropertyType (net.opengis.gml.x32.FeaturePropertyType)2 Test (org.junit.Test)2 LineString (org.locationtech.jts.geom.LineString)2 ObservationStream (org.n52.shetland.ogc.om.ObservationStream)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1