CoordinateConversionTool.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <el-container>
  3. <div class="beforeTransformOption">
  4. <el-row class="first_level_title"> 坐标转换 </el-row>
  5. <el-row>
  6. <el-col class="second_level_title_1" :span="8">
  7. 转换前坐标系统:
  8. <el-select
  9. v-model="beforeTransformOption"
  10. @change="changeAfterTransformOptions"
  11. placeholder="请选择"
  12. >
  13. <el-option
  14. v-for="item in beforeTransformOptions"
  15. :key="item.value"
  16. :label="item.label"
  17. :value="item.value"
  18. >
  19. </el-option>
  20. </el-select>
  21. </el-col>
  22. <el-col class="second_level_title_1" :span="8">
  23. 转换后坐标系统:
  24. <el-select v-model="afterTransformOption" placeholder="请选择">
  25. <el-option
  26. v-for="item in afterTransformOptions"
  27. :key="item.value"
  28. :label="item.label"
  29. :value="item.value"
  30. >
  31. </el-option>
  32. </el-select>
  33. </el-col>
  34. </el-row>
  35. <el-row>
  36. <el-col class="second_level_title_1" :span="8">
  37. 转换前经度(或x):
  38. <el-input v-model="beforeX" type="number"></el-input>
  39. </el-col>
  40. <el-col class="second_level_title_1" :span="8">
  41. 转换前纬度(或y):
  42. <el-input v-model="beforeY" type="number"></el-input>
  43. </el-col>
  44. <el-col :span="8">
  45. <el-button class="change_address" type="primary" @click="transform">
  46. 转换
  47. </el-button>
  48. <el-button
  49. class="change_address"
  50. type="primary"
  51. @click="clearResult"
  52. v-if="isTransform"
  53. >
  54. 结果清空
  55. </el-button>
  56. </el-col>
  57. </el-row>
  58. <el-row class="tips">
  59. <el-col> 注:转换前的经纬度(或x y)的值应为数字 </el-col>
  60. </el-row>
  61. </div>
  62. <div class="afterTransformOption">
  63. <el-row class="first_level_title"> 转换结果 </el-row>
  64. <el-row>
  65. <el-col class="second_level_title_2" :span="8">
  66. 转换后经度(或x):{{ afterX }}
  67. </el-col>
  68. <el-col class="second_level_title_2" :span="8">
  69. 转换后纬度(或y):{{ afterY }}
  70. </el-col>
  71. <el-col :span="8" style="min-heigth: 1px"></el-col>
  72. </el-row>
  73. <div id="transform_map"></div>
  74. <el-row class="third_level_title">
  75. 注:当转换后的坐标系为 WGS84 或 Web Mercator 时,结果将在地图上展示
  76. </el-row>
  77. </div>
  78. </el-container>
  79. </template>
  80. <script>
  81. import Map from "ol/Map";
  82. import View from "ol/View";
  83. import Tile from "ol/layer/Tile";
  84. import XYZ from "ol/source/XYZ";
  85. import Feature from "ol/Feature";
  86. import Point from "ol/geom/Point";
  87. import VectorSource from "ol/source/Vector";
  88. import VectorLayer from "ol/layer/Vector";
  89. import { defaults } from "ol/control";
  90. import { transform } from "ol/proj";
  91. import { Style, Circle, Fill, Stroke, Text } from "ol/style";
  92. export default {
  93. data() {
  94. return {
  95. defaultOptions: [],
  96. beforeTransformOption: 0,
  97. beforeTransformOptions: [],
  98. afterTransformOption: 1,
  99. afterTransformOptions: [],
  100. isTransform: false,
  101. beforeX: "",
  102. beforeY: "",
  103. afterX: "",
  104. afterY: "",
  105. };
  106. },
  107. created() {
  108. this.defaultOptions = systemConfig.coordinateSystem.map(function (
  109. item,
  110. index
  111. ) {
  112. item.value = index;
  113. return item;
  114. });
  115. this.beforeTransformOptions = Object.assign({}, this.defaultOptions);
  116. this.changeAfterTransformOptions(this.beforeTransformOption);
  117. },
  118. mounted() {
  119. let center = transform(
  120. [systemConfig.mapDefault.center.lng, systemConfig.mapDefault.center.lat],
  121. "EPSG:4326",
  122. "EPSG:3857"
  123. );
  124. let view = new View({
  125. center: center,
  126. zoom: systemConfig.mapDefault.zoom,
  127. constrainResolution: true, // 以整数级别缩放地图
  128. });
  129. this.map = new Map({
  130. target: "transform_map", //地图标签id
  131. logo: false,
  132. controls: defaults({
  133. attribution: false,
  134. zoom: false,
  135. rotate: false,
  136. }),
  137. view: view,
  138. });
  139. let urlvec =
  140. systemConfig.tdt_url +
  141. "/vec_w/wmts?tk=" +
  142. systemConfig.tdt_tk +
  143. "&LAYER=vec&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";
  144. let urlcva =
  145. systemConfig.tdt_url +
  146. "/cva_w/wmts?tk=" +
  147. systemConfig.tdt_tk +
  148. "&LAYER=cva&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&STYLE=default&TILEMATRIXSET=w&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=tiles";
  149. let layer_vec = new Tile({
  150. source: new XYZ({ url: urlvec }),
  151. zIndex: 0,
  152. });
  153. let layer_cva = new Tile({
  154. source: new XYZ({ url: urlcva }),
  155. zIndex: 0,
  156. });
  157. this.map.addLayer(layer_vec);
  158. this.map.addLayer(layer_cva);
  159. var markerStyle = new Style({
  160. image: new Circle({
  161. radius: 7,
  162. fill: new Fill({
  163. color: "#ff0000",
  164. }),
  165. stroke: new Stroke({
  166. color: "0000",
  167. width: 2,
  168. }),
  169. }),
  170. });
  171. this.markerSource = new VectorSource();
  172. var markerLayer = new VectorLayer({
  173. source: this.markerSource,
  174. style: markerStyle,
  175. zIndex: 1,
  176. });
  177. this.map.addLayer(markerLayer);
  178. },
  179. methods: {
  180. changeAfterTransformOptions(value) {
  181. this.afterTransformOptions = this.defaultOptions.filter(function (item) {
  182. return item.value != value;
  183. });
  184. this.afterTransformOption = this.afterTransformOptions[0].value;
  185. this.clearResult();
  186. },
  187. transform() {
  188. let that = this;
  189. let beforeTransformItem = this.defaultOptions.filter(function (item) {
  190. return item.value == that.beforeTransformOption;
  191. })[0];
  192. let afterTransformItem = this.defaultOptions.filter(function (item) {
  193. return item.value == that.afterTransformOption;
  194. })[0];
  195. proj4.defs(beforeTransformItem.uuid, beforeTransformItem.proj4);
  196. proj4.defs(afterTransformItem.uuid, afterTransformItem.proj4);
  197. let after = proj4(beforeTransformItem.uuid, afterTransformItem.uuid, [
  198. Number(this.beforeX),
  199. Number(this.beforeY),
  200. ]);
  201. this.afterX = after[0];
  202. this.afterY = after[1];
  203. this.isTransform = true;
  204. if (afterTransformItem.uuid.indexOf("4326") > -1) {
  205. let t = proj4("EPSG:4326", "EPSG:3857", after);
  206. this.addPoint(t[0], t[1]);
  207. }
  208. if (afterTransformItem.uuid.indexOf("3857") > -1) {
  209. this.addPoint(after[0], after[1]);
  210. }
  211. },
  212. clearResult() {
  213. this.afterX = "";
  214. this.afterY = "";
  215. this.isTransform = false;
  216. this.clearPoint();
  217. },
  218. addPoint(x, y) {
  219. var feature = new Feature({
  220. geometry: new Point([x, y]),
  221. });
  222. this.markerSource.addFeatures([feature]);
  223. this.map.getView().animate({
  224. center: [x, y], //动画结尾的视图中心
  225. zoom: 15,
  226. duration: 2000, //动画的持续时间
  227. });
  228. // this.map.getView().setZoom(15);
  229. },
  230. clearPoint() {
  231. if (this.markerSource) this.markerSource.clear();
  232. },
  233. },
  234. };
  235. </script>
  236. <style lang="less" scoped>
  237. .el-container {
  238. display: block;
  239. width: 100%;
  240. padding: 10px 20px;
  241. box-sizing: border-box;
  242. background: #ffffff;
  243. .beforeTransformOption {
  244. overflow: hidden;
  245. .el-row {
  246. margin-bottom: 20px;
  247. line-height: 40px;
  248. &:last-child {
  249. margin-bottom: 10px;
  250. }
  251. }
  252. .el-input {
  253. width: fit-content;
  254. }
  255. .tips {
  256. margin-bottom: 0px !important;
  257. line-height: 30px;
  258. font-size: 14px;
  259. text-indent: 42px;
  260. color: #c3c3c3;
  261. overflow: hidden;
  262. text-align: left;
  263. }
  264. }
  265. .afterTransformOption {
  266. height: calc(100% - 172px);
  267. position: relative;
  268. .el-row {
  269. margin-bottom: 10px;
  270. line-height: 40px;
  271. }
  272. // .el-main {
  273. // height: calc(100% - 50px);
  274. // padding-top: 2px;
  275. // padding-bottom: 10px;
  276. // overflow: hidden;
  277. // overflow-y: auto;
  278. // }
  279. #transform_map {
  280. height: calc(100% - 150px);
  281. }
  282. }
  283. .first_level_title {
  284. font-size: 18px;
  285. font-weight: bold;
  286. }
  287. .second_level_title_1 {
  288. font-size: 16px;
  289. font-weight: bold;
  290. text-align: right;
  291. }
  292. .second_level_title_2 {
  293. font-size: 16px;
  294. font-weight: bold;
  295. }
  296. .third_level_title {
  297. font-size: 14px;
  298. font-weight: bold;
  299. color: #cbcbcb;
  300. }
  301. .must_tips {
  302. color: #ff0000;
  303. }
  304. .change_address {
  305. margin-left: 20px;
  306. }
  307. ::v-deep input::-webkit-outer-spin-button,
  308. ::v-deep input::-webkit-inner-spin-button {
  309. -webkit-appearance: none !important;
  310. }
  311. ::v-deep input[type="number"] {
  312. -moz-appearance: textfield !important;
  313. }
  314. }
  315. </style>