uniqueThemeLayer.html 20 KB

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