turf_classificationAnalysis.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_turfClassificationAnalysis"></title>
  9. <style>
  10. .ol-popup {
  11. position: absolute;
  12. background-color: white;
  13. -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  14. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  15. padding: 15px;
  16. border-radius: 10px;
  17. border: 1px solid #cccccc;
  18. bottom: 12px;
  19. left: -50px;
  20. min-width: 300px;
  21. }
  22. .ol-popup:after, .ol-popup:before {
  23. top: 100%;
  24. border: solid transparent;
  25. content: " ";
  26. height: 0;
  27. width: 0;
  28. position: absolute;
  29. pointer-events: none;
  30. }
  31. .ol-popup:after {
  32. border-top-color: white;
  33. border-width: 10px;
  34. left: 48px;
  35. margin-left: -10px;
  36. }
  37. .ol-popup:before {
  38. border-top-color: #cccccc;
  39. border-width: 11px;
  40. left: 48px;
  41. margin-left: -11px;
  42. }
  43. .ol-popup-closer {
  44. text-decoration: none;
  45. position: absolute;
  46. top: 2px;
  47. right: 8px;
  48. }
  49. .ol-popup-closer:after {
  50. content: "✖";
  51. }
  52. .editPane {
  53. position: absolute;
  54. right: 10px;
  55. top: 10px;
  56. width: 420px;
  57. text-align: center;
  58. background: #FFF;
  59. z-index: 1000;
  60. }
  61. .tooltip {
  62. position: relative;
  63. background: rgba(0, 0, 0, 0.5);
  64. border-radius: 4px;
  65. color: white;
  66. padding: 4px 8px;
  67. opacity: 0.7;
  68. white-space: nowrap;
  69. }
  70. </style>
  71. </head>
  72. <body style='margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;'>
  73. <div id='map' style='margin:0 auto;width: 100%;height: 100%'></div>
  74. <div id='info' class='ol-popup'>
  75. <a href='#' id='popup-closer' class='ol-popup-closer'></a>
  76. <div id='info-content'></div>
  77. </div>
  78. <div class='panel panel-primary editPane' id='editPane'>
  79. <div class='panel-heading'>
  80. <h5 class='panel-title text-center' data-i18n="resources.text_classificationAnalysis"></h5></div>
  81. <div class='panel-body content' id='classificationBody'>
  82. <div class='panel'>
  83. <div class='input-group'>
  84. <span class='input-group-addon'><span data-i18n="resources.text_classificationAnalysisType"></span><span
  85. data-i18n="[title]resources.text_requiredField" style='color: red;'> * </span></span>
  86. <select class='form-control' id='classificationAnalyzeType' name='interpolationAnalyzeType'>
  87. <option value='nearestOption' selected>nearestAnalyze</option>
  88. </select>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
  94. <script type="text/javascript" include="turf" src="../../dist/ol/include-ol.js"></script>
  95. <script>
  96. var host = window.isLocal ? window.server : "https://iserver.supermap.io",
  97. nearestDataUrl = host + "/iserver/services/map-china400/rest/maps/China_4326",
  98. mapUrl = host + "/iserver/services/map-china400/rest/maps/ChinaDark",
  99. turfLayer, nearestQueryResultLayer,
  100. map = new ol.Map({
  101. target: 'map',
  102. maxExtent: [-180.0, -90, 180.0, 90],
  103. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  104. .extend([new ol.supermap.control.Logo()]),
  105. view: new ol.View({
  106. center: [106, 36],
  107. zoom: 5,
  108. projection: 'EPSG:4326',
  109. multiWorld: true
  110. })
  111. });
  112. map.addLayer(new ol.layer.Tile({
  113. source: new ol.source.TileSuperMapRest({
  114. url: mapUrl,
  115. prjCoordSys: {"epsgCode": 4326}
  116. }),
  117. projection: 'EPSG:4326'
  118. }));
  119. //添加turf 图层
  120. var turfSource = new ol.source.Turf({
  121. wrapX: false,
  122. attributions: ""
  123. });
  124. //添加pupop x按钮
  125. document.getElementById('popup-closer').onclick = function () {
  126. classificationAnalyst.overlay.setPosition(undefined);
  127. this.blur();
  128. return false;
  129. };
  130. //创建鼠标操作提示:
  131. var helpTooltipElement, helpTooltip;
  132. createHelpTooltip();
  133. function createHelpTooltip() {
  134. if (helpTooltipElement) {
  135. helpTooltipElement.parentNode.removeChild(helpTooltipElement);
  136. }
  137. helpTooltipElement = document.createElement('div');
  138. helpTooltipElement.className = 'tooltip hidden';
  139. helpTooltip = new ol.Overlay({
  140. element: helpTooltipElement,
  141. offset: [15, 0],
  142. positioning: 'center-left'
  143. });
  144. map.addOverlay(helpTooltip);
  145. }
  146. //鼠标移动监听事件
  147. var pointerMoveHandler = function (evt) {
  148. var helpMsg = resources.msg_findAirport;
  149. helpTooltipElement.innerHTML = helpMsg;
  150. helpTooltip.setPosition(evt.coordinate);
  151. helpTooltipElement.classList.remove('hidden');
  152. };
  153. //绑定鼠标移动事件
  154. map.on('pointermove', pointerMoveHandler);
  155. map.getViewport().addEventListener('mouseout', function () {
  156. helpTooltipElement.classList.add('hidden');
  157. });
  158. //创建map click 事件:
  159. var extentPolygon = new ol.geom.Polygon([[[-180.0, 90], [-180.0, -90], [180.0, -90], [180.0, 90], [-180.0, 90]]]);
  160. function mapOnClickListener(e) {
  161. widgets.alert.clearAlert();
  162. //判断是否分析超出地图范围
  163. if (!extentPolygon.intersectsCoordinate(e.coordinate)) {
  164. widgets.alert.showAlert(resources.msg_beyondScope, false);
  165. return;
  166. }
  167. if (!classificationAnalyst.sourcePoints || classificationAnalyst.sourcePoints.features.length === 0) {
  168. widgets.alert.showAlert(resources.text_loadingData, true);
  169. return;
  170. }
  171. clearLayer();
  172. classificationAnalyst.overlay.setPosition(undefined);
  173. var selected = false;
  174. map.forEachFeatureAtPixel(e.pixel, function (feature) {
  175. if (!feature.getProperties().NAME) {
  176. return;
  177. }
  178. classificationAnalyst.content.innerHTML = feature.getProperties().NAME;
  179. classificationAnalyst.overlay.setPosition(feature.getGeometry().getCoordinates());
  180. selected = true;
  181. }, {
  182. layerFilter: function (layer) {
  183. if (layer === nearestQueryResultLayer) {
  184. return true;
  185. }
  186. }, hitTolerance: 10
  187. });
  188. if (selected) {
  189. return;
  190. }
  191. var selectPointSource = new ol.source.Vector({
  192. wrapX: false
  193. });
  194. var selectPointMarker = new ol.Feature({
  195. geometry: new ol.geom.Point(e.coordinate)
  196. });
  197. selectPointMarker.setStyle(new ol.style.Style({
  198. image: new ol.style.Icon(({
  199. anchor: [0.2, 0.9],
  200. crossOrigin: 'anonymous',
  201. src: '../img/position.png',
  202. scale: 0.7
  203. }))
  204. }));
  205. selectPointSource.addFeature(selectPointMarker);
  206. classificationAnalyst.selectPointLayer = new ol.layer.Vector({
  207. source: selectPointSource,
  208. extent: [-180.0, -90, 180.0, 90]
  209. });
  210. map.addLayer(classificationAnalyst.selectPointLayer);
  211. classificationAnalyst.nearestAnalyze(classificationAnalyst.sourcePoints, e.coordinate);
  212. }
  213. //创建分析邻近分析对象
  214. var classificationAnalyst = {
  215. content: document.getElementById('info-content'),
  216. overlay: new ol.Overlay(({
  217. element: document.getElementById('info'),
  218. autoPan: true,
  219. autoPanAnimation: {
  220. duration: 250
  221. }
  222. })),
  223. sourcePoints: null,
  224. lineLayer: null,
  225. selectPointLayer: null,
  226. loadAnalystLayer: function () {
  227. map.getView().animate({zoom: 5}, {center: [106, 36]});
  228. turfLayer = new ol.layer.Vector({
  229. source: turfSource,
  230. style: new ol.style.Style({
  231. image: new ol.style.Icon(({
  232. crossOrigin: 'anonymous',
  233. src: '../img/airportRed.png',
  234. scale: 0.7
  235. })),
  236. }),
  237. zIndex: 1000
  238. });
  239. var airportsParam = new SuperMap.QueryBySQLParameters({
  240. queryParams: {
  241. name: "Airport_pt@China.1",
  242. }
  243. });
  244. //只请求一次数据
  245. if (!nearestQueryResultLayer) {
  246. new ol.supermap.QueryService(nearestDataUrl).queryBySQL(airportsParam, function (serviceResult) {
  247. classificationAnalyst.sourcePoints = serviceResult.result.recordsets[0].features;
  248. var features = (new ol.format.GeoJSON()).readFeatures(classificationAnalyst.sourcePoints);
  249. for (var i = 0; i < features.length; i++) {
  250. features[i].setStyle(new ol.style.Style({
  251. image: new ol.style.Icon(({
  252. crossOrigin: 'anonymous',
  253. src: '../img/airportBlue.png',
  254. scale: 0.7
  255. }))
  256. }));
  257. }
  258. var vectorSource = new ol.source.Vector({
  259. features: features,
  260. wrapX: false
  261. });
  262. nearestQueryResultLayer = new ol.layer.Vector({
  263. source: vectorSource,
  264. extent: [-180.0, -90, 180.0, 90]
  265. });
  266. map.addLayer(nearestQueryResultLayer);
  267. });
  268. } else {
  269. map.addLayer(nearestQueryResultLayer);
  270. }
  271. //绑定机场popup
  272. map.addOverlay(classificationAnalyst.overlay);
  273. //点击地图进行最近飞机场分析
  274. map.on('click', mapOnClickListener);
  275. },
  276. nearestAnalyze: function (sourcePoints, coordinate) {
  277. turfSource.process("Classification.nearestPoint", {
  278. "targetPoint": [coordinate[0], coordinate[1]],
  279. "points": sourcePoints
  280. }, function (result) {
  281. var latlng = [result.geometry.coordinates[0], result.geometry.coordinates[1]];
  282. var innerHtml = resources.msg_nearestAirport + result.properties.NAME;
  283. classificationAnalyst.content.innerHTML = innerHtml;
  284. classificationAnalyst.overlay.setPosition(result.geometry.coordinates);
  285. //添加线段:
  286. var lineString = new ol.geom.LineString([coordinate, latlng]);
  287. var lineSource = new ol.source.Vector({
  288. features: [new ol.Feature(lineString)],
  289. wrapX: true
  290. });
  291. classificationAnalyst.lineLayer = new ol.layer.Vector({
  292. source: lineSource,
  293. extent: [-180.0, -90, 180.0, 90],
  294. style: new ol.style.Style({
  295. stroke: new ol.style.Stroke({
  296. color: 'red',
  297. weight: 1,
  298. lineDash: [1, 2, 3, 4, 5, 6]
  299. })
  300. })
  301. });
  302. map.addLayer(classificationAnalyst.lineLayer);
  303. map.addLayer(turfLayer);
  304. });
  305. },
  306. };
  307. classificationAnalyst.loadAnalystLayer();
  308. function clearLayer() {
  309. if (turfSource.getFeatures().length > 0) {
  310. turfSource.clear();
  311. }
  312. map.removeLayer(turfLayer);
  313. if (classificationAnalyst.lineLayer) {
  314. map.removeLayer(classificationAnalyst.lineLayer);
  315. }
  316. if (classificationAnalyst.selectPointLayer) {
  317. map.removeLayer(classificationAnalyst.selectPointLayer);
  318. }
  319. }
  320. </script>
  321. </body>
  322. </html>