예제 #1
0
  private CoordinateReferenceSystem getCRS(Object source) {
    CoordinateReferenceSystem crs = null;
    if (source instanceof File
        || (source instanceof URL && (((URL) source).getProtocol() == "file"))) {
      // getting name for the prj file
      final String sourceAsString;

      if (source instanceof File) {
        sourceAsString = ((File) source).getAbsolutePath();
      } else {
        String auth = ((URL) source).getAuthority();
        String path = ((URL) source).getPath();
        if (auth != null && !auth.equals("")) {
          sourceAsString = "//" + auth + path;
        } else {
          sourceAsString = path;
        }
      }

      final int index = sourceAsString.lastIndexOf(".");
      final String base =
          index > 0 ? sourceAsString.substring(0, index) + ".prj" : sourceAsString + ".prj";

      // does it exist?
      final File prjFile = new File(base.toString());
      if (prjFile.exists()) {
        // it exists then we have top read it
        PrjFileReader projReader = null;
        FileInputStream instream = null;
        try {
          instream = new FileInputStream(prjFile);
          final FileChannel channel = instream.getChannel();
          projReader = new PrjFileReader(channel);
          crs = projReader.getCoordinateReferenceSystem();
        } catch (FileNotFoundException e) {
          // warn about the error but proceed, it is not fatal
          // we have at least the default crs to use
          LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        } catch (IOException e) {
          // warn about the error but proceed, it is not fatal
          // we have at least the default crs to use
          LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        } catch (FactoryException e) {
          // warn about the error but proceed, it is not fatal
          // we have at least the default crs to use
          LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        } finally {
          if (projReader != null)
            try {
              projReader.close();
            } catch (IOException e) {
              // warn about the error but proceed, it is not fatal
              // we have at least the default crs to use
              LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
            }

          if (instream != null)
            try {
              instream.close();
            } catch (IOException e) {
              // warn about the error but proceed, it is not fatal
              // we have at least the default crs to use
              LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
            }
        }
      }
    }
    return crs;
  }
  /**
   * Gets the coordinate reference system that will be associated to the {@link GridCoverage} by
   * looking for a related PRJ.
   */
  protected void parsePRJFile() {
    String prjPath = null;

    this.crs = null;
    prjPath = this.parentPath + File.separatorChar + coverageName + ".prj";

    // read the prj serviceInfo from the file
    PrjFileReader projReader = null;

    FileInputStream inStream = null;
    FileChannel channel = null;
    try {
      final File prj = new File(prjPath);
      if (prj.exists() && prj.canRead()) {

        inStream = new FileInputStream(prj);
        channel = inStream.getChannel();
        projReader = new PrjFileReader(channel);
        this.crs = projReader.getCoordinateReferenceSystem();
      }
      // If some exception occurs, warn about the error but proceed
      // using a default CRS
    } catch (FileNotFoundException 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);
      }
    } catch (FactoryException e) {
      if (LOGGER.isLoggable(Level.WARNING)) {
        LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
      }
    } finally {
      if (projReader != null) {
        try {
          projReader.close();
        } catch (IOException e) {
          if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
          }
        }
      }

      if (inStream != null) {
        try {
          inStream.close();
        } catch (Throwable e) {
          if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
          }
        }
      }

      if (channel != null) {
        try {
          channel.close();
        } catch (Throwable e) {
          if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.log(Level.WARNING, e.getLocalizedMessage(), e);
          }
        }
      }
    }
  }