1

I can view a layer (called wmsSource), created as a vector point-set, and stored in a PostGIS table from an Esri shapefile, in my OpenLayers map, as a WMS Image layer, using this code, a modification from the OpenLayers sample:

<!DOCTYPE html>
<html>
  <head>
    <title>WMS GetFeatureInfo (Image Layer)</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <div id="info">&nbsp;</div>
    <script>
  
    var geosource = new ol.source.ImageWMS({
        url: 'http://localhost:8080/geoserver/Workspace/wms',
        params: {'LAYERS': 'Workspace:Shapefile layers for the Bay of Plenty'},
        ratio: 1,
        serverType: 'geoserver'
    });

    var geolayer = new ol.layer.Image ({source: geosource,});

    var view = new ol.View({
        center: [19687000, -4573021],
        zoom: 11
      });

      var map = new ol.Map({
        layers: [geolayer],
        target: 'map',
        view: view
      });

      map.on('singleclick', function(evt) {
        document.getElementById('info').innerHTML = '';
        var viewResolution = /** @type {number} */ (view.getResolution());
        var url = geosource.getGetFeatureInfoUrl(
            evt.coordinate, viewResolution, 'EPSG:3857',
            {'INFO_FORMAT': 'text/html'});
        if (url) {
          document.getElementById('info').innerHTML =
              '<iframe seamless src="' + url + '"></iframe>';
        }
      });

      map.on('pointermove', function(evt) {
        if (evt.dragging) {
          return;
        }
        var pixel = map.getEventPixel(evt.originalEvent);
        var hit = map.forEachLayerAtPixel(pixel, function() {
          return true;
        });
        map.getTargetElement().style.cursor = hit ? 'pointer' : '';
      });

    </script>
  </body>
</html>

The example displays all the data as an image in a frame (although Firefox blocks the image as an inset and forces me to open it in another tab), but I would like to be able to click an item on the map and get a pop-up of information, just the fid would be a start.

The layer covers a small area, so native SRS used is EPSG:2106.

What is the syntax I should use to get data from the fid field into innerHTML for a pop-up?

8
  • Shapefiles are not stored in PostGIS databases. The data from a shapefile may be loaded into a PostgreSQL database, with the geometry stored in a PostGIS geometry type, but at that point it is no longer a shapefile.
    – Vince
    Commented Mar 31, 2023 at 23:27
  • That should be possible with WMS openlayers.org/en/latest/examples/getfeatureinfo-image.html The data may also be available as GeoJSON via WFS but you would need to style a vector layer yourself.
    – Mike
    Commented Mar 31, 2023 at 23:29
  • That has clarified things - I was able to see the full table of information using the OpenLayers example suggested by Mike. Now I have to work out how to extract individual field data and return them as constants for the pop-up. Commented Apr 1, 2023 at 1:46
  • If the source supports it {'INFO_FORMAT': 'application/json'} will return the feature in GeoJSON format.
    – Mike
    Commented Apr 1, 2023 at 8:59
  • 1
    It should be possible by combining the FeatureInfo example with the Popup example. If application/json is not supported you could try test/plain and then extract field data using regular expressions. codesandbox.io/s/getfeatureinfo-image-forked-7hs4pi?file=/…
    – Mike
    Commented Apr 1, 2023 at 11:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.