Search in sources :

Example 1 with IncDecInteger

use of org.n52.sos.util.IncDecInteger in project SOS by 52North.

the class ResultHandlingHelper method getSamplingGeometry.

private String getSamplingGeometry(DataEntity<?> observation, String tokenSeparator, SweAbstractDataComponent sweAbstractDataComponent, String noDataPlaceholder) throws OwsExceptionReport {
    SweVector vector = getVector(sweAbstractDataComponent);
    if (vector != null && vector.isSetCoordinates()) {
        final Map<Integer, String> valueOrder = new HashMap<>(0);
        addOrderAndVectorDefinitionToMap(vector.getCoordinates(), valueOrder, new IncDecInteger());
        final StringBuilder builder = new StringBuilder();
        Geometry samplingGeometry = null;
        if (observation.isSetGeometryEntity()) {
            samplingGeometry = getGeomtryHandler().switchCoordinateAxisFromToDatasourceIfNeeded(observation.getGeometryEntity().getGeometry());
        }
        for (final Entry<Integer, String> entry : valueOrder.entrySet()) {
            final String definition = entry.getValue();
            if (samplingGeometry != null && samplingGeometry instanceof Point) {
                Coordinate coordinate = samplingGeometry.getCoordinate();
                if (helper.hasAltitudeName(definition) && checkCoordinate(coordinate.getZ())) {
                    builder.append(coordinate.getZ());
                } else if (helper.hasNorthingName(definition)) {
                    if (getGeomtryHandler().isNorthingFirstEpsgCode(samplingGeometry.getSRID())) {
                        builder.append(coordinate.x);
                    } else {
                        builder.append(coordinate.y);
                    }
                } else if (helper.hasEastingName(definition)) {
                    if (getGeomtryHandler().isNorthingFirstEpsgCode(samplingGeometry.getSRID())) {
                        builder.append(coordinate.y);
                    } else {
                        builder.append(coordinate.x);
                    }
                } else {
                    builder.append(noDataPlaceholder);
                }
            } else {
                builder.append(noDataPlaceholder);
            }
            builder.append(tokenSeparator);
        }
        return builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()).toString();
    }
    return noDataPlaceholder;
}
Also used : IncDecInteger(org.n52.sos.util.IncDecInteger) Geometry(org.locationtech.jts.geom.Geometry) SweVector(org.n52.shetland.ogc.swe.SweVector) HashMap(java.util.HashMap) Coordinate(org.locationtech.jts.geom.Coordinate) SweCoordinate(org.n52.shetland.ogc.swe.SweCoordinate) Point(org.locationtech.jts.geom.Point) IncDecInteger(org.n52.sos.util.IncDecInteger)

Example 2 with IncDecInteger

use of org.n52.sos.util.IncDecInteger in project SOS by 52North.

the class ResultHandlingHelper method getValueOrderMap.

private Map<Integer, String> getValueOrderMap(final SweAbstractDataComponent sweDataElement) {
    final Map<Integer, String> valueOrder = new HashMap<>(0);
    if (sweDataElement instanceof SweDataArray && ((SweDataArray) sweDataElement).getElementType() instanceof SweDataRecord) {
        final SweDataArray dataArray = (SweDataArray) sweDataElement;
        addOrderAndDefinitionToMap(((SweDataRecord) dataArray.getElementType()).getFields(), valueOrder, new IncDecInteger());
    } else if (sweDataElement instanceof SweDataRecord) {
        final SweDataRecord dataRecord = (SweDataRecord) sweDataElement;
        addOrderAndDefinitionToMap(dataRecord.getFields(), valueOrder, new IncDecInteger());
    }
    return new TreeMap<>(valueOrder);
}
Also used : IncDecInteger(org.n52.sos.util.IncDecInteger) HashMap(java.util.HashMap) SweDataRecord(org.n52.shetland.ogc.swe.SweDataRecord) TreeMap(java.util.TreeMap) SweDataArray(org.n52.shetland.ogc.swe.SweDataArray) IncDecInteger(org.n52.sos.util.IncDecInteger)

Example 3 with IncDecInteger

use of org.n52.sos.util.IncDecInteger in project SOS by 52North.

the class ObservationUnfolder method unfold.

public List<OmObservation> unfold(boolean complexToSingleProfiles) throws OwsExceptionReport {
    if (multiObservation.getValue() instanceof SingleObservationValue) {
        return Collections.singletonList(multiObservation);
    } else {
        final List<OmObservation> observationCollection = new ArrayList<OmObservation>();
        Map<String, AbstractFeature> features = new HashMap<>();
        Map<String, SosProcedureDescription<?>> procedures = new HashMap<>();
        boolean complex = false;
        if (((MultiObservationValues<?>) multiObservation.getValue()).getValue() instanceof SweDataArrayValue) {
            final SweDataArrayValue arrayValue = (SweDataArrayValue) ((MultiObservationValues<?>) multiObservation.getValue()).getValue();
            final List<List<String>> values = arrayValue.getValue().getValues();
            SweDataRecord elementType = null;
            if (arrayValue.getValue().getElementType() != null && arrayValue.getValue().getElementType() instanceof SweDataRecord) {
                elementType = (SweDataRecord) arrayValue.getValue().getElementType();
            } else {
                throw new NoApplicableCodeException().withMessage("sweElementType type \"%s\" not supported", arrayValue.getValue().getElementType() != null ? arrayValue.getValue().getElementType().getClass().getName() : NULL);
            }
            for (final List<String> block : values) {
                IncDecInteger tokenIndex = new IncDecInteger();
                Time phenomenonTime = null;
                TimeInstant resultTime = null;
                final List<Value<?>> observedValues = new LinkedList<Value<?>>();
                // map to store the observed properties
                final Map<Value<?>, String> definitionsForObservedValues = Maps.newHashMap();
                Value<?> observedValue = null;
                GeometryHolder samplingGeometry = new GeometryHolder();
                ParameterHolder parameterHolder = getParameterHolder(multiObservation.getParameterHolder());
                String featureOfInterest = null;
                String procedure = null;
                String identifier = null;
                String name = null;
                String description = null;
                for (SweField field : elementType.getFields()) {
                    final SweAbstractDataComponent dataComponent = field.getElement();
                    String token = block.get(tokenIndex.get());
                    /*
                         * get phenomenon time
                         */
                    if (dataComponent instanceof SweTime) {
                        try {
                            if (dataComponent.isSetDefinition() && OmConstants.RESULT_TIME.equals(dataComponent.getDefinition())) {
                                resultTime = new TimeInstant(DateTimeHelper.parseIsoString2DateTime(token));
                            } else {
                                if (phenomenonTime == null) {
                                    phenomenonTime = new TimeInstant(DateTimeHelper.parseIsoString2DateTime(token));
                                }
                            }
                        } catch (final Exception e) {
                            /*
                                 * FIXME what is the valid exception code if the
                                 * result is not correct?
                                 */
                            throw new NoApplicableCodeException().causedBy(e).withMessage(ERROR_PARSING_TIME_LOG);
                        }
                    } else if (dataComponent instanceof SweTimeRange) {
                        if (!token.contains("/")) {
                            throw new NoApplicableCodeException().withMessage(ERROR_PARSING_TIME_LOG + " A time range is expected instead of an instance!");
                        }
                        try {
                            final String[] subTokens = token.split("/");
                            phenomenonTime = new TimePeriod(DateTimeHelper.parseIsoString2DateTime(subTokens[0]), DateTimeHelper.parseIsoString2DateTime(subTokens[1]));
                        } catch (final Exception e) {
                            /*
                                 * FIXME what is the valid exception code if the
                                 * result is not correct?
                                 */
                            throw new NoApplicableCodeException().causedBy(e).withMessage(ERROR_PARSING_TIME_LOG);
                        }
                    } else if (dataComponent instanceof SweAbstractSimpleType) {
                        if (dataComponent instanceof SweText && dataComponent.getDefinition().contains("om:featureOfInterest")) {
                            featureOfInterest = token;
                        } else if (dataComponent instanceof SweText && dataComponent.getDefinition().contains("om:procedure")) {
                            procedure = token;
                        } else if (dataComponent instanceof SweText && dataComponent.getDefinition().contains("gml:identifier")) {
                            identifier = token;
                        } else if (dataComponent instanceof SweText && dataComponent.getDefinition().contains("gml:name")) {
                            name = token;
                        } else if (dataComponent instanceof SweText && dataComponent.getDefinition().contains("gml:description")) {
                            description = token;
                        } else if (dataComponent instanceof SweQuantity && checkDefinitionForDephtHeight(field)) {
                            parseFieldAsParameter(field, token, parameterHolder);
                        } else {
                            observedValue = parseSweAbstractSimpleType(dataComponent, token);
                        }
                    } else if (dataComponent instanceof SweDataRecord) {
                        if (dataComponent.getDefinition().contains(OmConstants.OM_PARAMETER)) {
                            parseDataRecordAsParameter((SweDataRecord) dataComponent, block, tokenIndex, parameterHolder);
                        } else {
                            observedValue = parseSweDataRecord(((SweDataRecord) dataComponent).copy(), block, tokenIndex, parameterHolder);
                        }
                    } else if (dataComponent instanceof SweDataArray) {
                        observedValue = parseSweDataArray(((SweDataArray) dataComponent).copy(), block, tokenIndex, parameterHolder, multiObservation.getObservationConstellation().getObservablePropertyIdentifier());
                    } else if (dataComponent instanceof SweVector) {
                        parseSweVectorAsGeometry(((SweVector) dataComponent).copy(), block, tokenIndex, samplingGeometry);
                    } else {
                        throw new NoApplicableCodeException().withMessage(SWE_FILE_NOT_SUPPORTTED_LOG_TEMPLATE, dataComponent != null ? dataComponent.getClass().getName() : NULL);
                    }
                    if (observedValue != null) {
                        definitionsForObservedValues.put(observedValue, dataComponent.getDefinition());
                        observedValues.add(observedValue);
                        observedValue = null;
                    }
                    tokenIndex.incrementAndGet();
                }
                for (final Value<?> iValue : observedValues) {
                    List<OmObservation> newObservations = new ArrayList<>();
                    if (isProfileObservations(parameterHolder)) {
                        if (iValue instanceof ComplexValue && complexToSingleProfiles) {
                            complex = true;
                            for (SweField field : ((ComplexValue) iValue).getValue().getFields()) {
                                if (!checkDefinitionForDephtHeight(field)) {
                                    String definition = field.getElement().getDefinition();
                                    newObservations.add(createSingleValueObservation(multiObservation, phenomenonTime, resultTime, definition, convertToProfileValue(field.accept(ValueCreatingSweDataComponentVisitor.getInstance()), samplingGeometry, phenomenonTime, parameterHolder)));
                                }
                            }
                        } else {
                            newObservations.add(createSingleValueObservation(multiObservation, phenomenonTime, resultTime, convertToProfileValue(iValue, samplingGeometry, phenomenonTime, parameterHolder)));
                        }
                        if (parameterHolder.isSetHeightDepthParameter()) {
                            parameterHolder.removeParameter(parameterHolder.getHeightDepthParameter());
                        }
                        if (parameterHolder.isSetFromToParameter()) {
                            parameterHolder.removeParameter(parameterHolder.getFromParameter());
                            parameterHolder.removeParameter(parameterHolder.getToParameter());
                        }
                    } else {
                        newObservations.add(createSingleValueObservation(multiObservation, phenomenonTime, resultTime, iValue));
                    }
                    for (OmObservation newObservation : newObservations) {
                        if (samplingGeometry.hasGeometry()) {
                            try {
                                newObservation.addSpatialFilteringProfileParameter(samplingGeometry.getGeometry());
                            } catch (ParseException e) {
                                throw new NoApplicableCodeException().causedBy(e);
                            }
                        }
                        if (!Strings.isNullOrEmpty(featureOfInterest)) {
                            if (!features.containsKey(featureOfInterest)) {
                                features.put(featureOfInterest, new SamplingFeature(new CodeWithAuthority(featureOfInterest)));
                            }
                            newObservation.getObservationConstellation().setFeatureOfInterest(features.get(featureOfInterest));
                        }
                        if (!Strings.isNullOrEmpty(procedure)) {
                            if (!procedures.containsKey(procedure)) {
                                procedures.put(procedure, new SosProcedureDescription<AbstractFeature>(new SensorML().setIdentifier(procedure)));
                            }
                            newObservation.getObservationConstellation().setProcedure(procedures.get(procedure));
                        }
                        if (!Strings.isNullOrEmpty(identifier)) {
                            newObservation.setIdentifier(identifier);
                        }
                        if (!Strings.isNullOrEmpty(name)) {
                            newObservation.setName(new CodeType(name));
                        }
                        if (!Strings.isNullOrEmpty(description)) {
                            newObservation.setDescription(description);
                        }
                        if (parameterHolder.isSetParameter()) {
                            newObservation.setParameter(parameterHolder.getParameter());
                        }
                        observationCollection.add(newObservation);
                    }
                }
                featureOfInterest = null;
                procedure = null;
            }
        }
        if (isProfileObservations()) {
            if (complex) {
                List<OmObservation> observations = new ArrayList<>();
                for (ObservationStream stream : getProfileLists(observationCollection)) {
                    observations.addAll(toList(stream));
                }
                return observations;
            } else {
                List<OmObservation> observations = new ArrayList<>();
                for (ObservationStream stream : getProfileLists(observationCollection, 1)) {
                    observations.addAll(toList(stream));
                }
                return observations;
            }
        }
        return observationCollection;
    }
}
Also used : SweQuantity(org.n52.shetland.ogc.swe.simpleType.SweQuantity) SweText(org.n52.shetland.ogc.swe.simpleType.SweText) HashMap(java.util.HashMap) SweAbstractSimpleType(org.n52.shetland.ogc.swe.simpleType.SweAbstractSimpleType) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) SweAbstractDataComponent(org.n52.shetland.ogc.swe.SweAbstractDataComponent) OmObservation(org.n52.shetland.ogc.om.OmObservation) ArrayList(java.util.ArrayList) Time(org.n52.shetland.ogc.gml.time.Time) SweTime(org.n52.shetland.ogc.swe.simpleType.SweTime) DateTime(org.joda.time.DateTime) SamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.SamplingFeature) SweTimeRange(org.n52.shetland.ogc.swe.simpleType.SweTimeRange) SweDataArrayValue(org.n52.shetland.ogc.om.values.SweDataArrayValue) SingleObservationValue(org.n52.shetland.ogc.om.SingleObservationValue) SosProcedureDescription(org.n52.shetland.ogc.sos.SosProcedureDescription) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IncDecInteger(org.n52.sos.util.IncDecInteger) ComplexValue(org.n52.shetland.ogc.om.values.ComplexValue) SweTime(org.n52.shetland.ogc.swe.simpleType.SweTime) TimePeriod(org.n52.shetland.ogc.gml.time.TimePeriod) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) SweDataArray(org.n52.shetland.ogc.swe.SweDataArray) SensorML(org.n52.shetland.ogc.sensorML.SensorML) LinkedList(java.util.LinkedList) CodedException(org.n52.shetland.ogc.ows.exception.CodedException) ParseException(org.locationtech.jts.io.ParseException) NoSuchElementException(java.util.NoSuchElementException) MissingParameterValueException(org.n52.shetland.ogc.ows.exception.MissingParameterValueException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) SweVector(org.n52.shetland.ogc.swe.SweVector) SweDataRecord(org.n52.shetland.ogc.swe.SweDataRecord) SweField(org.n52.shetland.ogc.swe.SweField) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) SingleObservationValue(org.n52.shetland.ogc.om.SingleObservationValue) BooleanValue(org.n52.shetland.ogc.om.values.BooleanValue) ComplexValue(org.n52.shetland.ogc.om.values.ComplexValue) QuantityValue(org.n52.shetland.ogc.om.values.QuantityValue) CountValue(org.n52.shetland.ogc.om.values.CountValue) SweDataArrayValue(org.n52.shetland.ogc.om.values.SweDataArrayValue) NamedValue(org.n52.shetland.ogc.om.NamedValue) CategoryValue(org.n52.shetland.ogc.om.values.CategoryValue) ProfileValue(org.n52.shetland.ogc.om.values.ProfileValue) TextValue(org.n52.shetland.ogc.om.values.TextValue) ObservationValue(org.n52.shetland.ogc.om.ObservationValue) Value(org.n52.shetland.ogc.om.values.Value) CodeType(org.n52.shetland.ogc.gml.CodeType) ParseException(org.locationtech.jts.io.ParseException) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority) ParameterHolder(org.n52.shetland.ogc.om.ParameterHolder) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 4 with IncDecInteger

use of org.n52.sos.util.IncDecInteger in project SOS by 52North.

the class ObservationUnfolder method parseSweVectorAsGeometry.

private void parseSweVectorAsGeometry(SweVector sweVector, List<String> block, IncDecInteger tokenIndex, GeometryHolder holder) throws OwsExceptionReport {
    if (OmConstants.PARAM_NAME_SAMPLING_GEOMETRY.equals(sweVector.getDefinition())) {
        if (sweVector.isSetReferenceFrame()) {
            holder.setSrid(getCrsFromString(sweVector.getReferenceFrame()));
        }
        boolean tokenIndexIncreased = false;
        for (SweCoordinate<?> coordinate : sweVector.getCoordinates()) {
            if (coordinate == null) {
                throw new NoApplicableCodeException().withMessage("sweCoordinate is null");
            }
            String token = block.get(tokenIndex.get());
            if (coordinate.getValue() instanceof SweQuantity) {
                double value = Double.parseDouble(token);
                if (helper.hasAltitudeName(coordinate.getName(), coordinate.getValue().getDefinition())) {
                    holder.setAltitude(value);
                } else if (helper.hasNorthingName(coordinate.getName(), coordinate.getValue().getDefinition())) {
                    holder.setLatitude(value);
                } else if (helper.hasEastingName(coordinate.getName(), coordinate.getValue().getDefinition())) {
                    holder.setLongitude(value);
                }
            } else {
                throw new NoApplicableCodeException().withMessage("sweCoordinate type '%s' not yet supported", coordinate.getClass().getName());
            }
            tokenIndex.incrementAndGet();
            tokenIndexIncreased = true;
        }
        // method.
        if (tokenIndexIncreased) {
            tokenIndex.decrementAndGet();
        }
    }
}
Also used : SweQuantity(org.n52.shetland.ogc.swe.simpleType.SweQuantity) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)

Example 5 with IncDecInteger

use of org.n52.sos.util.IncDecInteger in project SOS by 52North.

the class ObservationUnfolder method parseDataRecordAsParameter.

private void parseDataRecordAsParameter(SweDataRecord record, List<String> block, IncDecInteger tokenIndex, ParameterHolder parameterHolder) throws CodedException {
    boolean tokenIndexIncreased = false;
    for (SweField field : record.getFields()) {
        String token = block.get(tokenIndex.get());
        parseFieldAsParameter(field, token, parameterHolder);
        tokenIndex.incrementAndGet();
        tokenIndexIncreased = true;
    }
    // decrease token index because it is increased in the calling method.
    if (tokenIndexIncreased) {
        tokenIndex.decrementAndGet();
    }
}
Also used : SweField(org.n52.shetland.ogc.swe.SweField)

Aggregations

SweField (org.n52.shetland.ogc.swe.SweField)5 SweDataRecord (org.n52.shetland.ogc.swe.SweDataRecord)4 HashMap (java.util.HashMap)3 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)3 SweDataArray (org.n52.shetland.ogc.swe.SweDataArray)3 SweVector (org.n52.shetland.ogc.swe.SweVector)3 SweQuantity (org.n52.shetland.ogc.swe.simpleType.SweQuantity)3 IncDecInteger (org.n52.sos.util.IncDecInteger)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ComplexValue (org.n52.shetland.ogc.om.values.ComplexValue)2 SweDataArrayValue (org.n52.shetland.ogc.om.values.SweDataArrayValue)2 NoSuchElementException (java.util.NoSuchElementException)1 TreeMap (java.util.TreeMap)1 DateTime (org.joda.time.DateTime)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 Point (org.locationtech.jts.geom.Point)1 ParseException (org.locationtech.jts.io.ParseException)1