Search in sources :

Example 1 with DataSourceException

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();
        }
    }
}
Also used : DataSourceException(org.geotools.data.DataSourceException) FactoryException(org.opengis.referencing.FactoryException) Dataset(org.gdal.gdal.Dataset) SpatialReference(org.gdal.osr.SpatialReference)

Example 2 with DataSourceException

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;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) Hints(org.geotools.util.factory.Hints) DataSourceException(org.geotools.data.DataSourceException) FactoryException(org.opengis.referencing.FactoryException) GeoTiffReader(org.geotools.gce.geotiff.GeoTiffReader) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) IOException(java.io.IOException)

Example 3 with DataSourceException

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);
    }
}
Also used : MathTransform(org.opengis.referencing.operation.MathTransform) DataSourceException(org.geotools.data.DataSourceException) Rectangle2D(java.awt.geom.Rectangle2D) GeneralEnvelope(org.geotools.geometry.GeneralEnvelope) TransformException(org.opengis.referencing.operation.TransformException) DataSourceException(org.geotools.data.DataSourceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 4 with DataSourceException

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;
}
Also used : Style(org.geotools.styling.Style) TransformerException(javax.xml.transform.TransformerException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) CommonFactoryFinder(org.geotools.factory.CommonFactoryFinder) FeatureCollection(org.geotools.feature.FeatureCollection) NamedLayer(org.geotools.styling.NamedLayer) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) StyleFactory(org.geotools.styling.StyleFactory) DocServiceException(org.georchestra.mapfishapp.ws.DocServiceException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) SLDTransformer(org.geotools.xml.styling.SLDTransformer) WFSDataStore(org.geotools.data.wfs.WFSDataStore) FeatureIterator(org.geotools.feature.FeatureIterator) Symbolizer(org.geotools.styling.Symbolizer) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) URIBuilder(org.apache.http.client.utils.URIBuilder) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) DataSourceException(org.geotools.data.DataSourceException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Set(java.util.Set) WFSDataStoreFactory(org.geotools.data.wfs.WFSDataStoreFactory) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Serializable(java.io.Serializable) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) E_ClassifType(org.georchestra.mapfishapp.ws.classif.ClassifierCommand.E_ClassifType) Rule(org.geotools.styling.Rule) Optional(java.util.Optional) FeatureSource(org.geotools.data.FeatureSource) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) NameValuePair(org.apache.http.NameValuePair) NameValuePair(org.apache.http.NameValuePair) MalformedURLException(java.net.MalformedURLException) Serializable(java.io.Serializable) HashMap(java.util.HashMap) URISyntaxException(java.net.URISyntaxException) WFSDataStore(org.geotools.data.wfs.WFSDataStore) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) DocServiceException(org.georchestra.mapfishapp.ws.DocServiceException) SocketTimeoutException(java.net.SocketTimeoutException) DataSourceException(org.geotools.data.DataSourceException)

Aggregations

DataSourceException (org.geotools.data.DataSourceException)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)2 FactoryException (org.opengis.referencing.FactoryException)2 Rectangle2D (java.awt.geom.Rectangle2D)1 Serializable (java.io.Serializable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetAddress (java.net.InetAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1