Example usage for org.hibernate.spatial.criterion SpatialProjections extent

List of usage examples for org.hibernate.spatial.criterion SpatialProjections extent

Introduction

In this page you can find the example usage for org.hibernate.spatial.criterion SpatialProjections extent.

Prototype

public static Projection extent(final String propertyName) 

Source Link

Document

Applies an extent projection to the specified geometry function

The extent of a set of Geometry s is the union of their bounding boxes.

Usage

From source file:org.n52.sos.ds.hibernate.dao.AbstractObservationDAO.java

License:Open Source License

public SosEnvelope getSpatialFilteringProfileEnvelopeForOfferingId(String offeringID, Session session)
        throws OwsExceptionReport {
    try {/*from   w w w  .  j  a v  a 2 s  .c om*/
        // XXX workaround for Hibernate Spatial's lack of support for
        // GeoDB's extent aggregate see
        // http://www.hibernatespatial.org/pipermail/hibernatespatial-users/2013-August/000876.html
        Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
        if (GeometryHandler.getInstance().isSpatialDatasource()
                && HibernateHelper.supportsFunction(dialect, HibernateConstants.FUNC_EXTENT)) {
            Criteria criteria = getDefaultObservationInfoCriteria(session);
            criteria.setProjection(SpatialProjections.extent(AbstractObservationTime.SAMPLING_GEOMETRY));
            criteria.createCriteria(AbstractObservation.OFFERINGS)
                    .add(Restrictions.eq(Offering.IDENTIFIER, offeringID));
            LOGGER.debug("QUERY getSpatialFilteringProfileEnvelopeForOfferingId(offeringID): {}",
                    HibernateHelper.getSqlString(criteria));
            Geometry geom = (Geometry) criteria.uniqueResult();
            geom = GeometryHandler.getInstance().switchCoordinateAxisFromToDatasourceIfNeeded(geom);
            if (geom != null) {
                return new SosEnvelope(geom.getEnvelopeInternal(),
                        GeometryHandler.getInstance().getStorageEPSG());
            }
        } else {
            final Envelope envelope = new Envelope();
            Criteria criteria = getDefaultObservationInfoCriteria(session);
            criteria.createCriteria(AbstractObservation.OFFERINGS)
                    .add(Restrictions.eq(Offering.IDENTIFIER, offeringID));
            LOGGER.debug("QUERY getSpatialFilteringProfileEnvelopeForOfferingId(offeringID): {}",
                    HibernateHelper.getSqlString(criteria));
            @SuppressWarnings("unchecked")
            final List<AbstractObservationTime> observationTimes = criteria.list();
            if (CollectionHelper.isNotEmpty(observationTimes)) {
                for (final AbstractObservationTime observationTime : observationTimes) {
                    if (observationTime.hasSamplingGeometry()) {
                        final Geometry geom = observationTime.getSamplingGeometry();
                        if (geom != null && geom.getEnvelopeInternal() != null) {
                            envelope.expandToInclude(geom.getEnvelopeInternal());
                        }
                    }
                }
                if (!envelope.isNull()) {
                    return new SosEnvelope(envelope, GeometryHandler.getInstance().getStorageEPSG());
                }
            }
        }
    } catch (final HibernateException he) {
        throw new NoApplicableCodeException().causedBy(he)
                .withMessage("Exception thrown while requesting feature envelope for observation ids");
    }
    return null;
}

From source file:org.n52.sos.ds.hibernate.dao.AbstractSpatialFilteringProfileDAO.java

License:Open Source License

/**
 * Get the Capabilities envelope for offering from Spatial Filtering Profile
 * //from w ww.  java  2s  .co  m
 * @param clazz
 *            Entity to create Criteria for
 * @param offeringID
 *            Offering identifier
 * @param session
 *            Hibernate session
 * @return Envelope
 * @throws OwsExceptionReport
 *             If coordinate switching fails
 */
@SuppressWarnings("rawtypes")
protected SosEnvelope getEnvelopeForOfferingId(Class clazz, String offeringID, Session session)
        throws OwsExceptionReport {
    try {
        // XXX workaround for Hibernate Spatial's lack of support for
        // GeoDB's extent aggregate see
        // http://www.hibernatespatial.org/pipermail/hibernatespatial-users/2013-August/000876.html
        Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
        if (GeometryHandler.getInstance().isSpatialDatasource()
                && HibernateHelper.supportsFunction(dialect, HibernateConstants.FUNC_EXTENT)) {
            Criteria criteria = session.createCriteria(clazz);
            criteria.setProjection(SpatialProjections.extent(FeatureOfInterest.GEOMETRY));
            Criteria createCriteria = criteria.createCriteria(AbstractSpatialFilteringProfile.OBSERVATION);
            createCriteria.createCriteria(AbstractObservation.OFFERINGS)
                    .add(Restrictions.eq(Offering.IDENTIFIER, offeringID));
            LOGGER.debug("QUERY getEnvelopeForOfferingId(offeringID): {}",
                    HibernateHelper.getSqlString(criteria));
            Geometry geom = (Geometry) criteria.uniqueResult();
            if (geom != null) {
                int srid = geom.getSRID() > 0 ? geom.getSRID() : GeometryHandler.getInstance().getStorageEPSG();
                geom.setSRID(srid);
                return new SosEnvelope(GeometryHandler.getInstance()
                        .switchCoordinateAxisFromToDatasourceIfNeeded(geom).getEnvelopeInternal(), srid);
            }
        } else {
            final Envelope envelope = new Envelope();
            Criteria criteria = session.createCriteria(getSpatialFilteringProfileImpl().getClass());
            Criteria createCriteria = criteria.createCriteria(AbstractSpatialFilteringProfile.OBSERVATION);
            createCriteria.createCriteria(AbstractObservation.OFFERINGS)
                    .add(Restrictions.eq(Offering.IDENTIFIER, offeringID));
            LOGGER.debug("QUERY getEnvelopeForOfferingId(offeringID): {}",
                    HibernateHelper.getSqlString(criteria));
            @SuppressWarnings("unchecked")
            final List<AbstractSpatialFilteringProfile> spatialFilteringProfiles = criteria.list();
            if (CollectionHelper.isNotEmpty(spatialFilteringProfiles)) {
                for (final AbstractSpatialFilteringProfile spatialFilteringProfile : spatialFilteringProfiles) {
                    try {
                        final Geometry geom = getGeomtery(spatialFilteringProfile);
                        if (geom != null && geom.getEnvelopeInternal() != null) {
                            envelope.expandToInclude(geom.getEnvelopeInternal());
                        }
                    } catch (final OwsExceptionReport owse) {
                        LOGGER.warn(String.format("Error while adding '%s' to envelope!",
                                spatialFilteringProfile.getSpatialFilteringProfileId()), owse);
                    }

                }
                if (!envelope.isNull()) {
                    return new SosEnvelope(envelope, GeometryHandler.getInstance().getStorageEPSG());
                }
            }
        }
    } catch (final HibernateException he) {
        throw new NoApplicableCodeException().causedBy(he)
                .withMessage("Exception thrown while requesting feature envelope for observation ids");
    }
    return null;
}

From source file:org.n52.sos.ds.hibernate.HibernateFeatureQueryHandler.java

License:Open Source License

@Override
public SosEnvelope getEnvelopeForFeatureIDs(FeatureQueryHandlerQueryObject queryObject)
        throws OwsExceptionReport {
    final Session session = HibernateSessionHolder.getSession(queryObject.getConnection());
    if (queryObject.isSetFeatureIdentifiers()) {
        try {// w ww . j av a2  s .c om
            // XXX workaround for Hibernate Spatial's lack of support for
            // GeoDB's extent aggregate
            // see
            // http://www.hibernatespatial.org/pipermail/hibernatespatial-users/2013-August/000876.html
            Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
            if (GeometryHandler.getInstance().isSpatialDatasource()
                    && HibernateHelper.supportsFunction(dialect, HibernateConstants.FUNC_EXTENT)) {
                // Criteria featureExtentCriteria =
                // session.createCriteria(FeatureOfInterest.class)
                // .add(Restrictions.in(FeatureOfInterest.IDENTIFIER,
                // featureIDs))
                // .setProjection(SpatialProjections.extent(FeatureOfInterest.GEOMETRY));
                // LOGGER.debug("QUERY getEnvelopeForFeatureIDs(featureIDs): {}",
                // HibernateHelper.getSqlString(featureExtentCriteria));
                // Geometry geom = (Geometry)
                // featureExtentCriteria.uniqueResult();
                Geometry geom = (Geometry) session.createCriteria(FeatureOfInterest.class)
                        .add(Restrictions.in(FeatureOfInterest.IDENTIFIER, queryObject.getFeatureIdentifiers()))
                        .setProjection(SpatialProjections.extent(FeatureOfInterest.GEOMETRY)).uniqueResult();
                if (geom != null) {
                    int srid = geom.getSRID() > 0 ? geom.getSRID() : getStorageEPSG();
                    geom.setSRID(srid);
                    geom = getGeometryHandler().switchCoordinateAxisFromToDatasourceIfNeeded(geom);
                    return new SosEnvelope(geom.getEnvelopeInternal(), srid);
                }
            } else {
                final Envelope envelope = new Envelope();
                final List<FeatureOfInterest> featuresOfInterest = new FeatureOfInterestDAO()
                        .getFeatureOfInterestObject(queryObject.getFeatureIdentifiers(), session);
                for (final FeatureOfInterest feature : featuresOfInterest) {
                    try {
                        // TODO Check if prepareGeometryForResponse required
                        // transform/switch
                        // final Geometry geom =
                        // getGeometryHandler().prepareGeometryForResponse(getGeomtery(feature),
                        // queryObject.getRequestedSrid());
                        final Geometry geom = getGeomtery(feature, session);
                        if (geom != null) {
                            envelope.expandToInclude(geom.getEnvelopeInternal());
                        }
                    } catch (final OwsExceptionReport owse) {
                        LOGGER.warn(String.format("Error while adding '%s' to envelope!",
                                feature.getFeatureOfInterestId()), owse);
                    }
                }
                if (!envelope.isNull()) {
                    return new SosEnvelope(envelope, getDefaultEPSG());
                }
            }
        } catch (final HibernateException he) {
            throw new NoApplicableCodeException().causedBy(he)
                    .withMessage("Exception thrown while requesting global feature envelope");
        }
    }
    return null;
}