Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Select features example</title>
  4. <meta http-equiv="X-UA-Compatible" content="chrome=1">
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.10.1/ol.css" type="text/css">
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.10.1/ol-debug.js"></script>
  8. <script src="ol.js"></script>
  9. <script src="jsts_geom_Polygon.js"></script>
  10. <script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/lib/0.16.0/javascript.util.min.js"></script>
  11. <script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/lib/0.16.0/jsts.min.js"></script>
  12. <script src="jsts_io_GeoJSONParser.js"></script>
  13. <style>
  14. html, body {
  15. padding: 0;
  16. margin: 0;
  17. }
  18. html, body, .map {
  19. height: 100%;
  20. width: 100%;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="map" class="map"></div>
  26. <button onclick=withoutaoi();><img src="area.png"></button>
  27.  
  28. </div>
  29. </div>
  30. <script>
  31.  
  32. var geojsonFormat = new ol.format.GeoJSON();
  33.  
  34. var style = new ol.style.Style({
  35. fill: new ol.style.Fill({
  36. color: 'rgba(255, 255, 255, 0.3)'
  37. }),
  38. stroke: new ol.style.Stroke({
  39. color: 'rgba(255, 120, 0, 0.6)',
  40. width: 1
  41. })
  42. });
  43. var styles = [style];
  44.  
  45. var highlightStyle = [
  46. new ol.style.Style({
  47. fill: new ol.style.Fill({
  48. color: 'rgba(255, 120, 0, 0.3)'
  49. }),
  50. stroke: new ol.style.Stroke({
  51. color: 'rgba(255, 120, 0, 0.6)',
  52. width: 1
  53. })
  54. })
  55. ];
  56.  
  57. var vector3 = new ol.layer.Vector({
  58. maxResolution: 0.8,
  59. source: new ol.source.Vector({
  60. url: 'ap_taluk.geojson',
  61. format: new ol.format.GeoJSON({
  62. defaultDataProjection:'EPSG:4326'
  63. })
  64. }),
  65. style: styles
  66. });
  67.  
  68.  
  69. var vector2 = new ol.layer.Vector({
  70. maxResolution: 0.8,
  71. source: new ol.source.Vector({
  72. url: 'flood.geojson',
  73. format: new ol.format.GeoJSON({
  74. defaultDataProjection:'EPSG:4326'
  75. })
  76. }),
  77. style: styles
  78. });
  79.  
  80.  
  81. var layer1 = new ol.layer.Tile({
  82. title: 'India',
  83. source: new ol.source.TileWMS({
  84. url: '',
  85. params: {LAYERS: ''}
  86. }),
  87. transparent: false
  88. });
  89. var dist = new ol.layer.Tile({
  90. title: 'flood',
  91. source: new ol.source.TileWMS({
  92. url: '',
  93.  
  94. params: {LAYERS: ''}
  95.  
  96. })
  97. });
  98. var distaluk = new ol.layer.Tile({
  99. title: 'taluk',
  100. source: new ol.source.TileWMS({
  101. url: '',
  102.  
  103. params: {LAYERS: ''}
  104.  
  105. })
  106. });
  107.  
  108. intersectionLayer = new ol.layer.Vector({
  109. source: new ol.source.Vector()
  110. });
  111.  
  112. var map = new ol.Map({
  113. layers: [layer1,vector2,vector3,intersectionLayer,distaluk],
  114. target: 'map',
  115. view: new ol.View({
  116. projection: 'EPSG:4326',
  117. center: [79.419,15.428],
  118. zoom: 8
  119.  
  120. })
  121. });
  122.  
  123. var draw_inter = new ol.interaction.Draw({
  124. type: 'Polygon'
  125. });
  126. map.addInteraction(draw_inter);
  127.  
  128. draw_inter.on('drawstart', function(evt) {
  129.  
  130. intersectionLayer.getSource().clear();
  131. });
  132.  
  133. var olParser = new jsts.io.olParser();
  134.  
  135. var geojsonParser = new jsts.io.GeoJSONParser();
  136.  
  137. draw_inter.on('drawend', function(evt) {
  138. var poly1 = olParser.read(evt.feature.getGeometry());
  139.  
  140. var extent1 = evt.feature.getGeometry().getExtent();
  141.  
  142. var source = vector2.getSource();
  143.  
  144. var source2 = vector3.getSource();
  145. //var source4 = vector4.getSource();
  146. var features = source.getFeatures();
  147. var features2 = source2.getFeatures();
  148. // var features4 = source4.getFeatures();
  149.  
  150. features.forEach(function(feature) {
  151.  
  152. if (!ol.extent.intersects(extent1, feature.getGeometry().getExtent())) {
  153. return;
  154. }
  155. var poly2 = olParser.read(feature.getGeometry());
  156.  
  157. var intersection = poly1.intersection(poly2);
  158. //alert(intersection);
  159.  
  160. intersection = geojsonParser.write(intersection);
  161. if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
  162. return;
  163. } else {
  164. intersectionLayer.getSource().addFeature(geojsonFormat.readFeature({
  165. type: 'Feature',
  166. properties: {},
  167. geometry: intersection
  168. }));
  169. //alert("flood"+feature.get('geometry'));
  170.  
  171. }
  172. });
  173.  
  174.  
  175.  
  176. features2.forEach(function(feature) {
  177.  
  178. if (!ol.extent.intersects(extent1, feature.getGeometry().getExtent())) {
  179. return;
  180. }
  181. var poly3 = olParser.read(feature.getGeometry());
  182.  
  183. var intersection = poly1.intersection(poly3);
  184. //alert(intersection);
  185.  
  186. intersection = geojsonParser.write(intersection);
  187. if(intersection.type === 'GeometryCollection' && intersection.geometries.length === 0) {
  188. return;
  189. } else {
  190.  
  191. alert("village"+feature.get('taluk'));
  192. /*intersectionLayer.getSource().addFeature(geojsonFormat.readFeature({
  193. type: 'Feature',
  194. properties: {},
  195. geometry: intersection
  196. }));
  197.  
  198.  
  199. /*layersize = intersectionLayer.getSource().getFeatures().length;
  200. alert(" size"+layersize);
  201. for(i=0;i<layersize;i++){
  202. alert("village"+feature.get('tehs_name'));
  203. var count = feature.get('tehs_name');
  204. alert(count);
  205. totalarea=0;
  206. for(j=0;count!==0;j++)
  207. {
  208. //alert();
  209. geoma = intersectionLayer.getSource().getFeatures()[i].getGeometry();
  210. // alert(geoma);
  211. coordinates = geoma.getLinearRing(0).getCoordinates();
  212. alert(coordinates);
  213. wgs84Sphere = new ol.Sphere(6378137);
  214. area = Math.abs(wgs84Sphere.geodesicArea(coordinates));
  215. alert(area);
  216. totalarea=totalarea+area;
  217. alert(totalarea)
  218. //totalarea=totalarea+area;
  219.  
  220. }*/
  221. }
  222. //alert("village"+feature.get('tehs_name'));
  223.  
  224. //}
  225. });
  226.  
  227.  
  228. layersize = intersectionLayer.getSource().getFeatures().length;
  229. alert(" village names size"+layersize);
  230. totalarea=0;
  231. for(i=0;i<layersize;i++){
  232. geoma = intersectionLayer.getSource().getFeatures()[i].getGeometry();
  233. coordinates = geoma.getLinearRing(0).getCoordinates();
  234. //alert(coordinates)
  235. wgs84Sphere = new ol.Sphere(6378137);
  236. area = Math.abs(wgs84Sphere.geodesicArea(coordinates));
  237. totalarea=totalarea+area;
  238. //alert("village"+feature.get('tehs_name'));
  239. alert(totalarea);
  240. }
  241. if (totalarea > 10000) {
  242. output = (Math.round(totalarea / 1000000 * 100) / 100) +
  243. ' ' + 'km<sup>2</sup>';
  244. } else {
  245. output = (Math.round(totalarea * 100) / 100) +
  246. ' ' + 'm<sup>2</sup>';
  247. }
  248. alert(output);
  249. });
  250.  
  251. </script>
  252. </body>
  253. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement