use of org.geotools.data.DataSourceException in project geoserver-grass-raster-datastore by mundialis.
the class GrassGdalReader method initialize.
private void initialize(File file) throws DataSourceException {
synchronized (GrassGdalReader.class) {
Dataset dataset = gdal.OpenShared(file.getAbsolutePath(), gdalconstConstants.GA_ReadOnly);
if (dataset == null || !dataset.GetDriver().getShortName().equals("GRASS")) {
throw new DataSourceException("The file is not a valid GRASS raster.");
}
try {
width = dataset.getRasterXSize();
height = dataset.getRasterYSize();
String crsWkt;
String projRef = dataset.GetProjectionRef();
if (projRef != null) {
SpatialReference spatialReference = new SpatialReference(projRef);
crsWkt = spatialReference.ExportToPrettyWkt();
spatialReference.delete();
try {
crs = CRS.parseWKT(crsWkt);
} catch (FactoryException e) {
LOGGER.info("CRS WKT could not be parsed, ignoring.");
LOGGER.fine(e.getMessage() + ExceptionUtils.getStackTrace(e));
}
}
calculateEnvelope(dataset);
numBands = dataset.getRasterCount();
} finally {
// this closes the dataset...
dataset.delete();
}
}
}
use of org.geotools.data.DataSourceException in project geo-platform by geosdi.
the class GPPublisherBasicServiceImpl method getCRSFromGeotiff.
private Integer getCRSFromGeotiff(File file) {
Integer code = null;
try {
GeoTiffReader geotiffReader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, TRUE));
GridCoverage2D coverage = (GridCoverage2D) geotiffReader.read(null);
CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
code = CRS.lookupEpsgCode(crs, TRUE);
} catch (DataSourceException ex) {
logger.error("Errore retrieving the crs: " + ex);
} catch (IOException ioe) {
logger.error("Errore retrieving the crs: " + ioe);
} catch (FactoryException ioe) {
logger.error("Errore retrieving the crs: " + ioe);
}
return code;
}
use of org.geotools.data.DataSourceException in project geowave by locationtech.
the class GeoWaveRasterReader method transformRequestEnvelope.
/**
* transforms (if necessary) the requested envelope into the CRS used by this reader.
*
* @throws DataSourceException
*/
public static void transformRequestEnvelope(final GeoWaveRasterReaderState state, final CoordinateReferenceSystem crs) throws DataSourceException {
if (CRS.equalsIgnoreMetadata(state.getRequestedEnvelope().getCoordinateReferenceSystem(), crs)) {
state.setRequestEnvelopeXformed(state.getRequestedEnvelope());
// and finish
return;
}
try {
/**
* Buffered factory for coordinate operations.
*/
// transforming the envelope back to the dataset crs in
final MathTransform transform = OPERATION_FACTORY.createOperation(state.getRequestedEnvelope().getCoordinateReferenceSystem(), crs).getMathTransform();
if (transform.isIdentity()) {
// Identity Transform ?
state.setRequestEnvelopeXformed(state.getRequestedEnvelope());
// and finish
return;
}
state.setRequestEnvelopeXformed(CRS.transform(transform, state.getRequestedEnvelope()));
state.getRequestEnvelopeXformed().setCoordinateReferenceSystem(crs);
// if (config.getIgnoreAxisOrder() == false) { // check for axis
// order
// required
final int indexX = indexOfX(crs);
final int indexY = indexOfY(crs);
final int indexRequestedX = indexOfX(state.getRequestedEnvelope().getCoordinateReferenceSystem());
final int indexRequestedY = indexOfY(state.getRequestedEnvelope().getCoordinateReferenceSystem());
// x Axis problem ???
if ((indexX == indexRequestedY) && (indexY == indexRequestedX)) {
state.setAxisSwap(true);
final Rectangle2D tmp = new Rectangle2D.Double(state.getRequestEnvelopeXformed().getMinimum(1), state.getRequestEnvelopeXformed().getMinimum(0), state.getRequestEnvelopeXformed().getSpan(1), state.getRequestEnvelopeXformed().getSpan(0));
state.setRequestEnvelopeXformed(new GeneralEnvelope(tmp));
state.getRequestEnvelopeXformed().setCoordinateReferenceSystem(crs);
} else if ((indexX == indexRequestedX) && (indexY == indexRequestedY)) {
// everything is fine
} else {
throw new DataSourceException("Unable to resolve the X Axis problem");
}
// }
} catch (final Exception e) {
throw new DataSourceException("Unable to create a coverage for this source", e);
}
}
use of org.geotools.data.DataSourceException in project georchestra by georchestra.
the class SLDClassifier method connectToWFS.
/**
* Gives a connection to a remote WFS
*
* @param getCapsUrl URL of the WFS. Should be a GetCapabilities request
* @return Virtual DataStore. All features can be extracted from it.
* @throws DocServiceException
*/
private WFSDataStore connectToWFS(URL getCapsUrl) throws DocServiceException {
try {
// Force WFS version to 1.1.0, if not specified, defaulting to the higest server
// version (2.0) will break parsing
URIBuilder uribuilder = new URIBuilder(getCapsUrl.toURI());
Optional<NameValuePair> version = uribuilder.getQueryParams().stream().filter(p -> p.getName().equalsIgnoreCase("version")).findFirst();
if (!version.isPresent()) {
uribuilder.setParameter("VERSION", "1.1.0");
} else if (!"1.1.0".contentEquals(version.get().getValue())) {
uribuilder.setParameter(version.get().getName(), "1.1.0");
}
getCapsUrl = uribuilder.build().toURL();
} catch (URISyntaxException | MalformedURLException e) {
throw new IllegalArgumentException("Invalid uri: " + getCapsUrl, e);
}
WFSDataStore wfs = null;
Map<String, Serializable> m = new HashMap<>();
try {
UsernamePasswordCredentials credentials = findCredentials(getCapsUrl);
if (credentials != null) {
m.put(WFSDataStoreFactory.USERNAME.key, credentials.getUserName());
m.put(WFSDataStoreFactory.PASSWORD.key, credentials.getPassword());
}
// connect to remote WFS
m.put(WFSDataStoreFactory.URL.key, getCapsUrl);
// default: 3000
m.put(WFSDataStoreFactory.TIMEOUT.key, 60000);
// TODO : .key necessary for those two ?
// try to optimize communication
m.put(WFSDataStoreFactory.TRY_GZIP.key, Boolean.TRUE);
// try to force UTF-8
m.put(WFSDataStoreFactory.ENCODING.key, "UTF-8");
// TODO : configurable ?
m.put(WFSDataStoreFactory.MAXFEATURES.key, 2000);
m.put(WFSDataStoreFactory.LENIENT.key, Boolean.TRUE);
wfs = _factory.createDataStore(m);
} catch (SocketTimeoutException e) {
throw new DocServiceException("WFS is unavailable", HttpServletResponse.SC_GATEWAY_TIMEOUT);
} catch (DataSourceException e) {
// happens when wfs url is wrong (missing request or service parameters...)
throw new DocServiceException(e.getMessage(), HttpServletResponse.SC_BAD_REQUEST);
} catch (IOException e) {
LOG.error("Error occured while connecting to the WFS url", e);
throw new DocServiceException("Error while creating a WFS object", HttpServletResponse.SC_BAD_REQUEST);
}
return wfs;
}
Aggregations