CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.98k stars 3.5k forks source link

Fix proxy WMS Doc Example #7272

Open OmarShehata opened 5 years ago

OmarShehata commented 5 years ago

The doc example on WebMapServiceImageryProvider tells you to pass the proxy as an option:

var provider = new Cesium.WebMapServiceImageryProvider({
    url : 'https://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer',
    layers : '0',
    proxy: new Cesium.DefaultProxy('/proxy/')
});

viewer.imageryLayers.addImageryProvider(provider);

But proxy is not an option on this class! The right way to pass this proxy would be:

var provider = new Cesium.WebMapServiceImageryProvider({
    url : new Cesium.Resource({
         url : ''https://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer",
         proxy: new Cesium.DefaultProxy('/proxy/')
    }),
    layers : '0'
});

viewer.imageryLayers.addImageryProvider(provider);

I'm opening this issue instead of just a PR because I'm running into another problem now. It seems that after I do this, the request doesn't work because the URL is parsed/re-encoded. So the following code:

var viewer = new Cesium.Viewer('cesiumContainer');

var deformationNaples = new Cesium.WebMapServiceImageryProvider({
    url : new Cesium.Resource({
        url : "http://server.com",
        proxy: new Cesium.DefaultProxy("https://proxy.com/?proxyTo=")
    }),
    layers: "geonode:NAPOLI_DEFORMAZIONE_MAP",
    rectangle: Cesium.Rectangle.fromDegrees(14.05072, 40.82471, 14.30817, 40.91915),    
});

viewer.flyTo(viewer.imageryLayers.addImageryProvider(deformationNaples));

Is expected to make a request to:

https://proxy.com/?proxyTo=http://server.com

But instead it makes a request to:

https://proxy.com/?proxyTo=http%3A%2F%2Fserver.com

I remember seeing an issue that was closed by @hpinkos about this but I can't seem to find it now. This is the forum thread where it most recently came up.

mramato commented 5 years ago

But instead it makes a request to:

https://proxy.com/?proxyTo=http%3A%2F%2Fserver.com

This is the correct behavior, url parameters must be encoded. Your "expected" version is incorrect. All that's needed here is the doc update (proxy used to be directly on the options object but changed at the beginning of the year when we added Resource)

giswqs commented 5 years ago

Could anyone provide the correct example? I tried the following code, but it did not work. Any help will be appreciated.

var viewer = new Cesium.Viewer('cesiumContainer');

var layerUrl = 'https://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer';
var myResource = new Cesium.Resource( {"url":layerUrl, "proxy":new Cesium.DefaultProxy('/proxy')} ); 
var provider = new Cesium.WebMapServiceImageryProvider({ "url" : myResource, "layers" : '0' });

viewer.imageryLayers.addImageryProvider(provider);
OmarShehata commented 5 years ago

@giswqs this is the correct version. If you look at the network tab you can see it using the proxy you define.

If you're having trouble getting your imagery work in CesiumJS, please open a new thread on the Cesium forum (https://groups.google.com/forum/#!forum/cesium-dev) and we can discuss it there.

giswqs commented 5 years ago

@OmarShehata See below a screenshot of the error message. I am not sure how to resolve the issue. I also replied to a related thread on the Cesium forum. Many thanks!

Imgur