WMSLayer.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import Image from 'ol/layer/Image'
  2. import ImageWMS from 'ol/source/ImageWMS'
  3. import {GeoJSON,WFS} from 'ol/format'
  4. import * as filter from 'ol/format/filter'
  5. import * as extent from 'ol/extent'
  6. import KBaseObject from './KBaseObject'
  7. import Projection from 'ol/proj/Projection'
  8. import TileWMS from 'ol/source/TileWMS'
  9. import Tile from 'ol/layer/Tile'
  10. /**
  11. * @description LTMap.WMSLayer WMS图层类
  12. */
  13. class WMSLayer extends KBaseObject{
  14. /**
  15. * @description 构造函数
  16. * @param {String} url wms图层服务地址
  17. * @param {LTMap.Map} [mapInstance=null] map对象,单地图的时候可不传,多地图时候需要传
  18. * @memberof WMSLayer
  19. */
  20. constructor(url,layerName,mapInstance = null){
  21. super(mapInstance)
  22. const vm = this
  23. vm.format = "image/png"
  24. vm.initImageLayer(url,layerName)
  25. // vm.initTileLayer(url,layerName)
  26. }
  27. /**
  28. * @description 初始化ImageLayer
  29. * @memberof WMSLayer
  30. */
  31. initImageLayer(url,layerName){
  32. const vm = this
  33. var projection = new Projection({
  34. code: 'EPSG:4326',
  35. units: 'degrees',
  36. axisOrientation: 'neu',
  37. global: true
  38. })
  39. vm.source = new ImageWMS({
  40. url: url,
  41. ratio: 1,
  42. params: {
  43. 'FORMAT':vm.format,
  44. "LAYERS": layerName,
  45. "STYLES": '',
  46. 'VERSION': '1.1.1',
  47. "exceptions": 'application/vnd.ogc.se_inimage'
  48. },
  49. serverType: 'geoserver',
  50. crossOrigin: 'anonymous',
  51. projection:projection
  52. })
  53. vm.layer = new Image({
  54. source:vm.source
  55. })
  56. vm.state = true
  57. //地图加载WMS图层
  58. vm.map.addLayer(vm.layer)
  59. }
  60. /**
  61. * @description 切片加载wms图层
  62. * @param {*} url
  63. * @param {*} layerName
  64. * @memberof WMSLayer
  65. */
  66. initTileLayer(url,layerName){
  67. const vm = this
  68. var projection = new Projection({
  69. code: 'EPSG:4326',
  70. units: 'degrees',
  71. axisOrientation: 'neu',
  72. global: true
  73. })
  74. vm.source = new TileWMS({
  75. url: url,
  76. params: {
  77. 'FORMAT': vm.format,
  78. 'VERSION': '1.1.1',
  79. tiled: true,
  80. "STYLES": '',
  81. "LAYERS": layerName,
  82. "exceptions": 'application/vnd.ogc.se_inimage',
  83. // tilesOrigin: -124.73142200000001 + "," + 24.955967
  84. },
  85. projection:projection
  86. })
  87. vm.layer = new Tile({
  88. source:vm.source
  89. })
  90. vm.map.addLayer(vm.layer)
  91. }
  92. /**
  93. * @description 添加WMS图层到地图
  94. * @memberof WMSLayer
  95. */
  96. add(){
  97. const vm = this
  98. if(!vm.state) {
  99. vm.map.addLayer(vm.layer)
  100. vm.state = true
  101. }
  102. }
  103. /**
  104. * @description 从当前地图中移除WMS图层
  105. * @memberof WMSLayer
  106. */
  107. remove() {
  108. const vm = this
  109. if(vm.state) {
  110. vm.map.removeLayer(vm.layer)
  111. vm.state = false
  112. }
  113. }
  114. /**
  115. * @description 显示WMS图层数据
  116. * @memberof WMSLayer
  117. */
  118. show(){
  119. const vm = this
  120. if(vm.state) {
  121. vm.layer.setVisible(true)
  122. }
  123. }
  124. /**
  125. * @description 隐藏WMS图层数据
  126. * @memberof WMSLayer
  127. */
  128. hide(){
  129. const vm = this
  130. if(vm.state) {
  131. vm.layer.setVisible(false)
  132. }
  133. }
  134. /**
  135. * @description 更新WMS图层内容
  136. * @param {String} data 属性过滤参数(例如:RoadNo=11),选填,不传值默认加载全部数据
  137. * @memberof WMSLayer
  138. */
  139. update(data) {
  140. const vm = this
  141. if(data!=null && data!="" && data!=undefined && state) {
  142. vm.source.updateParams({
  143. "CQL_FILTER": data
  144. })
  145. }
  146. else{
  147. vm.source.updateParams({
  148. "CQL_FILTER": null
  149. })
  150. }
  151. }
  152. /**
  153. * @description 缩放到过滤后的地图要素对应范围
  154. * @memberof WMSLayer
  155. */
  156. zoomTo() {
  157. const vm = this
  158. //组装查询过滤条件
  159. let cqlfilter = vm.source.getParams().CQL_FILTER
  160. if(cqlfilter == null){
  161. //缩放到图层范围
  162. }
  163. else{
  164. let cqlfilters = cqlfilter.split("=")
  165. //实现思路为通过WFS进行属性查询,查找到要素后再缩放到该要素
  166. let wfsurl = url.substr(0, url.lastIndexOf("/") + 1 ) + "wfs"
  167. let featureRequest = new WFS().writeGetFeature({
  168. //srsName: 'EPSG:3857',
  169. //featureNS: 'http://openstreemap.org',
  170. //featurePrefix: 'skyline',
  171. featureTypes: [layer],
  172. outputFormat: 'application/json',
  173. filter: filter.equalTo(cqlfilters[0],cqlfilters[1])
  174. })
  175. let myfilter = WFS.writeFilter(filter.equalTo(cqlfilters[0],cqlfilters[1]))
  176. $.ajax({
  177. type: 'get',
  178. url: wfsurl,
  179. data: {
  180. service: 'WFS',
  181. request: 'GetFeature',
  182. typeNames: layer,//查询图层
  183. filter:new XMLSerializer().serializeToString(myfilter),//查询条件
  184. outputFormat: 'application/json'
  185. },
  186. async : false,
  187. success: function(res) {
  188. let features = new GeoJSON().readFeatures(res)
  189. //let coordinateArray = new Array();
  190. let extentBound = new extent.createEmpty()
  191. for(let i=0; i<features.length; i++) {
  192. /*let coordinates = features[i].getGeometry().getCoordinates();//由多个分部构成,与Polyline函数生成的结构不一致
  193. for(j=0; j<coordinates.length; j++) {
  194. let pathcoordinates = coordinates[j];
  195. for(k = 0 ; k < pathcoordinates.length ; k ++)
  196. coordinateArray.push(pathcoordinates[k]);
  197. }*/
  198. extentBound = new extent.extend(extentBound,features[i].getGeometry().getExtent());
  199. }
  200. //let extentBound = new extent.boundingExtent(coordinateArray);
  201. if(features.length > 0){
  202. vm.map.getView().fit(extentBound,{
  203. duration: 1
  204. })
  205. }
  206. },
  207. error: function(e) {
  208. console.log("failed")
  209. }
  210. })
  211. /*fetch(wfsurl, {
  212. method: 'POST',
  213. body: new XMLSerializer().serializeToString(featureRequest)
  214. }).then(function(response) {
  215. return response.json();
  216. }).then(function(json) {
  217. let features = new GeoJSON().readFeatures(json);
  218. let extentBound = new extent.createEmpty();
  219. for(let i=0; i<features.length; i++) {
  220. extentBound = new extent.extend(extentBound,features[i].getGeometry().getExtent());
  221. }
  222. if(features.length > 0)
  223. {
  224. map.getView().fit(extentBound,{
  225. duration: 1
  226. });
  227. }
  228. });*/
  229. }
  230. }
  231. /**
  232. *@description 根据经纬度坐标查询坐标点所在位置的要素
  233. *@param coordinate LTMap.LngLat格式的经纬度 必填
  234. *@returns 查询到要素的结果集合,json数组格式,json中包含对应要素的所有字段名和字段值
  235. */
  236. getFeaturesByCoor(coordinate){
  237. const vm = this
  238. //将经纬度坐标转平面坐标
  239. coordinate = vm.mapInstance.lnglatToPixel(coordinate)
  240. //将LT平面坐标转换为OL的平面坐标
  241. coordinate = Common.LTMapPixel2MapPixel(coordinate)
  242. let resolution = vm.mapInstance.getResolution()
  243. let projection = vm.mapInstance.getProjection()
  244. let url = vm.source.getGetFeatureInfoUrl(coordinate, resolution, projection,
  245. {'INFO_FORMAT': 'application/json', 'FEATURE_COUNT': 10})
  246. let geojsonFormat = new GeoJSON({
  247. defaultDataProjection: "EPSG:3857"
  248. })
  249. let result = []
  250. if(url) {
  251. $.ajax({
  252. type: 'get',
  253. url: url,
  254. async : false,
  255. success: function(res) {
  256. //获取所有的要素
  257. let features = geojsonFormat.readFeatures(res)
  258. let jsonDatas = []
  259. for(let i = 0 ; i < features.length ; i ++){
  260. let jsonData = {}
  261. for(let key in features[i].getProperties()){
  262. if (key == 'geometry'){
  263. continue
  264. }
  265. jsonData[key] = features[i].get(key)
  266. }
  267. jsonDatas.push(jsonData)
  268. }
  269. result = jsonDatas
  270. },
  271. error:function(e) {
  272. console.log("faile")
  273. }
  274. })
  275. }
  276. return result
  277. }
  278. /**
  279. *@description 设置鼠标悬停在元素上时的样式
  280. *@param {String} cursorStyle 鼠标样式("default"默认指针,"pointer"小手,
  281. *"move"移动指针,"text"文本指针,
  282. *"wait"等待状态,"help"帮助)
  283. *必填,由于跨域问题,该方法存在问题
  284. */
  285. setFeatureCursor(cursorStyle){
  286. const vm = this
  287. cursorStyle = (cursorStyle == undefined)?"default":cursorStyle
  288. let mapContainer = vm.map.getTargetElement()
  289. let defaultCursor = mapContainer.style.cursor
  290. vm.map.on("pointermove",function(e){
  291. //let features = map.forEachFeatureAtPixel(e.pixel,function(feature) { return feature; })
  292. if (e.dragging){
  293. return
  294. }
  295. let pixel = vm.map.getEventPixel(e.originalEvent)
  296. let hit = vm.map.forEachLayerAtPixel(pixel,function(){
  297. return true
  298. })
  299. if(hit){
  300. mapContainer.style.cursor = cursorStyle
  301. }
  302. else{
  303. mapContainer.style.cursor = defaultCursor
  304. }
  305. })
  306. }
  307. }
  308. export default WMSLayer