Example #1
0
 private Rectangle getScaledRectangle(Rectangle rectangle) {
   final AffineTransform i2mTransform =
       ImageManager.getImageToModelTransform(product.getGeoCoding());
   final double scaleX = i2mTransform.getScaleX();
   final double scaleY = i2mTransform.getScaleY();
   double scaleFactorY = Math.abs(scaleY / scaleX);
   final AffineTransform scaleTransform = AffineTransform.getScaleInstance(1.0, scaleFactorY);
   return scaleTransform.createTransformedShape(rectangle).getBounds();
 }
  public void addGeocoding(Product product) throws IOException {

    double pixelX = 0.5;
    double pixelY = 0.5;
    double easting;
    double northing;
    double pixelSizeX;
    double pixelSizeY;
    boolean pixelRegistered = true;
    if (productReader.getProductType() == SeadasProductReader.ProductType.ANCNRT) {
      pixelRegistered = false;
    }
    if (productReader.getProductType() == SeadasProductReader.ProductType.OISST) {

      Variable lon = ncFile.findVariable("lon");
      Variable lat = ncFile.findVariable("lat");
      Array lonData = lon.read();
      // TODO: handle the 180 degree shift with the NOAA products - need to modify
      // SeaDasFileReader:readBandData
      // Below is a snippet from elsewhere in BEAM that deals with this issue...
      // SPECIAL CASE: check if we have a global geographic lat/lon with lon from 0..360 instead of
      // -180..180
      //            if (isShifted180(lonData)) {
      //                // if this is true, subtract 180 from all longitudes and
      //                // add a global attribute which will be analyzed when setting up the
      // image(s)
      //                final List<Variable> variables = ncFile.getVariables();
      //                for (Variable next : variables) {
      //                    next.getAttributes().add(new Attribute("LONGITUDE_SHIFTED_180", 1));
      //                }
      //                for (int i = 0; i < lonData.getSize(); i++) {
      //                    final Index ii = lonData.getIndex().set(i);
      //                    final double theLon = lonData.getDouble(ii) - 180.0;
      //                    lonData.setDouble(ii, theLon);
      //                }
      //            }
      final Array latData = lat.read();

      final int lonSize = lon.getShape(0);
      final Index i0 = lonData.getIndex().set(0);
      final Index i1 = lonData.getIndex().set(lonSize - 1);

      pixelSizeX =
          (lonData.getDouble(i1) - lonData.getDouble(i0)) / (product.getSceneRasterWidth() - 1);
      easting = lonData.getDouble(i0);

      final int latSize = lat.getShape(0);
      final Index j0 = latData.getIndex().set(0);
      final Index j1 = latData.getIndex().set(latSize - 1);
      pixelSizeY =
          (latData.getDouble(j1) - latData.getDouble(j0)) / (product.getSceneRasterHeight() - 1);

      // this should be the 'normal' case
      if (pixelSizeY < 0) {
        pixelSizeY = -pixelSizeY;
        northing = latData.getDouble(latData.getIndex().set(0));
      } else {
        northing = latData.getDouble(latData.getIndex().set(latSize - 1));
      }
      northing -= pixelSizeX / 2.0;
      easting += pixelSizeY / 2.0;

      try {
        product.setGeoCoding(
            new CrsGeoCoding(
                DefaultGeographicCRS.WGS84,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                easting,
                northing,
                pixelSizeX,
                pixelSizeY,
                pixelX,
                pixelY));
      } catch (FactoryException e) {
        throw new IllegalStateException(e);
      } catch (TransformException e) {
        throw new IllegalStateException(e);
      }

    } else {

      String east = "Easternmost_Longitude";
      String west = "Westernmost_Longitude";
      String north = "Northernmost_Latitude";
      String south = "Southernmost_Latitude";
      Attribute latmax = ncFile.findGlobalAttributeIgnoreCase("geospatial_lat_max");
      if (latmax != null) {
        east = "geospatial_lon_max";
        west = "geospatial_lon_min";
        north = "geospatial_lat_max";
        south = "geospatial_lat_min";
      } else {
        latmax = ncFile.findGlobalAttributeIgnoreCase("upper_lat");
        if (latmax != null) {
          east = "right_lon";
          west = "left_lon";
          north = "upper_lat";
          south = "lower_lat";
        }
      }

      final MetadataElement globalAttributes =
          product.getMetadataRoot().getElement("Global_Attributes");
      easting = (float) globalAttributes.getAttribute(east).getData().getElemDouble();
      float westing = (float) globalAttributes.getAttribute(west).getData().getElemDouble();
      pixelSizeX = Math.abs(easting - westing) / product.getSceneRasterWidth();
      northing = (float) globalAttributes.getAttribute(north).getData().getElemDouble();
      float southing = (float) globalAttributes.getAttribute(south).getData().getElemDouble();
      if (northing < southing) {
        mustFlipY = true;
        northing = (float) globalAttributes.getAttribute(south).getData().getElemDouble();
        southing = (float) globalAttributes.getAttribute(north).getData().getElemDouble();
      }
      pixelSizeY = Math.abs(northing - southing) / product.getSceneRasterHeight();
      if (pixelRegistered) {
        northing -= pixelSizeY / 2.0;
        westing += pixelSizeX / 2.0;
      } else {
        pixelX = 0.0;
        pixelY = 0.0;
      }
      try {
        product.setGeoCoding(
            new CrsGeoCoding(
                DefaultGeographicCRS.WGS84,
                product.getSceneRasterWidth(),
                product.getSceneRasterHeight(),
                westing,
                northing,
                pixelSizeX,
                pixelSizeY,
                pixelX,
                pixelY));
      } catch (FactoryException e) {
        throw new IllegalStateException(e);
      } catch (TransformException e) {
        throw new IllegalStateException(e);
      }
    }
  }