From: Michael B. <mb...@sh...> - 2005-07-04 15:18:37
|
Hi all I have created a little map viewer application in which I display a map (.shp). I would now like to do the following: 1. Programmatically zoom in, soom out and "zoom all" (making the full world map visible in the panel). 2. Place a marker on the map, given its coordinates (e.g. a filled circle shape) Sorry if this sounds trivial to you, but I am a bit overwhelmed by the functionality and dont know where to start. i am also not really a pro in this mapping stuff. I would greatly appreciate if you can share some code that explains how to do this or point me to an article that covers this. Feel free to ask if you need to know more about what I am doing / want to do. Thanks & Cheers, Michael PS: Here is the source code of my little test-application: =========================================================== package com.shiftthink.geo.mapviewer; import java.awt.*; import java.awt.event.*; import javax.swing.*; // J2SE dependencies import java.net.URL; import java.io.File; import java.io.IOException; import java.io.FileNotFoundException; // User interface import javax.swing.JFrame; import javax.swing.JApplet; import javax.swing.JLabel; import java.awt.Container; import java.awt.BorderLayout; import java.awt.Color; import org.geotools.map.MapContext; import org.geotools.data.DataSourceException; import org.geotools.data.DataStore; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.FeatureSource; import org.geotools.styling.SLDParser; import org.geotools.styling.StyleFactory; import org.geotools.styling.Style; import org.geotools.styling.StyleBuilder; import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.MultiLineString; import com.vividsolutions.jts.geom.MultiPoint; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapLayer; import org.geotools.map.DefaultMapLayer; import org.geotools.gui.swing.StyledMapPane; import org.geotools.renderer.j2d.RenderedMapScale; import org.geotools.gui.swing.StatusBar; public class MapViewerFrame extends JFrame { JPanel contentPane; JToolBar jToolBar = new JToolBar(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); ImageIcon image1; ImageIcon image2; ImageIcon image3; JLabel statusBar = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); //Construct the frame public MapViewerFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { image1 = new ImageIcon(com.shiftthink.geo.mapviewer.MapViewerFrame.class.getResource("openFile.png")); image2 = new ImageIcon(com.shiftthink.geo.mapviewer.MapViewerFrame.class.getResource("closeFile.png")); image3 = new ImageIcon(com.shiftthink.geo.mapviewer.MapViewerFrame.class.getResource("help.png")); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(1024, 768)); this.setTitle("shiftTHINK MapViewer Demo"); statusBar.setText(" "); jButton1.setIcon(image1); jButton1.setToolTipText("Open File"); jButton2.setIcon(image2); jButton2.setToolTipText("Close File"); jButton3.setIcon(image3); jButton3.setToolTipText("Help"); jToolBar.add(jButton1); jToolBar.add(jButton2); jToolBar.add(jButton3); contentPane.add(jToolBar, BorderLayout.NORTH); contentPane.add(statusBar, BorderLayout.SOUTH); final MapContext context; context=MapViewerFrame.loadContext(new File("D:\\CRYPTO\\repository\\coding\\java_technology\\gis\\world\\country_col_region.shp").toURL(), null); //context=MapViewerFrame.loadContext(new File("D:\\CRYPTO\\repository\\coding\\java_technology\\gis\\data_ESRI_worldmap\\ESRI_Province_Areas_1.shp").toURL(), null); showMapPane(context); } protected static MapContext loadContext(final URL url, final URL sld) throws IOException, DataSourceException { // Load the file if (url == null) { throw new FileNotFoundException("Resource not found"); } final DataStore store = new ShapefileDataStore(url); final FeatureSource features = store.getFeatureSource(store.getTypeNames()[0]); // Create the style final Style style; if(sld != null){ SLDParser styleReader = new SLDParser(StyleFactory.createStyleFactory(),sld); style = styleReader.readXML()[0]; } else { final StyleBuilder builder = new StyleBuilder(); Class geometryClass = features.getSchema().getDefaultGeometry().getType(); if (LineString.class.isAssignableFrom(geometryClass) || MultiLineString.class.isAssignableFrom(geometryClass)) { style = builder.createStyle(builder.createLineSymbolizer()); } else if (Point.class.isAssignableFrom(geometryClass) || MultiPoint.class.isAssignableFrom(geometryClass)) { style = builder.createStyle(builder.createPointSymbolizer()); } else { style = builder.createStyle(builder.createPolygonSymbolizer( new Color(228, 232, 236), Color.BLACK, 1)); } } // Create the context MapContext context = new DefaultMapContext(); MapLayer layer = new DefaultMapLayer(features, style); layer.setTitle("The shapefile"); context.addLayer(layer); context.setTitle("Hello World"); return context; } protected void showMapPane(final MapContext context) throws Exception { // Create the map pane and add a map scale layer to it. final StyledMapPane mapPane = new StyledMapPane(); //mapPane.setDoubleBuffered(true); mapPane.setMapContext(context); mapPane.setPaintingWhileAdjusting(true); mapPane.getRenderer().addLayer(new RenderedMapScale()); mapPane.setBackground(Color.white); contentPane.setLayout(new BorderLayout()); contentPane.add(mapPane.createScrollPane(), BorderLayout.CENTER); contentPane.add(new StatusBar(mapPane), BorderLayout.SOUTH); } //File | Exit action performed public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } //Help | About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) { } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } } =============================================== |
From: Martin D. <mar...@no...> - 2005-07-05 00:10:05
|
Michael Boeni a =E9crit : > 1. Programmatically zoom in, soom out and "zoom all" (making the full > world map visible in the panel). You applies arbitrary zooms by invoking MapPane.transform(...). Otherwise, if you want an action identical to the one executed when a=20 key is pressed (e.g. the "Page Down" action for a zoom), use for example: Action zoomIn =3D mapPane.getActionMap().get("ZoomIn"); See the ZoomPane javadoc for a list of available actions. > 2. Place a marker on the map, given its coordinates (e.g. a filled > circle shape) A possible approach is to write yours own subclass of RenderedLayer and=20 drawn yours circle (in "real world" coordinates) in the "paint" method.=20 Then add this layer to the map pane using: mapPane.getRenderer().addLayer(...). Martin. |
From: James B. <br...@sc...> - 2005-08-13 16:52:33
|
Hi, I'm writing some data to an ESRI Shapefile with GT2.1. On failure to write, something is not currently closing properly, even though the closing methods are in a finally clause. For example, when I try to write over an existing file to which writing previously failed, the method: addFeatures() in FeatureStore suspends and fails to exit. Below is an example of the code I'm using. Any help would be much appreciated. Thanks, James ####CODE FeatureReader aReader = null; Transaction t = null; File f = null; try { //Create the datastore ShapefileDataStore store = new ShapefileDataStore(f.toURL()); store.createSchema(ftType); //Write the file FeatureStore featureStore = (FeatureStore)store.getFeatureSource("Features"); t = featureStore.getTransaction(); aReader = DataUtilities.reader(ftArray); featureStore.addFeatures(aReader); t.commit(); } catch(Exception e){e.printStackTrace()} //Close finally { if(aReader != null) {aReader.close()}; if(t!= null){t.close()}; } ###END CODE |