MultiPolygon reprojectRoiArea(
     CoordinateReferenceSystem maskCrs,
     CoordinateReferenceSystem resourceCrs,
     MultiPolygon roiArea) {
   try {
     if (LOGGER.isLoggable(Level.FINE)) {
       LOGGER.fine("Mask layer CRS: " + maskCrs.getName());
     }
     if (LOGGER.isLoggable(Level.FINE)) {
       LOGGER.fine("Resource CRS: " + resourceCrs.getName());
     }
     if ((resourceCrs != null) && !CRS.equalsIgnoreMetadata(maskCrs, resourceCrs)) {
       if (LOGGER.isLoggable(Level.FINE)) {
         LOGGER.fine("Mask and Resource CRS differ, ROI area will be reprojected");
       }
       MathTransform mt = CRS.findMathTransform(maskCrs, resourceCrs, true);
       return (MultiPolygon) JTS.transform(roiArea, mt);
     } else {
       if (LOGGER.isLoggable(Level.FINE)) {
         LOGGER.fine("Mask and Resource CRS coincide, no reprojection is necessary");
       }
     }
   } catch (Exception e) {
     throw new RuntimeException(
         "Failed to reproject the restricted area to the layer's native SRS", e);
   }
   return roiArea;
 }
 /**
  * Display the name of the coordinate reference system
  *
  * @param crs the CRS to display
  */
 public void displayCRS(CoordinateReferenceSystem crs) {
   if (crs == null) {
     crsBtn.setText(stringRes.getString("crs_undefined"));
   } else {
     crsBtn.setText(crs.getName().toString());
   }
 }
  /** {@inheritDoc} */
  @Override
  public CoordinateReferenceSystem getResponseCRS() throws FactoryException {
    if (output == null
        || output.getGridCRS() == null
        || output.getGridCRS().getSrsName() == null
        || output.getGridCRS().getSrsName().getValue() == null) {
      return null;
    }
    final CoordinateReferenceSystem objCrs =
        CRS.forCode(output.getGridCRS().getSrsName().getValue());
    final BoundingBoxType boundingBox = domainSubset.getBoundingBox().getValue();

    /*
     * If the bounding box contains at least 3 dimensions and the CRS specified is just
     * a 2D one, then we have to add a VerticalCRS to the one gotten by the crs decoding step.
     * Otherwise the CRS decoded is already fine, and we just return it.
     */
    if (boundingBox.getDimensions().intValue() > 2
        && objCrs.getCoordinateSystem().getDimension() < 3) {
      final VerticalCRS verticalCRS = CommonCRS.Vertical.ELLIPSOIDAL.crs();
      return new GeodeticObjectBuilder()
          .addName(objCrs.getName().getCode() + " (3D)")
          .createCompoundCRS(objCrs, verticalCRS);
    } else {
      return objCrs;
    }
  }
  /** {@inheritDoc} */
  @Override
  public CoordinateReferenceSystem getCRS() throws FactoryException {
    if (domainSubset == null || domainSubset.getBoundingBox() == null) {
      return null;
    }
    final BoundingBoxType boundingBox = domainSubset.getBoundingBox().getValue();
    final CoordinateReferenceSystem objCrs =
        AbstractCRS.castOrCopy(CRS.forCode(boundingBox.getCrs()))
            .forConvention(AxesConvention.RIGHT_HANDED);

    // final List<DirectPositionType> positions =
    // domainSubset.getSpatialSubSet().getEnvelope().getPos();

    /*
     * If the bounding box contains at least 3 dimensions and the CRS specified is just
     * a 2D one, then we have to add a VerticalCRS to the one gotten by the crs decoding step.
     * Otherwise the CRS decoded is already fine, and we just return it.
     */
    if (boundingBox.getUpperCorner().size() > 2
        && objCrs.getCoordinateSystem().getDimension() < 3) {
      final VerticalCRS verticalCRS = CommonCRS.Vertical.ELLIPSOIDAL.crs();
      return new GeodeticObjectBuilder()
          .addName(objCrs.getName().getCode() + " (3D)")
          .createCompoundCRS(objCrs, verticalCRS);
    } else {
      return objCrs;
    }
  }
  void transformCoverage(
      final File sourceFile,
      final File file,
      final WcsReaderRequest targetRequest,
      final WcsReaderRequest executedRequest,
      final boolean handleFormatTranform)
      throws IOException {
    final CoordinateReferenceSystem original = targetRequest.responseCRS;
    CoordinateReferenceSystem actual = executedRequest.responseCRS;

    if (!CRS.equalsIgnoreMetadata(original, actual)) {
      try {
        LOG.info(
            "Need to reproject coverage from "
                + CRS.lookupIdentifier(actual, false)
                + " to "
                + CRS.lookupIdentifier(original, false));
      } catch (FactoryException e) {
        LOG.info(
            "Need to reproject coverage from " + actual.getName() + " to " + original.getName());
      }
      if (targetRequest.useCommandLineGDAL) {
        GDALCommandLine.gdalTransformation(sourceFile, file, executedRequest, targetRequest);
      } else {
        geotoolsTranformation(sourceFile, file, targetRequest, original);
      }

      LOG.info("Coverage reprojection/transformation complete");
    } else if (handleFormatTranform) {
      if (targetRequest.useCommandLineGDAL) {
        GDALCommandLine.gdalTransformation(sourceFile, file, executedRequest, targetRequest);
      } else {
        // we need to re-implement convertFormat so it can handle non-world+image outputformats
        throw new UnsupportedOperationException(
            "We do not convert format from geotiff to another format yet in localReproject mode.  Should be pretty easy to implement though");
      }
    } else if (!sourceFile.equals(file)) {
      FileUtils.moveFile(sourceFile, file);
    }

    String name = file.getName().toLowerCase();

    if (name.endsWith(".tif") || name.endsWith("tiff")) {
      //            writeWorldImageExt(targetRequest, file);
    }
  }
 /**
  * Convenience method for checking coordinate reference system validity.
  *
  * @param crs The coordinate reference system to check.
  * @param expected the dimension expected.
  * @throws MismatchedDimensionException if the CRS dimension is not valid.
  */
 static void checkCoordinateReferenceSystemDimension(
     final CoordinateReferenceSystem crs, final int expected) throws MismatchedDimensionException {
   if (crs != null) {
     final int dimension = crs.getCoordinateSystem().getDimension();
     if (dimension != expected) {
       throw new MismatchedDimensionException(
           Errors.format(
               ErrorKeys.MISMATCHED_DIMENSION_$3, crs.getName().getCode(), dimension, expected));
     }
   }
 }
  /**
   * Compara las proyecciones de la vista y la capa. En caso de ser diferentes pregunta por las
   * opciones a realizar.
   *
   * @param lyr
   * @param mapControl
   */
  private void compareProjections(FLyrRasterSE lyr) {
    CoordinateReferenceSystem viewCrs = getMapControl().getCrs();
    CoordinateReferenceSystem lyrCrs = lyr.readProjection();
    if (lyrCrs == null) {
      lyr.setCrs(viewCrs);
      actionList.add(new Integer(defaultActionLayer));
      return;
    }
    if (viewCrs == null) {
      getMapControl().setCrs(lyrCrs);
      actionList.add(new Integer(defaultActionLayer));
      return;
    }

    /*
     * Si las proyecciones de vista y raster son distintas se lanza el
     * dialogo de selección de opciones. Este dialogo solo se lanza en caso
     * de que el checkbox de aplicar a todos los ficheros no haya sido
     * marcado. En este caso para el resto de ficheros de esa selección se
     * hará la misma acción que se hizo para el primero.
     */
    if (!viewCrs.getName().equals(lyrCrs.getName())) {
      boolean showDialog = false;
      if (!RasterProjectionActionsPanel.selectAllFiles) showDialog = true;

      if (showDialog) {
        dialog = new RasterProjectionActionsDialog(lyr);
      } else {
        if (defaultActionLayer == FileOpenRaster.REPROJECT && !lyr.isReproyectable())
          dialog = new RasterProjectionActionsDialog(lyr);
      }
      int select = defaultActionLayer;
      if (dialog != null) select = dialog.getRasterProjectionActionsPanel().getSelection();

      actionList.add(new Integer(select));
      return;
    }
    actionList.add(new Integer(defaultActionLayer));
  }
  /**
   * Transforms the referenced envelope to the specified coordinate reference system using the
   * specified amount of points.
   *
   * <p>This method can handle the case where the envelope contains the North or South pole, or when
   * it cross the &plusmn;180� longitude.
   *
   * @param targetCRS The target coordinate reference system.
   * @param lenient {@code true} if datum shift should be applied even if there is insuffisient
   *     information. Otherwise (if {@code false}), an exception is thrown in such case.
   * @param numPointsForTransformation The number of points to use for sampling the envelope.
   * @return The transformed envelope.
   * @throws FactoryException if the math transform can't be determined.
   * @throws TransformException if at least one coordinate can't be transformed.
   * @see CRS#transform(CoordinateOperation, org.opengis.geometry.Envelope)
   * @since 2.3
   */
  public ReferencedEnvelope transform(
      final CoordinateReferenceSystem targetCRS,
      final boolean lenient,
      final int numPointsForTransformation)
      throws TransformException, FactoryException {
    if (crs == null) {
      if (isEmpty()) {
        // We don't have a CRS yet because we are still empty, being empty is
        // something we can represent in the targetCRS
        return new ReferencedEnvelope(targetCRS);
      } else {
        // really this is a the code that created this ReferencedEnvelope
        throw new NullPointerException(
            "Unable to transform referenced envelope, crs has not yet been provided.");
      }
    }
    if (getDimension() != targetCRS.getCoordinateSystem().getDimension()) {
      if (lenient) {
        return JTS.transformTo3D(this, targetCRS, lenient, numPointsForTransformation);
      } else {
        throw new MismatchedDimensionException(
            Errors.format(
                ErrorKeys.MISMATCHED_DIMENSION_$3,
                crs.getName().getCode(),
                new Integer(getDimension()),
                new Integer(targetCRS.getCoordinateSystem().getDimension())));
      }
    }
    /*
     * Gets a first estimation using an algorithm capable to take singularity in account
     * (North pole, South pole, 180� longitude). We will expand this initial box later.
     */
    CoordinateOperationFactory coordinateOperationFactory =
        CRS.getCoordinateOperationFactory(lenient);

    final CoordinateOperation operation =
        coordinateOperationFactory.createOperation(crs, targetCRS);
    final GeneralEnvelope transformed = CRS.transform(operation, this);
    transformed.setCoordinateReferenceSystem(targetCRS);

    /*
     * Now expands the box using the usual utility methods.
     */
    final ReferencedEnvelope target = new ReferencedEnvelope(transformed);
    final MathTransform transform = operation.getMathTransform();
    JTS.transform(this, target, transform, numPointsForTransformation);

    return target;
  }
 /**
  * Convenience method for checking coordinate reference system validity.
  *
  * @throws IllegalArgumentException if the CRS dimension is not valid.
  */
 protected void checkCoordinateReferenceSystemDimension() throws MismatchedDimensionException {
   if (crs != null) {
     final int expected = getDimension();
     final int dimension = crs.getCoordinateSystem().getDimension();
     if (dimension > expected) {
       // check dimensions and choose ReferencedEnvelope or ReferencedEnvelope3D
       // or the factory method ReferencedEnvelope.reference( CoordinateReferenceSystem )
       throw new MismatchedDimensionException(
           Errors.format(
               ErrorKeys.MISMATCHED_DIMENSION_$3,
               crs.getName().getCode(),
               new Integer(dimension),
               new Integer(expected)));
     }
   }
 }
  public synchronized void render(
      Graphics2D destination, ReferencedEnvelope bounds, IProgressMonitor monitor)
      throws RenderException {

    int endLayerStatus = ILayer.DONE;
    try {
      if (bounds == null || bounds.isNull()) {
        bounds = getContext().getImageBounds();
      }

      if (monitor.isCanceled()) return;

      getContext().setStatus(ILayer.WAIT);

      WebMapServer wms = getWMS();

      GetMapRequest request = wms.createGetMapRequest();

      // put in default exception format we understand as a client
      // (if suppoted by the server)
      WMSCapabilities capabilities = wms.getCapabilities();
      if (capabilities
          .getRequest()
          .getGetMap()
          .getFormats()
          .contains(GetMapRequest.EXCEPTION_XML)) {
        request.setExceptions(GetMapRequest.EXCEPTION_XML);
      }
      setImageFormat(wms, request);

      if (monitor.isCanceled()) return;

      double currScale = getContext().getViewportModel().getScaleDenominator();
      List<ILayer> layers = getLayers();
      for (int i = layers.size() - 1; i >= 0; i--) {
        ILayer ilayer = layers.get(i);
        Layer layer;
        double minScale = 0;
        double maxScale = Double.MAX_VALUE;
        layer = ilayer.getResource(org.geotools.data.ows.Layer.class, null);
        // check if there are min/max scale rules
        StyleBlackboard sb = (StyleBlackboard) ilayer.getStyleBlackboard();
        Style style = (Style) sb.lookup(Style.class);
        if (style != null) {
          Rule rule = style.getFeatureTypeStyles()[0].getRules()[0];
          minScale = rule.getMinScaleDenominator();
          maxScale = rule.getMaxScaleDenominator();
        }

        if (currScale >= minScale && currScale <= maxScale) {
          // check for a wms style
          StyleImpl wmsStyle =
              (StyleImpl) ilayer.getStyleBlackboard().get(WMSStyleContent.WMSSTYLE);
          if (wmsStyle != null) {
            request.addLayer(layer, wmsStyle);
          } else {
            request.addLayer(layer);
          }
        }
      }

      if (monitor.isCanceled()) return;

      List<Layer> wmsLayers = getWMSLayers();
      if (wmsLayers == null || wmsLayers.isEmpty()) {
        endLayerStatus = ILayer.WARNING;
        return;
      }

      // figure out request CRS
      String requestCRScode = findRequestCRS(wmsLayers, getViewportCRS(), getContext().getMap());
      // TODO: make findRequestCRS more efficient (we are running CRS.decode at *least* twice)
      CoordinateReferenceSystem requestCRS = CRS.decode(requestCRScode);

      // figure out viewport
      //            ReferencedEnvelope viewport;
      //            Envelope viewportBBox = getViewportBBox();
      //            CoordinateReferenceSystem viewportCRS = getViewportCRS();
      //            if (viewportBBox == null) {
      //                // change viewport to world
      //                viewportBBox = new Envelope(-180, 180, -90, 90);
      //                if (!DefaultGeographicCRS.WGS84.equals(viewportCRS)) { // reproject
      //                    viewport = new ReferencedEnvelope(viewportBBox,
      // DefaultGeographicCRS.WGS84);
      //                    viewportBBox = viewport.transform(viewportCRS, true);
      //                }
      //            }

      ReferencedEnvelope requestBBox = null;
      Envelope backprojectedBBox = null; // request bbox projected to the viewport crs
      //            viewport = new ReferencedEnvelope(viewportBBox, viewportCRS);
      //            requestBBox = calculateRequestBBox(wmsLayers, viewport, requestCRS);

      requestBBox = calculateRequestBBox(wmsLayers, bounds, requestCRS, capabilities.getVersion());

      // check that a request is needed (not out of a bounds, invalid, etc)
      if (requestBBox == NILL_BOX) {
        endLayerStatus = ILayer.WARNING;
        return;
      }
      assert requestBBox.getCoordinateReferenceSystem().equals(requestCRS);

      if (requestBBox.getCoordinateReferenceSystem().equals(getViewportCRS())) {
        backprojectedBBox = (Envelope) requestBBox;
      } else {
        backprojectedBBox = (Envelope) requestBBox.transform(getViewportCRS(), true);
      }

      if (WMSPlugin.isDebugging(Trace.RENDER)) {
        WMSPlugin.trace("Viewport CRS: " + getViewportCRS().getName()); // $NON-NLS-1$
        WMSPlugin.trace("Request CRS: " + requestCRS.getName()); // $NON-NLS-1$
        WMSPlugin.trace("Context Image bounds: " + getContext().getImageBounds()); // $NON-NLS-1$
        WMSPlugin.trace("Request BBox  bounds: " + requestBBox); // $NON-NLS-1$
        WMSPlugin.trace("Backprojected request bounds: " + backprojectedBBox); // $NON-NLS-1$
      }

      Service wmsService = capabilities.getService();
      Dimension maxDimensions = new Dimension(wmsService.getMaxWidth(), wmsService.getMaxHeight());
      //            Dimension imageDimensions =
      // calculateImageDimensions(getContext().getMapDisplay()
      //                    .getDisplaySize(), maxDimensions, getViewportBBox(), backprojectedBBox);
      Dimension imageDimensions =
          calculateImageDimensions(
              getContext().getImageSize(), maxDimensions, bounds, backprojectedBBox);
      if (imageDimensions.height < 1 || imageDimensions.width < 1) {
        endLayerStatus = ILayer.WARNING;
        return;
      }
      request.setDimensions(
          imageDimensions.width + "", imageDimensions.height + ""); // $NON-NLS-1$ //$NON-NLS-2$
      // epsg could be under identifiers or authority.
      Set<ReferenceIdentifier> identifiers = requestCRS.getIdentifiers();
      String srs = identifiers.isEmpty() ? EPSG_4326 : identifiers.iterator().next().toString();
      request.setSRS(srs); // EPSG_4326
      request.setBBox(requestBBox);
      // request.setBBox(requestBBox.getMinX() + "," + requestBBox.getMinY()+ "," +
      // requestBBox.getMaxX()+ "," + requestBBox.getMaxY());

      if (monitor.isCanceled()) return;

      setFilter(wms, request);

      // request.setProperty("DACS_ACS", null);
      BufferedImage image = readImage(wms, request, monitor);

      if (monitor.isCanceled()) return;

      if (image == null) {
        Exception e = new RuntimeException(Messages.BasicWMSRenderer2_unable_to_decode_image);
        throw wrapException(e);
      }

      // backprojectedBBox or viewportBBox
      renderGridCoverage(destination, backprojectedBBox, imageDimensions, requestBBox, image);

    } catch (Exception e) {
      if (e instanceof RenderException) throw (RenderException) e;
      throw new RenderException(e);
    } finally {
      getContext().setStatus(endLayerStatus);
      if (endLayerStatus == ILayer.DONE) {
        // clear the status message (rendering was successful)
        getContext().setStatusMessage(null);
      }
    }
  }
Beispiel #11
0
  /**
   * Applies a crop operation to a coverage.
   *
   * @see
   *     org.geotools.coverage.processing.AbstractOperation#doOperation(org.opengis.parameter.ParameterValueGroup,
   *     org.geotools.factory.Hints)
   */
  @SuppressWarnings("unchecked")
  public Coverage doOperation(ParameterValueGroup parameters, Hints hints) {

    final Geometry cropRoi; // extracted from parameters
    GeneralEnvelope cropEnvelope = null; // extracted from parameters
    final GridCoverage2D source; // extracted from parameters
    final double roiTolerance = parameters.parameter(Crop.PARAMNAME_ROITOLERANCE).doubleValue();
    final boolean forceMosaic = parameters.parameter(Crop.PARAMNAME_FORCEMOSAIC).booleanValue();

    // /////////////////////////////////////////////////////////////////////
    //
    // Assigning and checking input parameters
    //
    // ///////////////////////////////////////////////////////////////////

    // source coverage
    final ParameterValue sourceParameter = parameters.parameter("Source");
    if (sourceParameter == null || !(sourceParameter.getValue() instanceof GridCoverage2D)) {
      throw new CannotCropException(
          Errors.format(ErrorKeys.NULL_PARAMETER_$2, "Source", GridCoverage2D.class.toString()));
    }
    source = (GridCoverage2D) sourceParameter.getValue();

    // Check Envelope and ROI existence - we need at least one of them
    final ParameterValue envelopeParameter = parameters.parameter(PARAMNAME_ENVELOPE);
    final ParameterValue roiParameter = parameters.parameter(PARAMNAME_ROI);

    if ((envelopeParameter == null || envelopeParameter.getValue() == null)
        && (roiParameter == null || roiParameter.getValue() == null))
      throw new CannotCropException(
          Errors.format(
              ErrorKeys.NULL_PARAMETER_$2, PARAMNAME_ENVELOPE, GeneralEnvelope.class.toString()));

    Object envelope = envelopeParameter.getValue();
    if (envelope != null) {
      if (envelope instanceof GeneralEnvelope) {
        cropEnvelope = (GeneralEnvelope) envelope;
      } else if (envelope instanceof Envelope) {
        cropEnvelope = new GeneralEnvelope((Envelope) envelope);
      }
    }
    // may be null

    // Check crop ROI
    try {
      cropRoi =
          IntersectUtils.unrollGeometries(
              (Geometry) roiParameter.getValue()); // may throw if format not correct
    } catch (IllegalArgumentException ex) {
      throw new CannotCropException(
          Errors.format(ErrorKeys.ILLEGAL_ARGUMENT_$2, PARAMNAME_ROI, ex.getMessage()), ex);
    }

    // Setting a GeneralEnvelope from ROI if needed
    if (cropRoi != null && cropEnvelope == null) {
      Envelope e2d =
          JTS.getEnvelope2D(cropRoi.getEnvelopeInternal(), source.getCoordinateReferenceSystem());
      cropEnvelope = new GeneralEnvelope(e2d);
    }

    // /////////////////////////////////////////////////////////////////////
    //
    // Initialization
    //
    // We take the crop envelope and the source envelope then we check their
    // crs and we also check if they ever overlap.
    //
    // /////////////////////////////////////////////////////////////////////
    // envelope of the source coverage
    final Envelope2D sourceEnvelope = source.getEnvelope2D();
    // crop envelope
    Envelope2D destinationEnvelope = new Envelope2D(cropEnvelope);
    CoordinateReferenceSystem sourceCRS = sourceEnvelope.getCoordinateReferenceSystem();
    CoordinateReferenceSystem destinationCRS = destinationEnvelope.getCoordinateReferenceSystem();
    if (destinationCRS == null) {
      // Do not change the user provided object - clone it first.
      final Envelope2D ge = new Envelope2D(destinationEnvelope);
      destinationCRS = source.getCoordinateReferenceSystem2D();
      ge.setCoordinateReferenceSystem(destinationCRS);
      destinationEnvelope = ge;
    }

    // //
    //
    // Source and destination crs must be equals
    //
    // //
    if (!CRS.equalsIgnoreMetadata(sourceCRS, destinationCRS)) {
      throw new CannotCropException(
          Errors.format(
              ErrorKeys.MISMATCHED_ENVELOPE_CRS_$2,
              sourceCRS.getName().getCode(),
              destinationCRS.getName().getCode()));
    }

    if (cropRoi != null) {
      // TODO: check ROI SRID
    }

    // //
    //
    // Check the intersection and, if needed, do the crop operation.
    //
    // //
    final GeneralEnvelope intersectionEnvelope =
        new GeneralEnvelope((Envelope) destinationEnvelope);
    intersectionEnvelope.setCoordinateReferenceSystem(source.getCoordinateReferenceSystem());
    // intersect the envelopes
    intersectionEnvelope.intersect(sourceEnvelope);
    if (intersectionEnvelope.isEmpty())
      throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP));

    // intersect the ROI with the intersection envelope and throw an error if they do not intersect
    if (cropRoi != null) {
      final Geometry jis =
          JTS.toGeometry(
              (com.vividsolutions.jts.geom.Envelope) new ReferencedEnvelope(intersectionEnvelope));
      if (!IntersectUtils.intersects(cropRoi, jis))
        throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP));
    }

    // //
    //
    // Get the grid-to-world transform by keeping into account translation
    // of grid geometry constructor for respecting OGC PIXEL-IS-CENTER
    // ImageDatum assumption.
    //
    // //
    final AffineTransform sourceCornerGridToWorld =
        (AffineTransform)
            ((GridGeometry2D) source.getGridGeometry()).getGridToCRS(PixelInCell.CELL_CORNER);

    // //
    //
    // I set the tolerance as half the scale factor of the grid-to-world
    // transform. This should more or less means in most cases "don't bother
    // to crop if the new envelope is as close to the old one that we go
    // deep under pixel size."
    //
    // //
    final double tolerance = XAffineTransform.getScale(sourceCornerGridToWorld);
    if (cropRoi != null || !intersectionEnvelope.equals(sourceEnvelope, tolerance / 2.0, false)) {
      cropEnvelope = intersectionEnvelope.clone();
      return buildResult(
          cropEnvelope,
          cropRoi,
          roiTolerance,
          forceMosaic,
          (hints instanceof Hints) ? (Hints) hints : new Hints(hints),
          source,
          sourceCornerGridToWorld);
    } else {
      // //
      //
      // Note that in case we don't crop at all, WE DO NOT UPDATE the
      // envelope. If we did we might end up doing multiple successive
      // crop without actually cropping the image but, still, we would
      // shrink the envelope each time. Just think about having a loop
      // that crops recursively the same coverage specifying each time an
      // envelope whose URC is only a a scale quarter close to the LLC of
      // the old one. We would never crop the raster but we would modify
      // the grid-to-world transform each time.
      //
      // //
      return source;
    }
  }
  public boolean extractTiffMetadata(File tifFile, TiffMeta surface)
      throws UnknownCRSException, FactoryException, TransformException, IOException {

    Preconditions.checkArgument(tifFile != null, "File is null.");

    GeoTiffReader gtr = new GeoTiffReader(tifFile);

    try {

      CoordinateReferenceSystem crs = gtr.getCoordinateReferenceSystem();
      surface.setCRS(crs);
      Integer epsgCode = CRS.lookupEpsgCode(crs, true);

      if (epsgCode == null) {
        ReferenceIdentifier name = crs.getName();
        String crsName = "Unknown";
        if (name != null) {
          crsName = name.toString();
        }
        throw new UnknownCRSException(crsName);
      }

      String srid = "EPSG:" + epsgCode;
      surface.setSrid(srid);

      //
      // extremaOp(surface, gtr.read(null));

      /*
       * Build the envelope and set to WGS84
       */
      GeneralEnvelope origEnv = gtr.getOriginalEnvelope();
      DirectPosition ll = origEnv.getLowerCorner();
      DirectPosition ur = origEnv.getUpperCorner();

      Envelope e = new Envelope();
      e.expandToInclude(ll.getOrdinate(0), ll.getOrdinate(1));
      e.expandToInclude(ur.getOrdinate(0), ur.getOrdinate(1));

      Geometry poly = envelopeToWgs84(epsgCode, e);

      if (poly instanceof Polygon) {
        surface.setEnvelope((Polygon) poly);
      }

      /*
       * Figure out the pixel size
       */
      ImageLayout imageLayout = gtr.getImageLayout();
      int imageWidth = imageLayout.getWidth(null);
      int imageHeight = imageLayout.getHeight(null);

      double pixelSizeX = e.getWidth() / imageWidth;
      double pixelSizeY = e.getHeight() / imageHeight;

      surface.setPixelSizeX(pixelSizeX);
      surface.setPixelSizeY(pixelSizeY);

      surface.setMinVal(0d);
      surface.setMaxVal(100d);

      GridCoverage2D gridCoverage2D = gtr.read(null);

      try {
        int nDims = gridCoverage2D.getNumSampleDimensions();
        surface.setNumSampleDimensions(nDims);

        extremaOp(surface, gridCoverage2D);

      } finally {
        gridCoverage2D.dispose(false);
      }

    } finally {

      gtr.dispose();
    }

    return true;
  }
Beispiel #13
0
  /**
   * Writes the systems byte order to the out stream. In java only high byte first.
   *
   * @param out the stream to write to
   * @param rasterDataNode
   */
  private static void writeMapProjectionInfo(PrintWriter out, RasterDataNode rasterDataNode) {
    Product product = rasterDataNode.getProduct();
    if (product == null) {
      return;
    }

    String mapProjectionName = "Arbitrary";
    String mapUnits = "Meters";
    double referencePixelX = 0, referencePixelY = 0;
    double easting = 0, northing = 0;
    double pixelSizeX = 0, pixelSizeY = 0;
    String datumName = "";
    int utmZone = -1;
    String utmHemisphere = "";
    MapProjection mapProjection = null;

    if (product.getGeoCoding() instanceof CrsGeoCoding) {
      final CrsGeoCoding crsGeoCoding = (CrsGeoCoding) product.getGeoCoding();
      final CoordinateReferenceSystem crs = crsGeoCoding.getMapCRS();

      final ImageGeometry imgGeom =
          ImageGeometry.createTargetGeometry(
              product, crs, null, null, null, null, null, null, null, null, null);

      final String crsName = crs.getName().toString().toUpperCase();
      if (crsName.equals("WGS84(DD)")) {
        mapProjectionName = "Geographic Lat/Lon";
        mapUnits = "Degrees";
      } else if (crsName.contains("UTM")) {
        mapProjectionName = "UTM";
        String zoneStr = crsName.substring(crsName.indexOf("ZONE") + 5, crsName.length()).trim();
        int i = 0;
        String zoneNumStr = "";
        while (Character.isDigit(zoneStr.charAt(i))) {
          zoneNumStr += zoneStr.charAt(i++);
        }
        utmZone = Integer.parseInt(zoneNumStr);

        GeoPos centrePos =
            crsGeoCoding.getGeoPos(
                new PixelPos(product.getSceneRasterWidth() / 2, product.getSceneRasterHeight() / 2),
                null);
        utmHemisphere = centrePos.getLat() > 0 ? "North" : "South";
      }
      referencePixelX = imgGeom.getReferencePixelX();
      referencePixelY = imgGeom.getReferencePixelY();
      easting = imgGeom.getEasting();
      northing = imgGeom.getNorthing();
      pixelSizeX = imgGeom.getPixelSizeX();
      pixelSizeY = imgGeom.getPixelSizeY();
      datumName = crsGeoCoding.getDatum().getName();

    } else if (product.getGeoCoding() instanceof MapGeoCoding) {
      final MapGeoCoding mapGeoCoding = (MapGeoCoding) product.getGeoCoding();

      final MapInfo info = mapGeoCoding.getMapInfo();
      if (info == null) {
        return;
      }
      mapProjection = info.getMapProjection();

      if (mapProjection instanceof UTMProjection) {
        mapProjectionName = "UTM";
        final UTMProjection utmProjection = (UTMProjection) mapProjection;
        utmZone = utmProjection.getZone();
        utmHemisphere = utmProjection.isNorth() ? "North" : "South";
      } else if (mapProjection.isPreDefined()) {
        mapProjectionName = mapProjection.getName();
      }

      if ("meter".equals(mapProjection.getMapUnit())) {
        mapUnits = "Meters";
      } else if ("degree".equals(mapProjection.getMapUnit())) {
        mapUnits = "Degrees";
      } else {
        mapUnits = mapProjection.getMapUnit();
      }

      datumName = mapGeoCoding.getDatum().getName();
    } else {
      return;
    }

    out.print(_enviMapInfo);
    out.print(" = {");
    out.print(mapProjectionName);
    out.print(",");
    out.print(referencePixelX + 1.0f);
    out.print(",");
    out.print(referencePixelY + 1.0f);
    out.print(",");
    out.print(easting);
    out.print(",");
    out.print(northing);
    out.print(",");
    out.print(pixelSizeX);
    out.print(",");
    out.print(pixelSizeY);
    out.print(",");
    if (utmZone != -1) {
      out.print(utmZone);
      out.print(",");
      out.print(utmHemisphere);
      out.print(",");
    }
    out.print(datumName);
    out.print(",");
    out.print("units=" + mapUnits);
    out.print("}");
    out.println();

    if (mapProjection != null && !mapProjection.isPreDefined()) {
      final MapTransform mapTransform = mapProjection.getMapTransform();
      final double[] parameterValues = mapTransform.getParameterValues();
      final String transformName = mapTransform.getDescriptor().getName();
      out.print(_enviProjectionInfo);
      out.print(" = {");
      if (transformName.equals(TransverseMercatorDescriptor.NAME)) {
        out.print(3);
        out.print(",");
        out.print(parameterValues[0]); // semi_major (meters)
        out.print(",");
        out.print(parameterValues[1]); // semi_minor (meters)
        out.print(",");
        out.print(parameterValues[2]); // latitude_of_origin (degree)
        out.print(",");
        out.print(parameterValues[3]); // central_meridian (degree)
        out.print(",");
        out.print(parameterValues[5]); // false_easting (meters)
        out.print(",");
        out.print(parameterValues[6]); // false_northing (meters)
        out.print(",");
        out.print(parameterValues[4]); //  scaling_factor (no unit)
        out.print(",");
      } else if (transformName.equals(LambertConformalConicDescriptor.NAME)) {
        out.print(4);
        out.print(",");
        out.print(parameterValues[0]); // semi_major (meters)
        out.print(",");
        out.print(parameterValues[1]); // semi_minor (meters)
        out.print(",");
        out.print(parameterValues[2]); // latitude_of_origin (degree)
        out.print(",");
        out.print(parameterValues[3]); // central_meridian (degree)
        out.print(",");
        out.print(0.0); // false_easting (meters)
        out.print(",");
        out.print(0.0); // false_northing (meters)
        out.print(",");
        out.print(parameterValues[4]); // latitude_of_intersection_1 (meters)
        out.print(",");
        out.print(parameterValues[5]); // latitude_of_intersection_2 (meters)
        out.print(",");
      }
      out.print(mapProjectionName);
      out.print(",");
      out.print("units=" + mapUnits);
      out.print("}");
      out.println();
    }
  }
 public ReferenceIdentifier getName() {
   return base.getName();
 }