From: Casson S. <cas...@ea...> - 2004-06-22 04:08:11
|
Hi all, Awhile ago I had some questions on mouse operations and coordinate systems. Martin answered some of them. I will share some other answers here, possibly to be corrected by those that know more. And also raise some more questions. Below are a program log and the Applet it was run from. I have been running the applet directly from eclipse and not from within a web page. Don't know if this makes a difference. THINGS I LEARNED. (Correct me if I am wrong) Attach the MouseListener to the StyledMapPane. Map data does not have a defined coordinate system until it is added to a mapContext with a defined coordinate system. I could find no way to define the coordinate system at the DataStore or FeatureCollection level. (After some more reading I did find the SLDRenderedGeometries, but its not clear to me if this is its role.) When you pull a renderer off of the StyledMapPane with a defined coordinate system, the renderer does not keep the knowledge of the coordinate system. It must be assigned separately if the renderer needs to know it. In the output see "Renderer Coorsys 1" and "Renderer Coorsys 2". The first is pulled off of the renderer before the separate assigning of the coordinate system. NEW QUESTIONS Should the renderer maintain the coordinate system of the MapPane it is pulled from? At least in my tests (as an applet under eclipse) the coordinates returned by MouseEvent.getMapCoordinates were always initially the pixel coordinates. See output log below. The mapPane knew the appropriate boundaries in real world coordinates. It was not until I resized the applet window or zoomed in on the data that getMapCoordinates started returning the correct coordinates. I tried doing .resets .validates, and resetting the bounds of the data in the program, but these did not seem to help. Any idea what is going on? Clearly there is something I am missing. I do not see how you can set up a mappane to display multiple set of data stored in different coordinate systems if you cannot define separate coordinate systems or transforms for them. How is this done unless you use SLDRenderedGeometries to create RenderedLayers? However, the documentation on this class does not seem to indicate it is for general use. Maybe I'll have to test it. Thanks Casson ----Program Log--- ShapeFile: dotairport.shp URL: file:/D:/prj/geotools/nav1/dotairport.shp WKT IS: PROJCS["NAD_1983_StatePlane_NC_FIPS_3200", GEOGCS["GCS_North_American_1983", DATUM["D_North_American_1983", SPHEROID["GRS_1980",6378137,298.257222101]], PRIMEM["Greenwich",0], UNIT["Degree",0.017453292519943295]], PROJECTION["Lambert_Conformal_Conic_2SP"], PARAMETER["False_Easting",609601.22], PARAMETER["False_Northing",0], PARAMETER["Central_Meridian",-79], PARAMETER["Standard_Parallel_1",34.33333333333334], PARAMETER["Standard_Parallel_2",36.16666666666666], PARAMETER["Latitude_Of_Origin",33.75], UNIT["Meter",1]] Layer Bounds: Env[616232.625 : 668809.5625, 198807.828125 : 237864.546875] Renderer CoorSys1 LOCAL_CS["Cartesian", LOCAL_DATUM["Unknow", 32767], UNIT["metre",1.0], AXIS["x",EAST], AXIS["y",NORTH]] Renderer CoorSys2 PROJCS["NAD_1983_StatePlane_NC_FIPS_3200", GEOGCS["GCS_North_American_1983", DATUM["D_North_American_1983", SPHEROID["GRS_1980", 6378137.0, 298.257222101]], PRIMEM["Greenwich", 0.0], UNIT["degree of angle",0.017453292519943295], AXIS["Longitude",EAST], AXIS["Latitude",NORTH]], PROJECTION["Lambert_Conformal_Conic_2SP"], PARAMETER["semi_major", 6378137.0], PARAMETER["semi_minor", 6356752.314140356], PARAMETER["central_meridian", -79.0], PARAMETER["latitude_of_origin", 33.75], PARAMETER["false_easting", 609601.22], PARAMETER["false_northing", 0.0], PARAMETER["standard_parallel_1", 34.33333333333334], PARAMETER["standard_parallel_2", 36.16666666666666], UNIT["metre",1.0], AXIS["x",EAST], AXIS["y",NORTH]] org.geotools.gui.swing.StyledMapPane MouseEntered org.geotools.gui.swing.StyledMapPane MousePressed org.geotools.gui.swing.StyledMapPane MouseReleased org.geotools.gui.swing.StyledMapPane PNT Point2D.Double[106.0, 81.0] org.geotools.gui.swing.StyledMapPane MouseExited org.geotools.gui.swing.StyledMapPane MouseEntered org.geotools.gui.swing.StyledMapPane MousePressed org.geotools.gui.swing.StyledMapPane MouseReleased org.geotools.gui.swing.StyledMapPane PNT Point2D.Double[107.75, 81.5] org.geotools.gui.swing.StyledMapPane MouseExited org.geotools.gui.swing.StyledMapPane MouseEntered org.geotools.gui.swing.StyledMapPane MousePressed org.geotools.gui.swing.StyledMapPane MouseReleased org.geotools.gui.swing.StyledMapPane PNT Point2D.Double[79.0, 81.0] org.geotools.gui.swing.StyledMapPane MousePressed org.geotools.gui.swing.StyledMapPane MouseReleased org.geotools.gui.swing.StyledMapPane MousePressed org.geotools.gui.swing.StyledMapPane MouseReleased org.geotools.gui.swing.StyledMapPane PNT Point2D.Double[628919.1264605854, 237230.85086414425] org.geotools.gui.swing.StyledMapPane MouseExited ----- Program File ----- import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.MouseEvent; import java.net.URL; import java.security.AccessControlException; import java.lang.String; import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.event.MouseListener; import org.geotools.data.FeatureSource; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.gui.swing.StyledMapPane; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapContext; import org.geotools.styling.LineSymbolizer; import org.geotools.styling.Style; import org.geotools.styling.StyleBuilder; import org.geotools.renderer.j2d.GeoMouseEvent; import org.geotools.renderer.j2d.Renderer; import org.geotools.cs.CoordinateSystem; import org.geotools.cs.CoordinateSystemFactory; import org.geotools.cs.FactoryException; /** * @author Casson */ public class JAppletTest0 extends JApplet implements MouseListener { private String shapeFile = "dotairport.shp"; // Hardcoded shapefile name for testing; private String shapeFileName = "dotairport"; // FeatureSourceName for ShapeFileDataStore; private JButton jb; private Renderer mapRender; // The renderer; private CoordinateSystem mapCS; // CS defined in setCS; public void init() { loadAppletParameters(); //Execute a job on the event-dispatching thread: //creating this applet's GUI. try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } //Called by init. protected void loadAppletParameters() { //Get the applet parameters. String sf = getParameter("ShapeFile"); this.shapeFile = (sf != null) ? sf : shapeFile; } public void createGUI() { jb = new JButton("Init"); JPanel cPane = new JPanel(new BorderLayout()); cPane.setOpaque(false); cPane.setBorder(BorderFactory.createLineBorder(Color.BLUE)); setContentPane(cPane); getContentPane().add(jb, BorderLayout.SOUTH); } public void start() { jb.setText(this.shapeFile); addMap(); } public void addMap() { try { URL rdURL = new URL(getCodeBase() + shapeFile); System.out.println("ShapeFile: " + this.shapeFile); System.out.println("URL: " + rdURL.toString()); //Create the ShapeFilDataStore and then derive the freature source from it; ShapefileDataStore dsAir = new ShapefileDataStore(rdURL); FeatureSource fsAir = dsAir.getFeatureSource(shapeFileName); //Define the style; StyleBuilder sb = new StyleBuilder(); LineSymbolizer symAir = sb.createLineSymbolizer(Color.BLACK, 3); Style styAir = sb.createStyle(symAir); //Create MapContext and StyledMapPane, Add coordinate system; MapContext map = new DefaultMapContext(); StyledMapPane mapPane = new StyledMapPane(setCS()); mapPane.createScrollPane(); map.addLayer(fsAir, styAir); getContentPane().add(mapPane, BorderLayout.CENTER); //Layer is given coordinates when it is added to map; System.out.println("Layer Bounds: " + map.getLayerBounds().toString()); //.createScrollPane()); try { mapPane.setMapContext(map); } catch (AccessControlException e) { System.err.println("Error setting map context!"); System.err.println(e.getMessage()); e.printStackTrace(); } mapRender = mapPane.getRenderer(); System.out.println("Renderer CoorSys1 " + mapRender.getCoordinateSystem().toString()); mapRender.setCoordinateSystem(mapCS); System.out.println("Renderer CoorSys2 " + mapRender.getCoordinateSystem().toString()); //Add the MouseListener to the MapPane; mapPane.addMouseListener(this); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); } } public CoordinateSystem setCS(){ String wkt = "PROJCS[\"NAD_1983_StatePlane_NC_FIPS_3200\", " + "GEOGCS[\"GCS_North_American_1983\", " + "DATUM[\"D_North_American_1983\", " + "SPHEROID[\"GRS_1980\",6378137,298.257222101]], " + "PRIMEM[\"Greenwich\",0], " + "UNIT[\"Degree\",0.017453292519943295]], " + "PROJECTION[\"Lambert_Conformal_Conic_2SP\"], " + "PARAMETER[\"False_Easting\",609601.22], " + "PARAMETER[\"False_Northing\",0], " + "PARAMETER[\"Central_Meridian\",-79], " + "PARAMETER[\"Standard_Parallel_1\",34.33333333333334], " + "PARAMETER[\"Standard_Parallel_2\",36.16666666666666], " + "PARAMETER[\"Latitude_Of_Origin\",33.75], " + "UNIT[\"Meter\",1]]"; System.out.println("WKT IS: " + wkt); CoordinateSystemFactory csFactory = CoordinateSystemFactory.getDefault(); try { mapCS = csFactory.createFromWKT(wkt); } catch (FactoryException fe) { System.err.println(fe.getMessage()); fe.printStackTrace(); } return mapCS; } public void mouseClicked(MouseEvent e) { java.awt.geom.Point2D pnt = null; GeoMouseEvent ge = (GeoMouseEvent) e; pnt = ge.getMapCoordinate(pnt); System.out.println(e.getComponent().getClass().getName()+ " PNT "+pnt.toString()); } public void mouseExited(MouseEvent e) { System.out.println(e.getComponent().getClass().getName() + " MouseExited"); } public void mouseReleased(MouseEvent e) { System.out.println(e.getComponent().getClass().getName() + " MouseReleased"); } public void mouseEntered(MouseEvent e) { System.out.println(e.getComponent().getClass().getName() + " MouseEntered"); } public void mousePressed(MouseEvent e) { System.out.println(e.getComponent().getClass().getName() + " MousePressed"); } } ------------------------------ Casson Stallings cas...@ea... |