コード例 #1
0
  public void draw(MapGraphicContext context) {

    boolean showLabels = true;
    Color gridColor = Color.black;
    Font font = new Font("Times", Font.PLAIN, 8);

    Envelope bounds = context.getViewportModel().getBounds();
    List<Double>[] gridLines = chooseGridLines(bounds);

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

    List<Double> xCoords = gridLines[0];
    List<Double> yCoords = gridLines[1];

    // draw vert lines
    for (int i = 0; i < xCoords.size(); i++) {
      double x = xCoords.get(i).doubleValue();

      for (int j = 0; j < yCoords.size() - 1; j++) {

        double y1 = yCoords.get(j).doubleValue();
        Coordinate lastCoord = new Coordinate(x, y1);
        double y2 = yCoords.get(j + 1).doubleValue();
        Coordinate thisCoord = new Coordinate(x, y2);

        Point lastPixel = context.worldToPixel(lastCoord);
        Point thisPixel = context.worldToPixel(thisCoord);

        graphics.drawLine(lastPixel.x, lastPixel.y, thisPixel.x, thisPixel.y);
      }
    }

    // draw horiz lines

    for (int i = 0; i < yCoords.size(); i++) {
      double y = yCoords.get(i).doubleValue();
      for (int j = 0; j < xCoords.size() - 1; j++) {

        double x1 = xCoords.get(j).doubleValue();
        Coordinate lastCoord = new Coordinate(x1, y);
        double x2 = xCoords.get(j + 1).doubleValue();
        Coordinate thisCoord = new Coordinate(x2, y);

        Point lastPixel = context.worldToPixel(lastCoord);
        Point thisPixel = context.worldToPixel(thisCoord);

        graphics.drawLine(lastPixel.x, lastPixel.y, thisPixel.x, thisPixel.y);
      }
    }

    // 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.05);
    final int GRID_LINE_EXTENSION = (int) (RIGHT_STRIP_WIDTH * 0.1);
    graphics.setColor(Color.white);
    graphics.setBackground(Color.white);

    graphics.clearRect(mapPixelWidth - RIGHT_STRIP_WIDTH, 0, RIGHT_STRIP_WIDTH, mapPixelHeight);
    graphics.clearRect(0, mapPixelHeight - BOTTOM_STRIP_HEIGHT, mapPixelWidth, BOTTOM_STRIP_HEIGHT);

    // outline the map
    graphics.setColor(Color.BLACK);
    graphics.drawRect(
        0, 0, mapPixelWidth - RIGHT_STRIP_WIDTH - 1, mapPixelHeight - BOTTOM_STRIP_HEIGHT - 1);

    // draw labels
    graphics.setFont(font);
    if (showLabels) {
      graphics.setColor(gridColor);
      for (int i = 1; i < yCoords.size() - 1; i++) {
        double y = yCoords.get(i).doubleValue();
        double x = xCoords.get(xCoords.size() - 1).doubleValue();
        Point pixel = context.worldToPixel(new Coordinate(x, y));
        // line is covered by white overlay
        if (pixel.y > mapPixelHeight - BOTTOM_STRIP_HEIGHT) break;
        graphics.drawString(
            (int) (y) + "",
            mapPixelWidth - RIGHT_STRIP_WIDTH + GRID_LINE_EXTENSION,
            pixel.y,
            ViewportGraphics.ALIGN_LEFT,
            ViewportGraphics.ALIGN_MIDDLE);
      }
      for (int i = 1; i < xCoords.size() - 1; i++) {
        double x = xCoords.get(i).doubleValue();
        double y = yCoords.get(yCoords.size() - 1).doubleValue();
        Point pixel = context.worldToPixel(new Coordinate(x, y));
        // line is covered by white overlay
        if (pixel.x > mapPixelWidth - RIGHT_STRIP_WIDTH) break;
        graphics.drawString(
            (int) (x) + "",
            pixel.x,
            mapPixelHeight - BOTTOM_STRIP_HEIGHT + GRID_LINE_EXTENSION,
            ViewportGraphics.ALIGN_MIDDLE,
            ViewportGraphics.ALIGN_TOP);
      }
    } // showlabels
  }
コード例 #2
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);
  }