| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- import Image from 'ol/layer/Image'
- import ImageWMS from 'ol/source/ImageWMS'
- import {GeoJSON,WFS} from 'ol/format'
- import * as filter from 'ol/format/filter'
- import * as extent from 'ol/extent'
- import KBaseObject from './KBaseObject'
- import Projection from 'ol/proj/Projection'
- import TileWMS from 'ol/source/TileWMS'
- import Tile from 'ol/layer/Tile'
- /**
- * @description LTMap.WMSLayer WMS图层类
- */
- class WMSLayer extends KBaseObject{
- /**
- * @description 构造函数
- * @param {String} url wms图层服务地址
- * @param {LTMap.Map} [mapInstance=null] map对象,单地图的时候可不传,多地图时候需要传
- * @memberof WMSLayer
- */
- constructor(url,layerName,mapInstance = null){
- super(mapInstance)
- const vm = this
- vm.format = "image/png"
- vm.initImageLayer(url,layerName)
- // vm.initTileLayer(url,layerName)
- }
-
- /**
- * @description 初始化ImageLayer
- * @memberof WMSLayer
- */
- initImageLayer(url,layerName){
- const vm = this
- var projection = new Projection({
- code: 'EPSG:4326',
- units: 'degrees',
- axisOrientation: 'neu',
- global: true
- })
-
- vm.source = new ImageWMS({
- url: url,
- ratio: 1,
- params: {
- 'FORMAT':vm.format,
- "LAYERS": layerName,
- "STYLES": '',
- 'VERSION': '1.1.1',
- "exceptions": 'application/vnd.ogc.se_inimage'
- },
- serverType: 'geoserver',
- crossOrigin: 'anonymous',
- projection:projection
- })
- vm.layer = new Image({
- source:vm.source
- })
- vm.state = true
- //地图加载WMS图层
- vm.map.addLayer(vm.layer)
- }
- /**
- * @description 切片加载wms图层
- * @param {*} url
- * @param {*} layerName
- * @memberof WMSLayer
- */
- initTileLayer(url,layerName){
- const vm = this
- var projection = new Projection({
- code: 'EPSG:4326',
- units: 'degrees',
- axisOrientation: 'neu',
- global: true
- })
- vm.source = new TileWMS({
- url: url,
- params: {
- 'FORMAT': vm.format,
- 'VERSION': '1.1.1',
- tiled: true,
- "STYLES": '',
- "LAYERS": layerName,
- "exceptions": 'application/vnd.ogc.se_inimage',
- // tilesOrigin: -124.73142200000001 + "," + 24.955967
- },
- projection:projection
- })
- vm.layer = new Tile({
- source:vm.source
- })
- vm.map.addLayer(vm.layer)
- }
- /**
- * @description 添加WMS图层到地图
- * @memberof WMSLayer
- */
- add(){
- const vm = this
- if(!vm.state) {
- vm.map.addLayer(vm.layer)
- vm.state = true
- }
- }
- /**
- * @description 从当前地图中移除WMS图层
- * @memberof WMSLayer
- */
- remove() {
- const vm = this
- if(vm.state) {
- vm.map.removeLayer(vm.layer)
- vm.state = false
- }
- }
- /**
- * @description 显示WMS图层数据
- * @memberof WMSLayer
- */
- show(){
- const vm = this
- if(vm.state) {
- vm.layer.setVisible(true)
- }
- }
- /**
- * @description 隐藏WMS图层数据
- * @memberof WMSLayer
- */
- hide(){
- const vm = this
- if(vm.state) {
- vm.layer.setVisible(false)
- }
- }
- /**
- * @description 更新WMS图层内容
- * @param {String} data 属性过滤参数(例如:RoadNo=11),选填,不传值默认加载全部数据
- * @memberof WMSLayer
- */
- update(data) {
- const vm = this
- if(data!=null && data!="" && data!=undefined && state) {
- vm.source.updateParams({
- "CQL_FILTER": data
- })
- }
- else{
- vm.source.updateParams({
- "CQL_FILTER": null
- })
- }
- }
- /**
- * @description 缩放到过滤后的地图要素对应范围
- * @memberof WMSLayer
- */
- zoomTo() {
- const vm = this
- //组装查询过滤条件
- let cqlfilter = vm.source.getParams().CQL_FILTER
- if(cqlfilter == null){
- //缩放到图层范围
- }
- else{
- let cqlfilters = cqlfilter.split("=")
- //实现思路为通过WFS进行属性查询,查找到要素后再缩放到该要素
- let wfsurl = url.substr(0, url.lastIndexOf("/") + 1 ) + "wfs"
- let featureRequest = new WFS().writeGetFeature({
- //srsName: 'EPSG:3857',
- //featureNS: 'http://openstreemap.org',
- //featurePrefix: 'skyline',
- featureTypes: [layer],
- outputFormat: 'application/json',
- filter: filter.equalTo(cqlfilters[0],cqlfilters[1])
- })
- let myfilter = WFS.writeFilter(filter.equalTo(cqlfilters[0],cqlfilters[1]))
- $.ajax({
- type: 'get',
- url: wfsurl,
- data: {
- service: 'WFS',
- request: 'GetFeature',
- typeNames: layer,//查询图层
- filter:new XMLSerializer().serializeToString(myfilter),//查询条件
- outputFormat: 'application/json'
- },
- async : false,
- success: function(res) {
- let features = new GeoJSON().readFeatures(res)
- //let coordinateArray = new Array();
- let extentBound = new extent.createEmpty()
- for(let i=0; i<features.length; i++) {
- /*let coordinates = features[i].getGeometry().getCoordinates();//由多个分部构成,与Polyline函数生成的结构不一致
- for(j=0; j<coordinates.length; j++) {
- let pathcoordinates = coordinates[j];
- for(k = 0 ; k < pathcoordinates.length ; k ++)
- coordinateArray.push(pathcoordinates[k]);
- }*/
- extentBound = new extent.extend(extentBound,features[i].getGeometry().getExtent());
- }
- //let extentBound = new extent.boundingExtent(coordinateArray);
- if(features.length > 0){
- vm.map.getView().fit(extentBound,{
- duration: 1
- })
- }
- },
- error: function(e) {
- console.log("failed")
- }
- })
- /*fetch(wfsurl, {
- method: 'POST',
- body: new XMLSerializer().serializeToString(featureRequest)
- }).then(function(response) {
- return response.json();
- }).then(function(json) {
- let features = new GeoJSON().readFeatures(json);
- let extentBound = new extent.createEmpty();
- for(let i=0; i<features.length; i++) {
- extentBound = new extent.extend(extentBound,features[i].getGeometry().getExtent());
- }
- if(features.length > 0)
- {
- map.getView().fit(extentBound,{
- duration: 1
- });
- }
- });*/
- }
- }
-
- /**
- *@description 根据经纬度坐标查询坐标点所在位置的要素
- *@param coordinate LTMap.LngLat格式的经纬度 必填
- *@returns 查询到要素的结果集合,json数组格式,json中包含对应要素的所有字段名和字段值
- */
- getFeaturesByCoor(coordinate){
- const vm = this
- //将经纬度坐标转平面坐标
- coordinate = vm.mapInstance.lnglatToPixel(coordinate)
- //将LT平面坐标转换为OL的平面坐标
- coordinate = Common.LTMapPixel2MapPixel(coordinate)
- let resolution = vm.mapInstance.getResolution()
- let projection = vm.mapInstance.getProjection()
- let url = vm.source.getGetFeatureInfoUrl(coordinate, resolution, projection,
- {'INFO_FORMAT': 'application/json', 'FEATURE_COUNT': 10})
- let geojsonFormat = new GeoJSON({
- defaultDataProjection: "EPSG:3857"
- })
- let result = []
- if(url) {
- $.ajax({
- type: 'get',
- url: url,
- async : false,
- success: function(res) {
- //获取所有的要素
- let features = geojsonFormat.readFeatures(res)
- let jsonDatas = []
- for(let i = 0 ; i < features.length ; i ++){
- let jsonData = {}
- for(let key in features[i].getProperties()){
- if (key == 'geometry'){
- continue
- }
- jsonData[key] = features[i].get(key)
- }
- jsonDatas.push(jsonData)
- }
- result = jsonDatas
- },
- error:function(e) {
- console.log("faile")
- }
- })
- }
- return result
- }
- /**
- *@description 设置鼠标悬停在元素上时的样式
- *@param {String} cursorStyle 鼠标样式("default"默认指针,"pointer"小手,
- *"move"移动指针,"text"文本指针,
- *"wait"等待状态,"help"帮助)
- *必填,由于跨域问题,该方法存在问题
- */
- setFeatureCursor(cursorStyle){
- const vm = this
- cursorStyle = (cursorStyle == undefined)?"default":cursorStyle
- let mapContainer = vm.map.getTargetElement()
- let defaultCursor = mapContainer.style.cursor
- vm.map.on("pointermove",function(e){
- //let features = map.forEachFeatureAtPixel(e.pixel,function(feature) { return feature; })
- if (e.dragging){
- return
- }
- let pixel = vm.map.getEventPixel(e.originalEvent)
- let hit = vm.map.forEachLayerAtPixel(pixel,function(){
- return true
- })
- if(hit){
- mapContainer.style.cursor = cursorStyle
- }
- else{
- mapContainer.style.cursor = defaultCursor
- }
- })
- }
- }
- export default WMSLayer
|