Skip to content

Commit

Permalink
add encoding of swe data array with time and value as WML 2.0 and TSM…
Browse files Browse the repository at this point in the history
…L 1.0 tvp values
  • Loading branch information
Carsten Hollmann committed Nov 6, 2019
1 parent 2cf75e4 commit f29f733
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.n52.svalbard.encode;

import org.n52.shetland.ogc.om.values.SweDataArrayValue;
import org.n52.shetland.ogc.om.values.Value;
import org.n52.shetland.ogc.swe.SweAbstractDataRecord;
import org.n52.shetland.ogc.swe.simpleType.SweCount;
import org.n52.shetland.ogc.swe.simpleType.SweQuantity;
import org.n52.shetland.ogc.swe.simpleType.SweTime;
import org.n52.shetland.ogc.swe.simpleType.SweTimeRange;

public interface WmlTmlHelper {

default boolean checkSweDataArray(Value<?> value) {
return value instanceof SweDataArrayValue && ((SweDataArrayValue) value).getValue().getElementType() instanceof SweAbstractDataRecord
&& checkFields((SweAbstractDataRecord) ((SweDataArrayValue) value).getValue().getElementType());
}

default boolean checkFields(SweAbstractDataRecord sweAbstractDataRecord) {
return sweAbstractDataRecord.getFields().size() == 2
&& (sweAbstractDataRecord.getFields().get(0).getElement() instanceof SweTime
|| sweAbstractDataRecord.getFields().get(0).getElement() instanceof SweTimeRange)
&& (sweAbstractDataRecord.getFields().get(0).getElement() instanceof SweQuantity
|| sweAbstractDataRecord.getFields().get(0).getElement() instanceof SweCount);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
*/
public abstract class AbstractWmlEncoderv20
extends AbstractOmEncoderv20
implements ProcedureEncoder<XmlObject, Object> {
implements ProcedureEncoder<XmlObject, Object>, WmlTmlHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractWmlEncoderv20.class);

private static final Map<String, ImmutableMap<String, Set<String>>> SUPPORTED_PROCEDURE_DESCRIPTION_FORMATS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.n52.shetland.ogc.om.values.CountValue;
import org.n52.shetland.ogc.om.values.ProfileValue;
import org.n52.shetland.ogc.om.values.QuantityValue;
import org.n52.shetland.ogc.om.values.SweDataArrayValue;
import org.n52.shetland.ogc.om.values.TVPValue;
import org.n52.shetland.ogc.ows.exception.CodedException;
import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException;
Expand Down Expand Up @@ -80,9 +81,7 @@
* Encoder class for TimeseriesML 1.0 TimeseriesValuePair (TVP)
*
*/
public class TsmlTVPEncoderv10
extends
AbstractTsmlEncoderv10 {
public class TsmlTVPEncoderv10 extends AbstractTsmlEncoderv10 implements WmlTmlHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(TsmlTVPEncoderv10.class);

Expand Down Expand Up @@ -154,8 +153,7 @@ public boolean supportsResultStreamingForMergedValues() {

@Override
public XmlObject encode(Object element, EncodingContext additionalValues)
throws EncodingException,
UnsupportedEncoderInputException {
throws EncodingException, UnsupportedEncoderInputException {
if (element instanceof ObservationValue) {
return encodeResult((ObservationValue<?>) element);
} else {
Expand All @@ -181,8 +179,7 @@ public void encode(Object objectToEncode, OutputStream outputStream, EncodingCon
}

@Override
protected XmlObject createResult(OmObservation sosObservation)
throws EncodingException {
protected XmlObject createResult(OmObservation sosObservation) throws EncodingException {
try {
return createMeasurementTimeseries(sosObservation);
} catch (OwsExceptionReport ce) {
Expand All @@ -192,8 +189,7 @@ protected XmlObject createResult(OmObservation sosObservation)
}

@Override
protected XmlObject encodeResult(ObservationValue<?> observationValue)
throws EncodingException {
protected XmlObject encodeResult(ObservationValue<?> observationValue) throws EncodingException {
try {
return createMeasurementTimeseries((AbstractObservationValue<?>) observationValue);
} catch (OwsExceptionReport ce) {
Expand Down Expand Up @@ -233,8 +229,7 @@ protected OMObservationType createOmObservationType() {
* @throws CodedException
* if the encoding fails
*/
private XmlObject createMeasurementTimeseries(OmObservation sosObservation)
throws OwsExceptionReport {
private XmlObject createMeasurementTimeseries(OmObservation sosObservation) throws OwsExceptionReport {
TimeseriesTVPDocument measurementTimeseriesDoc = TimeseriesTVPDocument.Factory.newInstance();
TimeseriesTVPType measurementTimeseries = measurementTimeseriesDoc.addNewTimeseriesTVP();
((AbstractGMLType) measurementTimeseries).setId(TIMESERIES_ID_PREFIX + sosObservation.getObservationID());
Expand Down Expand Up @@ -287,8 +282,7 @@ private XmlObject createMeasurementTimeseries(OmObservation sosObservation)
return measurementTimeseriesDoc;
}

private XmlObject createMeasurementTimeseries(AbstractObservationValue<?> observationValue)
throws CodedException {
private XmlObject createMeasurementTimeseries(AbstractObservationValue<?> observationValue) throws CodedException {
TimeseriesTVPDocument measurementTimeseriesDoc = TimeseriesTVPDocument.Factory.newInstance();
TimeseriesTVPType measurementTimeseries = measurementTimeseriesDoc.addNewTimeseriesTVP();
((AbstractGMLType) measurementTimeseries).setId(TIMESERIES_ID_PREFIX + observationValue.getObservationID());
Expand Down Expand Up @@ -364,6 +358,14 @@ private String addValues(TimeseriesTVPType measurementTimeseries, ObservationVal
}
}
}
} else if (checkSweDataArray(observationValue.getValue())) {
SweDataArrayValue sweDataArrayValue = (SweDataArrayValue) observationValue.getValue();
for (List<String> list : sweDataArrayValue.getValue().getValues()) {
for (int i = 0; i < list.size(); i = i + 2) {
measurementTimeseries.addNewPoint()
.set(this.createMeasurementTVP(list.get(i), list.get(i + 1)));
}
}
}
measurementTimeseries.addNewPoint().set(this.createMeasurementTVP(time, value));
} else if (observationValue instanceof MultiObservationValues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.n52.shetland.ogc.om.values.CountValue;
import org.n52.shetland.ogc.om.values.ProfileValue;
import org.n52.shetland.ogc.om.values.QuantityValue;
import org.n52.shetland.ogc.om.values.SweDataArrayValue;
import org.n52.shetland.ogc.om.values.TVPValue;
import org.n52.shetland.ogc.ows.exception.CodedException;
import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException;
Expand Down Expand Up @@ -371,6 +372,14 @@ private String addValues(MeasurementTimeseriesType measurementTimeseries, Observ
}
}
}
} else if (checkSweDataArray(observationValue.getValue())) {
SweDataArrayValue sweDataArrayValue = (SweDataArrayValue) observationValue.getValue();
for (List<String> list : sweDataArrayValue.getValue().getValues()) {
for (int i = 0; i < list.size(); i = i + 2) {
addValuesToMeasurementTVP(measurementTimeseries.addNewPoint().addNewMeasurementTVP(),
list.get(i), list.get(i + 1));
}
}
}
addValuesToMeasurementTVP(measurementTimeseries.addNewPoint().addNewMeasurementTVP(), time, value);
} else if (observationValue instanceof MultiObservationValues) {
Expand Down Expand Up @@ -413,7 +422,7 @@ private String addValues(MeasurementTimeseriesType measurementTimeseries, Observ

return unit;
}

/**
* Add a time an value to MeasureTVPType
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
import org.n52.shetland.ogc.om.values.CountValue;
import org.n52.shetland.ogc.om.values.ProfileValue;
import org.n52.shetland.ogc.om.values.QuantityValue;
import org.n52.shetland.ogc.om.values.SweDataArrayValue;
import org.n52.shetland.ogc.om.values.TVPValue;
import org.n52.shetland.ogc.om.values.TextValue;
import org.n52.shetland.ogc.om.values.Value;
import org.n52.shetland.ogc.ows.exception.OwsExceptionReport;
import org.n52.shetland.util.DateTimeFormatException;
import org.n52.shetland.w3c.W3CConstants;
import org.n52.svalbard.encode.EncodingContext;
import org.n52.svalbard.encode.WmlTmlHelper;
import org.n52.svalbard.encode.exception.EncodingException;

import com.google.common.base.Strings;
Expand All @@ -60,7 +62,7 @@
*
*/
public class TsmlTVPEncoderv10XmlStreamWriter
extends AbstractOmV20XmlStreamWriter {
extends AbstractOmV20XmlStreamWriter implements WmlTmlHelper {
public TsmlTVPEncoderv10XmlStreamWriter(
EncodingContext context,
OutputStream outputStream,
Expand All @@ -80,9 +82,19 @@ protected void writeResult() throws XMLStreamException, EncodingException {
if (observation.getValue() instanceof SingleObservationValue) {
SingleObservationValue<?> observationValue = (SingleObservationValue<?>) observation.getValue();
writeDefaultPointMetadata(observationValue, observationValue.getValue().getUnit());
String time = getTimeString(observationValue.getPhenomenonTime());
writePoint(time, getValue(observation.getValue().getValue()));
close();
if (checkSweDataArray(observationValue.getValue())) {
SweDataArrayValue sweDataArrayValue = (SweDataArrayValue) observationValue.getValue();
for (List<String> list : sweDataArrayValue.getValue().getValues()) {
for (int i = 0; i < list.size(); i = i + 2) {
writePoint(list.get(i), list.get(i + 1));
close();
}
}
} else {
String time = getTimeString(observationValue.getPhenomenonTime());
writePoint(time, getValue(observation.getValue().getValue()));
close();
}
} else if (observation.getValue() instanceof MultiObservationValues) {
// XML streaming to client
MultiObservationValues<?> observationValue = (MultiObservationValues<?>) observation.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
import org.n52.shetland.ogc.om.values.CountValue;
import org.n52.shetland.ogc.om.values.ProfileValue;
import org.n52.shetland.ogc.om.values.QuantityValue;
import org.n52.shetland.ogc.om.values.SweDataArrayValue;
import org.n52.shetland.ogc.om.values.TVPValue;
import org.n52.shetland.ogc.om.values.TextValue;
import org.n52.shetland.ogc.om.values.Value;
import org.n52.shetland.ogc.ows.exception.OwsExceptionReport;
import org.n52.shetland.util.DateTimeFormatException;
import org.n52.shetland.w3c.W3CConstants;
import org.n52.svalbard.encode.EncodingContext;
import org.n52.svalbard.encode.WmlTmlHelper;
import org.n52.svalbard.encode.exception.EncodingException;

import com.google.common.base.Strings;
Expand All @@ -60,7 +62,7 @@
*
*/
public class WmlTVPEncoderv20XmlStreamWriter
extends AbstractOmV20XmlStreamWriter {
extends AbstractOmV20XmlStreamWriter implements WmlTmlHelper {
public WmlTVPEncoderv20XmlStreamWriter(
EncodingContext context,
OutputStream outputStream,
Expand All @@ -80,9 +82,19 @@ protected void writeResult() throws XMLStreamException, EncodingException {
if (observation.getValue() instanceof SingleObservationValue) {
SingleObservationValue<?> observationValue = (SingleObservationValue<?>) observation.getValue();
writeDefaultPointMetadata(observationValue, observationValue.getValue().getUnit());
String time = getTimeString(observationValue.getPhenomenonTime());
writePoint(time, getValue(observation.getValue().getValue()));
close();
if (checkSweDataArray(observationValue.getValue())) {
SweDataArrayValue sweDataArrayValue = (SweDataArrayValue) observationValue.getValue();
for (List<String> list : sweDataArrayValue.getValue().getValues()) {
for (int i = 0; i < list.size(); i = i + 2) {
writePoint(list.get(i), list.get(i + 1));
close();
}
}
} else {
String time = getTimeString(observationValue.getPhenomenonTime());
writePoint(time, getValue(observation.getValue().getValue()));
close();
}
} else if (observation.getValue() instanceof MultiObservationValues) {
// XML streaming to client
MultiObservationValues<?> observationValue = (MultiObservationValues<?>) observation.getValue();
Expand Down Expand Up @@ -356,7 +368,7 @@ private void writeValueMetadata() throws XMLStreamException {
endInline(WaterMLConstants.QN_METADATA);

}

@Override
protected Optional<String> getDefaultFeatureEncodingNamespace() {
return Optional.of(WaterMLConstants.NS_WML_20);
Expand Down

0 comments on commit f29f733

Please sign in to comment.