package com.vividsolutions.jump.workbench.imagery.geotiff;_x000D_
_x000D_
/*_x000D_
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI _x000D_
 * for visualizing and manipulating spatial features with geometry and attributes._x000D_
 *_x000D_
 * Copyright (C) 2003 Vivid Solutions_x000D_
 * _x000D_
 * This program is free software; you can redistribute it and/or_x000D_
 * modify it under the terms of the GNU General Public License_x000D_
 * as published by the Free Software Foundation; either version 2_x000D_
 * of the License, or (at your option) any later version._x000D_
 * _x000D_
 * This program is distributed in the hope that it will be useful,_x000D_
 * but WITHOUT ANY WARRANTY; without even the implied warranty of_x000D_
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the_x000D_
 * GNU General Public License for more details._x000D_
 * _x000D_
 * You should have received a copy of the GNU General Public License_x000D_
 * along with this program; if not, write to the Free Software_x000D_
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA._x000D_
 * _x000D_
 * For more information, contact:_x000D_
 *_x000D_
 * Vivid Solutions_x000D_
 * Suite #1A_x000D_
 * 2328 Government Street_x000D_
 * Victoria BC  V8T 5G5_x000D_
 * Canada_x000D_
 *_x000D_
 * (250)385-6040_x000D_
 * www.vividsolutions.com_x000D_
 */_x000D_
import java.awt.geom.AffineTransform;_x000D_
import java.util.List;_x000D_
_x000D_
import org.geotiff.image.jai.GeoTIFFDescriptor;_x000D_
import org.geotiff.image.jai.GeoTIFFDirectory;_x000D_
import org.libtiff.jai.codec.XTIFF;_x000D_
import org.libtiff.jai.codec.XTIFFField;_x000D_
_x000D_
import com.vividsolutions.jts.geom.Coordinate;_x000D_
import com.vividsolutions.jump.util.FileUtil;_x000D_
_x000D_
public class GeoTIFFRaster extends GeoReferencedRaster_x000D_
{_x000D_
  private final String MSG_GENERAL = "This is not a valid GeoTIFF file.";_x000D_
  String fileName;_x000D_
_x000D_
  boolean hoPatch = false;_x000D_
_x000D_
  /**_x000D_
   * Called by Java2XML_x000D_
   */_x000D_
  public GeoTIFFRaster(String imageFileLocation)_x000D_
      throws Exception_x000D_
  {_x000D_
    super(imageFileLocation);_x000D_
    registerWithJAI();_x000D_
    readRasterfile();_x000D_
  }_x000D_
_x000D_
  private void registerWithJAI()_x000D_
  {_x000D_
    // Register the GeoTIFF descriptor with JAI._x000D_
    GeoTIFFDescriptor.register();_x000D_
  }_x000D_
_x000D_
  private void parseGeoTIFFDirectory(GeoTIFFDirectory dir) throws Exception_x000D_
  {_x000D_
    // Find the ModelTiePoints field_x000D_
    XTIFFField fieldModelTiePoints = dir.getField(XTIFF.TIFFTAG_GEO_TIEPOINTS);_x000D_
    if (fieldModelTiePoints == null)_x000D_
      throw new Exception("Missing tiepoints-tag in file.\n" + MSG_GENERAL);_x000D_
_x000D_
    // Get the number of modeltiepoints_x000D_
//    int numModelTiePoints = fieldModelTiePoints.getCount() / 6;_x000D_
_x000D_
    // ToDo: alleen numModelTiePoints == 1 ondersteunen._x000D_
_x000D_
    // Map the modeltiepoints from raster to model space_x000D_
_x000D_
    // Read the tiepoints_x000D_
    setCoorRasterTiff_tiepointLT(new Coordinate(fieldModelTiePoints_x000D_
        .getAsDouble(0), fieldModelTiePoints.getAsDouble(1), 0));_x000D_
    setCoorModel_tiepointLT(new Coordinate(fieldModelTiePoints.getAsDouble(3),_x000D_
        fieldModelTiePoints.getAsDouble(4), 0));_x000D_
    setEnvelope();_x000D_
    // Find the ModelPixelScale field_x000D_
    XTIFFField fieldModelPixelScale = dir_x000D_
        .getField(XTIFF.TIFFTAG_GEO_PIXEL_SCALE);_x000D_
    if (fieldModelPixelScale == null)_x000D_
      throw new Exception("Missing pixelscale-tag in file." + "\n"_x000D_
          + MSG_GENERAL);_x000D_
_x000D_
    setDblModelUnitsPerRasterUnit_X(fieldModelPixelScale.getAsDouble(0));_x000D_
    setDblModelUnitsPerRasterUnit_Y(fieldModelPixelScale.getAsDouble(1));_x000D_
  }_x000D_
_x000D_
  /**_x000D_
   * @return filename of the tiff worldfile_x000D_
   */_x000D_
  private String worldFileName()_x000D_
  {_x000D_
    int posDot = fileName.lastIndexOf('.');_x000D_
    if (posDot == -1)_x000D_
    {_x000D_
      posDot = fileName.length();_x000D_
    }_x000D_
    return fileName.substring(0, posDot) + ".tfw";_x000D_
  }_x000D_
_x000D_
  private void parseWorldFile() throws Exception_x000D_
  {_x000D_
    // Get the name of the tiff worldfile._x000D_
    String name = worldFileName();_x000D_
_x000D_
    // Read the tags from the tiff worldfile._x000D_
    List lines = FileUtil.getContents(name);_x000D_
    double[] tags = new double[6];_x000D_
    for (int i = 0; i < 6; i++)_x000D_
    {_x000D_
      String line = (String) lines.get(i);_x000D_
      tags[i] = Double.parseDouble(line);_x000D_
    }_x000D_
    setAffineTransformation(new AffineTransform(tags));_x000D_
  }_x000D_
_x000D_
  protected void readRasterfile() throws Exception_x000D_
  {_x000D_
//    ImageCodec originalCodec = ImageCodec.getCodec("tiff");_x000D_
_x000D_
    try_x000D_
    {_x000D_
      super.readRasterfile();_x000D_
_x000D_
      // Get access to the tags and geokeys._x000D_
      // First, get the TIFF directory_x000D_
      GeoTIFFDirectory dir = (GeoTIFFDirectory) src_x000D_
          .getProperty("tiff.directory");_x000D_
      if (dir == null)_x000D_
        throw new Exception("This is not a (geo)tiff file.");_x000D_
_x000D_
      try_x000D_
      {_x000D_
        // Try to parse any embedded geotiff tags._x000D_
        parseGeoTIFFDirectory(dir);_x000D_
      } catch (Exception E)_x000D_
      {_x000D_
        // Embedded geotiff tags have not been found. Try_x000D_
        // to use a geotiff world file._x000D_
        try_x000D_
        {_x000D_
          parseWorldFile();_x000D_
        } catch (Exception e)_x000D_
        {_x000D_
          throw new Exception(_x000D_
              "Neither geotiff tags nor valid worldfile found.\n" + MSG_GENERAL);_x000D_
        }_x000D_
      }_x000D_
    } finally_x000D_
    {_x000D_
    }_x000D_
  }_x000D_
_x000D_
}
// ToDo: alleen numModelTiePoints == 1 ondersteunen._x000D_
package com.vividsolutions.jump.workbench.imagery.geotiff;_x000D_
_x000D_
/*_x000D_
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI _x000D_
 * for visualizing and manipulating spatial features with geometry and attributes._x000D_
 *_x000D_
 * Copyright (C) 2003 Vivid Solutions_x000D_
 * _x000D_
 * This program is free software; you can redistribute it and/or_x000D_
 * modify it under the terms of the GNU General Public License_x000D_
 * as published by the Free Software Foundation; either version 2_x000D_
 * of the License, or (at your option) any later version._x000D_
 * _x000D_
 * This program is distributed in the hope that it will be useful,_x000D_
 * but WITHOUT ANY WARRANTY; without even the implied warranty of_x000D_
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the_x000D_
 * GNU General Public License for more details._x000D_
 * _x000D_
 * You should have received a copy of the GNU General Public License_x000D_
 * along with this program; if not, write to the Free Software_x000D_
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA._x000D_
 * _x000D_
 * For more information, contact:_x000D_
 *_x000D_
 * Vivid Solutions_x000D_
 * Suite #1A_x000D_
 * 2328 Government Street_x000D_
 * Victoria BC  V8T 5G5_x000D_
 * Canada_x000D_
 *_x000D_
 * (250)385-6040_x000D_
 * www.vividsolutions.com_x000D_
 */_x000D_
import java.awt.geom.AffineTransform;_x000D_
import java.util.List;_x000D_
_x000D_
import org.geotiff.image.jai.GeoTIFFDescriptor;_x000D_
import org.geotiff.image.jai.GeoTIFFDirectory;_x000D_
import org.libtiff.jai.codec.XTIFF;_x000D_
import org.libtiff.jai.codec.XTIFFField;_x000D_
_x000D_
import com.vividsolutions.jts.geom.Coordinate;_x000D_
import com.vividsolutions.jump.util.FileUtil;_x000D_
_x000D_
public class GeoTIFFRaster extends GeoReferencedRaster_x000D_
{_x000D_
  private final String MSG_GENERAL = "This is not a valid GeoTIFF file.";_x000D_
  String fileName;_x000D_
_x000D_
  boolean hoPatch = false;_x000D_
_x000D_
  /**_x000D_
   * Called by Java2XML_x000D_
   */_x000D_
  public GeoTIFFRaster(String imageFileLocation)_x000D_
      throws Exception_x000D_
  {_x000D_
    super(imageFileLocation);_x000D_
    registerWithJAI();_x000D_
    readRasterfile();_x000D_
  }_x000D_
_x000D_
  private void registerWithJAI()_x000D_
  {_x000D_
    // Register the GeoTIFF descriptor with JAI._x000D_
    GeoTIFFDescriptor.register();_x000D_
  }_x000D_
_x000D_
  private void parseGeoTIFFDirectory(GeoTIFFDirectory dir) throws Exception_x000D_
  {_x000D_
    // Find the ModelTiePoints field_x000D_
    XTIFFField fieldModelTiePoints = dir.getField(XTIFF.TIFFTAG_GEO_TIEPOINTS);_x000D_
    if (fieldModelTiePoints == null)_x000D_
      throw new Exception("Missing tiepoints-tag in file.\n" + MSG_GENERAL);_x000D_
_x000D_
    // Get the number of modeltiepoints_x000D_
//    int numModelTiePoints = fieldModelTiePoints.getCount() / 6;_x000D_
_x000D_
    // ToDo: alleen<SUF>
_x000D_
    // Map the modeltiepoints from raster to model space_x000D_
_x000D_
    // Read the tiepoints_x000D_
    setCoorRasterTiff_tiepointLT(new Coordinate(fieldModelTiePoints_x000D_
        .getAsDouble(0), fieldModelTiePoints.getAsDouble(1), 0));_x000D_
    setCoorModel_tiepointLT(new Coordinate(fieldModelTiePoints.getAsDouble(3),_x000D_
        fieldModelTiePoints.getAsDouble(4), 0));_x000D_
    setEnvelope();_x000D_
    // Find the ModelPixelScale field_x000D_
    XTIFFField fieldModelPixelScale = dir_x000D_
        .getField(XTIFF.TIFFTAG_GEO_PIXEL_SCALE);_x000D_
    if (fieldModelPixelScale == null)_x000D_
      throw new Exception("Missing pixelscale-tag in file." + "\n"_x000D_
          + MSG_GENERAL);_x000D_
_x000D_
    setDblModelUnitsPerRasterUnit_X(fieldModelPixelScale.getAsDouble(0));_x000D_
    setDblModelUnitsPerRasterUnit_Y(fieldModelPixelScale.getAsDouble(1));_x000D_
  }_x000D_
_x000D_
  /**_x000D_
   * @return filename of the tiff worldfile_x000D_
   */_x000D_
  private String worldFileName()_x000D_
  {_x000D_
    int posDot = fileName.lastIndexOf('.');_x000D_
    if (posDot == -1)_x000D_
    {_x000D_
      posDot = fileName.length();_x000D_
    }_x000D_
    return fileName.substring(0, posDot) + ".tfw";_x000D_
  }_x000D_
_x000D_
  private void parseWorldFile() throws Exception_x000D_
  {_x000D_
    // Get the name of the tiff worldfile._x000D_
    String name = worldFileName();_x000D_
_x000D_
    // Read the tags from the tiff worldfile._x000D_
    List lines = FileUtil.getContents(name);_x000D_
    double[] tags = new double[6];_x000D_
    for (int i = 0; i < 6; i++)_x000D_
    {_x000D_
      String line = (String) lines.get(i);_x000D_
      tags[i] = Double.parseDouble(line);_x000D_
    }_x000D_
    setAffineTransformation(new AffineTransform(tags));_x000D_
  }_x000D_
_x000D_
  protected void readRasterfile() throws Exception_x000D_
  {_x000D_
//    ImageCodec originalCodec = ImageCodec.getCodec("tiff");_x000D_
_x000D_
    try_x000D_
    {_x000D_
      super.readRasterfile();_x000D_
_x000D_
      // Get access to the tags and geokeys._x000D_
      // First, get the TIFF directory_x000D_
      GeoTIFFDirectory dir = (GeoTIFFDirectory) src_x000D_
          .getProperty("tiff.directory");_x000D_
      if (dir == null)_x000D_
        throw new Exception("This is not a (geo)tiff file.");_x000D_
_x000D_
      try_x000D_
      {_x000D_
        // Try to parse any embedded geotiff tags._x000D_
        parseGeoTIFFDirectory(dir);_x000D_
      } catch (Exception E)_x000D_
      {_x000D_
        // Embedded geotiff tags have not been found. Try_x000D_
        // to use a geotiff world file._x000D_
        try_x000D_
        {_x000D_
          parseWorldFile();_x000D_
        } catch (Exception e)_x000D_
        {_x000D_
          throw new Exception(_x000D_
              "Neither geotiff tags nor valid worldfile found.\n" + MSG_GENERAL);_x000D_
        }_x000D_
      }_x000D_
    } finally_x000D_
    {_x000D_
    }_x000D_
  }_x000D_
_x000D_
}