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