2

Using examples I found, I was able to connect to a webservice, download a world map and have it display properly in a JMapPane. My goal is to have the JMapPane only display a very specific portion of the world.

I have a geographic CRS based on NAD83 and UTM 17N. I prefer to use Northing and Easting values, for which I have the bounds of the area I wish to display.

Here is the relevant code:

Rectangle bounds=projectdata.getPlanRange();

ReferencedEnvelope envelope = new ReferencedEnvelope(bounds.getMinX(), bounds.getMaxX(), bounds.getMinY(), bounds.getMaxY(), projectdata.getWorkPlancrs());

mapPane.setDisplayArea(envelope);

If I don't set the displayarea with the envelope, I see the world. If I set it, it gives me a view of water somewhere in the world. I can only assume that the ReferencedEnvelope can handle Northing/Easting values with a geographic CRS provided. I've read the documentation a few times and searched a long time before posting this question. I'm sure there is a simple answer as to what I'm doing wrong.

In case it matters at all, the wms source I am using is

http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&Request=GetCapabilities&Service=WMS
Ian Turton
  • 10,018
  • 1
  • 28
  • 47

1 Answers1

0

You don't say how you are creating your map in the JFrame. But the following works for me (subject to some caveats I'll go into below). The trick is to set the viewport with the CRS and the Bounding Box you require. You can get the full code here.

  private void createWMS() {
    content.getViewport().setCoordinateReferenceSystem(crs);
    content.getViewport().setBounds(bbox);
    if (url == null) {
      throw new IllegalStateException("URL is not set");
    }
    try {
      wms = new WebMapServer(url);
    } catch (ServiceException | IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(2);
    }
    WMSCapabilities capabilities = wms.getCapabilities();
    org.geotools.data.ows.Layer[] wmsLayers = WMSUtils.getNamedLayers(capabilities);
    for (org.geotools.data.ows.Layer wmsLayer : wmsLayers) {
      if(layers.isEmpty()) {//just write out layers
        System.out.println(wmsLayer.getName());
      } 
      if (layers.contains(wmsLayer.getName())) {
        WMSLayer displayLayer = new WMSLayer(wms, wmsLayer);
        if (!styles.isEmpty()) {
          String wmsStyle = styles.get(layers.indexOf(wmsLayer.getName()));
          displayLayer = new WMSLayer(wms, wmsLayer);
          List<StyleImpl> layerStyles = wmsLayer.getStyles();
          for (StyleImpl s : layerStyles) {
            if (s.getName().equalsIgnoreCase(wmsStyle)) {
              displayLayer.setStyle((Style) s);
            }
          }
        }

        content.addLayer(displayLayer);

      }
    }
  }

This produces with the CRS set to EPSG:26917 and a bbox of roughly -157051,5400992, 279239,5664905 of a map like:

enter image description here

As you can see this service is ending soon! and also it doesn't handle EPSG:26917 very well as it has drawn the "back" of Russia on the other side of the globe and labelled it before drawing Canada which is a bit confusing and if you zoom out much further the map becomes unusable due to the borders and rivers etc crossing "behind" the map.

Ian Turton
  • 10,018
  • 1
  • 28
  • 47
  • The example helped a lot Ian. I was able to replicate what you did in the example and see better how this is supposed to work. What complicated the matter for me was that the wms source had so little detail it was hard to figure out what was being displayed when I selected a very small area (about 1 sq mile) – Stephen Cuminger Jan 14 '18 at 23:00
  • Would you happen to have a similar example based on retrieving tiles from a server? In my application the map will need to pan a lot, so wms probably isn't the best solution. I just need pretty basic stuff like streets, roads, rivers, etc. – Stephen Cuminger Jan 14 '18 at 23:17