0

I'm facing a problem with GetFeatureInfo from WMS layer (published by my own via geoserver). After singleClick I have response no value. The WMS layers is EPSG 3857 like map view as well. I have been trying with example from openlayer and everything works fine. I do not have any idea what is wrong. Please find below my main.js file

import './style.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import TileWMS from 'ol/source/TileWMS';


const wmsSource1 = new TileWMS({
  url: 'https://portal.solarmap.pl/geoserver/ows?service=wms',
  params: {'LAYERS': 'solarmap-swidnica:VEG_IRR_FIELD_4'},
  serverType: 'geoserver',
  // projection: 'EPSG:3857',
  crossOrigin: 'anonymous',
});

const basemap = new TileLayer({
  source: new OSM(),
});


const wmsLayer1 = new TileLayer({
  source: wmsSource1,
});

const view = new View({
  center: [0, 0],
  zoom: 1,
});

const map = new Map({
  layers: [basemap, wmsLayer1],
  target: 'map',
  view: new View({
    projection: 'EPSG:3857',
    center: [1833048, 6593400],
    zoom: 18,
}),
})

map.on('singleclick', function (evt) {
  // document.getElementById('info').innerHTML = '';
  const viewResolution = /** @type {number} */ (view.getResolution());
  const url = wmsSource1.getFeatureInfoUrl(
    evt.coordinate,
    viewResolution,
    'EPSG:3857',
    {'INFO_FORMAT': 'text/html'}
  );
  console.log(url);
  if (url) {
    fetch(url)
      .then((response) => response.text())
      .then((html) => {
        document.getElementById('info').innerHTML = html;
      });
  }
});

I have chekced the WMS layer by goeserver - preview. There is a possibility to view feature value, so the problem is not in WMS I hope. Link to the WMS preview: preview WMS

geocodezip
  • 158,664
  • 13
  • 220
  • 245
piorkoo
  • 35
  • 4
  • Your code does not display the WMS unless I comment out `crossOrigin: 'anonymous',` For a workaround see https://gis.stackexchange.com/a/442378/102658 - or If it is your own server you could enable CORS https://docs.geoserver.org/latest/en/user/production/container.html#enable-cors – Mike Oct 15 '22 at 12:41
  • Thanks Mike, The cross-origin is an issue but I use chrome extension to allow this. When I grab generated Url from "preview WMS" and put it to my code as url const everything is displaying well. I think problem is with my URL... – piorkoo Oct 15 '22 at 13:05
  • I have just tried enabled CORS on geoserver but i have an error 503. I have tried also wokaround from link above (with iframe) but I have then another problem: `chromewebdata/:1 Refused to display 'https://portal.solarmap.pl/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.` – piorkoo Oct 15 '22 at 17:43
  • I figured out that I have wrong BBX values in generated url. Is always the same and incomplite. When I change manualy it works. below wrong bbx `&BBOX=0%2C0%2C20037508.342789244%2C20037508.342789244` I have no idea why the bbx is wrong calculated, any suggestions? – piorkoo Oct 15 '22 at 20:41
  • Because your `const view' is not the view used by the map – Mike Oct 15 '22 at 20:44
  • Mike, thank you! It works corectly – piorkoo Oct 15 '22 at 21:05

0 Answers0