uniqueThemeLayer.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8. <title data-i18n="resources.title_themeUnique"></title>
  9. <script type="text/javascript" include="jquery,bootstrap-css" src="../js/include-web.js"></script>
  10. <style>
  11. .legendItemHeader,
  12. .legendItemValue {
  13. width: 84px;
  14. height: 18px;
  15. font-size: 14px;
  16. }
  17. </style>
  18. </head>
  19. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
  20. <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
  21. <script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
  22. <script type="text/javascript">
  23. var host = window.isLocal ? window.server : "https://iserver.supermap.io";
  24. var map, themeLayer, infoView,
  25. baseUrl = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
  26. dataUrl = host + "/iserver/services/data-jingjin/rest/data";
  27. init();
  28. function init() {
  29. // 检测是否支持 Canvas
  30. if (!document.createElement('canvas').getContext) {
  31. alert(resources.msg_supportCanvas);
  32. return;
  33. }
  34. map = L.map("map", {
  35. crs: L.CRS.EPSG4326,
  36. center: [40, 117],
  37. maxZoom: 18,
  38. zoom: 7
  39. });
  40. L.supermap.tiledMapLayer(baseUrl).addTo(map);
  41. initThemeLayer();
  42. }
  43. function initThemeLayer() {
  44. // 定义 Unique 单值专题图层
  45. themeLayer = L.supermap.uniqueThemeLayer("ThemeLayer", {
  46. // 开启 hover 高亮效果
  47. isHoverAble: true,
  48. opacity: 0.8,
  49. alwaysMapCRS: true
  50. }).addTo(map);
  51. // 图层基础样式
  52. themeLayer.style = new SuperMap.ThemeStyle({
  53. shadowBlur: 3,
  54. shadowColor: "#000000",
  55. shadowOffsetX: 1,
  56. shadowOffsetY: 1,
  57. fillColor: "#FFFFFF"
  58. });
  59. // hover 高亮样式
  60. themeLayer.highlightStyle = new SuperMap.ThemeStyle({
  61. stroke: true,
  62. strokeWidth: 2,
  63. strokeColor: 'blue',
  64. fillColor: "#00F5FF",
  65. fillOpacity: 0.2
  66. });
  67. // 用于单值专题图的属性字段名称
  68. themeLayer.themeField = "LANDTYPE";
  69. // 风格数组,设定值对应的样式
  70. themeLayer.styleGroups = [
  71. {
  72. value: "草地",
  73. style: {
  74. fillColor: "#C1FFC1"
  75. }
  76. }, {
  77. value: "城市",
  78. style: {
  79. fillColor: "#CD7054"
  80. }
  81. }, {
  82. value: "灌丛",
  83. style: {
  84. fillColor: "#7CCD7C"
  85. }
  86. }, {
  87. value: "旱地",
  88. style: {
  89. fillColor: "#EE9A49"
  90. }
  91. }, {
  92. value: "湖泊水库",
  93. style: {
  94. fillColor: "#8EE5EE"
  95. }
  96. }, {
  97. value: "经济林",
  98. style: {
  99. fillColor: "#548B54"
  100. }
  101. }, {
  102. value: "沙漠",
  103. style: {
  104. fillColor: "#DEB887"
  105. }
  106. }, {
  107. value: "水浇地",
  108. style: {
  109. fillColor: "#E0FFFF"
  110. }
  111. }, {
  112. value: "水田",
  113. style: {
  114. fillColor: "#388E8E"
  115. }
  116. }, {
  117. value: "用材林",
  118. style: {
  119. fillColor: "#556B2F"
  120. }
  121. }, {
  122. value: "沼泽",
  123. style: {
  124. fillColor: "#2F4F4F"
  125. }
  126. }, {
  127. value: "缺省风格",
  128. style: {
  129. fillColor: "#ABABAB"
  130. }
  131. }
  132. ];
  133. //专题图层 mousemove 事件
  134. themeLayer.on("mousemove", highLightLayer);
  135. addThemeLayer();
  136. }
  137. //查询feature数据
  138. function addThemeLayer() {
  139. var getFeatureBySQLParams = new SuperMap.GetFeaturesBySQLParameters({
  140. queryParameter: new SuperMap.FilterParameter({
  141. name: "Jingjin",
  142. attributeFilter: "SMID > -1"
  143. }),
  144. toIndex: 500,
  145. datasetNames: ["Jingjin:Landuse_R"]
  146. });
  147. L.supermap.featureService(dataUrl)
  148. .getFeaturesBySQL(getFeatureBySQLParams, processComplete, SuperMap.DataFormat.ISERVER);
  149. }
  150. //数据预处理并添加到专题图图层
  151. function processComplete(serviceResult) {
  152. var result = serviceResult.result;
  153. var feas = [];
  154. if (result && result.features) {
  155. var features = result.features;
  156. var IHFeas = []; //岛洞多面
  157. for (var i = 0, len = features.length; i < len; i++) {
  158. var feature = features[i];
  159. feature.attributes = concatAttributes(feature.fieldNames, feature.fieldValues);
  160. var smid = feature.attributes.SMID.toString();
  161. if (smid === "86" || smid === "87" || smid === "89") {
  162. // islandHoleHandlerForFeature 处理岛洞面
  163. IHFeas.push(islandHoleHandlerForFeature(feature));
  164. } else {
  165. feas.push(feature);
  166. }
  167. }
  168. // 岛洞多面要素必需在其他要素之前添加
  169. feas = IHFeas.concat(feas);
  170. themeLayer.addFeatures(feas);
  171. }
  172. initLegendView();
  173. initInfoView();
  174. }
  175. //图例控件
  176. function initLegendView() {
  177. var legendView = L.control({position: 'bottomright'});
  178. legendView.onAdd = function () {
  179. this._div = L.DomUtil.create('div', 'panel panel-primary legend ');
  180. var title = "<div class='panel-heading'><h5 class='panel-title text-center'>" + resources.text_legend + "</h5></div>";
  181. $(title + "<div class='panel-body text-left' ><table>" +
  182. "<tr><td class='legendItemHeader'>" + resources.text_landType + "</td><td class='legendItemValue'>" + resources.text_color + "</td></tr>" +
  183. "<tr> <td class='legendItemHeader' >" + resources.text_grassland + "</td> <td class='legendItemValue' style='background: #C1FFC1'></td></tr>" +
  184. "<tr> <td class='legendItemHeader'>" + resources.text_city + "</td> <td class='legendItemValue' style='background: #CD7054'></td> </tr>" +
  185. "<tr> <td class='legendItemHeader'>" + resources.text_shrub + "</td> <td class='legendItemValue' style='background: #7CCD7C'></td> </tr>" +
  186. "<tr> <td class='legendItemHeader'>" + resources.text_dryLand + "</td> <td class='legendItemValue' style='background: #EE9A49'></td> </tr>" +
  187. "<tr> <td class='legendItemHeader'>" + resources.text_lakeReservoir + "</td> <td class='legendItemValue' style='background: #8EE5EE'></td> </tr>" +
  188. "<tr> <td class='legendItemHeader'>" + resources.text_economicForest + "</td> <td class='legendItemValue' style='background: #548B54'></td> </tr>" +
  189. "<tr><td class='legendItemHeader'>" + resources.text_desert + "</td> <td class='legendItemValue' style='background: #DEB887'></td> </tr>" +
  190. "<tr><td class='legendItemHeader'>" + resources.text_irrigatedLand + "</td> <td class='legendItemValue' style='background: #E0FFFF'></td> </tr>" +
  191. "<tr><td class='legendItemHeader'>" + resources.text_paddyField + "</td> <td class='legendItemValue' style='background: #388E8E'></td> </tr>" +
  192. "<tr> <td class='legendItemHeader'>" + resources.text_timberForest + "</td> <td class='legendItemValue' style='background: #556B2F'></td> </tr>" +
  193. "<tr> <td class='legendItemHeader'>" + resources.text_swamp + "</td> <td class='legendItemValue' style='background: #2F4F4F'></td> </tr>" +
  194. "<tr> <td class='legendItemHeader'>" + resources.text_DefaultStyle + "</td> <td class='legendItemValue' style='background: #ABABAB'></td> </tr>" +
  195. "</table></div>"
  196. ).appendTo(this._div);
  197. handleMapEvent(this._div, this._map);
  198. return this._div;
  199. };
  200. legendView.addTo(map);
  201. }
  202. //高亮时显示图层信息框的控件
  203. function initInfoView() {
  204. infoView = L.control({position: 'bottomright'});
  205. infoView.onAdd = function () {
  206. this._div = L.DomUtil.create('div', 'panel panel-primary infoPane');
  207. $(this._div).css("width", "200px");
  208. $("<div class='panel-heading'><h5 class='panel-title text-center'>" + resources.text_attributeTable + "</h5></div>").appendTo(this._div);
  209. var content = $("<div class='panel-body content'></div>").appendTo(this._div);
  210. content.css('fontSize', '14px');
  211. handleMapEvent(this._div, this._map);
  212. return this._div;
  213. };
  214. infoView.update = function (fea) {
  215. var content = $(".content");
  216. content.text("");
  217. if (!fea) {
  218. return;
  219. }
  220. var innerHtml = "ID: " + fea.attributes.SMID + "<br/>";
  221. innerHtml += resources.text_landType + ": " + fea.attributes.LANDTYPE + "<br/>";
  222. innerHtml += resources.text_area + parseFloat(fea.attributes.SMAREA).toFixed(5) + "<br/>";
  223. content.html(innerHtml);
  224. }
  225. }
  226. function concatAttributes(fieldNames, filedValues) {
  227. var attr = {};
  228. for (var i = 0; i < fieldNames.length; i++) {
  229. attr[fieldNames[i]] = filedValues[i];
  230. }
  231. return attr;
  232. }
  233. function highLightLayer(e) {
  234. if (e.target && e.target.refDataID) {
  235. var fea = themeLayer.getFeatureById(e.target.refDataID);
  236. if (fea) {
  237. infoView.addTo(map);
  238. infoView.update(fea);
  239. }
  240. } else if (infoView) {
  241. infoView.remove();
  242. }
  243. }
  244. function handleMapEvent(div, map) {
  245. if (!div || !map) {
  246. return;
  247. }
  248. div.addEventListener('mouseover', function () {
  249. map.dragging.disable();
  250. map.scrollWheelZoom.disable();
  251. map.doubleClickZoom.disable();
  252. });
  253. div.addEventListener('mouseout', function () {
  254. map.dragging.enable();
  255. map.scrollWheelZoom.enable();
  256. map.doubleClickZoom.enable();
  257. });
  258. }
  259. </script>
  260. <script type="text/javascript">
  261. /*
  262. * Method: islandHoleHandlerForFeature。
  263. * 要素岛洞处理。
  264. *
  265. * 多面中,一个子面包含另一个子面,则被包含子面处理为岛洞。
  266. *
  267. * Parameters:
  268. * multiPolygon - {<SuperMap.Feature.Vector>} 需要进行岛洞处理的要素。
  269. *
  270. * Returns:
  271. * {<SuperMap.Feature.Vector>} 处理后的要素。
  272. */
  273. function islandHoleHandlerForFeature(feature) {
  274. if (feature.geometry instanceof SuperMap.Geometry.MultiPolygon && feature.geometry.components.length > 1) {
  275. var newGeometry = islandHoleHandlerForMultiPolygon(feature.geometry);
  276. feature.geometry = newGeometry;
  277. }
  278. return feature;
  279. /*
  280. * Method: islandHoleHandlerForMultiPolygon
  281. * 处理误判为岛洞的多面。
  282. *
  283. * iClient 在解析 iServer 数据时,默认将面要素处理为 MultiPolygon 类型,但有的面要素带有岛洞,
  284. * 这种情况下应该做特殊处理,本函数可以对一个多面进行岛洞处理,并返回新的多面。
  285. *
  286. * Parameters:
  287. * multiPolygon - {<SuperMap.Geometry.MultiPolygon>} 需要进行岛洞处理的多面。
  288. *
  289. * Returns:
  290. * {<SuperMap.Geometry.MultiPolygon>} 处理后的多面。
  291. */
  292. function islandHoleHandlerForMultiPolygon(multiPolygon) {
  293. if (!(multiPolygon instanceof SuperMap.Geometry.MultiPolygon) || multiPolygon.components.length < 2) {
  294. return multiPolygon;
  295. }
  296. var mPTmp = multiPolygon.clone();
  297. var componentsPolygons = mPTmp.components;
  298. //洞面关系数组
  299. var polygonHoleGroup = [];
  300. for (var k = 0, len = componentsPolygons.length; k < len; k++) {
  301. var geoPolygon = componentsPolygons[k];
  302. //不处理已经是岛洞的面
  303. if (geoPolygon.components.length != 1) {
  304. continue;
  305. }
  306. var lineRings = geoPolygon.components[0];
  307. //将每个点放到面中进行判断
  308. for (var j = 0, len1 = componentsPolygons.length; j < len1; j++) {
  309. if (componentsPolygons[j].components.length != 1 || (j == k)) continue;
  310. var polygonGeoComp = componentsPolygons[j].components[0].components;
  311. //假设此面为岛洞
  312. var isAllPoiIn = true;
  313. for (var i = 0, len2 = geoPolygon.components.length; i < len2; i++) {
  314. var point = lineRings.components[i];
  315. if (isPointInPoly(point, polygonGeoComp) == false) {
  316. isAllPoiIn = false;
  317. }
  318. }
  319. //确定并记录洞面关系
  320. if (isAllPoiIn == true) {
  321. var polygonHole = [j, k];
  322. polygonHoleGroup.push(polygonHole);
  323. }
  324. }
  325. }
  326. // 根据洞面信息重构多面 Geometry。
  327. var bPsTmp = [];
  328. var hPsTmp = [];
  329. for (var m = 0, len3 = polygonHoleGroup.length; m < len3; m++) {
  330. bPsTmp.push(polygonHoleGroup[m][0]);
  331. hPsTmp.push(polygonHoleGroup[m][1]);
  332. }
  333. //岛洞基础面
  334. var bPs = delRepeatInArray(bPsTmp);
  335. //洞面
  336. var hPs = delRepeatInArray(hPsTmp);
  337. //独立面
  338. var iPs = [];
  339. //查找独立面
  340. for (var isIPs = 0, compLen = componentsPolygons.length; isIPs < compLen; isIPs++) {
  341. var isNoHP = true;
  342. for (var o = 0, len = bPs.length; o < len; o++) {
  343. if (isIPs == bPs[o]) {
  344. isNoHP = false;
  345. break;
  346. }
  347. }
  348. if (isNoHP == true) {
  349. for (var o = 0, len = hPs.length; o < len; o++) {
  350. if (isIPs == hPs[o]) {
  351. isNoHP = false;
  352. break;
  353. }
  354. }
  355. }
  356. if (isNoHP == true) {
  357. iPs.push(isIPs);
  358. }
  359. }
  360. //新洞面信息
  361. var hpInfo = [];
  362. //组织新geometry所需要的信息
  363. for (var o = 0, len4 = bPs.length; o < len4; o++) {
  364. var ph = [];
  365. ph.push(bPs[o]);
  366. for (var m = 0, len3 = polygonHoleGroup.length; m < len3; m++) {
  367. if (bPs[o] == polygonHoleGroup[m][0]) {
  368. ph.push(polygonHoleGroup[m][1]);
  369. }
  370. }
  371. if (ph.length > 1) {
  372. hpInfo.push(ph);
  373. }
  374. }
  375. var newComponents = [];
  376. //岛洞子面处理
  377. for (var m = 0, len3 = hpInfo.length; m < len3; m++) {
  378. var geoP = hpInfo[m];
  379. var newLineRings = [];
  380. for (var n = 0, len6 = geoP.length; n < len6; n++) {
  381. newLineRings.push(componentsPolygons[geoP[n]].components[0]);
  382. }
  383. var newGeoPolygon = new SuperMap.Geometry.Polygon(newLineRings);
  384. newComponents.push(newGeoPolygon)
  385. }
  386. //独立子面处理
  387. for (var s = 0, len7 = iPs.length; s < len7; s++) {
  388. var is = iPs[s];
  389. newComponents.push(componentsPolygons[is])
  390. }
  391. multiPolygon.components = newComponents;
  392. return multiPolygon;
  393. }
  394. /*
  395. * Method: delRepeatInArray
  396. * 删除数组中的重复元素。
  397. *
  398. * Parameters:
  399. * arr - {Array} 要进行重复元素删除的数组。
  400. *
  401. * Returns:
  402. * {Array} 无重复元素的数组。
  403. */
  404. function delRepeatInArray(arr) {
  405. var newArray = [];
  406. var provisionalTable = {};
  407. for (var i = 0, a; (a = arr[i]) != null; i++) {
  408. if (!provisionalTable[a]) {
  409. newArray.push(a);
  410. provisionalTable[a] = true;
  411. }
  412. }
  413. return newArray;
  414. }
  415. /*
  416. * Method: PointInPoly
  417. * 判断一个点是否在多边形里面。(射线法)
  418. *
  419. * Parameters:
  420. * pt - {Object} 需要判定的点对象,该对象含有属性x(横坐标),属性y(纵坐标)。
  421. * poly - {Array(Objecy)} 多边形节点数组。例如一个四边形:[{"x":1,"y":1},{"x":3,"y":1},{"x":6,"y":4},{"x":2,"y":10},{"x":1,"y":1}]。
  422. *
  423. * Returns:
  424. * {Boolean} 点是否在多边形内。
  425. */
  426. function isPointInPoly(pt, poly) {
  427. for (var isIn = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
  428. ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
  429. && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
  430. && (isIn = !isIn);
  431. return isIn;
  432. }
  433. }
  434. </script>
  435. </body>
  436. </html>