|
| 1 | +import { MapGIS } from '../Base'; |
| 2 | +import { OGCService } from './OGCService'; |
| 3 | +import xml from 'fast-xml-parser'; |
| 4 | +import axios from 'axios'; |
| 5 | +import qs from 'qs'; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author 基础平台-潘卓然 |
| 9 | + * @class module:OGC服务.OGCService |
| 10 | + * @classdesc OGC服务类 |
| 11 | + * @description MapGIS.OGC.OGCService |
| 12 | + * @extends OGCService |
| 13 | + * @link https://www.ogc.org/standards/wfs |
| 14 | + * @param option - {Object} 属性键值对。<br> |
| 15 | + * @param {String} [option.url = ""] 必选。服务url |
| 16 | + * @param {String} [option.version = "1.1.1"] 请求的版本号,1.1.1或1.3.0,服务默认版本为1.1.1。(getWMSInfo,getFeatureInfo) |
| 17 | + */ |
| 18 | +class WFS extends OGCService { |
| 19 | + constructor(option) { |
| 20 | + var options = option || {}; |
| 21 | + super(options); |
| 22 | + this.url = options.url; |
| 23 | + this.featureTypes = []; |
| 24 | + this.layers = []; |
| 25 | + } |
| 26 | + |
| 27 | + isBaseUrl() { |
| 28 | + let valid = false; |
| 29 | + const name = 'WFSServer'; |
| 30 | + const length = this.url.length; |
| 31 | + const index = this.url.indexOf(name) + name.length; |
| 32 | + let fullurl; |
| 33 | + if (index === length) { |
| 34 | + fullurl = `${this.url}?request=GetCapabilities&service=WFS`; |
| 35 | + valid = true; |
| 36 | + } else if (index === length - 1) { |
| 37 | + fullurl = `${this.url}request=GetCapabilities&service=WFS`; |
| 38 | + valid = true; |
| 39 | + } |
| 40 | + return valid; |
| 41 | + } |
| 42 | + |
| 43 | + setVersion(version) { |
| 44 | + this.version = version; |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + setCrs(crs) { |
| 49 | + this.srsname = crs; |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + setName(name) { |
| 54 | + this.typename = name; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + setOutputFormat(format) { |
| 59 | + this.outputFormat = format; |
| 60 | + } |
| 61 | + |
| 62 | + async getCapabilities() { |
| 63 | + const name = 'WFSServer'; |
| 64 | + const length = this.url.length; |
| 65 | + const index = this.url.indexOf(name) + name.length; |
| 66 | + let fullurl = this.url; |
| 67 | + if (index === length) { |
| 68 | + fullurl = `${this.url}?request=GetCapabilities&service=WFS`; |
| 69 | + } else if (index === length - 1) { |
| 70 | + fullurl = `${this.url}request=GetCapabilities&service=WFS`; |
| 71 | + } |
| 72 | + let res = await axios.get(fullurl); |
| 73 | + let data = res.data; |
| 74 | + let obj = xml.getTraversalObj(data, {}); |
| 75 | + let json = xml.convertToJson(obj, {}); |
| 76 | + let version = json['wfs:WFS_Capabilities']['ows:ServiceIdentification']['ows:ServiceTypeVersion']; |
| 77 | + if (version === '1.1.0') { |
| 78 | + // MapGIS 10.3.5 10.5.0 |
| 79 | + this.featureTypes = json['wfs:WFS_Capabilities']['FeatureTypeList']['FeatureType']; |
| 80 | + let crs; |
| 81 | + let name = ''; |
| 82 | + this.featureTypes.forEach((f, i) => { |
| 83 | + if (i == 0) { |
| 84 | + crs = f['DefaultSRS']; |
| 85 | + name = `${f['Name']}`; |
| 86 | + } |
| 87 | + }); |
| 88 | + this.layers = this.featureTypes.map((f) => f['Name']); |
| 89 | + this.setName(name).setCrs(crs).setVersion(version).setOutputFormat('text/xml; subtype=gml/3.2'); |
| 90 | + } else if (version === '2.0.0') { |
| 91 | + let featureType = json['wfs:WFS_Capabilities']['wfs:FeatureTypeList']['wfs:FeatureType']; |
| 92 | + let crs = featureType['wfs:DefaultCRS']; |
| 93 | + let name = featureType['wfs:Name']; |
| 94 | + this.setName(name).setCrs(crs).setVersion(version); |
| 95 | + } |
| 96 | + return json; |
| 97 | + } |
| 98 | + |
| 99 | + async getFeature() { |
| 100 | + const name = 'WFSServer'; |
| 101 | + const length = this.url.length; |
| 102 | + const index = this.url.indexOf(name) + name.length; |
| 103 | + let fullurl = this.url; |
| 104 | + |
| 105 | + let { maxFeatures = 100, version = '1.1.0', typename = '', outputFormat, srsname = 'EPSG:4326', bbox } = this; |
| 106 | + let object = { |
| 107 | + service: 'WFS', |
| 108 | + request: 'GetFeature', |
| 109 | + version, |
| 110 | + typename, |
| 111 | + outputFormat, |
| 112 | + srsname, |
| 113 | + maxFeatures |
| 114 | + }; |
| 115 | + |
| 116 | + if (version === '1.1.0') { |
| 117 | + if (bbox) { |
| 118 | + object = { ...object, bbox: `${bbox},${srsname}` }; |
| 119 | + } else { |
| 120 | + object = { ...object }; |
| 121 | + } |
| 122 | + } else if (version === '2.0.0') { |
| 123 | + object = { ...object, Envelope: `${bbox},${srsname}` }; |
| 124 | + } |
| 125 | + |
| 126 | + if (index === length) { |
| 127 | + fullurl = `${this.url}?${qs.stringify(object)}`; |
| 128 | + } else if (index === length - 1) { |
| 129 | + fullurl = `${this.url}${qs.stringify(object)}`; |
| 130 | + } |
| 131 | + |
| 132 | + let res = await axios.get(fullurl); |
| 133 | + let data = res.data; |
| 134 | + let obj = xml.getTraversalObj(data, {}); |
| 135 | + let json = xml.convertToJson(obj, {}); |
| 136 | + |
| 137 | + let result = { |
| 138 | + geojson: undefined, |
| 139 | + xml: json |
| 140 | + }; |
| 141 | + let geojson; |
| 142 | + switch (version) { |
| 143 | + case '1.1.0': |
| 144 | + geojson = convert |
| 145 | + break; |
| 146 | + case '2.0.0': |
| 147 | + break; |
| 148 | + } |
| 149 | + return result; |
| 150 | + } |
| 151 | +} |
| 152 | +export { WFS }; |
| 153 | +MapGIS.OGC.WFS = WFS; |
0 commit comments