Search in sources :

Example 11 with AbstractFeatureEntity

use of org.n52.series.db.beans.AbstractFeatureEntity in project SOS by 52North.

the class GetFeatureOfInterestHandler method createFeature.

private AbstractFeature createFeature(AbstractFeatureEntity feature) throws InvalidSridException, OwsExceptionReport {
    final SamplingFeature sampFeat = new SamplingFeature(new CodeWithAuthority(feature.getIdentifier()));
    if (feature.isSetName()) {
        sampFeat.addName(feature.getName());
    }
    if (!Strings.isNullOrEmpty(feature.getDescription())) {
        sampFeat.setDescription(feature.getDescription());
    }
    if (feature.isSetGeometry() && !feature.getGeometryEntity().isEmpty()) {
        sampFeat.setGeometry(getGeometryHandler().switchCoordinateAxisFromToDatasourceIfNeeded(feature.getGeometryEntity().getGeometry()));
    }
    final Set<FeatureEntity> parentFeatures = feature.getParents();
    if (parentFeatures != null && !parentFeatures.isEmpty()) {
        final List<AbstractFeature> sampledFeatures = new ArrayList<>(parentFeatures.size());
        for (final FeatureEntity parentFeature : parentFeatures) {
            sampledFeatures.add(createFeature(parentFeature));
        }
        sampFeat.setSampledFeatures(sampledFeatures);
    }
    return sampFeat;
}
Also used : AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) FeatureEntity(org.n52.series.db.beans.FeatureEntity) ArrayList(java.util.ArrayList) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) SamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.SamplingFeature) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority)

Example 12 with AbstractFeatureEntity

use of org.n52.series.db.beans.AbstractFeatureEntity in project SOS by 52North.

the class ResultTemplateDAO method checkOrInsertResultTemplate.

/**
 * Check or insert result template
 *
 * @param request
 *            Insert result template request
 * @param dataset
 *            Observation constellation object
 * @param procedure
 *            the procedure
 * @param featureOfInterest
 *            FeatureOfInterest object
 * @param session
 *            Hibernate session
 * @return created or exiting {@link ResultTemplateEntity}
 * @throws OwsExceptionReport
 *             If the requested structure/encoding is invalid
 */
public ResultTemplateEntity checkOrInsertResultTemplate(InsertResultTemplateRequest request, DatasetEntity dataset, ProcedureEntity procedure, AbstractFeatureEntity featureOfInterest, Session session) throws OwsExceptionReport {
    try {
        String offering = dataset.getOffering().getIdentifier();
        String observableProperty = dataset.getObservableProperty().getIdentifier();
        List<ResultTemplateEntity> resultTemplates = getResultTemplateObject(offering, observableProperty, null, session);
        if (CollectionHelper.isEmpty(resultTemplates)) {
            return createAndSaveResultTemplate(request, dataset, procedure, featureOfInterest, session);
        } else {
            List<String> storedIdentifiers = new ArrayList<>(0);
            for (ResultTemplateEntity storedResultTemplate : resultTemplates) {
                storedIdentifiers.add(storedResultTemplate.getIdentifier());
                SosResultStructure storedStructure = createSosResultStructure(storedResultTemplate.getStructure());
                SosResultEncoding storedEncoding = createSosResultEncoding(storedResultTemplate.getEncoding());
                SosResultStructure storedObservationStructure = createSosResultStructure(storedResultTemplate.getObservationStructure());
                SosResultEncoding storedObservationEncoding = createSosResultEncoding(storedResultTemplate.getObservationEncoding());
                if (request instanceof InternalInsertResultTemplateRequest) {
                    ((InternalInsertResultTemplateRequest) request).getObservationStructure().clearXml();
                    ((InternalInsertResultTemplateRequest) request).getObservationEncoding().clearXml();
                    if (storedObservationStructure != null && !storedObservationStructure.equals(((InternalInsertResultTemplateRequest) request).getObservationStructure())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                    if (storedObservationEncoding != null && !storedObservationEncoding.equals(((InternalInsertResultTemplateRequest) request).getObservationEncoding())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                } else {
                    if (storedStructure != null && !storedStructure.equals(request.getResultStructure())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                    if (storedEncoding != null && !storedEncoding.equals(request.getResultEncoding())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                }
            }
            if (request.getIdentifier() != null && request.getIdentifier().isSetValue()) {
                if (!resultTemplates.isEmpty() && request instanceof InternalInsertResultTemplateRequest && request.getIdentifier().getValue().startsWith("OBS_")) {
                    updateTemplates(resultTemplates, (InternalInsertResultTemplateRequest) request, session);
                    return resultTemplates.iterator().next();
                }
                if (!storedIdentifiers.contains(request.getIdentifier().getValue())) {
                    /* save it only if the identifier is different */
                    return createAndSaveResultTemplate(request, dataset, procedure, featureOfInterest, session);
                }
            }
            return resultTemplates.iterator().next();
        }
    } catch (EncodingException | DecodingException ex) {
        throw new NoApplicableCodeException().causedBy(ex);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) ArrayList(java.util.ArrayList) XmlDecodingException(org.n52.svalbard.decode.exception.XmlDecodingException) DecodingException(org.n52.svalbard.decode.exception.DecodingException) InternalInsertResultTemplateRequest(org.n52.sos.request.InternalInsertResultTemplateRequest) SosResultEncoding(org.n52.shetland.ogc.sos.SosResultEncoding) InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) SosResultStructure(org.n52.shetland.ogc.sos.SosResultStructure)

Example 13 with AbstractFeatureEntity

use of org.n52.series.db.beans.AbstractFeatureEntity in project sensorweb-server-sta by 52North.

the class ServiceUtils method createFeatureOfInterest.

static AbstractFeatureEntity<?> createFeatureOfInterest(LocationEntity location) {
    FeatureEntity featureOfInterest = new FeatureEntity();
    featureOfInterest.setIdentifier(location.getIdentifier());
    featureOfInterest.setStaIdentifier(location.getStaIdentifier());
    // Used to distinguish in FeatureOfInterestService
    featureOfInterest.setXml("autogenerated");
    featureOfInterest.setName(location.getName());
    featureOfInterest.setDescription(location.getDescription());
    featureOfInterest.setGeometryEntity(location.getGeometryEntity());
    featureOfInterest.setFeatureType(createFeatureType(location.getGeometry()));
    return featureOfInterest;
}
Also used : AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) FeatureEntity(org.n52.series.db.beans.FeatureEntity)

Example 14 with AbstractFeatureEntity

use of org.n52.series.db.beans.AbstractFeatureEntity in project sensorweb-server-sta by 52North.

the class ObservationService method createEntity.

@Override
public ObservationEntity<?> createEntity(ObservationEntity<?> entity) throws STACRUDException {
    synchronized (getLock(entity.getStaIdentifier())) {
        ObservationEntity observation = entity;
        if (!observation.isProcessed()) {
            observation.setProcessed(true);
            check(observation);
            DatastreamEntity datastream = getDatastreamService().createEntity(observation.getDatastream());
            observation.setDatastream(datastream);
            // Fetch with all needed associations
            datastream = datastreamRepository.findByStaIdentifier(datastream.getStaIdentifier(), EntityGraphRepository.FetchGraph.FETCHGRAPH_THINGLOCATION, EntityGraphRepository.FetchGraph.FETCHGRAPH_PROCEDURE, EntityGraphRepository.FetchGraph.FETCHGRAPH_UOM, EntityGraphRepository.FetchGraph.FETCHGRAPH_OBS_TYPE, EntityGraphRepository.FetchGraph.FETCHGRAPH_OBSERVABLE_PROP, EntityGraphRepository.FetchGraph.FETCHGRAPH_DATASETS).orElseThrow(() -> new STACRUDException("Unable to find Datastream!"));
            AbstractFeatureEntity<?> feature = checkFeature(observation, datastream);
            // category (obdProp)
            CategoryEntity category = checkCategory();
            // offering (sensor)
            OfferingEntity offering = checkOffering(datastream);
            // dataset
            DatasetEntity dataset = checkDataset(datastream, feature, category, offering);
            // observation
            ObservationEntity<?> data = checkData(observation, dataset);
            if (data != null) {
                updateDataset(dataset, data);
                updateDatastream(datastream, dataset, data);
            }
            return data;
        }
        return observation;
    }
}
Also used : DatasetEntity(org.n52.series.db.beans.DatasetEntity) DatastreamEntity(org.n52.series.db.beans.sta.DatastreamEntity) CategoryObservationEntity(org.n52.series.db.beans.sta.CategoryObservationEntity) QuantityObservationEntity(org.n52.series.db.beans.sta.QuantityObservationEntity) TextObservationEntity(org.n52.series.db.beans.sta.TextObservationEntity) BooleanObservationEntity(org.n52.series.db.beans.sta.BooleanObservationEntity) ObservationEntity(org.n52.series.db.beans.sta.ObservationEntity) CountObservationEntity(org.n52.series.db.beans.sta.CountObservationEntity) OfferingEntity(org.n52.series.db.beans.OfferingEntity) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException) CategoryEntity(org.n52.series.db.beans.CategoryEntity)

Example 15 with AbstractFeatureEntity

use of org.n52.series.db.beans.AbstractFeatureEntity in project sensorweb-server-sta by 52North.

the class FeatureOfInterestService method updateFeatureOfInterestGeometry.

/**
 * Extends the geometry of the FOI with given id by geom. Used for automatically expanding FOIs e.g. for
 * Observations along a track.
 * Used for non-standard feature 'updateFOI'.
 *
 * @param id   id of the FOI
 * @param geom geom to expand the existing Geometry
 * @throws STACRUDException if an error occurred
 */
public void updateFeatureOfInterestGeometry(String id, Geometry geom) throws STACRUDException {
    synchronized (getLock(id)) {
        Optional<AbstractFeatureEntity<?>> existing = getRepository().findByStaIdentifier(id, EntityGraphRepository.FetchGraph.FETCHGRAPH_FEATURETYPE);
        if (existing.isPresent()) {
            AbstractFeatureEntity<?> featureOfInterest = existing.get();
            if (featureOfInterest.isSetGeometry()) {
                if (geom instanceof Point) {
                    List<Coordinate> coords = new ArrayList<>();
                    Geometry convert = featureOfInterest.getGeometry();
                    if (convert instanceof Point) {
                        coords.add(convert.getCoordinate());
                    } else if (convert instanceof LineString || convert instanceof GeometryCollection) {
                        coords.addAll(Arrays.asList(convert.getCoordinates()));
                    } else {
                        LOGGER.error("Could not update FOI geometry. Unknown GeometryType." + convert.getClass().getSimpleName());
                        throw new STACRUDException("Could not update FeatureOfInterest. Unknown GeometryType:" + convert.getClass().getSimpleName());
                    }
                    Geometry newGeometry;
                    if (!coords.isEmpty()) {
                        coords.add(geom.getCoordinate());
                        newGeometry = new GeometryFactory().createLineString(coords.toArray(new Coordinate[coords.size()]));
                    } else {
                        newGeometry = new GeometryFactory().createPoint(geom.getCoordinate());
                    }
                    newGeometry.setSRID(featureOfInterest.getGeometry().getSRID());
                    featureOfInterest.setGeometry(newGeometry);
                }
            } else {
                featureOfInterest.setGeometry(geom);
            }
            getRepository().save(featureOfInterest);
        } else {
            throw new STACRUDException("Could not update FeatureOfInterest. No FeatureOfInterest with id \"" + id + "\" found!");
        }
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) ArrayList(java.util.ArrayList) Point(org.locationtech.jts.geom.Point) STACRUDException(org.n52.shetland.ogc.sta.exception.STACRUDException)

Aggregations

AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)23 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)8 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)8 ArrayList (java.util.ArrayList)7 Criteria (org.hibernate.Criteria)7 Session (org.hibernate.Session)7 DatasetEntity (org.n52.series.db.beans.DatasetEntity)7 HashMap (java.util.HashMap)6 Geometry (org.locationtech.jts.geom.Geometry)6 FeatureEntity (org.n52.series.db.beans.FeatureEntity)6 HibernateException (org.hibernate.HibernateException)5 LineString (org.locationtech.jts.geom.LineString)4 LocationEntity (org.n52.series.db.beans.sta.LocationEntity)4 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)4 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)4 FormatEntity (org.n52.series.db.beans.FormatEntity)3 ResultTemplateEntity (org.n52.series.db.beans.ResultTemplateEntity)3 HistoricalLocationEntity (org.n52.series.db.beans.sta.HistoricalLocationEntity)3 SpatialFilter (org.n52.shetland.ogc.filter.SpatialFilter)3 AbstractSamplingFeature (org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature)3