예제 #1
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);
  }
  public MapContext buildContext() {
    RandomStyleFactory RANDOM_STYLE_FACTORY = new RandomStyleFactory();

    MapContext context = null;
    MapLayer layer;

    try {
      context = new DefaultMapContext(DefaultGeographicCRS.WGS84);
      DataStore store =
          DataStoreFinder.getDataStore(
              new SingletonMap(
                  "url",
                  Demo_ContextTree.class.getResource(
                      "/org/geotools/gui/swing/demo/shape/test_polygon.shp")));
      FeatureSource<SimpleFeatureType, SimpleFeature> fs =
          store.getFeatureSource(store.getTypeNames()[0]);
      Style style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs);
      layer = new DefaultMapLayer(fs, style);
      layer.setTitle("demo_polygon.shp");
      context.addLayer(layer);

      store =
          DataStoreFinder.getDataStore(
              new SingletonMap(
                  "url",
                  Demo_ContextTree.class.getResource(
                      "/org/geotools/gui/swing/demo/shape/test_ligne.shp")));
      fs = store.getFeatureSource(store.getTypeNames()[0]);
      style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs);
      layer = new DefaultMapLayer(fs, style);
      layer.setTitle("demo_line.shp");
      context.addLayer(layer);

      store =
          DataStoreFinder.getDataStore(
              new SingletonMap(
                  "url",
                  Demo_ContextTree.class.getResource(
                      "/org/geotools/gui/swing/demo/shape/test_point.shp")));
      fs = store.getFeatureSource(store.getTypeNames()[0]);
      style = RANDOM_STYLE_FACTORY.createRandomVectorStyle(fs);
      layer = new DefaultMapLayer(fs, style);
      layer.setTitle("demo_point.shp");
      context.addLayer(layer);
      context.setTitle("DemoContext");
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return context;
  }