private static void constructMapUI(final File shapefile) {

    JMapFrame frame;
    MapContent map = new MapContent();

    FileDataStore dataStore;
    SimpleFeatureSource shapefileSource;

    try {
      dataStore = FileDataStoreFinder.getDataStore(shapefile);
      shapefileSource = dataStore.getFeatureSource();
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }
    Style shpStyle = SLD.createPolygonStyle(Color.RED, null, 0.0f);
    Layer shpLayer = new FeatureLayer(shapefileSource, shpStyle);
    map.addLayer(shpLayer);

    frame = new JMapFrame(map);
    frame.enableLayerTable(true);
    frame.setSize(1000, 800);
    frame.enableStatusBar(true);
    frame.enableToolBar(true);
    frame.setTitle("Map Viewer (courtesy of GeoTools");

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JMapFrame.HIDE_ON_CLOSE);
  }
Esempio n. 2
0
  /**
   * This method connects to the shapefile; retrieves information about its features; creates a map
   * frame to display the shapefile and adds a custom feature selection tool to the toolbar of the
   * map frame.
   */
  public void displayShapefile(File file) throws Exception {
    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    featureSource = store.getFeatureSource();
    setGeometry();

    /*
     * Create the JMapFrame and set it to display the shapefile's features
     * with a default line and colour style
     */
    MapContext map = new DefaultMapContext();
    map.setTitle("Feature selection tool example");
    Style style = createDefaultStyle();
    map.addLayer(featureSource, style);
    mapFrame = new JMapFrame(map);
    mapFrame.enableToolBar(true);
    mapFrame.enableStatusBar(true);

    /*
     * Before making the map frame visible we add a new button to its
     * toolbar for our custom feature selection tool
     */
    JToolBar toolBar = mapFrame.getToolBar();
    JButton btn = new JButton("Select");
    toolBar.addSeparator();
    toolBar.add(btn);

    /*
     * When the user clicks the button we want to enable
     * our custom feature selection tool. Since the only
     * mouse action we are intersted in is 'clicked', and
     * we are not creating control icons or cursors here,
     * we can just create our tool as an anonymous sub-class
     * of CursorTool.
     */
    btn.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent e) {
            mapFrame
                .getMapPane()
                .setCursorTool(
                    new CursorTool() {

                      @Override
                      public void onMouseClicked(MapMouseEvent ev) {
                        selectFeatures(ev);
                      }
                    });
          }
        });

    /** Finally, we display the map frame. When it is closed this application will exit. */
    mapFrame.setSize(600, 600);
    mapFrame.setVisible(true);
  }
Esempio n. 3
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();
  }
Esempio n. 4
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;
    }
  }