Exemplo n.º 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;
  }
  @Execute
  public void process() throws Exception {
    checkNull(inPath, inServiceUrl, pEpsg, pMinzoom, pMaxzoom, pWest, pEast, pSouth, pNorth);

    CoordinateReferenceSystem boundsCrs = CRS.decode(pEpsg);
    CoordinateReferenceSystem mercatorCrs = CRS.decode(EPSG_MERCATOR);
    CoordinateReferenceSystem latLongCrs = CRS.decode(EPSG_LATLONG);

    ReferencedEnvelope inBounds = new ReferencedEnvelope(pWest, pEast, pSouth, pNorth, boundsCrs);

    MathTransform in2MercatorTransform = CRS.findMathTransform(boundsCrs, mercatorCrs);
    Envelope mercatorEnvelope = JTS.transform(inBounds, in2MercatorTransform);
    ReferencedEnvelope mercatorBounds = new ReferencedEnvelope(mercatorEnvelope, mercatorCrs);

    MathTransform transform = CRS.findMathTransform(boundsCrs, latLongCrs);
    Envelope latLongBounds = JTS.transform(inBounds, transform);
    Coordinate latLongCentre = latLongBounds.centre();

    File inFolder = new File(inPath);
    File baseFolder = new File(inFolder, pName);

    double w = latLongBounds.getMinX();
    double s = latLongBounds.getMinY();
    double e = latLongBounds.getMaxX();
    double n = latLongBounds.getMaxY();

    GlobalMercator mercator = new GlobalMercator();

    for (int z = pMinzoom; z <= pMaxzoom; z++) {

      // get ul and lr tile number in GOOGLE tiles
      int[] llTileXY = mercator.GoogleTile(s, w, z);
      int[] urTileXY = mercator.GoogleTile(n, e, z);

      int startXTile = Math.min(llTileXY[0], urTileXY[0]);
      int endXTile = Math.max(llTileXY[0], urTileXY[0]);
      int startYTile = Math.min(llTileXY[1], urTileXY[1]);
      int endYTile = Math.max(llTileXY[1], urTileXY[1]);

      int tileNum = 0;

      ReferencedEnvelope levelBounds = new ReferencedEnvelope();

      pm.beginTask("Generating tiles at zoom level: " + z, (endXTile - startXTile + 1));
      for (int i = startXTile; i <= endXTile; i++) {

        for (int j = startYTile; j <= endYTile; j++) {
          tileNum++;

          double[] bounds = mercator.TileLatLonBounds(i, j, z);
          double west = bounds[0];
          double south = bounds[1];
          double east = bounds[2];
          double north = bounds[3];

          ReferencedEnvelope tmpBounds =
              new ReferencedEnvelope(west, east, south, north, latLongCrs);
          levelBounds.expandToInclude(tmpBounds);

          if (!doDryrun) {
            int[] onlineTileNumbers = {i, j};
            int[] fileNameTileNumbers = {i, j};
            // switch( pType ) {
            // case 1:
            // need to convert in TMS format
            int[] tmsNUms = mercator.TMSTileFromGoogleTile(i, j, z);
            fileNameTileNumbers = tmsNUms;

            // break;
            // case 0:
            // default:
            // break;
            // }

            File imageFolder = new File(baseFolder, z + "/" + fileNameTileNumbers[0]);
            if (!imageFolder.exists()) {
              if (!imageFolder.mkdirs()) {
                throw new ModelsIOException("Unable to create folder:" + imageFolder, this);
              }
            }
            File imageFile = new File(imageFolder, fileNameTileNumbers[1] + ".png");
            if (imageFile.exists()) {
              continue;
            }

            String tmp = inServiceUrl.replaceFirst("ZZZ", String.valueOf(z));
            tmp = tmp.replaceFirst("XXX", String.valueOf(onlineTileNumbers[0]));
            tmp = tmp.replaceFirst("YYY", String.valueOf(onlineTileNumbers[1]));
            // System.out.println(tmp);

            URL url = new URL(tmp);
            InputStream imgStream = null;
            OutputStream out = null;
            try {
              imgStream = url.openStream();
              out = new FileOutputStream(imageFile);
              int read = 0;
              byte[] bytes = new byte[1024];
              while ((read = imgStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
              }
            } catch (Exception ex) {
              pm.errorMessage("Unable to get image: " + tmp);
            } finally {
              if (imgStream != null) imgStream.close();
              if (out != null) {
                out.flush();
                out.close();
              }
            }
          }
        }
        pm.worked(1);
      }
      pm.done();

      pm.message("Zoom level: " + z + " has " + tileNum + " tiles.");
      pm.message("Boundary covered at Zoom level: " + z + ": " + levelBounds);
      pm.message("Total boundary wanted: " + mercatorBounds);
    }

    StringBuilder properties = new StringBuilder();
    properties.append("url=").append(pName).append("/ZZZ/XXX/YYY.png\n");
    properties.append("minzoom=").append(pMinzoom).append("\n");
    properties.append("maxzoom=").append(pMaxzoom).append("\n");
    properties
        .append("center=")
        .append(latLongCentre.x)
        .append(" ")
        .append(latLongCentre.y)
        .append("\n");
    properties.append("type=tms").append("\n");

    File propFile = new File(inFolder, pName + ".mapurl");
    FileUtilities.writeFile(properties.toString(), propFile);
  }