Exemplo n.º 1
0
  @Test
  public void testNetCDFWithDifferentTimeDimensions() throws MalformedURLException, IOException {
    // Selection of the input file
    final File workDir = new File(TestData.file(this, "."), "times");
    if (!workDir.mkdir()) {
      FileUtils.deleteDirectory(workDir);
      assertTrue("Unable to create workdir:" + workDir, workDir.mkdir());
    }

    FileUtils.copyFile(TestData.file(this, "times.zip"), new File(workDir, "times.zip"));
    TestData.unzipFile(this, "times/times.zip");

    final File inputFile = TestData.file(this, "times/times.nc");
    // Get format
    final AbstractGridFormat format =
        (AbstractGridFormat) GridFormatFinder.findFormat(inputFile.toURI().toURL(), null);
    final NetCDFReader reader = new NetCDFReader(inputFile, null);
    Assert.assertNotNull(format);
    Assert.assertNotNull(reader);
    try {
      // Selection of all the Coverage names
      String[] names = reader.getGridCoverageNames();
      assertNotNull(names);
      assertEquals(2, names.length);

      // Parsing metadata values
      assertEquals("true", reader.getMetadataValue(names[0], "HAS_TIME_DOMAIN"));

      List<DimensionDescriptor> descriptors = reader.getDimensionDescriptors(names[0]);
      assertEquals(1, descriptors.size());
      DimensionDescriptor descriptor = descriptors.get(0);
      assertEquals("time", descriptor.getStartAttribute());
      assertEquals("TIME", descriptor.getName());

      descriptors = reader.getDimensionDescriptors(names[1]);
      assertEquals(1, descriptors.size());
      descriptor = descriptors.get(0);
      assertEquals("time1", descriptor.getStartAttribute());
      assertEquals("TIME", descriptor.getName());

      assertEquals("true", reader.getMetadataValue(names[1], "HAS_TIME_DOMAIN"));
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
      FileUtils.deleteDirectory(TestData.file(this, "times"));
    }
  }
    /**
     * Collects features domain to be exposed as metadata
     *
     * @param filePath
     * @param aggregate
     * @param firstFeature
     * @return
     */
    private Map<String, Object> computeGroupMetadata(
        String filePath, boolean aggregate, SimpleFeature firstFeature) {
      Map<String, Object> metadataMap = null;
      List<DimensionDescriptor> dimensionDescriptors = rasterManager.getDimensionDescriptors();
      // extract metadata for the available domains
      if (dimensionDescriptors != null && !dimensionDescriptors.isEmpty()) {
        Filter filter = FF.equals(FF.property("location"), FF.literal(filePath));
        metadataMap = new HashMap<String, Object>();
        try {
          // scan dimensions
          for (DimensionDescriptor descriptor : dimensionDescriptors) {
            String attribute = descriptor.getStartAttribute();
            String name = descriptor.getName();
            Comparable max = null;
            Comparable min = null;
            if (aggregate) {
              Query query = new Query(typeName);
              query.setFilter(filter);
              query.setPropertyNames(Arrays.asList(attribute));
              // Repeat the queries to avoid using a in-Memory
              // featureCollection
              // We may consider caching the features in case
              // the collection size isn't too big

              final MaxVisitor maxVisitor = new MaxVisitor(attribute);
              granuleCatalog.computeAggregateFunction(query, maxVisitor);
              max = maxVisitor.getMax();
              MinVisitor minVisitor = new MinVisitor(attribute);
              granuleCatalog.computeAggregateFunction(query, minVisitor);
              min = minVisitor.getMin();
            } else {
              max = min = (Comparable) firstFeature.getAttribute(attribute);
            }
            addMetadaElement(name, min, max, metadataMap);
          }

          addBBOX(aggregate, filter, firstFeature, metadataMap);
        } catch (IOException e) {
          throw new RuntimeException("Exception occurred while parsing the feature domains", e);
        }
      }
      return metadataMap;
    }
Exemplo n.º 3
0
  @Test
  public void testImageReaderPolyphemunsComplex2() throws Exception {
    File file = null;
    try {
      file = TestData.file(this, "polyphemus_20130301.nc");
    } catch (IOException e) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    if (!file.exists()) {
      LOGGER.warning("Unable to find file polyphemus_20130301.nc");
      return;
    }
    FileUtils.copyFile(file, new File(TestData.file(this, null), "polyphemus.nc"));
    file = TestData.file(this, "polyphemus.nc");
    final NetCDFImageReaderSpi unidataImageReaderSpi = new NetCDFImageReaderSpi();
    assertTrue(unidataImageReaderSpi.canDecodeInput(file));
    NetCDFImageReader reader = null;
    try {
      reader = (NetCDFImageReader) unidataImageReaderSpi.createReaderInstance();
      reader.setInput(file);
      int numImages = reader.getNumImages(true);
      assertEquals(1008, numImages);
      for (int i = 0; i < numImages; i++) {
        Slice2DIndex sliceIndex = reader.getSlice2DIndex(i);
        assertNotNull(sliceIndex);
        spitOutSliceInformation(i, sliceIndex);
      }

      // check dimensions
      CoverageSourceDescriptor cd = reader.getCoverageDescriptor(new NameImpl("NO2"));
      final List<AdditionalDomain> additionalDomains = cd.getAdditionalDomains();
      assertNull(additionalDomains);

      final List<DimensionDescriptor> dimensions = cd.getDimensionDescriptors();
      assertNotNull(dimensions);
      assertTrue(!dimensions.isEmpty());
      assertEquals("wrong dimensions", 2, dimensions.size());
      DimensionDescriptor dim = dimensions.get(0);
      assertTrue(dim.getName().equals("TIME"));
      assertTrue(dim.getStartAttribute().equals("time"));
      dim = dimensions.get(1);
      assertTrue(dim.getName().equals("ELEVATION"));
      assertTrue(dim.getStartAttribute().equals("z"));

      // check coverage names
      final List<Name> names = reader.getCoveragesNames();
      assertNotNull(names);
      assertTrue(!names.isEmpty());
      assertTrue(3 == names.size());
      assertTrue(names.contains(new NameImpl("NO2")));
      assertTrue(names.contains(new NameImpl("O3")));
      assertTrue(names.contains(new NameImpl("V")));

      // checking slice catalog
      final CoverageSlicesCatalog cs = reader.getCatalog();
      assertNotNull(cs);

      // get typenames
      final String[] typeNames = cs.getTypeNames();
      for (String typeName : typeNames) {
        final List<CoverageSlice> granules = cs.getGranules(new Query(typeName, Filter.INCLUDE));
        assertNotNull(granules);
        assertFalse(granules.isEmpty());
        for (CoverageSlice slice : granules) {
          final SimpleFeature sf = slice.getOriginator();
          if (TestData.isInteractiveTest()) {
            LOGGER.info(DataUtilities.encodeFeature(sf));
          }

          // checks
          for (Property p : sf.getProperties()) {
            assertNotNull("Property " + p.getName() + " had a null value!", p.getValue());
          }
        }
      }
    } finally {

      // close reader
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }

      // specific clean up
      FileUtils.deleteDirectory(TestData.file(this, ".polyphemus"));
    }
  }
Exemplo n.º 4
0
  @Test
  public void NetCDFTestOn4Dcoverages()
      throws NoSuchAuthorityCodeException, FactoryException, IOException, ParseException {
    File mosaic = new File(TestData.file(this, "."), "NetCDFTestOn4Dcoverages");
    if (mosaic.exists()) {
      FileUtils.deleteDirectory(mosaic);
    }
    assertTrue(mosaic.mkdirs());

    File file = TestData.file(this, "O3-NO2.nc");
    FileUtils.copyFileToDirectory(file, mosaic);
    file = new File(mosaic, "O3-NO2.nc");
    final Hints hints =
        new Hints(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, CRS.decode("EPSG:4326", true));
    // Get format
    final AbstractGridFormat format =
        (AbstractGridFormat) GridFormatFinder.findFormat(file.toURI().toURL(), hints);
    final NetCDFReader reader = (NetCDFReader) format.getReader(file.toURI().toURL(), hints);

    assertNotNull(format);
    try {
      String[] names = reader.getGridCoverageNames();
      names = new String[] {names[1]};

      for (String coverageName : names) {

        final String[] metadataNames = reader.getMetadataNames(coverageName);
        assertNotNull(metadataNames);
        assertEquals(metadataNames.length, 12);

        // Parsing metadata values
        assertEquals("true", reader.getMetadataValue(coverageName, "HAS_TIME_DOMAIN"));
        final String timeMetadata = reader.getMetadataValue(coverageName, "TIME_DOMAIN");
        assertEquals(
            "2012-04-01T00:00:00.000Z/2012-04-01T00:00:00.000Z,2012-04-01T01:00:00.000Z/2012-04-01T01:00:00.000Z",
            timeMetadata);
        assertNotNull(timeMetadata);
        assertEquals(
            "2012-04-01T00:00:00.000Z",
            reader.getMetadataValue(coverageName, "TIME_DOMAIN_MINIMUM"));
        assertEquals(
            "2012-04-01T01:00:00.000Z",
            reader.getMetadataValue(coverageName, "TIME_DOMAIN_MAXIMUM"));

        assertEquals("true", reader.getMetadataValue(coverageName, "HAS_ELEVATION_DOMAIN"));
        final String elevationMetadata = reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN");
        assertNotNull(elevationMetadata);
        assertEquals("10.0/10.0,450.0/450.0", elevationMetadata);
        assertEquals(2, elevationMetadata.split(",").length);
        assertEquals("10.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MINIMUM"));
        assertEquals("450.0", reader.getMetadataValue(coverageName, "ELEVATION_DOMAIN_MAXIMUM"));

        List<DimensionDescriptor> descriptors =
            ((StructuredGridCoverage2DReader) reader).getDimensionDescriptors(coverageName);
        assertNotNull(descriptors);
        assertEquals(2, descriptors.size());

        DimensionDescriptor descriptor = descriptors.get(0);
        assertEquals("TIME", descriptor.getName());
        assertEquals("time", descriptor.getStartAttribute());
        assertNull(descriptor.getEndAttribute());
        assertEquals(CoverageUtilities.UCUM.TIME_UNITS.getName(), descriptor.getUnits());
        assertEquals(CoverageUtilities.UCUM.TIME_UNITS.getSymbol(), descriptor.getUnitSymbol());

        descriptor = descriptors.get(1);
        assertEquals("ELEVATION", descriptor.getName());
        assertEquals("z", descriptor.getStartAttribute());
        assertNull(descriptor.getEndAttribute());
        assertEquals("meters", descriptor.getUnits());
        assertEquals(
            CoverageUtilities.UCUM.ELEVATION_UNITS.getSymbol(), descriptor.getUnitSymbol());

        // subsetting the envelope
        final ParameterValue<GridGeometry2D> gg =
            AbstractGridFormat.READ_GRIDGEOMETRY2D.createValue();
        final GeneralEnvelope originalEnvelope = reader.getOriginalEnvelope(coverageName);
        final GeneralEnvelope reducedEnvelope =
            new GeneralEnvelope(
                new double[] {
                  originalEnvelope.getLowerCorner().getOrdinate(0),
                  originalEnvelope.getLowerCorner().getOrdinate(1)
                },
                new double[] {
                  originalEnvelope.getMedian().getOrdinate(0),
                  originalEnvelope.getMedian().getOrdinate(1)
                });
        reducedEnvelope.setCoordinateReferenceSystem(
            reader.getCoordinateReferenceSystem(coverageName));

        // Selecting bigger gridRange for a zoomed result
        final Dimension dim = new Dimension();
        GridEnvelope gridRange = reader.getOriginalGridRange(coverageName);
        dim.setSize(gridRange.getSpan(0) * 4.0, gridRange.getSpan(1) * 2.0);
        final Rectangle rasterArea = ((GridEnvelope2D) gridRange);
        rasterArea.setSize(dim);
        final GridEnvelope2D range = new GridEnvelope2D(rasterArea);
        gg.setValue(new GridGeometry2D(range, reducedEnvelope));

        final ParameterValue<List> time = ImageMosaicFormat.TIME.createValue();
        final SimpleDateFormat formatD = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        formatD.setTimeZone(TimeZone.getTimeZone("GMT"));
        final Date timeD = formatD.parse("2012-04-01T00:00:00.000Z");
        time.setValue(
            new ArrayList() {
              {
                add(timeD);
              }
            });

        final ParameterValue<List> elevation = ImageMosaicFormat.ELEVATION.createValue();
        elevation.setValue(
            new ArrayList() {
              {
                add(450d); // Elevation
              }
            });

        GeneralParameterValue[] values = new GeneralParameterValue[] {gg, time, elevation};
        GridCoverage2D coverage = reader.read(coverageName, values);
        assertNotNull(coverage);
        if (TestData.isInteractiveTest()) {
          coverage.show();
        } else {
          PlanarImage.wrapRenderedImage(coverage.getRenderedImage()).getTiles();
        }
      }
    } catch (Throwable t) {
      throw new RuntimeException(t);
    } finally {
      if (reader != null) {
        try {
          reader.dispose();
        } catch (Throwable t) {
          // Does nothing
        }
      }
    }
  }