0

I have drawn a polygon feature on the WMS base layer, then when I try to intersect the drawn feature and base layer I am unable to find the intersection, but when I have used a GeoJSON layer instead of the WMS layer it intersects the polygon feature.

Reference code

<!DOCTYPE html>
<html>
<head>
<title>Select features example</title>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.10.1/ol.css" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.10.1/ol-debug.js"></script>
<script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/lib/0.16.0/javascript.util.min.js"></script>
<script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/lib/0.16.0/jsts.min.js"></script>
<style>
  html, body {
    padding: 0;
    margin: 0;
  }
  html, body, .map {
    height: 100%;
    width: 100%;
  }
</style>
</head>
<body>
  <div id="map" class="map"></div>
</div>
</div>
<script>
var swissProjection = new ol.proj.Projection({
  code: 'EPSG:21781',
  extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
  units: 'm'
});
ol.proj.addProjection(swissProjection);
var geojsonFormat = new ol.format.GeoJSON();

var style = new ol.style.Style({
  fill: new ol.style.Fill({
    color: 'rgba(255, 255, 255, 0.3)'
  }),
  stroke: new ol.style.Stroke({
    color: 'rgba(255, 120, 0, 0.6)',
    width: 1
  })
});
var styles = [style];

var highlightStyle = [
  new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 120, 0, 0.3)'
    }),
    stroke: new ol.style.Stroke({
      color: 'rgba(255, 120, 0, 0.6)',
      width: 1
    })
  })
];

var vector2 = new ol.layer.Vector({
  maxResolution: 0.8,
  source: new ol.source.Vector({
    url: 'map.geojson',
    format: new ol.format.GeoJSON({
      defaultDataProjection:'EPSG:4326'
    })
  }),
  style: styles
});
var layer1 = new ol.layer.Tile({
            title: 'India',
            source: new ol.source.TileWMS({
              url: 'http://ndem.nrsc.gov.in/geoserver/ndem50k/wms',
              params: {LAYERS: 'ndem50k:stateadmin50census2011'}
            }),
            transparent: false
          });
var dist = new ol.layer.Tile({
            title: 'flood',
            source: new ol.source.TileWMS({
              url: 'http://ndem.nrsc.gov.in/geoserver/ndem50d/wms',
              //http://172.31.4.37/geoserver/ndem50k/wms
              params: {LAYERS: 'ndem50d:apflood50dsc04122015'}
              //ndem50k:apdistrictadmin50census2011
            })
          });
var distaluk = new ol.layer.Tile({
            title: 'taluk',
            source: new ol.source.TileWMS({
              url: 'http://ndem.nrsc.gov.in/geoserver/ndem50k/wms',
              //http://172.31.4.37/geoserver/ndem50k/wms
              params: {LAYERS: 'ndem50k:aptaluk50soi2001'}
              //ndem50k:apdistrictadmin50census2011
            })
          });


var intersectionLayer = new ol.layer.Vector({
  source: new ol.source.Vector()
});

var map = new ol.Map({
  layers: [dist,layer1,vector2,intersectionLayer,distaluk],
  target: 'map',
  view: new ol.View({
  projection: 'EPSG:4326',
          center: [79.419,15.428],
          zoom: 8

  })
});

var draw = new ol.interaction.Draw({
  type: 'Polygon'
});
map.addInteraction(draw);

draw.on('drawstart', function(evt) {
alert();
  intersectionLayer.getSource().clear();
});

var olParser = new jsts.io.olParser();
var geojsonParser = new jsts.io.GeoJSONParser();

draw.on('drawend', function(evt) {
  var poly1 = olParser.read(evt.feature.getGeometry());
  alert(poly1);
  var extent1 = evt.feature.getGeometry().getExtent();
  //alert(extent1);
  var source = vector2.getSource();
  //var source = distaluk.getSource();

  var features = source.getFeatures();

  var start = Date.now();
  //alert(start);
  features.forEach(function(feature) {
          alert("1");
            if (!ol.extent.intersects(extent1, feature.getGeometry().getExtent())) {
            alert();
              return;
            }
            var poly2 = olParser.read(feature.getGeometry());
            alert(poly2);
            var intersection = poly1.intersection(poly2);

            intersection = geojsonParser.write(intersection);
            alert(intersection);

        alert(intersection);
            if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
              return;
            } else {
                intersectionLayer.getSource().addFeature(geojsonFormat.readFeature({
                  type: 'Feature',
                  properties: {},
                  geometry: intersection
                }));
            }
  });
  var end = Date.now();
  //alert(end);
  console.log (end - start);
});
</script>

</body>
</html>

Main intention is to perform intersection of layer without using database support.

1 Answer 1

1

You can't make an intersection between a layer provided by WMS and a vector layer, you can only make an intersection between two or more vector layers. So your drawn layer and your GeoJSON layer are both vector layers and the intersection works, but when you replace the GeoJSON with the WMS layer (an image) it doesn't work, as you observed.

The GeoServer service you are using as well as providing a WMS also provides a WFS. The output of a WFS is a set of vector data, so you should be able to use the WFS to achieve what you want.

3
  • can i convert that intermediate WFS to again WMS for viewing output on map
    – user28536
    Commented Aug 24, 2016 at 9:16
  • Can make geojson of the geometry : intersection by creating one geojson file, then after using that geojson calucate area for the intersection area?
    – user28536
    Commented Aug 24, 2016 at 9:18
  • can i convert that intermediate WFS to again WMS for viewing output on map You can load the WMS and use it as a backdrop as well as having the WFS, or you can style the WFS data.
    – nmtoken
    Commented Aug 24, 2016 at 13:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.