JMapframe 仅显示单个 shapefile

JMapframe displays only a single shapefile

我使用 Netbeans 和 GeoTools 编写了一个图形界面,以在同一个 JmapFrame 中显示多个 shapefile。我用了下面的代码不知道,执行的时候只显示一个shapefile.Svp,谁能帮帮我,我等你的答案。

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import java.io.File;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeature;

/**
 *
 * @author Brahim
 */
class ImportVecteur2
{
private JMapFrame fenMap;
private MapContext mapContext;
ImportVecteur2(JMapFrame fenMap)
{
//this.mapContext = mapContext;
this.fenMap = fenMap;
}

    @SuppressWarnings("static-access")
public void chercheAfficheVecteur() //throws Exception
{
try
{
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null)
{
return;
}

FileDataStore store = FileDataStoreFinder.getDataStore(file);
FeatureSource featureSource = store.getFeatureSource();
//get vertices of file

         
   
    
// Create a map context and add our shapefile to it
mapContext = new DefaultMapContext();

        
mapContext.addLayer(featureSource, null);
// Now display the map
fenMap.enableLayerTable(true);
fenMap.setMapContext(mapContext);
fenMap.setVisible(true);
  }

每次调用 chercheAfficheVecteur 时,您都会创建一个新的 MapContext,这样前一个 MapContext 就会被丢弃,而您之前的 shapefile 也会随之被丢弃。如果将方法更改为

public void chercheAfficheVecteur() {
    try {
       File file = JFileDataStoreChooser.showOpenFile("shp", null);
       if (file == null)    {
        return;
       }

    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    FeatureSource featureSource = store.getFeatureSource();
    //get vertices of file
    // Create a map context and add our shapefile to it
    if(mapContext == null){
        mapContext = new DefaultMapContext();
        fenMap.setMapContext(mapContext);
    }
      //make it look prettier
    Style style = SLD.createSimpleStyle(featureSource.getSchema());      
    mapContext.addLayer(featureSource, style);

}

ImportVecteur2(JMapFrame fenMap)
{
//this.mapContext = mapContext;
  this.fenMap = fenMap;
  fenMap.enableLayerTable(true);
  fenMap.setVisible(true);
}

它应该会更好用。

经过进一步测试(即我实际上编译了一些代码)- MapContext 已弃用(并且已经有一段时间了)请使用 MapContent。

package org.geotools.tutorial.quickstart;

import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

public class Test {
    private static final Color[] color = { Color.red, Color.blue, Color.green,
            Color.MAGENTA };
    private static MapContent mapContext;
    private static JMapFrame fenMap;

    public static void main(String args[]) throws IOException {
        Test me = new Test();
        me.run();
    }

    public void run() throws IOException {
        fenMap = new JMapFrame();
        mapContext = new MapContent();
        fenMap.setMapContent(mapContext);
        fenMap.enableToolBar(true);
        fenMap.setMinimumSize(new Dimension(300, 300));
        fenMap.setVisible(true);
        int i = 0;
        while (chercheAfficheVecteur(i)) {
            i++;
            i = i % color.length;
        }
    }

    public boolean chercheAfficheVecteur(int next) throws IOException {

        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return false;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();
        // get vertices of file
        // Create a map context and add our shapefile to it
        if (mapContext == null) {

        }
        // make it look prettier
        Style style = SLD.createSimpleStyle(featureSource.getSchema(), color[next]);

        Layer layer = new FeatureLayer(featureSource, style);
        mapContext.addLayer(layer);
        return true;
    }
}