예제 #1
0
  /** @throws IOException */
  static MathTransform parseWorldFile(Object source) throws IOException {
    MathTransform raster2Model = null;

    // TODO: Add support for FileImageInputStreamExt
    // TODO: Check for WorldFile on URL beside the actual connection.
    if (source instanceof File) {
      final File sourceFile = ((File) source);
      String parentPath = sourceFile.getParent();
      String filename = sourceFile.getName();
      final int i = filename.lastIndexOf('.');
      filename = (i == -1) ? filename : filename.substring(0, i);

      // getting name and extension
      final String base =
          (parentPath != null)
              ? new StringBuilder(parentPath).append(File.separator).append(filename).toString()
              : filename;

      // We can now construct the baseURL from this string.
      File file2Parse = new File(new StringBuilder(base).append(".wld").toString());

      if (file2Parse.exists()) {
        final WorldFileReader reader = new WorldFileReader(file2Parse);
        raster2Model = reader.getTransform();
      } else {
        // looking for another extension
        file2Parse = new File(new StringBuilder(base).append(".tfw").toString());

        if (file2Parse.exists()) {
          // parse world file
          final WorldFileReader reader = new WorldFileReader(file2Parse);
          raster2Model = reader.getTransform();
        }
      }
    }
    return raster2Model;
  }
  /**
   * Checks whether a world file is associated with the data source. If found, set a proper
   * envelope.
   *
   * @throws IllegalStateException
   * @throws IOException
   */
  protected void parseWorldFile() {
    final String worldFilePath =
        new StringBuffer(this.parentPath)
            .append(GridCoverageUtilities.SEPARATOR)
            .append(coverageName)
            .toString();

    File file2Parse = null;
    boolean worldFileExists = false;
    // //
    //
    // Check for a world file with the format specific extension
    //
    // //
    if (worldFileExt != null && worldFileExt.length() > 0) {
      file2Parse = new File(worldFilePath + worldFileExt);
      worldFileExists = file2Parse.exists();
    }

    // //
    //
    // Check for a world file with the default extension
    //
    // //
    if (!worldFileExists) {
      file2Parse = new File(worldFilePath + GridCoverageUtilities.DEFAULT_WORLDFILE_EXT);
      worldFileExists = file2Parse.exists();
    }

    if (worldFileExists) {

      try {
        final WorldFileReader reader = new WorldFileReader(file2Parse);
        raster2Model = reader.getTransform();

        // //
        //
        // In case we read from a real world file we have together the
        // envelope. World file transformation assumes to work in the
        // CELL_CENTER condition
        //
        // //
        MathTransform tempTransform =
            PixelTranslation.translate(
                raster2Model, PixelInCell.CELL_CENTER, PixelInCell.CELL_CORNER);
        final Envelope gridRange = new GeneralEnvelope((GridEnvelope2D) originalGridRange);
        final GeneralEnvelope coverageEnvelope = CRS.transform(tempTransform, gridRange);
        originalEnvelope = coverageEnvelope;
        return;
      } catch (TransformException e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
          LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
        }
      } catch (IllegalStateException e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
          LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
        }
      } catch (IOException e) {
        if (LOGGER.isLoggable(Level.WARNING)) {
          LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
        }
      }
    }
    // if we got here we did not find a suitable transform
    raster2Model = null;
  }