uniqueThemeLayer.html 20 KB

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