turf_gridAnalysis.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_turfGridAnalysis"></title>
  9. <script type="text/javascript" include="turf" src="../../dist/ol/include-ol.js"></script>
  10. <style>
  11. .editPane {
  12. position: absolute;
  13. right: 10px;
  14. top: 10px;
  15. width: 480px;
  16. text-align: center;
  17. background: #FFF;
  18. z-index: 1000;
  19. }
  20. .dragBoxCss {
  21. border: 1px solid #33CCFF;
  22. }
  23. .tooltip {
  24. position: relative;
  25. background: rgba(0, 0, 0, 0.5);
  26. border-radius: 4px;
  27. color: white;
  28. padding: 4px 8px;
  29. opacity: 0.7;
  30. white-space: nowrap;
  31. }
  32. </style>
  33. </head>
  34. <body style='margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;'>
  35. <div id='map' style='margin:0 auto;width: 100%;height: 100%'></div>
  36. <div id='info' class='ol-popup'>
  37. <a href='#' id='popup-closer' class='ol-popup-closer'></a>
  38. <div id='info-content'></div>
  39. </div>
  40. <div class='panel panel-primary editPane' id='editPane'>
  41. <div class='panel-heading'>
  42. <h5 class='panel-title text-center' data-i18n="resources.text_createGrid"></h5></div>
  43. <div class='panel-body' id='gridsBody'>
  44. <div class='input-group'>
  45. <span class='input-group-addon'><span data-i18n="resources.text_gridAnalysisType"></span><span data-i18n="[title]resources.text_requiredField" style='color: red;'> * </span></span>
  46. <select class='form-control' id='gridAnalyzeType' name='gridAnalyzeType'>
  47. <option value='hexGrid' selected data-i18n="resources.text_hexGrid"></option>
  48. <option value='pointGrid' data-i18n="resources.text_pointGrid"></option>
  49. <option value='squareGrid' data-i18n="resources.text_squareGrid"></option>
  50. <option value='triangleGrid' data-i18n="resources.text_triangleGrid"></option>
  51. </select>
  52. </div>
  53. <p></p>
  54. <div class='input-group'>
  55. <span class='input-group-addon'><span data-i18n="resources.text_unitGridSize"></span><span data-i18n="[title]resources.text_requiredField" style='color: red;'> * </span></span>
  56. <input id='cellSide' type='text' class='form-control' value='50'/>
  57. </div>
  58. <p></p>
  59. <div class='input-group'>
  60. <span class='input-group-addon'><span data-i18n="resources.text_gridUnitSizeUnit"></span><span data-i18n="[title]resources.text_requiredField" style='color: red;'> * </span></span>
  61. <select class='form-control' id='units' name='mode'>
  62. <option value='degrees' data-i18n="resources.text_degrees"></option>
  63. <option value='radians' data-i18n="resources.text_radians"></option>
  64. <option value='miles' selected data-i18n="resources.text_miles"></option>
  65. <option value='kilometers' data-i18n="resources.text_kilometers"></option>
  66. </select>
  67. </div>
  68. <p></p>
  69. <div id='squareGridChoic' class='input-group' style='display: none'>
  70. <span class='input-group-addon' data-i18n="resources.text_adjustSize"></span>
  71. <span class='form-control'>
  72. <input id='completelyWithin' type='checkbox'>
  73. </span>
  74. </div>
  75. <p></p>
  76. <div id='pointGridChoic' class='input-group' style='display: none'>
  77. <span class='input-group-addon' data-i18n="resources.text_adjustPoint"></span>
  78. <span class='form-control'>
  79. <input id='iscentered' type='checkbox'>
  80. </span><span class='input-group-addon' data-i18n="resources.text_returnedByTriangle"></span>
  81. <span class='form-control'><input id='isbboxIsMask' type='checkbox'>
  82. </span>
  83. </div>
  84. <p></p>
  85. <div id='hexGridChoic' class='input-group'>
  86. <span class='input-group-addon' data-i18n="resources.text_adjustPoint"></span>
  87. <span class='form-control'>
  88. <input id='triangles' type='checkbox'>
  89. </span>
  90. </div>
  91. <p></p>
  92. <div align='right' class='input-group'>
  93. <input type='button' id='createGridBtn' class='btn btn-primary' data-i18n="[value]resources.btn_createGrid"/>&nbsp
  94. <input type='button' id='clearGridBtn' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear"/>
  95. </div>
  96. </div>
  97. </div>
  98. </body>
  99. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  100. <script>
  101. var host = window.isLocal ? window.server : "https://iserver.supermap.io",
  102. mapUrl = host + "/iserver/services/map-china400/rest/maps/ChinaDark",
  103. turfLayer, bbox,
  104. map = new ol.Map({
  105. target: 'map',
  106. maxExtent: [-180.0, -85.05, 180.0, 85.05],
  107. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  108. .extend([new ol.supermap.control.Logo()]),
  109. view: new ol.View({
  110. center: [106, 36],
  111. zoom: 5,
  112. projection: 'EPSG:4326',
  113. multiWorld: true
  114. })
  115. });
  116. map.addLayer(new ol.layer.Tile({
  117. source: new ol.source.TileSuperMapRest({
  118. url: mapUrl,
  119. prjCoordSys: {"epsgCode": 4326}
  120. }),
  121. projection: 'EPSG:4326'
  122. }));
  123. //添加turf 图层
  124. var turfSource = new ol.source.Turf({
  125. wrapX: false,
  126. attributions: ""
  127. }),
  128. gridAnalyzeType = 'hexGrid';
  129. bindEvent();
  130. function bindEvent() {
  131. //格网分析事件绑定 --start
  132. $("#gridAnalyzeType").change(function (e) {
  133. bbox = null;
  134. clearLayer();
  135. //是否有需要删除的提示框
  136. widgets.alert.clearAlert();
  137. gridAnalyzeType = $("#gridAnalyzeType option:selected").val();
  138. switch (gridAnalyzeType) {
  139. case 'hexGrid':
  140. $("#squareGridChoic").hide();
  141. $("#pointGridChoic").hide();
  142. $("#hexGridChoic").show();
  143. return;
  144. case 'pointGrid':
  145. $("#squareGridChoic").hide();
  146. $("#pointGridChoic").show();
  147. $("#hexGridChoic").hide();
  148. return;
  149. case 'squareGrid':
  150. $("#squareGridChoic").show();
  151. $("#pointGridChoic").hide();
  152. $("#hexGridChoic").hide();
  153. return;
  154. case 'triangleGrid':
  155. $("#squareGridChoic").hide();
  156. $("#pointGridChoic").hide();
  157. $("#hexGridChoic").hide();
  158. return;
  159. }
  160. });
  161. $("#createGridBtn").click(function () {
  162. //是否有需要删除的提示框
  163. widgets.alert.clearAlert();
  164. turfSource.clear();
  165. if (bbox) {
  166. switch (gridAnalyzeType) {
  167. case 'hexGrid':
  168. gridAnalyst.createHexGrid();
  169. return;
  170. case 'pointGrid':
  171. gridAnalyst.createPointGrid();
  172. return;
  173. case 'squareGrid':
  174. gridAnalyst.createSquareGrid();
  175. return;
  176. case 'triangleGrid':
  177. gridAnalyst.createTriangleGrid();
  178. return;
  179. }
  180. } else {
  181. widgets.alert.showAlert(resources.msg_drawAnalysisRegion, true);
  182. }
  183. });
  184. $("#clearGridBtn").click(function () {
  185. //是否有需要删除的提示框
  186. widgets.alert.clearAlert();
  187. if (vector.getSource().getFeatures().length === 0 && turfSource.getFeatures().length == 0) {
  188. widgets.alert.showAlert(resources.msg_noDataToDelete, false);
  189. return;
  190. }
  191. bbox = null;
  192. clearLayer();
  193. });
  194. }
  195. //创建鼠标操作提示:
  196. var helpTooltipElement, helpTooltip;
  197. createHelpTooltip();
  198. function createHelpTooltip() {
  199. if (helpTooltipElement) {
  200. helpTooltipElement.parentNode.removeChild(helpTooltipElement);
  201. }
  202. helpTooltipElement = document.createElement('div');
  203. helpTooltipElement.className = 'tooltip hidden';
  204. helpTooltip = new ol.Overlay({
  205. element: helpTooltipElement,
  206. offset: [15, 0],
  207. positioning: 'center-left'
  208. });
  209. map.addOverlay(helpTooltip);
  210. }
  211. //鼠标移动监听事件
  212. var pointerMoveHandler = function (evt) {
  213. var helpMsg = resources.msg_drawMeshRegion;
  214. helpTooltipElement.innerHTML = helpMsg;
  215. helpTooltip.setPosition(evt.coordinate);
  216. helpTooltipElement.classList.remove('hidden');
  217. };
  218. //绑定鼠标移动事件
  219. map.on('pointermove', pointerMoveHandler);
  220. map.getViewport().addEventListener('mouseout', function () {
  221. helpTooltipElement.classList.add('hidden');
  222. });
  223. //创建格网的绘图工具
  224. var dragBox = new ol.interaction.DragBox({
  225. className: 'dragBoxCss',
  226. condition: ol.events.condition.platformModifierKeyOnly
  227. });
  228. var source = new ol.source.Vector({
  229. wrapX: false,
  230. });
  231. var vector = new ol.layer.Vector({
  232. source: source,
  233. extent: [-180.0, -90, 180.0, 90]
  234. });
  235. //创建格网分析对象
  236. var gridAnalyst = {
  237. loadAnalystLayer: function () {
  238. map.getView().animate({zoom: 5}, {center: [106, 36]});
  239. turfLayer = new ol.layer.Vector({
  240. source: turfSource,
  241. extent: [-180.0, -90, 180.0, 90],
  242. style: function (feature) {
  243. if (feature.getGeometry() instanceof ol.geom.Polygon) {
  244. feature.setStyle(new ol.style.Style({
  245. stroke: new ol.style.Stroke(({
  246. color: 'red',
  247. weight: 0.8,
  248. })),
  249. fill: new ol.style.Fill({
  250. color: 'rgba(255, 255, 255, 0.1)',
  251. })
  252. }));
  253. } else {
  254. feature.setStyle(new ol.style.Style({
  255. image: new ol.style.Icon(({
  256. crossOrigin: 'anonymous',
  257. src: '../img/marker-gold.png',
  258. scale: 0.5
  259. }))
  260. }));
  261. }
  262. }
  263. });
  264. map.addLayer(vector);
  265. map.addLayer(turfLayer);
  266. map.addInteraction(dragBox);
  267. dragBox.on('boxstart', function () {
  268. //是否有需要删除的提示框
  269. widgets.alert.clearAlert();
  270. clearLayer();
  271. });
  272. dragBox.on('boxend', function () {
  273. bbox = dragBox.getGeometry().getExtent();
  274. var rectangleFeature = new ol.Feature({
  275. geometry: dragBox.getGeometry()
  276. });
  277. var rectanglesource = new ol.source.Vector({
  278. features: [rectangleFeature],
  279. wrapX: false
  280. });
  281. vector.setSource(rectanglesource);
  282. });
  283. },
  284. //格网分析
  285. createHexGrid: function () {
  286. var cellSide = $("#cellSide").val();
  287. var units = $("#units option:selected").val();
  288. var triangles = $("#triangles").get(0).checked;
  289. turfSource.process("Grids.hexGrid", {
  290. "bbox": bbox,
  291. "cellSide": cellSide,
  292. "units": units,
  293. "triangles": triangles
  294. });
  295. },
  296. createPointGrid: function () {
  297. var cellSide = $("#cellSide").val();
  298. var units = $("#units option:selected").val();
  299. var iscentered = $('#iscentered').get(0).checked;
  300. var isbboxIsMask = $('#isbboxIsMask').get(0).checked;
  301. turfSource.process("Grids.pointGrid", {
  302. "bbox": bbox,
  303. "cellSide": cellSide,
  304. "units": units,
  305. "centered": iscentered,
  306. "bboxIsMask": isbboxIsMask
  307. });
  308. },
  309. createSquareGrid: function () {
  310. var cellSide = $("#cellSide").val();
  311. var units = $("#units option:selected").val();
  312. var completelyWithin = $("#completelyWithin").get(0).checked;
  313. turfSource.process("Grids.squareGrid", {
  314. "bbox": bbox,
  315. "cellSide": cellSide,
  316. "units": units,
  317. "completelyWithin": completelyWithin
  318. });
  319. },
  320. createTriangleGrid: function () {
  321. var cellSide = $("#cellSide").val();
  322. var units = $("#units option:selected").val();
  323. turfSource.process("Grids.triangleGrid", {
  324. "bbox": bbox,
  325. "cellSide": cellSide,
  326. "units": units
  327. });
  328. },
  329. };
  330. gridAnalyst.loadAnalystLayer();
  331. function clearLayer() {
  332. if (turfSource.getFeatures().length > 0) {
  333. turfSource.clear();
  334. }
  335. vector.getSource().clear();
  336. }
  337. </script>
  338. </html>