Search in sources :

Example 1 with CRSUtils

use of org.n52.io.crs.CRSUtils in project series-rest-api by 52North.

the class IoParameters method extendBy.

/**
     * Extends the bounding box with the given point. If point is contained by this instance nothing is
     * changed.
     *
     * @param point
     *        the point in CRS:84 which shall extend the bounding box.
     */
private void extendBy(Point point, BoundingBox bbox) {
    if (bbox.contains(point)) {
        return;
    }
    double llX = Math.min(point.getX(), bbox.getLowerLeft().getX());
    double llY = Math.max(point.getX(), bbox.getUpperRight().getX());
    double urX = Math.min(point.getY(), bbox.getLowerLeft().getY());
    double urY = Math.max(point.getY(), bbox.getUpperRight().getY());
    CRSUtils crsUtils = CRSUtils.createEpsgForcedXYAxisOrder();
    bbox.setLl(crsUtils.createPoint(llX, llY, bbox.getSrs()));
    bbox.setUr(crsUtils.createPoint(urX, urY, bbox.getSrs()));
}
Also used : CRSUtils(org.n52.io.crs.CRSUtils)

Example 2 with CRSUtils

use of org.n52.io.crs.CRSUtils in project series-rest-api by 52North.

the class IoParameters method mergeBounds.

private BoundingBox mergeBounds(BoundingBox bounds, BBox bboxBounds) {
    if (bboxBounds == null) {
        // nothing to merge
        return bounds;
    }
    CRSUtils crsUtils = CRSUtils.createEpsgForcedXYAxisOrder();
    Point lowerLeft = crsUtils.convertToPointFrom(bboxBounds.getLl());
    Point upperRight = crsUtils.convertToPointFrom(bboxBounds.getUr());
    if (bounds == null) {
        BoundingBox parsed = new BoundingBox(lowerLeft, upperRight, CRSUtils.DEFAULT_CRS);
        LOGGER.debug("Parsed bbox bounds: {}", parsed.toString());
        return parsed;
    } else {
        extendBy(lowerLeft, bounds);
        extendBy(upperRight, bounds);
        LOGGER.debug("Merged bounds: {}", bounds.toString());
        return bounds;
    }
}
Also used : BoundingBox(org.n52.io.crs.BoundingBox) CRSUtils(org.n52.io.crs.CRSUtils) Point(com.vividsolutions.jts.geom.Point) GeojsonPoint(org.n52.io.geojson.old.GeojsonPoint)

Example 3 with CRSUtils

use of org.n52.io.crs.CRSUtils in project series-rest-api by 52North.

the class IoParameters method createBbox.

/**
 * @return a {@link BBox} instance or <code>null</code> if no {@link #BBOX} parameter is present.
 * @throws IoParseException
 *         if parsing parameter fails.
 * @throws IoParseException
 *         if a requested {@value #CRS} object could not be created
 */
private BoundingBox createBbox() {
    if (!containsParameter(BBOX)) {
        return null;
    }
    String bboxValue = getAsString(BBOX);
    CRSUtils crsUtils = CRSUtils.createEpsgForcedXYAxisOrder();
    // Check if supplied in minx,miny,maxx,maxy format - else assume json
    if (bboxMatching(bboxValue, 3)) {
        String[] coordArray = bboxValue.split(SPLIT_REGEX);
        Point lowerLeft = crsUtils.createPoint(Double.valueOf(coordArray[0].trim()), Double.valueOf(coordArray[1].trim()), CRSUtils.DEFAULT_CRS);
        Point upperRight = crsUtils.createPoint(Double.valueOf(coordArray[2].trim()), Double.valueOf(coordArray[3].trim()), CRSUtils.DEFAULT_CRS);
        return new BoundingBox(lowerLeft, upperRight, CRSUtils.DEFAULT_CRS);
    }
    try {
        BBox bbox = handleJsonValueParseException(BBOX, BBox.class, this::parseJson);
        return new BoundingBox(bbox.getLl(), bbox.getUr(), CRSUtils.DEFAULT_CRS);
    } catch (IoParseException e) {
        throw e.addHint(createInvalidParameterMessage(Parameters.BBOX)).addHint("Check http://epsg-registry.org for EPSG CRS definitions and codes.").addHint("(alternate format of 'llLon,llLat,urLon,urLat' couldn't be detected)");
    }
}
Also used : IoParseException(org.n52.io.IoParseException) BoundingBox(org.n52.io.crs.BoundingBox) CRSUtils(org.n52.io.crs.CRSUtils) Point(org.locationtech.jts.geom.Point)

Example 4 with CRSUtils

use of org.n52.io.crs.CRSUtils in project series-rest-api by 52North.

the class Vicinity method calculateBounds.

/**
 * Calculates bounding box with the given CRS context.
 *
 * @param crsUtils the reference context.
 * @return a bounding rectangle.
 * @throws IllegalStateException if invalid crs was set.
 */
public BoundingBox calculateBounds(CRSUtils crsUtils) {
    double latInRad = Math.toRadians(center.getY());
    final double latitudeDelta = WGS84Util.getLatitudeDelta(radius);
    final double longitudeDelta = WGS84Util.getLongitudeDelta(latInRad, radius);
    double llEasting = WGS84Util.normalizeLongitude(center.getX() - longitudeDelta);
    double llNorthing = WGS84Util.normalizeLatitude(center.getY() - latitudeDelta);
    double urEasting = WGS84Util.normalizeLongitude(center.getX() + longitudeDelta);
    double urNorthing = WGS84Util.normalizeLatitude(center.getY() + latitudeDelta);
    try {
        if (crsUtils.isLatLonAxesOrder(crs)) {
            Point ll = crsUtils.createPoint(llNorthing, llEasting, crs);
            Point ur = crsUtils.createPoint(urNorthing, urEasting, crs);
            return new BoundingBox(ll, ur, crs);
        }
        Point ll = crsUtils.createPoint(llEasting, llNorthing, crs);
        Point ur = crsUtils.createPoint(urEasting, urNorthing, crs);
        return new BoundingBox(ll, ur, crs);
    } catch (FactoryException e) {
        throw new IllegalStateException("Illegal CRS parameter: " + crs, e);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) BoundingBox(org.n52.io.crs.BoundingBox) Point(org.locationtech.jts.geom.Point)

Example 5 with CRSUtils

use of org.n52.io.crs.CRSUtils in project series-rest-api by 52North.

the class IoParameters method transformToInnerCrs.

/**
     * @param point
     *        a GeoJSON point to be transformed to internally used CRS:84.
     * @param crsUtils
     *        a reference helper.
     * @return a transformed GeoJSON instance.
     * @throws IoParseException
     *         if point could not be transformed, or if requested CRS object could not be created.
     */
private GeojsonPoint transformToInnerCrs(GeojsonPoint point, CRSUtils crsUtils) {
    try {
        Point toTransformed = crsUtils.convertToPointFrom(point, getCrs());
        Point crs84Point = (Point) crsUtils.transformOuterToInner(toTransformed, getCrs());
        return crsUtils.convertToGeojsonFrom(crs84Point);
    } catch (TransformException e) {
        throw new IoParseException("Could not transform to internally used CRS:84.", e);
    } catch (FactoryException e) {
        throw new IoParseException("Check if 'crs' parameter is a valid EPSG CRS. Was: '" + getCrs() + "'.", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) Point(com.vividsolutions.jts.geom.Point) GeojsonPoint(org.n52.io.geojson.old.GeojsonPoint)

Aggregations

BoundingBox (org.n52.io.crs.BoundingBox)4 CRSUtils (org.n52.io.crs.CRSUtils)4 Point (org.locationtech.jts.geom.Point)3 Point (com.vividsolutions.jts.geom.Point)2 IoParseException (org.n52.io.IoParseException)2 GeojsonPoint (org.n52.io.geojson.old.GeojsonPoint)2 FactoryException (org.opengis.referencing.FactoryException)2 TransformException (org.opengis.referencing.operation.TransformException)1