Search in sources :

Example 1 with CapabilitiesException

use of org.geotoolkit.client.CapabilitiesException in project geotoolkit by Geomatys.

the class WMSResource method prepareQuery.

/**
 * Prepare parameters for a getMap query.
 * The given parameters will be modified !
 */
public void prepareQuery(final GetMapRequest request, final GeneralEnvelope env, final Dimension dim, final Point2D pickCoord) throws TransformException, FactoryException {
    // envelope before any modification
    GeneralEnvelope beforeEnv = new GeneralEnvelope(env);
    final CoordinateReferenceSystem crs = env.getCoordinateReferenceSystem();
    CoordinateReferenceSystem crs2D = CRSUtilities.getCRS2D(crs);
    GeneralEnvelope fakeEnv = new GeneralEnvelope(env);
    // check if we must make the  coverage reprojection ourself--------------
    boolean supportCRS = false;
    try {
        supportCRS = supportCRS(crs2D);
    } catch (CapabilitiesException ex) {
        LOGGER.log(Level.WARNING, ex.toString(), ex);
    }
    final boolean longitudeFirst = server.getVersion().longitudeFirst;
    if (isUseLocalReprojection() && !supportCRS) {
        try {
            crs2D = findOriginalCRS();
        } catch (CapabilitiesException ex) {
            // we tryed
            crs2D = null;
        }
        if (crs2D == null) {
            // last chance use : EPSG:4326
            crs2D = CommonCRS.WGS84.geographic();
        }
        if (longitudeFirst && (Utilities.equalsIgnoreMetadata(crs2D, CommonCRS.WGS84.normalizedGeographic()))) {
            // in case we are asking for a WMS in 1.1.0 and CRS:84
            // we must change the crs to 4326 but with CRS:84 coordinate
            final GeneralEnvelope trsEnv = new GeneralEnvelope(ReferencingUtilities.transform2DCRS(env, CommonCRS.WGS84.normalizedGeographic()));
            env.setEnvelope(trsEnv);
            final CoordinateReferenceSystem fakeCrs = ReferencingUtilities.change2DComponent(crs, CommonCRS.WGS84.geographic());
            trsEnv.setCoordinateReferenceSystem(fakeCrs);
            fakeEnv.setEnvelope(trsEnv);
        } else if (longitudeFirst) {
            // in case we are asking for a WMS in 1.1.0 and a geographic crs
            // we must set longitude coordinates first but preserve the crs
            final CoordinateReferenceSystem lfcrs = ReferencingUtilities.setLongitudeFirst(crs2D);
            final GeneralEnvelope trsEnv = new GeneralEnvelope(ReferencingUtilities.transform2DCRS(env, lfcrs));
            env.setEnvelope(trsEnv);
            trsEnv.setCoordinateReferenceSystem(ReferencingUtilities.change2DComponent(crs, crs2D));
            fakeEnv.setEnvelope(trsEnv);
        } else {
            final GeneralEnvelope trsEnv = new GeneralEnvelope(ReferencingUtilities.transform2DCRS(env, crs2D));
            env.setEnvelope(trsEnv);
            fakeEnv.setEnvelope(trsEnv);
        }
    } else {
        if (longitudeFirst && (Utilities.equalsIgnoreMetadata(crs2D, CommonCRS.WGS84.normalizedGeographic()))) {
            // in case we are asking for a WMS in 1.1.0 and CRS:84
            // we must change the crs to 4326 but with CRS:84 coordinate
            final GeneralEnvelope trsEnv = new GeneralEnvelope(env);
            final CoordinateReferenceSystem fakeCrs = ReferencingUtilities.change2DComponent(crs, CommonCRS.WGS84.geographic());
            trsEnv.setCoordinateReferenceSystem(fakeCrs);
            fakeEnv.setEnvelope(trsEnv);
        } else if (longitudeFirst) {
            // in case we are asking for a WMS in 1.1.0 and a geographic crs
            // we must set longitude coordinates first but preserve the crs
            final GeneralEnvelope trsEnv = new GeneralEnvelope(ReferencingUtilities.setLongitudeFirst(env));
            trsEnv.setCoordinateReferenceSystem(crs);
            fakeEnv.setEnvelope(trsEnv);
        }
    }
    // WMS returns images with EAST-WEST axis first, so we ensure we modify the crs as expected
    final Envelope longFirstEnvelope = ReferencingUtilities.setLongitudeFirst(env);
    env.setEnvelope(longFirstEnvelope);
    // Recalculate pick coordinate according to reverse transformation
    if (pickCoord != null) {
        beforeEnv = (GeneralEnvelope) ReferencingUtilities.setLongitudeFirst(beforeEnv);
        // calculate new coordinate in the reprojected query
        final AffineTransform beforeTrs = ReferencingUtilities.toAffine(dim, beforeEnv);
        final AffineTransform afterTrs = ReferencingUtilities.toAffine(dim, env);
        try {
            afterTrs.invert();
        } catch (NoninvertibleTransformException ex) {
            throw new TransformException("Failed to invert transform.", ex);
        }
        beforeTrs.transform(pickCoord, pickCoord);
        final DirectPosition pos = new GeneralDirectPosition(env.getCoordinateReferenceSystem());
        pos.setOrdinate(0, pickCoord.getX());
        pos.setOrdinate(1, pickCoord.getY());
        final MathTransform trs = CRS.findOperation(beforeEnv.getCoordinateReferenceSystem(), env.getCoordinateReferenceSystem(), null).getMathTransform();
        trs.transform(pos, pos);
        pickCoord.setLocation(pos.getOrdinate(0), pos.getOrdinate(1));
        afterTrs.transform(pickCoord, pickCoord);
    }
    prepareGetMapRequest(request, fakeEnv, dim);
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) DirectPosition(org.opengis.geometry.DirectPosition) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) AffineTransform(java.awt.geom.AffineTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CapabilitiesException(org.geotoolkit.client.CapabilitiesException) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope)

Example 2 with CapabilitiesException

use of org.geotoolkit.client.CapabilitiesException in project geotoolkit by Geomatys.

the class WMSUtilities method findClosestDate.

/**
 * Search in the getCapabilities the closest date.
 *
 * @param server web map server
 * @param layername wms layer name
 */
public static Long findClosestDate(final WebMapClient server, final String layername, final long date) throws CapabilitiesException {
    ArgumentChecks.ensureNonNull("server", server);
    ArgumentChecks.ensureNonNull("layer name", layername);
    final AbstractLayer layer = server.getServiceCapabilities().getLayerFromName(layername);
    if (layer != null) {
        for (AbstractDimension dim : layer.getAbstractDimension()) {
            if ("time".equalsIgnoreCase(dim.getName())) {
                // we found the temporal dimension
                final ISODateParser parser = new ISODateParser();
                final String[] dates = dim.getValue().split(",");
                final long d = date;
                Long closest = null;
                for (String str : dates) {
                    str = str.replaceAll("\n", "");
                    str = str.trim();
                    long candidate = parser.parseToMillis(str);
                    if (closest == null) {
                        closest = candidate;
                    } else if (Math.abs(d - candidate) < Math.abs(d - closest)) {
                        closest = candidate;
                    }
                }
                return closest;
            }
        }
    } else {
        throw new CapabilitiesException("Layer : " + layername + " could not be found in the getCapabilities. " + "This can be caused by an incorrect layer name " + "(check case-sensitivity) or a non-compliant wms serveur.");
    }
    return null;
}
Also used : AbstractDimension(org.geotoolkit.wms.xml.AbstractDimension) AbstractLayer(org.geotoolkit.wms.xml.AbstractLayer) ISODateParser(org.geotoolkit.temporal.object.ISODateParser) CapabilitiesException(org.geotoolkit.client.CapabilitiesException)

Example 3 with CapabilitiesException

use of org.geotoolkit.client.CapabilitiesException in project geotoolkit by Geomatys.

the class WMSUtilities method findOriginalCRS.

/**
 * Find the best original crs of the data in the capabilities.
 *
 * @param server web map server
 * @param layername wms layer name
 */
public static CoordinateReferenceSystem findOriginalCRS(final WebMapClient server, final String layername) throws CapabilitiesException {
    ArgumentChecks.ensureNonNull("server", server);
    ArgumentChecks.ensureNonNull("layer name", layername);
    final AbstractLayer[] stack = server.getServiceCapabilities().getLayerStackFromName(layername);
    if (stack != null) {
        // start by the most accurate layer
        for (int i = stack.length - 1; i >= 0; i--) {
            for (final String srid : stack[i].getCRS()) {
                // search and return the first crs that we succesfuly parsed.
                try {
                    CoordinateReferenceSystem crs = CRS.forCode(srid);
                    if (crs != null) {
                        return crs;
                    }
                } catch (FactoryException ex) {
                    LOGGER.log(Level.FINE, "Could not parse crs code : {0}", srid);
                }
            }
        }
    } else {
        throw new CapabilitiesException("Layer : " + layername + " could not be found in the getCapabilities. " + "This can be caused by an incorrect layer name " + "(check case-sensitivity) or a non-compliant wms serveur.");
    }
    return null;
}
Also used : AbstractLayer(org.geotoolkit.wms.xml.AbstractLayer) FactoryException(org.opengis.util.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) CapabilitiesException(org.geotoolkit.client.CapabilitiesException)

Example 4 with CapabilitiesException

use of org.geotoolkit.client.CapabilitiesException in project geotoolkit by Geomatys.

the class WebMapClient method getRootResource.

private synchronized Resource getRootResource() throws DataStoreException {
    if (rootNode != null) {
        return rootNode;
    }
    final AbstractWMSCapabilities capa;
    try {
        capa = getServiceCapabilities();
    } catch (CapabilitiesException ex) {
        throw new DataStoreException(ex);
    }
    rootNode = asResource(capa.getCapability().getLayer()).orElse(null);
    return rootNode;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) AbstractWMSCapabilities(org.geotoolkit.wms.xml.AbstractWMSCapabilities) CapabilitiesException(org.geotoolkit.client.CapabilitiesException)

Example 5 with CapabilitiesException

use of org.geotoolkit.client.CapabilitiesException in project geotoolkit by Geomatys.

the class WMSResource method prepareGetMapRequest.

/**
 * Prepare parameters for a GetMap request.
 *
 * @param request the GetMap request
 * @param env A valid envelope to request.
 * @param rect the output dimension
 * @throws TransformException
 */
private void prepareGetMapRequest(final GetMapRequest request, Envelope env, final Dimension rect) throws TransformException {
    // check the politics, the distant wms server might not be strict on axis orders
    // nor in it's crs definitions between CRS:84 and EPSG:4326
    final CoordinateReferenceSystem crs2D = CRSUtilities.getCRS2D(env.getCoordinateReferenceSystem());
    // check CRS84 politic---------------------------------------------------
    if (crs84Politic != CRS84Politic.STRICT) {
        if (Utilities.equalsIgnoreMetadata(crs2D, CommonCRS.WGS84.normalizedGeographic())) {
            switch(crs84Politic) {
                case CONVERT_TO_EPSG4326:
                    env = Envelopes.transform(env, crs2D);
                    env = new GeneralEnvelope(env);
                    ((GeneralEnvelope) env).setCoordinateReferenceSystem(CommonCRS.WGS84.geographic());
                    break;
            }
        }
    }
    // check EPSG4326 politic------------------------------------------------
    if (epsg4326Politic != EPSG4326Politic.STRICT) {
        if (Utilities.equalsIgnoreMetadata(crs2D, CommonCRS.WGS84.geographic())) {
            switch(epsg4326Politic) {
                case CONVERT_TO_CRS84:
                    env = Envelopes.transform(env, crs2D);
                    env = new GeneralEnvelope(env);
                    ((GeneralEnvelope) env).setCoordinateReferenceSystem(CommonCRS.WGS84.normalizedGeographic());
                    break;
            }
        }
    }
    if (matchCapabilitiesDates) {
        final CoordinateReferenceSystem crs = env.getCoordinateReferenceSystem();
        final int index = dimensionColinearWith(crs.getCoordinateSystem(), CommonCRS.Temporal.JULIAN.crs().getCoordinateSystem().getAxis(0));
        if (index >= 0) {
            // there is a temporal axis
            final double median = env.getMedian(index);
            Long closest = null;
            try {
                closest = findClosestDate((long) median);
            } catch (CapabilitiesException ex) {
            // at least we tryed
            }
            if (closest != null) {
                final GeneralEnvelope adjusted = new GeneralEnvelope(env);
                adjusted.setRange(index, closest, closest);
                env = adjusted;
                LOGGER.log(Level.FINE, "adjusted : {0}", new Date(closest));
            }
        }
    }
    request.setEnvelope(env);
    request.setDimension(rect);
    request.setLayers(getLayerNames());
    if (styles == null) {
        request.setStyles("");
    } else {
        request.setStyles(styles);
    }
    request.setSld(sld);
    request.setSldVersion(sldVersion);
    request.setSldBody(sldBody);
    request.setFormat(format);
    request.setExceptions(exceptionsFormat);
    request.setTransparent(transparent);
    request.dimensions().putAll(dimensions());
}
Also used : CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CapabilitiesException(org.geotoolkit.client.CapabilitiesException) Date(java.util.Date)

Aggregations

CapabilitiesException (org.geotoolkit.client.CapabilitiesException)10 AbstractLayer (org.geotoolkit.wms.xml.AbstractLayer)4 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)4 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)3 AbstractDimension (org.geotoolkit.wms.xml.AbstractDimension)3 AbstractWMSCapabilities (org.geotoolkit.wms.xml.AbstractWMSCapabilities)3 Date (java.util.Date)2 List (java.util.List)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 Capabilities (org.geotoolkit.wps.xml.v200.Capabilities)2 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)2 FactoryException (org.opengis.util.FactoryException)2 AffineTransform (java.awt.geom.AffineTransform)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1