/** @see org.geotools.data.coverage.grid.AbstractGridFormat#accepts(java.lang.Object input) */
 @Override
 public boolean accepts(Object input, Hints hints) {
   try {
     return spi.canDecodeInput(input);
   } catch (IOException e) {
     if (LOGGER.isLoggable(Level.FINE)) {
       LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
     }
     return false;
   }
 }
示例#2
0
  /** @see org.geotools.data.coverage.grid.AbstractGridFormat#accepts(java.lang.Object input) */
  @Override
  public boolean accepts(Object input, Hints hints) {
    try {

      // Directories aren't accepted
      if (input != null && input instanceof File) {
        final File directory = (File) input;
        if (!directory.exists() || directory.isDirectory()) return false;
      }
      final ImageInputStream stream = ImageIO.createImageInputStream(input);
      if (spi == null) {
        ImageReader reader = Utils.getReader(stream);
        if (reader != null) spi = reader.getOriginatingProvider();
        else return false;
      }
      return spi.canDecodeInput(stream);
    } catch (IOException e) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
      }
      return false;
    }
  }
示例#3
0
  @Override
  protected boolean canConnect(URL url, Map<String, Serializable> params) {
    if (url == null) {
      return false;
    }
    ImageInputStream inputStream = null;
    Object source = null;
    try {
      // /////////////////////////////////////////////////////////////
      //
      // URL management
      // In case the URL points to a file we need to get to the file
      // directly and avoid caching. In case it points to http or ftp
      // or it is an open stream we have very small to do and we need
      // to enable caching.
      //
      // /////////////////////////////////////////////////////////////
      if (url.getProtocol().equalsIgnoreCase("file")) {
        File file = urlToFile(url);
        if (file.exists() && file.canRead() && file.isFile()) {
          // setting source
          source = file;
        } else {
          return false;
        }
      } else if (url.getProtocol().equalsIgnoreCase("http")
          || url.getProtocol().equalsIgnoreCase("ftp")) {
        source = url.openStream();
      } else {
        return false;
      }

      // get a stream
      inputStream =
          (ImageInputStream)
              ((source instanceof ImageInputStream)
                  ? source
                  : ImageIO.createImageInputStream(source));
      if (inputStream == null) {
        if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Unable to get an ImageInputStream");
        return false;
      }
      // get a reader and check if it is a geotiff
      inputStream.mark();

      // tiff
      if (!readerSpi.canDecodeInput(inputStream)) {
        return false;
      }
      return true;
    } catch (Throwable e) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
      }
      return false;
    } finally {
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (Exception e) {
          if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
        }
      }
      if (source != null && (source instanceof InputStream)) {
        try {
          ((InputStream) source).close();
        } catch (Exception e) {
          if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
        }
      }
    }
  }
示例#4
0
  private void init(
      final BoundingBox granuleBBOX,
      final URL granuleUrl,
      final ImageReaderSpi suggestedSPI,
      final Geometry inclusionGeometry,
      final boolean heterogeneousGranules,
      final boolean handleArtifactsFiltering) {
    this.granuleBBOX = ReferencedEnvelope.reference(granuleBBOX);
    this.granuleUrl = granuleUrl;
    this.inclusionGeometry = inclusionGeometry;
    this.handleArtifactsFiltering = handleArtifactsFiltering;
    filterMe = handleArtifactsFiltering && inclusionGeometry != null;

    // create the base grid to world transformation
    ImageInputStream inStream = null;
    ImageReader reader = null;
    try {
      //
      // get info about the raster we have to read
      //

      // get a stream
      if (cachedStreamSPI == null) {
        cachedStreamSPI = ImageIOExt.getImageInputStreamSPI(granuleUrl, true);
        if (cachedStreamSPI == null) {
          final File file = DataUtilities.urlToFile(granuleUrl);
          if (file != null) {
            if (LOGGER.isLoggable(Level.WARNING)) {
              LOGGER.log(Level.WARNING, Utils.getFileInfo(file));
            }
          }
          throw new IllegalArgumentException(
              "Unable to get an input stream for the provided granule " + granuleUrl.toString());
        }
      }
      assert cachedStreamSPI != null : "no cachedStreamSPI available!";
      inStream =
          cachedStreamSPI.createInputStreamInstance(
              granuleUrl, ImageIO.getUseCache(), ImageIO.getCacheDirectory());
      if (inStream == null) {
        final File file = DataUtilities.urlToFile(granuleUrl);
        if (file != null) {
          if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, Utils.getFileInfo(file));
          }
        }
        throw new IllegalArgumentException(
            "Unable to get an input stream for the provided file " + granuleUrl.toString());
      }

      // get a reader and try to cache the suggested SPI first
      if (cachedReaderSPI == null) {
        inStream.mark();
        if (suggestedSPI != null && suggestedSPI.canDecodeInput(inStream)) {
          cachedReaderSPI = suggestedSPI;
          inStream.reset();
        } else {
          inStream.mark();
          reader = ImageIOExt.getImageioReader(inStream);
          if (reader != null) cachedReaderSPI = reader.getOriginatingProvider();
          inStream.reset();
        }
      }
      reader = cachedReaderSPI.createReaderInstance();
      if (reader == null)
        throw new IllegalArgumentException(
            "Unable to get an ImageReader for the provided file " + granuleUrl.toString());
      reader.setInput(inStream);
      // get selected level and base level dimensions
      final Rectangle originalDimension = Utils.getDimension(0, reader);

      // build the g2W for this tile, in principle we should get it
      // somehow from the tile itself or from the index, but at the moment
      // we do not have such info, hence we assume that it is a simple
      // scale and translate
      final GridToEnvelopeMapper geMapper =
          new GridToEnvelopeMapper(new GridEnvelope2D(originalDimension), granuleBBOX);
      geMapper.setPixelAnchor(
          PixelInCell
              .CELL_CENTER); // this is the default behavior but it is nice to write it down anyway
      this.baseGridToWorld = geMapper.createAffineTransform();

      try {
        if (inclusionGeometry != null) {
          geMapper.setPixelAnchor(PixelInCell.CELL_CORNER);
          Geometry mapped = JTS.transform(inclusionGeometry, geMapper.createTransform().inverse());
          this.granuleROIShape = new ROIGeometry(mapped);
        }

      } catch (TransformException e1) {
        throw new IllegalArgumentException(e1);
      }

      // add the base level
      this.granuleLevels.put(
          Integer.valueOf(0),
          new GranuleOverviewLevelDescriptor(
              1, 1, originalDimension.width, originalDimension.height));

      ////////////////////// Setting overviewController ///////////////////////
      if (heterogeneousGranules) {
        // //
        //
        // Right now we are setting up overviewsController by assuming that
        // overviews are internal images as happens in TIFF images
        // We can improve this by leveraging on coverageReaders
        //
        // //

        // Getting the first level descriptor
        final GranuleOverviewLevelDescriptor baseOverviewLevelDescriptor = granuleLevels.get(0);

        // Variables initialization
        final int numberOfOvervies = reader.getNumImages(true) - 1;
        final AffineTransform2D baseG2W = baseOverviewLevelDescriptor.getGridToWorldTransform();
        final int width = baseOverviewLevelDescriptor.getWidth();
        final int height = baseOverviewLevelDescriptor.getHeight();
        final double resX = AffineTransform2D.getScaleX0(baseG2W);
        final double resY = AffineTransform2D.getScaleY0(baseG2W);
        final double[] highestRes = new double[] {resX, resY};
        final double[][] overviewsResolution = new double[numberOfOvervies][2];

        // Populating overviews and initializing overviewsController
        for (int i = 0; i < numberOfOvervies; i++) {
          overviewsResolution[i][0] = (highestRes[0] * width) / reader.getWidth(i + 1);
          overviewsResolution[i][1] = (highestRes[1] * height) / reader.getWidth(i + 1);
        }
        overviewsController =
            new OverviewsController(highestRes, numberOfOvervies, overviewsResolution);
      }
      //////////////////////////////////////////////////////////////////////////

    } catch (IllegalStateException e) {
      throw new IllegalArgumentException(e);

    } catch (IOException e) {
      throw new IllegalArgumentException(e);
    } finally {
      // close/dispose stream and readers
      try {
        if (inStream != null) {
          inStream.close();
        }
      } catch (Throwable e) {
        throw new IllegalArgumentException(e);
      } finally {
        if (reader != null) {
          reader.dispose();
        }
      }
    }
  }
示例#5
0
  @Override
  protected boolean canConnect(URL url, Map<String, Serializable> params) {

    if (url == null) {
      return false;
    }
    ImageInputStream inputStream = null;
    Object source = null;
    try {
      // /////////////////////////////////////////////////////////////
      //
      // URL management
      // In case the URL points to a file we need to get to the file
      // directly and avoid caching. In case it points to http or ftp
      // or it is an open stream we have very small to do and we need
      // to enable caching.
      //
      // /////////////////////////////////////////////////////////////
      if (url.getProtocol().equalsIgnoreCase("file")) {
        File file = new File(URLDecoder.decode(url.getFile(), "UTF-8"));

        if (file.exists()) {
          if (!file.canRead() || !file.isFile()) {
            if (LOGGER.isLoggable(Level.FINE))
              LOGGER.fine("File cannot be read or it is a directory");
            return false;
          }

          if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Provided URL is a file");
          // setting source
          source = file;

        } else {
          if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("File doesn't exist");
          return false;
        }

      } else {
        if (url.getProtocol().toLowerCase().startsWith("http")
            || url.getProtocol().equalsIgnoreCase("dods")) {
          URIImageInputStream uriInStream = new URIImageInputStreamImpl(url.toURI());
          source = uriInStream;
          if (!spi.canDecodeInput(source)) {
            if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Unable to decode the inputStream");
            return false;
          }
          return true;
        } else if (url.getProtocol().equalsIgnoreCase("ftp")) {
          source = url.openStream();
        } else {
          if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Unsupported protocol");
          return false;
        }
      }

      // get a stream
      inputStream =
          (ImageInputStream)
              ((source instanceof ImageInputStream)
                  ? source
                  : (source instanceof File)
                      ? new FileImageInputStreamExtImpl((File) source)
                      : ImageIO.createImageInputStream(source));
      if (inputStream == null) {
        if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Unable to get an ImageInputStream");
        return false;
      }

      inputStream.mark();

      if (!spi.canDecodeInput(inputStream)) {
        if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Unable to decode the inputStream");
        return false;
      }

      return true;
    } catch (Throwable e) {
      if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
      return false;
    } finally {

      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (Exception e) {

        }
      }
      if (source != null && (source instanceof InputStream)) {
        try {
          ((InputStream) source).close();
        } catch (Exception e) {
          if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
        }
      }
    }
  }