Пример #1
0
  @SuppressWarnings("unchecked")
  private void paintMe(PaintEvent e) {
    int width = e.gc.getClipping().width;
    int height = e.gc.getClipping().height;

    if (map != null) {
      // sort out the transforms
      org.eclipse.swt.graphics.Rectangle paintArea =
          new org.eclipse.swt.graphics.Rectangle(0, 0, width, height);
      ReferencedEnvelope mapArea = map.getViewport().getBounds();
      setTransforms(mapArea, paintArea);

      StreamingRenderer renderer = new StreamingRenderer();
      renderer.setMapContent(map);

      RenderingHints hints =
          new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      renderer.setJava2DHints(hints);

      @SuppressWarnings("rawtypes")
      Map rendererParams = new HashMap();
      rendererParams.put("optimizedDataLoadingEnabled", new Boolean(true));

      renderer.setRendererHints(rendererParams);

      org.eclipse.swt.graphics.Rectangle curPaintArea = e.gc.getClipping();
      BufferedImage baseImage =
          new BufferedImage(
              curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2d = baseImage.createGraphics();
      g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      // renderer.setContext(context);
      java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea);
      final ReferencedEnvelope mapAOI = map.getViewport().getBounds();
      renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform());
      // swtImage.dispose();

      if (swtImage != null && !swtImage.isDisposed()) {
        swtImage.dispose();
        swtImage = null;
      }
      swtImage =
          new Image(
              canvas.getDisplay(),
              awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1));

      // org.eclipse.swt.graphics.Image image = new
      // org.eclipse.swt.graphics.Image(
      // e.display, convertToSWT(tmpImage));
      e.gc.drawImage(swtImage, 0, 0);
    }

    double y2 = Math.random() * 120d;
    e.gc.drawLine(20, 40, 80, (int) y2);
  };
Пример #2
0
  public Map sectionSigns(DirectPosition2D eventCoords) {
    Point2D eventPoint = new DirectPosition2D();
    eventPoint.setLocation(eventCoords);
    if (eventCoords != null) {
      try {
        AffineTransform transform_affine = map.getViewport().getWorldToScreen();
        transform_affine.transform(eventPoint, eventPoint);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    Map result = new HashMap();
    for (Iterator it1 = signsdb.keySet().iterator(); it1.hasNext(); ) {
      SimpleFeature feature = (SimpleFeature) it1.next();
      StructOffset offset = (StructOffset) signsdb.get(feature);
      int width = 60, height = 40;

      Point point = (Point) feature.getAttribute("the_geom");
      DirectPosition2D geoCoords = new DirectPosition2D(point.getX(), point.getY());
      // 经纬度坐标转换为屏幕坐标
      MathTransform transform_math;
      try {
        transform_math =
            CRS.findMathTransform(
                DefaultGeographicCRS.WGS84, map.getViewport().getCoordinateReferenceSystem(), true);
        transform_math.transform(geoCoords, geoCoords);
        AffineTransform transform_affine = map.getViewport().getWorldToScreen();
        transform_affine.transform(geoCoords, geoCoords);

        int signx = (int) (geoCoords.x + offset.getX() + offsetX);
        int signy = (int) (geoCoords.y + offset.getY() + offsetY);
        //				System.out.println(signsCoords.x+":"+signsCoords.y);
        if (inSection(signx, signy, width, height, (DirectPosition2D) eventPoint)) {
          result.put(feature.getID(), feature.getID());
        }
      } catch (Exception e) {

      }
    }
    return result;
  }
Пример #3
0
  /**
   * Draws map content data into image
   *
   * @param content
   * @return image
   */
  private BufferedImage saveImage(MapContent content) {
    BufferedImage image;
    if (isTile && bufferSize != 0.0d) {
      image =
          new BufferedImage(bufferedImageWidth, bufferedImageHeight, BufferedImage.TYPE_4BYTE_ABGR);
    } else {
      image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_4BYTE_ABGR);
    }

    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent(content);

    Graphics2D g = (Graphics2D) image.getGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    if (isTile && bufferSize != 0.0d) {
      renderer.paint(
          g,
          new Rectangle(bufferedImageWidth, bufferedImageHeight),
          content.getViewport().getBounds());
      try {
        image =
            image.getSubimage(
                (int) (imageWidth * bufferSize) / 2,
                (int) (imageWidth * bufferSize) / 2,
                imageWidth,
                imageHeight);
      } catch (Exception e) {
        log.error(e, "Image cropping failed");
      }
    } else {
      renderer.paint(g, new Rectangle(imageWidth, imageHeight), content.getViewport().getBounds());
    }

    content.dispose();
    return image;
  }
Пример #4
0
  /**
   * Called after the base image has been dragged. Sets the new map area and transforms
   *
   * @param env the display area (world coordinates) prior to the image being moved
   * @param paintArea the current drawing area (screen units)
   */
  private void afterImageMove() {
    final ReferencedEnvelope env = content.getViewport().getBounds();
    if (env == null) return;
    int dx = imageOrigin.x;
    int dy = imageOrigin.y;
    DirectPosition2D newPos = new DirectPosition2D(dx, dy);
    screenToWorld.transform(newPos, newPos);

    env.translate(env.getMinimum(0) - newPos.x, env.getMaximum(1) - newPos.y);
    doSetDisplayArea(env);
    imageOrigin.setLocation(0, 0);
    redrawBaseImage = true;
  }
Пример #5
0
  /**
   * Helper method for {@linkplain #setDisplayArea} which is also called by other methods that want
   * to set the display area without provoking repainting of the display
   *
   * @param envelope requested display area
   */
  private void doSetDisplayArea(Envelope envelope) {
    assert (content != null && curPaintArea != null && !curPaintArea.isEmpty());

    if (equalsFullExtent(envelope)) {
      setTransforms(fullExtent, curPaintArea);
    } else {
      setTransforms(envelope, curPaintArea);
    }
    ReferencedEnvelope adjustedEnvelope = getDisplayArea();
    content.getViewport().setBounds(adjustedEnvelope);

    MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.DISPLAY_AREA_CHANGED);
    publishEvent(ev);
  }
Пример #6
0
 /**
  * Internal initializer of the control. It starts up the map context, renderer and sets the
  * Reference System based in the SRID.
  *
  * @param srid
  * @throws InitializeMapException
  */
 private void initialize(int srid) throws InitializeMapException {
   MapContent content = new MapContent();
   try {
     content
         .getViewport()
         .setCoordinateReferenceSystem(this.generateCoordinateReferenceSystem(srid));
   } catch (FactoryException ex) {
     throw new InitializeMapException(
         Messaging.Ids.MAPCONTROL_MAPCONTEXT_WITHOUT_SRID_ERROR.toString(), ex);
   }
   if (content.getCoordinateReferenceSystem() == null) {
     throw new InitializeMapException(
         Messaging.Ids.MAPCONTROL_MAPCONTEXT_WITHOUT_SRID_ERROR.toString(), null);
   }
   this.srid = srid;
   this.setMapContent(content);
 }
Пример #7
0
  public void setCrs(CoordinateReferenceSystem crs) {
    try {
      // System.out.println(content.layers().size());
      ReferencedEnvelope rEnv = getDisplayArea();
      // System.out.println(rEnv);

      CoordinateReferenceSystem sourceCRS = rEnv.getCoordinateReferenceSystem();
      CoordinateReferenceSystem targetCRS = crs;

      MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
      com.vividsolutions.jts.geom.Envelope newJtsEnv = JTS.transform(rEnv, transform);

      ReferencedEnvelope newEnvelope = new ReferencedEnvelope(newJtsEnv, targetCRS);
      content.getViewport().setBounds(newEnvelope);
      fullExtent = null;
      doSetDisplayArea(newEnvelope);

      // ReferencedEnvelope displayArea =
      getDisplayArea();
      // System.out.println(displayArea);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #8
0
  @SuppressWarnings("deprecation")
  public void handleEvent(Event event) {

    curPaintArea = getVisibleRect();

    // System.out.println("event: " + event.type);
    if (event.type == SWT.MouseDown) {
      startX = event.x;
      startY = event.y;
      // start mouse activity
      mouseDown = true;
    } else if (event.type == SWT.MouseUp) {
      endX = event.x;
      endY = event.y;

      boolean mouseWasMoved = startX != endX || startY != endY;
      if (toolCanMove && mouseWasMoved) {
        // if the tool is able to move draw the moved image
        afterImageMove();
      }
      // stop mouse activity
      mouseDown = false;
      isDragging = false;
    } else if (event.type == SWT.Paint) {
      // System.out.println("PAINT CALLED (DOESN'T MEAN I'M DRAWING)");

      if (acceptRepaintRequests) {
        gc = event.gc;

        // System.out.println(toolCanDraw + "/" + toolCanMove + "/" + isDragging + "/" +
        // redrawBaseImage);

        /*
         * if the mouse is dragging and the current tool can
         * move the map we just draw what we already have
         * on white background. At the end of the moving
         * we will take care of adding the missing pieces.
         */
        if (toolCanMove && isDragging) {
          // System.out.println("toolCanMove && isDragging");
          if (gc != null && !gc.isDisposed() && swtImage != null) {
            /*
             * double buffer necessary, since the SWT.NO_BACKGROUND
             * needed by the canvas to properly draw background, doesn't
             * clean the parts outside the bounds of the moving panned image,
             * giving a spilling image effect.
             */
            Image tmpImage = new Image(getDisplay(), curPaintArea.width, curPaintArea.height);
            GC tmpGc = new GC(tmpImage);
            tmpGc.setBackground(white);
            tmpGc.fillRectangle(0, 0, curPaintArea.width, curPaintArea.height);
            tmpGc.drawImage(swtImage, imageOrigin.x, imageOrigin.y);
            gc.drawImage(tmpImage, 0, 0);
            tmpImage.dispose();
          }
          return;
        }

        /*
         * if the mouse is dragging and the current tool can
         * draw a boundingbox while dragging, we draw the box
         * keeping the current drawn image
         */
        if (toolCanDraw && toolManager.getCursorTool().isDrawing() && isDragging) {
          // System.out.println("draw box: " + startX + "/" + startY + "/" + endX +
          // "/" + endY);
          if (swtImage != null) {
            drawFinalImage(swtImage);
          }
          gc.setXORMode(true);

          org.eclipse.swt.graphics.Color fC = gc.getForeground();
          gc.setLineStyle(cursorToolLineStyle);
          gc.setLineWidth(cursorToolLineWidth);
          gc.setForeground(cursorToolColor);
          gc.drawRectangle(startX, startY, endX - startX, endY - startY);

          gc.setForeground(fC);
          gc.setXORMode(false);
          return;
        }

        if (!toolCanDraw && !toolCanMove && isDragging) {
          return;
        }

        if (curPaintArea == null || content == null || renderer == null) {
          return;
        }

        if (content.layers().size() == 0) {
          // if no layers available, return only if there are also no overlays

          gc.setForeground(yellow);
          gc.fillRectangle(0, 0, curPaintArea.width + 1, curPaintArea.height + 1);
          if (overlayImage == null) return;
        }

        final ReferencedEnvelope mapAOI = content.getViewport().getBounds();
        if (mapAOI == null) {
          return;
        }

        if (redrawBaseImage) {
          MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STARTED);
          publishEvent(ev);

          baseImage =
              new BufferedImage(
                  curPaintArea.width + 1, curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2d = baseImage.createGraphics();
          g2d.fillRect(0, 0, curPaintArea.width + 1, curPaintArea.height + 1);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

          // renderer.setContext(context);
          java.awt.Rectangle awtRectangle = Utils.toAwtRectangle(curPaintArea);
          renderer.paint(g2d, awtRectangle, mapAOI, getWorldToScreenTransform());
          // swtImage.dispose();

          if (swtImage != null && !swtImage.isDisposed()) {
            swtImage.dispose();
            swtImage = null;
          }
          // System.out.println("READRAWBASEIMAGE");
          swtImage =
              new Image(
                  getDisplay(),
                  awtToSwt(baseImage, curPaintArea.width + 1, curPaintArea.height + 1));
        }

        if (swtImage != null) {
          drawFinalImage(swtImage);
        }

        MapPaneEvent ev = new MapPaneEvent(this, MapPaneEvent.Type.RENDERING_STOPPED);
        publishEvent(ev);
        clearLabelCache = true;
        onRenderingCompleted();
        redrawBaseImage = false;
      }
    }
  }
Пример #9
0
  @Override
  public void draw(Graphics2D g2d, MapContent map, MapViewport viewport) {
    if (map != null && this.map == null) {
      this.map = map;
    }

    if (viewport == null) {
      viewport = map.getViewport(); // use the map viewport if one has not been provided
    }
    if (viewport == null || viewport.getScreenArea() == null) {
      return; // renderer is not set up for use yet
    }
    if (collection != null && signsdb == null) {
      signsdb = new Hashtable();
      for (Iterator it1 = collection.iterator(); it1.hasNext(); ) {
        SimpleFeature feature = (SimpleFeature) it1.next();
        StructOffset offset = new StructOffset(0, 0, -1);
        signsdb.put(feature, offset);
      }
    }
    // 消除线条锯齿
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    DefaultFeatureCollection collection = this.getCollection();
    // 遍历得到每个SimpleFeature

    for (Iterator it1 = collection.iterator(); it1.hasNext(); ) {
      SimpleFeature feature = (SimpleFeature) it1.next();
      String id = feature.getID();

      DirectPosition2D geoCoords = null;
      DirectPosition2D signCoords = null;

      Point point = (Point) feature.getAttribute("the_geom");
      if (point != null) {
        // 保存前一次刷新所处的位置
        double x = point.getX();
        double y = point.getY();
        geoCoords = new DirectPosition2D(x, y);
      }

      // 经纬度坐标转换为屏幕坐标
      MathTransform transform_math;
      try {
        transform_math =
            CRS.findMathTransform(
                DefaultGeographicCRS.WGS84, viewport.getCoordinateReferenceSystem(), true);
        transform_math.transform(geoCoords, geoCoords);
        AffineTransform transform_affine = viewport.getWorldToScreen();
        transform_affine.transform(geoCoords, geoCoords);

        g2d.setColor(Color.CYAN);
        g2d.drawOval((int) geoCoords.x, (int) geoCoords.y, 10, 10);

        // 如果被选中,填充空心成实体
        if (id.equals(selectedId)) g2d.fillOval((int) geoCoords.x, (int) geoCoords.y, 10, 10);

        if (signsdb.get(feature) != null) {
          StructOffset offset = (StructOffset) (signsdb.get(feature));
          double offsetX = offset.getX();
          double offsetY = offset.getY();
          signCoords = new DirectPosition2D(geoCoords.x + offsetX, geoCoords.y + offsetY);
        }

        if (signCoords != null) {
          // 坐标默认偏移量<-25,-60>
          int width = 60, height = 40;
          g2d.setColor(Color.CYAN);
          g2d.drawLine(
              (int) geoCoords.x + 5,
              (int) geoCoords.y + 5,
              (int) signCoords.x + offsetX + width / 2,
              (int) signCoords.y + offsetY + height / 2);
          g2d.setColor(Color.BLACK);
          g2d.drawRect((int) signCoords.x + offsetX, (int) signCoords.y + offsetY, width, height);
          g2d.setColor(Color.ORANGE);
          g2d.fillRect((int) signCoords.x + offsetX, (int) signCoords.y + offsetY, width, height);
          g2d.setColor(Color.RED);
          String signsText = (String) feature.getAttribute("name");
          g2d.drawString(
              signsText, (int) signCoords.x + offsetX + 2, (int) signCoords.y + offsetY + 12);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    // 绘制指南针
    //		int x[] = {20,10,20,30};
    //		int y[] = {10,20,30,20};
    //		int nPoint = 4;
    //		g2d.drawPolygon(x, y, nPoint);
    String APP_PATH = "";
    Bundle bundle = Platform.getBundle("MapControl");
    try {
      APP_PATH =
          FileLocator.resolve(bundle.getEntry("")).getPath().substring(1) + "icons/map/compass.png";
    } catch (Exception e) {
      System.out.println(e.getMessage());
      System.exit(1);
    }
    Image src = Toolkit.getDefaultToolkit().getImage(APP_PATH);
    double dx = 0.72, dy = 0.72;
    double x = 10, y = 10; // 指南针左上角
    g2d.rotate(
        -MainMapViewPart.rotateAngle + (Math.PI / 360) * 40,
        x + dx * src.getWidth(null) / 2,
        y + dy * src.getHeight(null) / 2);
    AffineTransform affine = new AffineTransform();
    affine.setToTranslation(x, y);
    affine.scale(dx, dy); // 缩放图片
    g2d.drawImage(src, affine, null);
  }