[geotk] First try with geo-tk. Display a map with Shapefile

Martin Desruisseaux martin.desruisseaux at geomatys.fr
Sun Sep 11 16:13:32 EDT 2011


Hello Arnaud

Johann is in better position than me for answering your question. Right now he 
is in a rush because of the FOSS4G event which is hapenning this week. But I 
guess that during the next week things will calm down a bit...

     Regards,

         Martin


Le 09/09/11 18:50, Arnaud Vandecasteele a écrit :
> Hi all,
>
>
> I'm "playing" with geo-tk and I'm trying to display a map which
> contains a Shapefile.
> I've almost succeed to do what I want but my data are not visible on the map.
>
> Did I miss something ?
> If you could have a look on my code it will be great.
>
> Thanks for your help
>
> Best regards
>
> Arnaud
>
>
> package geoTest;
>
> import java.awt.BorderLayout;
> import java.awt.Color;
> import java.awt.Dimension;
> import java.io.File;
> import java.net.MalformedURLException;
> import java.net.URL;
>
> import javax.swing.JFrame;
> import javax.swing.JPanel;
>
> import org.geotoolkit.data.FeatureCollection;
> import org.geotoolkit.data.query.QueryBuilder;
> import org.geotoolkit.data.shapefile.ShapefileDataStore;
> import org.geotoolkit.gui.swing.go2.JMap2D;
> import org.geotoolkit.gui.swing.go2.control.JNavigationBar;
> import org.geotoolkit.map.MapBuilder;
> import org.geotoolkit.map.MapContext;
> import org.geotoolkit.map.MapLayer;
> import org.geotoolkit.referencing.crs.DefaultGeographicCRS;
> import org.geotoolkit.storage.DataStoreException;
> import org.geotoolkit.util.RandomStyleFactory;
> import org.opengis.feature.simple.SimpleFeature;
> import org.opengis.feature.type.Name;
>
> public class createMapPanel {
>
> 	/**
> 	 * @param args
> 	 */
> 	public static void main(String[] args) {
> 		MapContext mapContext =
> MapBuilder.createContext(DefaultGeographicCRS.WGS84);
> 		addShpData(mapContext);
> 		
> 		JMap2D map = new JMap2D();
> 		map.getContainer().setContext(mapContext);		
> 		map.setBackground(new Color(0,150,150));		
> 		
> 		JFrame pJFrame = createGUI(map);
> 		pJFrame.setVisible(true);
> 	}
> 	
> 	public static JFrame createGUI(JMap2D map){
> 		JFrame frm = new JFrame("My first map with GeoToolkit");
> 		
> 		//Add elements
> 		JPanel pJPanel = new JPanel(new BorderLayout());		
> 		JNavigationBar navBar = new JNavigationBar(map);
> 		pJPanel.add(BorderLayout.NORTH,navBar);		
> 		pJPanel.add(BorderLayout.CENTER, map);
> 		
> 		//Create
> 		frm.setContentPane(pJPanel);
> 		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> 		frm.setSize(new Dimension(500,500));
> 		frm.setVisible(true);
> 		return frm;
> 	}
> 	
> 	public static void addShpData(MapContext context){
> 		try {
> 			URL shapeURL = new
> File("/home/arnaud/GisData/World/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp").toURI().toURL();
> 			//final DataStore store = DataStoreFinder.getDataStore("url", shapeURL);
> 			ShapefileDataStore store = new ShapefileDataStore(shapeURL);
> 			final Name name = store.getNames().iterator().next();
> 			final FeatureCollection<SimpleFeature>  fs =
> store.createSession(true).getFeatureCollection(QueryBuilder.all(name));
> 			final MapLayer layer = MapBuilder.createFeatureLayer(fs,
> RandomStyleFactory.createDefaultVectorStyle(fs));
> 			layer.setVisible(true);
> 			context.layers().add(layer);
> 		} catch (MalformedURLException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		} catch (DataStoreException e) {
> 			// TODO Auto-generated catch block
> 			e.printStackTrace();
> 		}
> 	}
>
> }



More information about the Geotoolkit mailing list