components_uniquetheme_react.html 15 KB

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