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;
}
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);
}
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;
}
}
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();
}
}
}
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();
}
}
Aggregations