Пример #1
0
  /**
   * Reads a {@link CoordinateReferenceSystem} from a prj file.
   *
   * @param filePath the path to the prj file or the connected datafile it sidecar file for.
   * @param extension the extension of the data file. If <code>null</code> it is assumed to be prj.
   * @return the read {@link CoordinateReferenceSystem}.
   * @throws Exception
   */
  @SuppressWarnings("nls")
  public static CoordinateReferenceSystem readProjectionFile(String filePath, String extension)
      throws Exception {
    CoordinateReferenceSystem crs = null;
    String prjPath = null;
    String filePathLower = filePath.trim().toLowerCase();
    if (filePathLower.endsWith(".prj")) {
      // it is the prj file
      prjPath = filePath;
    } else if (extension != null && filePathLower.endsWith("." + extension)) {
      // datafile was supplied (substitute extension)
      int dotLoc = filePath.lastIndexOf(".");
      prjPath = filePath.substring(0, dotLoc);
      prjPath = prjPath + ".prj";
    } else {
      prjPath = filePath + ".prj";
    }

    File prjFile = new File(prjPath);
    if (!prjFile.exists()) {
      throw new ModelsIOException("The prj file doesn't exist: " + prjPath, "CRSUTILITIES");
    }
    String wkt = FileUtilities.readFile(prjFile);
    crs = CRS.parseWKT(wkt);
    return crs;
  }