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);
  }
Beispiel #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);
  }