void matchAspect() {
   /* Basic sinusoidal projection of lat/lon data to square pixels */
   double yCenter = modelBounds.centre().y;
   float xScale = cos(radians((float) yCenter));
   double newX =
       modelBounds.getHeight() * (1 / xScale) * ((float) this.getWidth() / this.getHeight());
   modelBounds.expandBy((newX - modelBounds.getWidth()) / 2f, 0);
 }
Пример #2
0
 public void fit() throws Exception {
   MapContent generalMapContext = getGeneralMapContext();
   Dimension size = getSize();
   Envelope projectedBounds = generalMapContext.getMaxBounds();
   double destWidth = projectedBounds.getWidth();
   double destHeight = projectedBounds.getHeight();
   Coordinate centre = projectedBounds.centre();
   Point2D.Double screenLT = awtScreen2Cartesian(new Point(0, 0));
   Point2D.Double screenBR = awtScreen2Cartesian(new Point(size.width, size.height));
   double srcWidth = screenBR.x - screenLT.x;
   double srcHeight = screenBR.y - screenLT.y;
   double sx = srcWidth / destWidth;
   double sy = srcHeight / destHeight;
   double coef = Math.min(sx, sy);
   coef = snapScale(coef);
   scaleView(coef, coef, false);
   Point2D.Double projectedScreenCenter = screen2Cartesian(new Point(0, 0));
   translateView(projectedScreenCenter.x - centre.x, projectedScreenCenter.y - centre.y, true);
   repaint();
 }
  @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);
  }
Пример #4
0
  /**
   * Draws grid similarly to the "world" version, but with some changes: - Center the grid on the
   * screen - label the grid lines in CRS units - outline the grid
   *
   * @param context
   * @param showLabels is true, the bottom and right edges of the map are covered with white strips,
   *     and labels for the grid lines are placed in the white strips.
   */
  private void drawGrid(MapGraphicContext context, boolean showLabels) {

    GridStyle style = getStyle(context.getLayer());
    double[] gridSize = style.getGridSize();

    try {
      MathTransform mt = CRS.findMathTransform(DefaultGeographicCRS.WGS84, context.getCRS(), true);

      if (!mt.isIdentity()) {
        double x = gridSize[0] / 2.0;
        double y = gridSize[1] / 2.0;
        double[] toTransform = new double[] {-x, -y, x, y};
        double[] dest = new double[4];
        mt.transform(toTransform, 0, dest, 0, 2);
        gridSize = new double[] {Math.abs(dest[2] - dest[0]), Math.abs(dest[3] - dest[1])};
      }
    } catch (Exception e) {
      MapGraphicPlugin.log("", e); // $NON-NLS-1$
    }

    Envelope bounds = context.getViewportModel().getBounds();

    Coordinate centerCoord = bounds.centre();
    gridSize = style.getGridSize();
    Coordinate topLeftCenterCoord =
        new Coordinate(centerCoord.x - gridSize[0] / 2, centerCoord.y + gridSize[1] / 2);
    Coordinate topLeftMostCoord = new Coordinate(topLeftCenterCoord);
    while (topLeftMostCoord.x - gridSize[0] > bounds.getMinX()) {
      topLeftMostCoord.x -= gridSize[0];
    }
    while (topLeftMostCoord.y + gridSize[1] < bounds.getMaxY()) {
      topLeftMostCoord.y += gridSize[1];
    }
    Coordinate coord = topLeftMostCoord;

    ViewportGraphics graphics = context.getGraphics();
    int mapPixelWidth = context.getMapDisplay().getWidth();
    int mapPixelHeight = context.getMapDisplay().getHeight();

    // cover the right side and bottom of map with thin strips
    final int RIGHT_STRIP_WIDTH = (int) (mapPixelWidth * 0.05);
    final int BOTTOM_STRIP_HEIGHT = (int) (mapPixelHeight * 0.03);
    final int GRID_LINE_EXTENSION = (int) (RIGHT_STRIP_WIDTH * 0.1);
    graphics.setColor(Color.white);
    graphics.fillRect(mapPixelWidth - RIGHT_STRIP_WIDTH, 0, RIGHT_STRIP_WIDTH, mapPixelHeight);
    graphics.fillRect(0, mapPixelHeight - BOTTOM_STRIP_HEIGHT, mapPixelWidth, BOTTOM_STRIP_HEIGHT);

    // draw grid lines
    graphics.setColor(style.getColor());
    Point pixel = null;
    while (true) {
      pixel = context.worldToPixel(coord);
      coord.x += gridSize[0];
      coord.y -= gridSize[1];
      Point next = context.worldToPixel(coord);
      if (next.x - pixel.x < 2 || next.y - pixel.y < 2) {
        context.getLayer().setStatus(ILayer.WARNING);
        context.getLayer().setStatusMessage(Messages.GridMapGraphic_grids_too_close);
        break;
      }
      if ((pixel.x >= mapPixelWidth && pixel.y >= mapPixelHeight)) break;

      // draw vertical lines and labels
      if (pixel.x < mapPixelWidth)
        graphics.drawLine(
            pixel.x, 0, pixel.x, mapPixelHeight - BOTTOM_STRIP_HEIGHT + GRID_LINE_EXTENSION);
      if (showLabels) {
        graphics.drawString(
            (int) (coord.y) + "",
            pixel.x,
            mapPixelHeight - BOTTOM_STRIP_HEIGHT + GRID_LINE_EXTENSION,
            ViewportGraphics.ALIGN_MIDDLE,
            ViewportGraphics.ALIGN_TOP);
      }

      // draw horizontal lines and labels
      if (pixel.y < mapPixelHeight)
        graphics.drawLine(
            0, pixel.y, mapPixelWidth - RIGHT_STRIP_WIDTH + GRID_LINE_EXTENSION, pixel.y);
      if (showLabels) {
        graphics.drawString(
            (int) (coord.x) + "",
            mapPixelWidth - RIGHT_STRIP_WIDTH + GRID_LINE_EXTENSION,
            pixel.y,
            ViewportGraphics.ALIGN_LEFT,
            ViewportGraphics.ALIGN_MIDDLE);
      }
      pixel = next;
    }

    // outline the map
    graphics.drawRect(
        0, 0, mapPixelWidth - RIGHT_STRIP_WIDTH, mapPixelHeight - BOTTOM_STRIP_HEIGHT);
  }