@Test
  public void testForceCoverage() throws Exception {
    // force the data to another projection
    Catalog catalog = getCatalog();
    CoverageInfo ci = catalog.getCoverageByName("usa");
    ci.setProjectionPolicy(ProjectionPolicy.FORCE_DECLARED);
    ci.setSRS("EPSG:3857");
    catalog.save(ci);

    ci = catalog.getCoverageByName("usa");
    assertEquals(ProjectionPolicy.FORCE_DECLARED, ci.getProjectionPolicy());
    assertEquals("EPSG:3857", ci.getSRS());

    // now get the reader via the coverage info
    AbstractGridCoverage2DReader r;
    r = (AbstractGridCoverage2DReader) ci.getGridCoverageReader(null, GeoTools.getDefaultHints());
    assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:3857"), r.getCrs()));

    // and again without any hint
    r = (AbstractGridCoverage2DReader) ci.getGridCoverageReader(null, null);
    assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:3857"), r.getCrs()));

    // get the reader straight: we should get back the native projection
    CoverageStoreInfo store = catalog.getCoverageStoreByName("usa");
    final ResourcePool rpool = catalog.getResourcePool();
    r =
        (AbstractGridCoverage2DReader)
            rpool.getGridCoverageReader(store, GeoTools.getDefaultHints());
    assertTrue(CRS.equalsIgnoreMetadata(CRS.decode("EPSG:4326"), r.getCrs()));
  }
  @Test
  public void testReproject() throws Exception {
    String xml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<GetCoverage version=\"1.0.0\" service=\"WCS\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
            + "xmlns=\"http://www.opengis.net/wcs\" "
            + "xmlns:ows=\"http://www.opengis.net/ows/1.1\" "
            + "xmlns:gml=\"http://www.opengis.net/gml\" "
            + "xmlns:ogc=\"http://www.opengis.net/ogc\" "
            + "xsi:schemaLocation=\"http://www.opengis.net/wcs http://schemas.opengis.net/wcs/1.0.0/getCoverage.xsd\">\n"
            + "  <sourceCoverage>"
            + getLayerId(TASMANIA_BM)
            + "</sourceCoverage>\n"
            + "  <domainSubset>\n"
            + "    <spatialSubset>\n"
            + "      <gml:Envelope srsName=\"EPSG:4326\">\n"
            + "        <gml:pos>146 -45</gml:pos>\n"
            + "        <gml:pos>147 42</gml:pos>\n"
            + "      </gml:Envelope>\n"
            + "      <gml:Grid dimension=\"2\">\n"
            + "        <gml:limits>\n"
            + "          <gml:GridEnvelope>\n"
            + "            <gml:low>0 0</gml:low>\n"
            + "            <gml:high>150 150</gml:high>\n"
            + "          </gml:GridEnvelope>\n"
            + "        </gml:limits>\n"
            + "        <gml:axisName>x</gml:axisName>\n"
            + "        <gml:axisName>y</gml:axisName>\n"
            + "      </gml:Grid>\n"
            + "    </spatialSubset>\n"
            + "  </domainSubset>\n"
            + "  <output>\n"
            + "    <crs>EPSG:3857</crs>\n"
            + "    <format>image/geotiff</format>\n"
            + "  </output>\n"
            + "</GetCoverage>";

    MockHttpServletResponse response = postAsServletResponse("wcs", xml);
    assertEquals("image/tiff", response.getContentType());

    GeoTiffFormat format = new GeoTiffFormat();
    AbstractGridCoverage2DReader reader = format.getReader(getBinaryInputStream(response));

    assertEquals(
        CRS.decode("EPSG:3857"), reader.getOriginalEnvelope().getCoordinateReferenceSystem());
  }
  @Override
  public GridCoverage2D read(GeneralParameterValue[] parameters)
      throws IllegalArgumentException, IOException {
    File baseDir = FileUtils.createTempDirectory();

    try {
      File file = readToFile(baseDir, "WcsCoverageReader", parameters);

      // since the requested format may be one of several we need to look
      // up the format object
      // for reading the coverage from the file
      Format format = GridFormatFinder.findFormat(file);

      if (format instanceof AbstractGridFormat && UnknownFormat.class != format.getClass()) {
        AbstractGridFormat gridFormat = (AbstractGridFormat) format;

        // Now we need to find the parameters provided by the caller for
        // reading the coverage from the file
        // See the format API for details
        GeneralParameterValue[] readParams = new GeneralParameterValue[0];
        try {
          ParameterValueGroup group = format.getReadParameters();
          List<GeneralParameterValue> list = new ArrayList<GeneralParameterValue>();
          for (GeneralParameterValue paramValue : group.values()) {
            list.addAll(find(paramValue, parameters));
          }
          LOG.debug("Reading coverage from file using parameters: " + list);
          readParams = list.toArray(new GeneralParameterValue[list.size()]);
        } catch (Exception e) {
          // if can't configure the parameters then try to read with
          // what we have been able to configure
          LOG.warn("Exception occurred while getting request params for reading coverage", e);
        }
        AbstractGridCoverage2DReader reader = gridFormat.getReader(file);
        GridCoverage2D coverage = reader.read(readParams);
        return coverage;
      }
      throw new IllegalArgumentException(
          "Current configuration is unable to read coverage of "
              + file.getName()
              + " format.  "
              + "Check that you have the correct geotools plugins");

    } finally {
      FileUtils.delete(baseDir);
    }
  }
 public String[] listSubNames() {
   return delegate.listSubNames();
 }
 public boolean hasMoreGridCoverages() {
   return delegate.hasMoreGridCoverages();
 }
 public double[] getReadingResolutions(OverviewPolicy policy, double[] requestedResolution) {
   return delegate.getReadingResolutions(policy, requestedResolution);
 }
 public String getMetadataValue(String name) {
   return delegate.getMetadataValue(name);
 }
 public String[] getMetadataNames() {
   return delegate.getMetadataNames();
 }
 public ServiceInfo getInfo() {
   return delegate.getInfo();
 }
 public Format getFormat() {
   return delegate.getFormat();
 }
 public String getCurrentSubname() {
   return delegate.getCurrentSubname();
 }
 public void dispose() {
   delegate.dispose();
 }
 public DecoratingGridCoverage2DReader(AbstractGridCoverage2DReader delegate) {
   this.delegate = delegate;
   this.crs = delegate.getCrs();
   this.originalEnvelope = delegate.getOriginalEnvelope();
   this.originalGridRange = delegate.getOriginalGridRange();
 }
Exemple #14
0
  /**
   * Adds a raster layer to the setup.
   *
   * <p>This method configures a raster layer with the name <code>qName.getLocalPart()</code>. A
   * coverage store is created (if it doesn't already exist) with the same name. The workspace of
   * the resulting store and layer is determined by <code>qName.getPrefix()</code>.
   *
   * <p>The <tt>filename</tt> parameter defines the raster file to be loaded from the classpath and
   * copied into the data directory. The <tt>scope</tt> is used as the class from which to load the
   * file from.
   *
   * <p>In the case of adding a zipped archive that contains multiple file the <tt>filename</tt>
   * paramter should have a ".zip" extension and the <tt>extension</tt> parameter must define the
   * extension of the main raster file. The parameter is not necessary and may be null if the
   * <tt>filename</tt> does not refer to a zip file.
   *
   * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the
   * {@link LayerProperty} class for supported properties.
   *
   * @param qName The name of the raster layer.
   * @param filename The name of the file containing the raster, to be loaded from the classpath.
   * @param extension The file extension (without a ".") of the main raster file. This parameter my
   *     be <code>null</code> only if <tt>filename</tt> does not refer to a zip file.
   * @param props Custom properties to assign to the created raster layer.
   * @param scope The class from which to load the <tt>filename</tt> resource from.
   */
  public void addRasterLayer(
      QName qName,
      String filename,
      String extension,
      Map<LayerProperty, Object> props,
      Class scope,
      Catalog catalog)
      throws IOException {

    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();

    // setup the data
    File dir = new File(data, name);
    dir.mkdirs();

    File file = new File(dir, filename);
    catalog.getResourceLoader().copyFromClassPath(filename, file, scope);

    String ext = FilenameUtils.getExtension(filename);
    if ("zip".equalsIgnoreCase(ext)) {

      // unpack the archive
      IOUtils.decompress(file, dir);

      // delete archive
      file.delete();

      if (extension == null) {
        // zip with no extension, we just the directory as the file
        file = dir;
      } else {
        // files may have been top level, or one directory level deep
        file = new File(dir, FilenameUtils.getBaseName(filename) + "." + extension);
        if (!file.exists()) {
          File file2 = new File(new File(dir, dir.getName()), file.getName());
          if (file2.exists()) {
            file = file2;
          }
        }
      }

      if (!file.exists()) {
        throw new FileNotFoundException(file.getPath());
      }
    }

    // load the format/reader
    AbstractGridFormat format = (AbstractGridFormat) GridFormatFinder.findFormat(file);
    if (format == null) {
      throw new RuntimeException("No format for " + file.getCanonicalPath());
    }
    AbstractGridCoverage2DReader reader = null;
    try {
      reader = (AbstractGridCoverage2DReader) format.getReader(file);
      if (reader == null) {
        throw new RuntimeException(
            "No reader for " + file.getCanonicalPath() + " with format " + format.getName());
      }

      // configure workspace if it doesn;t already exist
      if (catalog.getWorkspaceByName(prefix) == null) {
        addWorkspace(prefix, qName.getNamespaceURI(), catalog);
      }
      // create the store
      CoverageStoreInfo store = catalog.getCoverageStoreByName(prefix, name);
      if (store == null) {
        store = catalog.getFactory().createCoverageStore();
      }

      store.setName(name);
      store.setWorkspace(catalog.getWorkspaceByName(prefix));
      store.setEnabled(true);
      store.setURL(DataUtilities.fileToURL(file).toString());
      store.setType(format.getName());

      if (store.getId() == null) {
        catalog.add(store);
      } else {
        catalog.save(store);
      }

      // create the coverage
      CatalogBuilder builder = new CatalogBuilder(catalog);
      builder.setStore(store);

      CoverageInfo coverage = null;

      try {

        coverage = builder.buildCoverage(reader, null);
        // coverage read params
        if (format instanceof ImageMosaicFormat) {
          //  make sure we work in immediate mode
          coverage
              .getParameters()
              .put(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().getCode(), Boolean.FALSE);
        }
      } catch (Exception e) {
        throw new IOException(e);
      }

      coverage.setName(name);
      coverage.setTitle(name);
      coverage.setDescription(name);
      coverage.setEnabled(true);

      CoverageInfo cov = catalog.getCoverageByCoverageStore(store, name);
      if (cov == null) {
        catalog.add(coverage);
      } else {
        builder.updateCoverage(cov, coverage);
        catalog.save(cov);
        coverage = cov;
      }

      LayerInfo layer = catalog.getLayerByName(new NameImpl(qName));
      if (layer == null) {
        layer = catalog.getFactory().createLayer();
      }
      layer.setResource(coverage);

      layer.setDefaultStyle(
          catalog.getStyleByName(LayerProperty.STYLE.get(props, DEFAULT_RASTER_STYLE)));
      layer.setType(LayerInfo.Type.RASTER);
      layer.setEnabled(true);

      if (layer.getId() == null) {
        catalog.add(layer);
      } else {
        catalog.save(layer);
      }
    } finally {
      if (reader != null) {
        reader.dispose();
      }
    }
  }
 public GridCoverage2D read(GeneralParameterValue[] parameters)
     throws IllegalArgumentException, IOException {
   return delegate.read(parameters);
 }
 public void skip() {
   delegate.skip();
 }
 public int getGridCoverageCount() {
   return delegate.getGridCoverageCount();
 }