コード例 #1
0
  private void applyGeoCoding(TiffFileInfo info, TIFFImageMetadata metadata, Product product) {
    if (info.containsField(GeoTIFFTagSet.TAG_MODEL_TIE_POINT)) {

      double[] tiePoints = info.getField(GeoTIFFTagSet.TAG_MODEL_TIE_POINT).getAsDoubles();

      boolean isGlobal = isGlobal(product, info);

      // check if we have a global geographic lat/lon with lon from 0..360 instead of -180..180
      final double deltaX = Math.ceil(360. / product.getSceneRasterWidth());
      if (isGlobal && tiePoints.length == 6 && Math.abs(tiePoints[3]) < deltaX) {
        // e.g. tiePoints[3] = -0.5, productWidth=722 --> we have a lon range of 361 which should
        // start
        // at or near -180 but not at zero
        isGlobalShifted180 = true;
        // subtract 180 from the longitudes
        tiePoints[3] -= 180.0;
      }

      if (canCreateTiePointGeoCoding(tiePoints)) {
        applyTiePointGeoCoding(info, tiePoints, product);
      } else if (canCreateGcpGeoCoding(tiePoints)) {
        applyGcpGeoCoding(info, tiePoints, product);
      }
    }

    if (product.getGeoCoding() == null) {
      try {
        applyGeoCodingFromGeoTiff(metadata, product);
      } catch (Exception ignored) {
      }
    }
  }
コード例 #2
0
  private static boolean isGlobal(Product product, TiffFileInfo info) {
    boolean isGlobal = false;
    final TIFFField pixelScaleField = info.getField(GeoTIFFTagSet.TAG_MODEL_PIXEL_SCALE);
    if (pixelScaleField != null) {
      double[] pixelScales = pixelScaleField.getAsDoubles();

      if (isPixelScaleValid(pixelScales)) {
        final double widthInDegree = pixelScales[0] * product.getSceneRasterWidth();
        isGlobal = Math.ceil(widthInDegree) >= 360;
      }
    }

    return isGlobal;
  }
コード例 #3
0
  Product readGeoTIFFProduct(final ImageInputStream stream, final File inputFile)
      throws IOException {
    Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(stream);
    while (imageReaders.hasNext()) {
      final ImageReader reader = imageReaders.next();
      if (reader instanceof TIFFImageReader) {
        imageReader = (TIFFImageReader) reader;
        break;
      }
    }
    if (imageReader == null) {
      throw new IOException("GeoTiff imageReader not found");
    }

    imageReader.setInput(stream);

    Product product = null;

    final TIFFImageMetadata imageMetadata =
        (TIFFImageMetadata) imageReader.getImageMetadata(FIRST_IMAGE);
    final TiffFileInfo tiffInfo = new TiffFileInfo(imageMetadata.getRootIFD());
    final TIFFField field = tiffInfo.getField(Utils.PRIVATE_BEAM_TIFF_TAG_NUMBER);
    if (field != null && field.getType() == TIFFTag.TIFF_ASCII) {
      final String s = field.getAsString(0).trim();
      if (s.contains("<Dimap_Document")) { // with DIMAP header
        InputStream is = null;
        try {
          final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          final DocumentBuilder builder = factory.newDocumentBuilder();
          is = new ByteArrayInputStream(s.getBytes());
          final Document document = new DOMBuilder().build(builder.parse(is));
          product = DimapProductHelpers.createProduct(document);
          removeGeoCodingAndTiePointGrids(product);
          initBandsMap(product);
        } catch (ParserConfigurationException | SAXException ignore) {
          // ignore if it can not be read
        } finally {
          if (is != null) {
            is.close();
          }
        }
      }
    }

    if (product == null) { // without DIMAP header
      final String productName;
      if (tiffInfo.containsField(BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION)) {
        final TIFFField field1 = tiffInfo.getField(BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION);
        productName = field1.getAsString(0);
      } else if (inputFile != null) {
        productName = FileUtils.getFilenameWithoutExtension(inputFile);
      } else {
        productName = "geotiff";
      }
      final String productType = getReaderPlugIn().getFormatNames()[0];

      final int width = imageReader.getWidth(FIRST_IMAGE);
      final int height = imageReader.getHeight(FIRST_IMAGE);
      product = new Product(productName, productType, width, height, this);
      addBandsToProduct(tiffInfo, product);
    }

    if (tiffInfo.isGeotiff()) {
      applyGeoCoding(tiffInfo, imageMetadata, product);
    }

    TiffTagToMetadataConverter.addTiffTagsToMetadata(
        imageMetadata, tiffInfo, product.getMetadataRoot());

    if (inputFile != null) {
      product.setFileLocation(inputFile);
    }
    setPreferredTiling(product);

    return product;
  }