Exemple #1
0
  /**
   * Sets the display to paint selected features yellow and unselected features in the default
   * style.
   *
   * @param IDs identifiers of currently selected features
   */
  public void displaySelectedFeatures(Set<FeatureId> IDs) {
    Style style;

    if (IDs.isEmpty()) {
      style = createDefaultStyle();

    } else {
      style = createSelectedStyle(IDs);
    }

    //     mapFrame.getMapContext().getLayer(0).setStyle(style);
    //     这里有点问题,需调整
    mapFrame.getMapPane().repaint();
  }
Exemple #2
0
  /**
   * This method is called by our feature selection tool when the user has clicked on the map.
   *
   * @param pos map (world) coordinates of the mouse cursor
   */
  void selectFeatures(MapMouseEvent ev) {

    System.out.println("Mouse click at: " + ev.getMapPosition());

    /*
     * Construct a 5x5 pixel rectangle centred on the mouse click position
     */
    Point screenPos = ev.getPoint();
    Rectangle screenRect = new Rectangle(screenPos.x - 2, screenPos.y - 2, 5, 5);

    /*
     * Transform the screen rectangle into bounding box in the coordinate
     * reference system of our map context. Note: we are using a naive method
     * here but GeoTools also offers other, more accurate methods.
     */
    AffineTransform screenToWorld = mapFrame.getMapPane().getScreenToWorldTransform();
    Rectangle2D worldRect = screenToWorld.createTransformedShape(screenRect).getBounds2D();
    ReferencedEnvelope bbox =
        new ReferencedEnvelope(
            worldRect,
            //             mapFrame.getMapContext().getCoordinateReferenceSystem());
            //             这里有点问题,需调整
            mapFrame.getMapContent().getCoordinateReferenceSystem());

    /*
     * Create a Filter to select features that intersect with
     * the bounding box
     */
    Filter filter = ff.intersects(ff.property(geometryAttributeName), ff.literal(bbox));

    // ff.bbox(ff.property(geometryAttributeName), bbox);

    /*
     * Use the filter to identify the selected features
     */
    try {
      FeatureCollection<SimpleFeatureType, SimpleFeature> selectedFeatures =
          featureSource.getFeatures(filter);

      FeatureIterator<SimpleFeature> iter = selectedFeatures.features();
      Set<FeatureId> IDs = new HashSet<FeatureId>();
      try {
        while (iter.hasNext()) {
          SimpleFeature feature = iter.next();
          IDs.add(feature.getIdentifier());

          System.out.println("   " + feature.getIdentifier());
        }

      } finally {
        iter.close();
      }

      if (IDs.isEmpty()) {
        System.out.println("   no feature selected");
      }

      displaySelectedFeatures(IDs);

    } catch (Exception ex) {
      ex.printStackTrace();
      return;
    }
  }