11readShapefile.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>读取shapefile文件</title>
  7. <link rel="stylesheet" href="../dist/ol/ol.css" />
  8. <script src="../dist/index.js"></script>
  9. </head>
  10. <body style="margin: 0; padding: 0;">
  11. <!-- <div id="map" style="height: 100vh; width: 100vw"></div> -->
  12. <div id="mapId" class="mapDIV"></div>
  13. <div class="input-card">
  14. SHP: <input type="file" id="shpFile"> <br>
  15. DBF: <input type="file" id="dbfFile"> <br>
  16. <!-- PRJ: <input type="file" id="prjFile"> <br> -->
  17. <input type="button" value="Parse" onclick="doParseShp()">
  18. <div class="input-item">
  19. <button class="btn" id="btn5" onclick="loadShp()">加载shapefile</button>
  20. </div>
  21. </div>
  22. </body>
  23. <script>
  24. var shpData , dbfData ;
  25. var loadStatus = false;
  26. var fLayer = new CTMapOl.layer.Vector({
  27. source: new CTMapOl.source.Vector()
  28. });
  29. const source = new CTMapOl.source.XYZ({
  30. url: 'http://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403'
  31. })
  32. const tileLayer = new CTMapOl.layer.Tile({
  33. title: '天地图',
  34. source: source
  35. })
  36. var sourceMarker = new CTMapOl.source.XYZ({
  37. url: 'http://t0.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=86db5390161c4da2abe96c3fca00f403'
  38. })
  39. const tileMarker = new CTMapOl.layer.Tile({
  40. title: '标注图层',
  41. source: sourceMarker
  42. })
  43. const map = new CTMapOl.Map({
  44. view: new CTMapOl.View({
  45. projection: 'EPSG:4326',
  46. center: [124.6112610521188, 41.530759008113655],
  47. zoom: 10,
  48. maxZoom: 18,
  49. minZoom: 1
  50. }),
  51. controls: CTMapOl.control.defaults({
  52. attribution: false,
  53. rotate: false,
  54. zoom: false
  55. }),
  56. layers: [tileLayer, tileMarker, fLayer],
  57. target: 'mapId'
  58. })
  59. function monitorIcon() {
  60. return new CTMapOl.style.Style({
  61. image: new CTMapOl.style.Icon({
  62. src: './assets/monitor-online.png',
  63. }),
  64. });
  65. }
  66. let fixPositionObj = new CTMapOl.extend.FixPosition(map, monitorIcon());
  67. function fixPoint1(visible) {
  68. fixPositionObj.position([124.56485712008785, 41.228574591618738], visible);
  69. }
  70. function fixPoint2(visible) {
  71. fixPositionObj.position([124.65766498414975, 41.832943424608573], visible);
  72. }
  73. function deletePoint() {
  74. fixPositionObj.removePoint();
  75. }
  76. function doParseShp() {
  77. shpData = null;
  78. dbfData = null;
  79. loadStatus = false;
  80. var shpFile = document.getElementById("shpFile").files[0];
  81. var dbfFile = document.getElementById("dbfFile").files[0];
  82. if(!shpFile){
  83. alert('shp文件不能为空');
  84. return ;
  85. }
  86. if(!dbfFile){
  87. alert('DBF文件不能为空');
  88. return ;
  89. }
  90. //通过HTML5 读取本地文件数据流
  91. var readDbf = new FileReader();
  92. readDbf.readAsArrayBuffer(dbfFile, 'UTF-8');//读取文件的内容
  93. var readShp = new FileReader();
  94. readShp.readAsArrayBuffer(shpFile, 'UTF-8');//读取文件的内容
  95. //SHP
  96. readShp.onload = function () {
  97. shpData = this.result;
  98. loadData();
  99. }
  100. //DBF
  101. readDbf.onload = function () {
  102. dbfData = this.result;
  103. loadData();
  104. }
  105. }
  106. function loadData() {
  107. if(!dbfData || !shpData || loadStatus) return ;
  108. loadStatus = true;
  109. //var shapefile = new shapefile();
  110. var features = [];
  111. var index = 0;
  112. CTMapOl.shapefile.open(shpData, dbfData , { encoding: 'gb2312' }).then(source => source.read().then(
  113. function next(result) {
  114. if (result.done) {
  115. layerReloadFeatures(features);
  116. return;
  117. }
  118. var geometry = result.value.geometry ;
  119. var propreties = result.value.properties ;
  120. var f = createFeature(geometry , propreties);
  121. if(f ){
  122. features.push(f);
  123. }
  124. index++ ;
  125. return source.read().then(next);
  126. }
  127. ))
  128. }
  129. function loadShp() {
  130. let shpfile = './shp/bou2_4p.shp';
  131. var features = [];
  132. var index = 0;
  133. CTMapOl.shapefile.open(shpfile, undefined, { encoding: 'gb2312' }).then(source => source.read().then(
  134. function next(result) {
  135. if (result.done) {
  136. layerReloadFeatures(features);
  137. return;
  138. }
  139. var geometry = result.value.geometry;
  140. var propreties = result.value.properties;
  141. var f = createFeature(geometry, propreties);
  142. if (f) {
  143. features.push(f);
  144. }
  145. index++;
  146. return source.read().then(next);
  147. }
  148. ))
  149. }
  150. function layerReloadFeatures(features) {
  151. fLayer.getSource().clear();
  152. fLayer.getSource().addFeatures(features);
  153. var extent = fLayer.getSource().getExtent();
  154. if (extent) {
  155. map.getView().fit(extent, map.getSize());
  156. }
  157. }
  158. /**
  159. * 根据shp解析出来的数据创建一个ol4 的feature
  160. */
  161. function createFeature(geometry, properties) {
  162. var type = geometry.type;
  163. var geom;
  164. if (type == 'Polygon') {
  165. geom = new CTMapOl.geom.Polygon(geometry.coordinates);
  166. } else if (type == 'Point') {
  167. geom = new CTMapOl.geom.Point(geometry.coordinates);
  168. } else if (type == 'LineString') {
  169. geom = new CTMapOl.geom.LineString(geometry.coordinates);
  170. } else if (type == 'MultiPolygon') {
  171. geom = new CTMapOl.geom.MultiPolygon(geometry.coordinates);
  172. } else if (type == 'MultiPoint') {
  173. geom = new CTMapOl.geom.MultiPoint(geometry.coordinates);
  174. } else if (type == 'MultiLineString') {
  175. geom = new CTMapOl.geom.MultiLineString(geometry.coordinates);
  176. }
  177. if (!geom) return null;
  178. var feature = new CTMapOl.Feature({
  179. geometry: geom
  180. });
  181. //属性
  182. for (var key in properties) {
  183. feature.set(key, properties[key]);
  184. }
  185. return feature;
  186. }
  187. </script>
  188. <style>
  189. /*地图*/
  190. .mapDIV {
  191. height: 100vh;
  192. width: 100vw;
  193. z-index: 1;
  194. }
  195. .add-switch {
  196. position: absolute;
  197. right: 30px;
  198. height: 68px;
  199. width: 30px;
  200. bottom: 112px;
  201. background: #FFFFFF 100%;
  202. color: #172537;
  203. border-radius: 4px;
  204. display: flex;
  205. flex-direction: column;
  206. align-items: center;
  207. z-index: 2;
  208. }
  209. .add-s-block {
  210. height: 20px;
  211. display: flex;
  212. align-items: center;
  213. justify-content: center;
  214. width: 100%;
  215. line-height: 20px;
  216. font-size: 12px;
  217. margin-bottom: 2px;
  218. color: #172537;
  219. }
  220. .add-zoom {
  221. width: 20px;
  222. height: 20px;
  223. background-image: url('../dist/assets/image/ar/switch/add-zoom.svg');
  224. background-size: 100% 100%;
  225. cursor: pointer;
  226. }
  227. .sub-zoom {
  228. width: 20px;
  229. height: 20px;
  230. background-image: url('../dist/assets/image/ar/switch/sub-zoom.svg');
  231. background-size: 100% 100%;
  232. cursor: pointer;
  233. }
  234. .add-zoom-out {
  235. width: 20px;
  236. height: 20px;
  237. background-image: url('../dist/assets/image/ar/switch/add-zoom-out.svg');
  238. background-size: 100% 100%;
  239. }
  240. .sub-zoom-out {
  241. width: 20px;
  242. height: 20px;
  243. background-image: url('../dist/assets/image/ar/switch/sub-zoom-out.svg');
  244. background-size: 100% 100%;
  245. }
  246. .add-s-block.btn-click {
  247. cursor: pointer;
  248. }
  249. .input-card {
  250. position: absolute;
  251. right: 15px;
  252. bottom: 15px;
  253. z-index: 999;
  254. }
  255. </style>
  256. </html>