|
From: George T. <geo...@ya...> - 2012-07-13 08:41:56
|
Hello Michael,
thank you for your suggestions. I have replaced MapContext with MapContent.
The JMapFrame shows GCS_WGS_1984 in the CRS button. If I click it to choose "4326: WGS 84" and force a redraw of the map, nothing changes.
I have also added 'map.getViewport().setCoordinateReferenceSystem(CRS.decode("EPSG:4326"));' immediately after 'map=new MapContent();' and also before 'mapFrame=new JMapFrame(map);'.
With this change nothing at all is displayed. Same for 'mapFrame.getMapPane().setDisplayArea(bounds);'.
Probably it is asked too much. But I would greatly appreciate if somebody modifies my code snippet or sends another example how to overlay a shapefile and a WMS such that the coordinates match.
Thank you
George
--- new code: ---
private void displayWmsAndShapefile() throws Exception {
File sourceFile;
SimpleFeatureSource featureSource;
MapContent map;
sourceFile = JFileDataStoreChooser.showOpenFile("shp", null);
if (sourceFile == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
featureSource = store.getFeatureSource();
// Create a map context and add our shapefile to it
map = new MapContent();
//map.getViewport().setCoordinateReferenceSystem(CRS.decode("EPSG:4326"));
Style coastStyle = SLD.createSimpleStyle(featureSource.getSchem$
map.addLayer(new FeatureLayer(featureSource,coastStyle));
//WMS Stuff:
URL capabilitiesURL = WMSChooser.showChooseWMS();
if( capabilitiesURL == null ){
System.exit(0); // canceled
}
WebMapServer wms = new WebMapServer( capabilitiesURL );
List<Layer> wmsLayers = WMSLayerChooser.showSelectLayer( wms );
if( wmsLayers == null ){
JOptionPane.showMessageDialog(null, "Could not connect $
System.exit(0);
}
for( Layer wmsLayer : wmsLayers ){
WMSLayer displayLayer = new WMSLayer(wms, wmsLayer );
map.addLayer( displayLayer );
}
//map.getViewport().setCoordinateReferenceSystem(CRS.decode("EPSG:4326"));
// Create a JMapFrame with custom toolbar buttons
JMapFrame mapFrame = new JMapFrame(map);
mapFrame.getMapPane().setD
mapFrame.enableToolBar(true);
mapFrame.enableStatusBar(true);
// Display the map frame. When it is closed the application will exit
mapFrame.setSize(800, 600);
mapFrame.setVisible(true);
}
-----------------
________________________________
Von: Michael Bedward <mic...@gm...>
An: George Taylor <geo...@ya...>
CC: "geo...@li..." <geo...@li...>
Gesendet: 1:53 Freitag, 13.Juli 2012
Betreff: Re: [Geotools-gt2-users] define projection when overlaying WMS and shapefile in JMapFrame
Hello George,
Firstly, with GeoTools 8 I'd recommend that you use MapContent in your
own code instead of the deprecated MapContext class.
There should be a status bar item (right hand side; you might need to
widen the frame to see it) to show or set the CRS for the display.
Programmatically you can do the same thing:
MapContent map = new MapContent();
// add layers, then...
map.getViewport().setCoordinateReferenceSystem( myDisplayCRS );
See the MapViewport javadocs for more info:
http://docs.geotools.org/latest/javadocs/org/geotools/map/MapViewport.html
Yet another way to do it is:
JMapFrame frame = ...
ReferencedEnvelope bounds = ... // create ReferencedEnvelope with desired CRS
frame.getMapPane().setDisplayArea( bounds );
Hope this helps.
Michael
On 12 July 2012 19:19, George Taylor <geo...@ya...> wrote:
> Hello,
> I am looking for some information how to set the projection correctly for a
> MapContext or JMapFrame, such that all layer overlays are correctly
> displayed. I am running displayWmsAndShapefile() (code below) to display a
> shapefile (110m_coastline.shp from
> http://www.naturalearthdata.com/downloads/110m-physical-vectors/) and the
> "Ocean Coastline" layer from the WMS at
> http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&Request=GetCapabilities&Service=WMS.
>
> The result shows that the two layers don't match: http://imagebin.org/220454
> If I show the shapefile alone, the JMapFrame sets the CRS to GCS_WGS_1984.
> If I show the WMS alone, the JMapFrame sets the CRS to EPSG:4326. How can I
> force the JMapFrame to paint everything using EPSG:4326?
>
> More generally, how can I choose the CRS that is requested from a WMS server
> if the server supports several CRSs? Using a GetMapRequest I could use the
> setSRS() method. But in the code below there is no GetMapRequest involved.
> Can I still force the JMapFrame to request the WMS Layer in a different CRS?
>
> Thank you
> George
>
> --- code snippet (using geotools-8.0-RC2) ---
>
> private void displayWmsAndShapefile() throws Exception {
> File sourceFile;
> SimpleFeatureSource featureSource;
> MapContext map;
>
> sourceFile = JFileDataStoreChooser.showOpenFile("shp", null);
> if (sourceFile == null) {
> return;
> }
> FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
> featureSource = store.getFeatureSource();
>
> // Create a map context and add our shapefile to it
> map = new DefaultMapContext();
> map.addLayer(featureSource, null);
>
> //WMS Stuff:
> URL capabilitiesURL = WMSChooser.showChooseWMS();
> if( capabilitiesURL == null ){
> System.exit(0); // canceled
> }
> WebMapServer wms = new WebMapServer( capabilitiesURL );
>
> List<Layer> wmsLayers = WMSLayerChooser.showSelectLayer( wms );
> if( wmsLayers == null ){
> JOptionPane.showMessageDialog(null, "Could not connect - check
> url");
> System.exit(0);
> }
>
> for( Layer wmsLayer : wmsLayers ){
> WMSMapLayer displayLayer = new WMSMapLayer(wms, wmsLayer );
> map.addLayer( displayLayer );
> }
>
> // Create a JMapFrame with custom toolbar buttons
> JMapFrame mapFrame = new JMapFrame(map);
> mapFrame.enableToolBar(true);
> mapFrame.enableStatusBar(true);
>
> // Display the map frame. When it is closed the application will
> exit
> mapFrame.setSize(800, 600);
> mapFrame.setVisible(true);
> }
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> GeoTools-GT2-Users mailing list
> Geo...@li...
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
> |