Search code examples
javawmsjxmapviewer

WMS Layer using JXMapViewer doesn't load


Using the basic example from the JXMapViewer sources (link to github), i modified the source to load the tiles from a wms server instead. However, the Tilefactory doesn't correctly load the tiles, despite tests showing the URL gets formed corretly.

The only modified part is this:

    TileFactoryInfo info = new WMSTileFactoryInfo(0, 10, 11, "https://mesonet.agron.iastate.edu/cgi-bin/wms/us/mrms.cgi", "mrms_p48h");
    DefaultTileFactory tileFactory = new DefaultTileFactory(info);
    mapViewer.setTileFactory(tileFactory);

and testing yields correctly formed urls that can be loaded in a webbrowser:

    //Test: Created URLs work. 10 = max zoom
    System.out.println(info.getTileUrl(0, 0, 10));
    System.out.println(info.getTileUrl(0, 1, 9));
    System.out.println(info.getTileUrl(1, 1, 9));

URL of the first tile: https://mesonet.agron.iastate.edu/cgi-bin/wms/us/mrms.cgi?version=1.1.1&request=GetMap&layers=mrms_p48h&format=image/jpeg&bbox=-180.0,0.0,0.0,85.05112877980659&width=255&height=255&srs=EPSG:4326&styles=&bgcolor=0xAFDAF6

However, when i try to display it in the simple JFrame (code unchanged from example except different center location Des Moins, which works with OSM), the tiles will not load.

Here is the full code for my test:

import javax.swing.JFrame;
import org.jxmapviewer.viewer.DefaultTileFactory;
import org.jxmapviewer.viewer.GeoPosition;
import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.WMSTileFactoryInfo;
import org.jxmapviewer.viewer.TileFactoryInfo;

public class WMSTest {           
    public static void main(String[] args) {
        JXMapViewer mapViewer = new JXMapViewer();

    // Create a TileFactoryInfo for WMS
    //WMSTileFactoryInfo int minZoom, int maxZoom, int totalMapZoom, java.lang.String baseURL, java.lang.String layers)
    TileFactoryInfo info = new WMSTileFactoryInfo(0, 10, 11, "https://mesonet.agron.iastate.edu/cgi-bin/wms/us/mrms.cgi", "mrms_p48h");

    //Test: Do created URLs work? 10 = max zoom, 9 = 1 level lower -> OK.
    System.out.println(info.getTileUrl(0, 0, 10));
    System.out.println(info.getTileUrl(0, 1, 9));
    System.out.println(info.getTileUrl(1, 1, 9));

    DefaultTileFactory tileFactory = new DefaultTileFactory(info);
    mapViewer.setTileFactory(tileFactory);

    // Create a TileFactoryInfo for OpenStreetMap        
    //TileFactoryInfo info2 = new OSMTileFactoryInfo();
    //DefaultTileFactory tileFactory2 = new DefaultTileFactory(info2);
    //mapViewer.setTileFactory(tileFactory2);

    // Use 8 threads in parallel to load the tiles
    tileFactory.setThreadPoolSize(8);
    // Set the focus        
    GeoPosition desMoines = new GeoPosition(41.590833, -93.620833);

    mapViewer.setZoom(9);
    mapViewer.setAddressLocation(desMoines);
    // Display the viewer in a JFrame        
    JFrame frame = new JFrame("JXMapviewer2 Example 1");
    frame.getContentPane().add(mapViewer);
    frame.setSize(800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

Solution

  • So the mistake is server response. Using

       ?SERVICE=WMS&REQUEST=GetCapabilities
    

    does return

       <AccessConstraints>None</AccessConstraints>
    

    but this doesn't mean the server will accept requests from any source & appid. I solved my problem by testing with different public and private wms servers, and the problem wasn't in the code, (some) servers just don't accept the requests from my app. Would still be happy about an answer from someone knowledgeable, but in the meantime - maybe this helps someone running into the same problem.