Search code examples
netbeansjframemultiplayergeotools

JMapframe displays only a single shapefile


I used the Netbeans and GeoTools to program a graphical interface to display multiple shapefiles in the same JmapFrame. I used the following code but I do not know, when execute, it display only one shapefile.Svp, someone can help me, I await your answers.

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


Solution

  • Each time you call chercheAfficheVecteur you create a new MapContext so the previous one is thrown away and with it your previous shapefile. If you change the method to be

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

    and

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

    It should work better.

    After further testing (i.e. I actually compiled some code) - MapContext is deprecated (and has been for some time) please use 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;
        }
    }