Exemple #1
0
  public WcsCoverage(GridDataset.Gridset coverage, WcsDataset dataset) {
    this.dataset = dataset;
    if (this.dataset == null) {
      log.error("WcsCoverage(): non-null dataset required.");
      throw new IllegalArgumentException("Non-null dataset required.");
    }

    this.coverage = coverage;
    if (this.coverage == null) {
      log.error("WcsCoverage(): non-null coverage required.");
      throw new IllegalArgumentException("Non-null coverage required.");
    }
    this.coordSys = coverage.getGeoCoordSystem();
    if (this.coordSys == null) {
      log.error("WcsCoverage(): Coverage must have non-null coordinate system.");
      throw new IllegalArgumentException("Non-null coordinate system required.");
    }

    this.name = this.coordSys.getName();
    this.label = this.coordSys.getName();

    this.range = new HashMap<String, WcsRangeField>();
    StringBuilder descripSB =
        new StringBuilder("All parameters on the \"")
            .append(this.name)
            .append("\" coordinate system: ");
    for (GridDatatype curField : this.coverage.getGrids()) {
      String stdName = curField.findAttValueIgnoreCase("standard_name", "");
      descripSB.append(stdName.length() == 0 ? curField.getFullName() : stdName).append(",");

      WcsRangeField field = new WcsRangeField(curField);
      range.put(field.getName(), field);
    }
    descripSB.setCharAt(descripSB.length() - 1, '.');
    this.description = descripSB.toString();

    this.nativeCRS = EPSG_OGC_CF_Helper.getWcs1_0CrsId(this.coordSys.getProjection());

    this.defaultRequestCrs = "OGC:CRS84";

    this.supportedCoverageFormatList = new ArrayList<WcsRequest.Format>();
    // this.supportedCoverageFormatList = "application/x-netcdf";
    this.supportedCoverageFormatList.add(WcsRequest.Format.GeoTIFF);
    this.supportedCoverageFormatList.add(WcsRequest.Format.GeoTIFF_Float);
    this.supportedCoverageFormatList.add(WcsRequest.Format.NetCDF3);
  }
Exemple #2
0
  public File writeCoverageDataToFile(
      WcsRequest.Format format,
      LatLonRect bboxLatLonRect,
      AxisSubset vertSubset,
      List<String> rangeSubset,
      CalendarDateRange timeRange)
      throws WcsException {
    boolean zRangeDone = false;
    boolean tRangeDone = false;

    try {
      // Get the height range.
      Range zRange = vertSubset != null ? vertSubset.getRange() : null;
      zRangeDone = true;

      // Get the time range.
      Range tRange = null;
      if (timeRange != null) {
        CoordinateAxis1DTime timeAxis = this.coordSys.getTimeAxis1D();
        int startIndex = timeAxis.findTimeIndexFromCalendarDate(timeRange.getStart());
        int endIndex = timeAxis.findTimeIndexFromCalendarDate(timeRange.getEnd());
        tRange = new Range(startIndex, endIndex);
        tRangeDone = true;
      }

      if (format == WcsRequest.Format.GeoTIFF || format == WcsRequest.Format.GeoTIFF_Float) {
        if (rangeSubset.size() != 1) {
          String msg =
              "GeoTIFF response encoding only available for single range field selection ["
                  + rangeSubset
                  + "].";
          log.error("writeCoverageDataToFile(): " + msg);
          throw new WcsException(WcsException.Code.InvalidParameterValue, "RangeSubset", msg);
        }
        String reqRangeFieldName = rangeSubset.get(0);

        File dir = new File(getDiskCache().getRootDirectory());
        File tifFile = File.createTempFile("WCS", ".tif", dir);
        if (log.isDebugEnabled())
          log.debug("writeCoverageDataToFile(): tifFile=" + tifFile.getPath());

        WcsRangeField rangeField = this.range.get(reqRangeFieldName);
        GridDatatype subset =
            rangeField.getGridDatatype().makeSubset(tRange, zRange, bboxLatLonRect, 1, 1, 1);
        Array data = subset.readDataSlice(0, 0, -1, -1);

        GeotiffWriter writer = new GeotiffWriter(tifFile.getPath());
        writer.writeGrid(
            this.dataset.getDataset(), subset, data, format == WcsRequest.Format.GeoTIFF);

        writer.close();

        return tifFile;
      } else if (format == WcsRequest.Format.NetCDF3) {
        File dir = new File(getDiskCache().getRootDirectory());
        File outFile = File.createTempFile("WCS", ".nc", dir);
        if (log.isDebugEnabled())
          log.debug("writeCoverageDataToFile(): ncFile=" + outFile.getPath());
        // WTF ?? this.coordSys.getVerticalAxis().isNumeric();

        NetcdfFileWriter writer =
            NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, outFile.getAbsolutePath());
        CFGridWriter2.writeFile(
            this.dataset.getDataset(),
            rangeSubset,
            bboxLatLonRect,
            null,
            1,
            zRange,
            timeRange,
            1,
            true,
            writer);
        return outFile;
      } else {
        log.error(
            "writeCoverageDataToFile(): Unsupported response encoding format [" + format + "].");
        throw new WcsException(
            WcsException.Code.InvalidFormat,
            "Format",
            "Unsupported response encoding format [" + format + "].");
      }
    } catch (InvalidRangeException e) {
      String msg = "Failed to subset coverage [" + this.getName();
      if (!zRangeDone) msg += "] along vertical axis [" + vertSubset + "]. ";
      else if (!tRangeDone) msg += "] along time axis [" + timeRange + "]. ";
      else msg += "] in horizontal plane [" + bboxLatLonRect + "]. ";
      log.error("writeCoverageDataToFile(): " + msg + e.getMessage());
      throw new WcsException(WcsException.Code.CoverageNotDefined, "", msg);
    } catch (IOException e) {
      log.error(
          "writeCoverageDataToFile(): Failed to write file for requested coverage <"
              + this.getName()
              + ">: "
              + e.getMessage());
      throw new WcsException(
          WcsException.Code.UNKNOWN, "", "Problem creating coverage [" + this.getName() + "].");
    }
  }