Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <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="ol.js"></script>
- <script src="jsts_geom_Polygon.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>
- <script src="jsts_io_GeoJSONParser.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>
- <button onclick=withoutaoi();><img src="area.png"></button>
- </div>
- </div>
- <script>
- 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 vector3 = new ol.layer.Vector({
- maxResolution: 0.8,
- source: new ol.source.Vector({
- url: 'ap_taluk.geojson',
- format: new ol.format.GeoJSON({
- defaultDataProjection:'EPSG:4326'
- })
- }),
- style: styles
- });
- var vector2 = new ol.layer.Vector({
- maxResolution: 0.8,
- source: new ol.source.Vector({
- url: 'flood.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: '',
- params: {LAYERS: ''}
- }),
- transparent: false
- });
- var dist = new ol.layer.Tile({
- title: 'flood',
- source: new ol.source.TileWMS({
- url: '',
- params: {LAYERS: ''}
- })
- });
- var distaluk = new ol.layer.Tile({
- title: 'taluk',
- source: new ol.source.TileWMS({
- url: '',
- params: {LAYERS: ''}
- })
- });
- intersectionLayer = new ol.layer.Vector({
- source: new ol.source.Vector()
- });
- var map = new ol.Map({
- layers: [layer1,vector2,vector3,intersectionLayer,distaluk],
- target: 'map',
- view: new ol.View({
- projection: 'EPSG:4326',
- center: [79.419,15.428],
- zoom: 8
- })
- });
- var draw_inter = new ol.interaction.Draw({
- type: 'Polygon'
- });
- map.addInteraction(draw_inter);
- draw_inter.on('drawstart', function(evt) {
- intersectionLayer.getSource().clear();
- });
- var olParser = new jsts.io.olParser();
- var geojsonParser = new jsts.io.GeoJSONParser();
- draw_inter.on('drawend', function(evt) {
- var poly1 = olParser.read(evt.feature.getGeometry());
- var extent1 = evt.feature.getGeometry().getExtent();
- var source = vector2.getSource();
- var source2 = vector3.getSource();
- //var source4 = vector4.getSource();
- var features = source.getFeatures();
- var features2 = source2.getFeatures();
- // var features4 = source4.getFeatures();
- features.forEach(function(feature) {
- if (!ol.extent.intersects(extent1, feature.getGeometry().getExtent())) {
- return;
- }
- var poly2 = olParser.read(feature.getGeometry());
- var intersection = poly1.intersection(poly2);
- //alert(intersection);
- intersection = geojsonParser.write(intersection);
- if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
- return;
- } else {
- intersectionLayer.getSource().addFeature(geojsonFormat.readFeature({
- type: 'Feature',
- properties: {},
- geometry: intersection
- }));
- //alert("flood"+feature.get('geometry'));
- }
- });
- features2.forEach(function(feature) {
- if (!ol.extent.intersects(extent1, feature.getGeometry().getExtent())) {
- return;
- }
- var poly3 = olParser.read(feature.getGeometry());
- var intersection = poly1.intersection(poly3);
- //alert(intersection);
- intersection = geojsonParser.write(intersection);
- if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
- return;
- } else {
- alert("village"+feature.get('taluk'));
- /*intersectionLayer.getSource().addFeature(geojsonFormat.readFeature({
- type: 'Feature',
- properties: {},
- geometry: intersection
- }));
- /*layersize = intersectionLayer.getSource().getFeatures().length;
- alert(" size"+layersize);
- for(i=0;i<layersize;i++){
- alert("village"+feature.get('tehs_name'));
- var count = feature.get('tehs_name');
- alert(count);
- totalarea=0;
- for(j=0;count!==0;j++)
- {
- //alert();
- geoma = intersectionLayer.getSource().getFeatures()[i].getGeometry();
- // alert(geoma);
- coordinates = geoma.getLinearRing(0).getCoordinates();
- alert(coordinates);
- wgs84Sphere = new ol.Sphere(6378137);
- area = Math.abs(wgs84Sphere.geodesicArea(coordinates));
- alert(area);
- totalarea=totalarea+area;
- alert(totalarea)
- //totalarea=totalarea+area;
- }*/
- }
- //alert("village"+feature.get('tehs_name'));
- //}
- });
- layersize = intersectionLayer.getSource().getFeatures().length;
- alert(" village names size"+layersize);
- totalarea=0;
- for(i=0;i<layersize;i++){
- geoma = intersectionLayer.getSource().getFeatures()[i].getGeometry();
- coordinates = geoma.getLinearRing(0).getCoordinates();
- //alert(coordinates)
- wgs84Sphere = new ol.Sphere(6378137);
- area = Math.abs(wgs84Sphere.geodesicArea(coordinates));
- totalarea=totalarea+area;
- //alert("village"+feature.get('tehs_name'));
- alert(totalarea);
- }
- if (totalarea > 10000) {
- output = (Math.round(totalarea / 1000000 * 100) / 100) +
- ' ' + 'km<sup>2</sup>';
- } else {
- output = (Math.round(totalarea * 100) / 100) +
- ' ' + 'm<sup>2</sup>';
- }
- alert(output);
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement