Skip to content

Commit b2227d3

Browse files
committed
【feature】1) webmap ol提交点符号支持偏移和旋转
2) webmap ol点标签和线标签支持位置设置 3) webmap ol标签增加阴影颜色 (reviewed by chengl)
1 parent f39a404 commit b2227d3

File tree

2 files changed

+147
-60
lines changed

2 files changed

+147
-60
lines changed

src/openlayers/core/StyleUtils.js

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ export class StyleUtils {
610610
lineCap,
611611
src,
612612
scale,
613+
offsetX,
614+
offsetY,
613615
//size,
614616
//imgSize,
615617
anchor
@@ -647,14 +649,15 @@ export class StyleUtils {
647649
}
648650
} else {
649651
newImage = new CircleStyle({
650-
radius: radius,
652+
radius,
651653
fill: new FillStyle({
652654
color: fillColorArray
653655
}),
654656
stroke: new StrokeStyle({
655657
width: strokeWidth || ZERO,
656658
color: strokeColorArray
657-
})
659+
}),
660+
displacement: this.getCircleDisplacement(radius, offsetX, offsetY)
658661
});
659662
}
660663
olStyle.setImage(newImage);
@@ -692,6 +695,44 @@ export class StyleUtils {
692695
return olStyle;
693696
}
694697

698+
/**
699+
* @function ol.supermap.StyleUtils.getIconAnchor
700+
* @description 获取图标的锚点
701+
* @param {number} offsetX - X方向偏移分数
702+
* @param {number} offsetY - Y方向偏移分数
703+
* @returns {array}
704+
*/
705+
static getIconAnchor(offsetX, offsetY) {
706+
return [offsetX, offsetY];
707+
}
708+
/**
709+
* @function ol.supermap.StyleUtils.getCircleDisplacement
710+
* @description 获取圆圈的偏移
711+
* @param {number} radius - 圆圈半径
712+
* @param {number} offsetX - X方向偏移分数
713+
* @param {number} offsetY - Y方向偏移分数
714+
* @returns {array}
715+
*/
716+
static getCircleDisplacement(radius, offsetX, offsetY) {
717+
const dispX = radius*offsetX, dispY = radius*offsetY;
718+
return [dispX, -dispY];
719+
}
720+
/**
721+
* @function ol.supermap.StyleUtils.getTextOffset
722+
* @description 获取字体图标的偏移值
723+
* @param {string} fontSize - 字体大小,如12px
724+
* @param {number} offsetX - X方向偏移分数
725+
* @param {number} offsetY - Y方向偏移分数
726+
* @returns {object}
727+
*/
728+
static getTextOffset(fontSize, offsetX, offsetY) {
729+
const radius = fontSize.substr(0, fontSize.length - 2) / 2;
730+
return {
731+
x: radius*offsetX,
732+
y: radius*offsetY
733+
};
734+
}
735+
695736
/**
696737
* 获取文字标注对应的canvas
697738
* @param style
@@ -1033,23 +1074,28 @@ export class StyleUtils {
10331074

10341075
let fontSize = isRank ? 2 * parameters.radius + "px" : parameters.fontSize;
10351076

1077+
const {offsetX, offsetY, rotation} = parameters;
1078+
const offset = StyleUtils.getTextOffset(fontSize, offsetX, offsetY);
10361079
return new Style({
1037-
text: new Text({
1038-
text: text,
1039-
font: fontSize + " supermapol-icons",
1040-
placement: 'point',
1041-
textAlign: 'center',
1042-
fill: new FillStyle({
1043-
color: fillColor
1044-
}),
1045-
backgroundFill: new FillStyle({
1046-
color: [0, 0, 0, 0]
1047-
}),
1048-
stroke: new StrokeStyle({
1049-
width: parameters.strokeWidth || 0.000001,
1050-
color: strokeColor
1080+
text: new Text({
1081+
text: text,
1082+
font: fontSize + " supermapol-icons",
1083+
placement: 'point',
1084+
textAlign: 'center',
1085+
fill: new FillStyle({
1086+
color: fillColor
1087+
}),
1088+
backgroundFill: new FillStyle({
1089+
color: [0, 0, 0, 0]
1090+
}),
1091+
stroke: new StrokeStyle({
1092+
width: parameters.strokeWidth || 0.000001,
1093+
color: strokeColor
1094+
}),
1095+
offsetX: offset.x,
1096+
offsetY: offset.y,
1097+
rotation
10511098
})
1052-
})
10531099
});
10541100
}
10551101
/**
@@ -1063,14 +1109,18 @@ export class StyleUtils {
10631109
that.svgDiv = document.createElement('div');
10641110
document.body.appendChild(that.svgDiv);
10651111
}
1066-
StyleUtils.getCanvasFromSVG(styleParams.url, that.svgDiv, function (canvas) {
1112+
const { url, radius, offsetX, offsetY, fillOpacity, rotation } = styleParams;
1113+
let anchor = this.getIconAnchor(offsetX, offsetY);
1114+
StyleUtils.getCanvasFromSVG(url, that.svgDiv, function (canvas) {
10671115
style = new Style({
10681116
image: new Icon({
10691117
img: that.setColorToCanvas(canvas, styleParams),
1070-
scale: styleParams.radius / canvas.width,
1118+
scale: 2 * radius / canvas.width,
10711119
imgSize: [canvas.width, canvas.height],
1072-
anchor: [0.5, 0.5],
1073-
opacity: styleParams.fillOpacity
1120+
anchor: anchor || [0.5, 0.5],
1121+
opacity: fillOpacity,
1122+
anchorOrigin: 'bottom-right',
1123+
rotation
10741124
})
10751125
});
10761126
});
@@ -1111,14 +1161,17 @@ export class StyleUtils {
11111161
//要组装成完整的url
11121162
imgDom.src = imageInfo.url;
11131163
}
1164+
const { offsetX, offsetY, rotation } = styleParams;
1165+
let anchor = this.getIconAnchor(offsetX, offsetY);
11141166
return new Style({
11151167
image: new Icon({
11161168
img: imgDom,
11171169
scale,
11181170
imgSize: [size.w, size.h],
1119-
anchor: [0.5, 0.5]
1171+
anchor: anchor || [0.5, 0.5],
1172+
anchorOrigin: 'bottom-right',
1173+
rotation
11201174
})
11211175
});
11221176
}
1123-
11241177
}

src/openlayers/mapping/WebMap.js

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import * as olSource from 'ol/source';
4040
import Feature from 'ol/Feature';
4141
import Style from 'ol/style/Style';
4242
import FillStyle from 'ol/style/Fill';
43+
import StrokeStyle from 'ol/style/Stroke';
4344
import Text from 'ol/style/Text';
4445
import Collection from 'ol/Collection';
4546
import { containsCoordinate, getCenter } from "ol/extent";
@@ -1112,6 +1113,25 @@ export class WebMap extends Observable {
11121113
that.errorCallback && that.errorCallback(error, 'getWmtsFaild', that.map)
11131114
});
11141115
}
1116+
/**
1117+
* @private
1118+
* @function ol.supermap.WebMap.prototype.getWMTSUrl
1119+
* @description 获取wmts请求文档的url
1120+
* @param {string} url - 图层信息。
1121+
* @param {boolean} isKvp - 是否为kvp模式
1122+
*/
1123+
getWMTSUrl(url, isKvp) {
1124+
let splitStr = '?';
1125+
if (url.indexOf('?') > -1) {
1126+
splitStr = '&'
1127+
}
1128+
if (isKvp) {
1129+
url += splitStr + 'SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetCapabilities';
1130+
} else {
1131+
url += splitStr + '/1.0.0/WMTSCapabilities.xml';
1132+
}
1133+
return this.getRequestUrl(url);
1134+
}
11151135

11161136
/**
11171137
* @private
@@ -1126,7 +1146,8 @@ export class WebMap extends Observable {
11261146
withCredentials: true,
11271147
withoutFormatSuffix: true
11281148
};
1129-
return FetchRequest.get(that.getRequestUrl(layerInfo.url), null, options).then(function (response) {
1149+
const isKvp = !layerInfo.requestEncoding || layerInfo.requestEncoding === 'KVP';
1150+
return FetchRequest.get(that.getWMTSUrl(layerInfo.url, isKvp), null, options).then(function (response) {
11301151
return response.text();
11311152
}).then(function (capabilitiesText) {
11321153
const format = new WMTSCapabilities();
@@ -1174,8 +1195,7 @@ export class WebMap extends Observable {
11741195
} else {
11751196
extent = olProj.get(that.baseProjection).getExtent()
11761197
}
1177-
const isKvp = !layerInfo.requestEncoding || layerInfo.requestEncoding === 'KVP';
1178-
layerInfo.tileUrl = that.getTileUrl(capabilities.OperationsMetadata.GetTile.DCP.HTTP.Get, isKvp, layerInfo.layer, layerInfo.tileMatrixSet);
1198+
layerInfo.tileUrl = that.getTileUrl(capabilities.OperationsMetadata.GetTile.DCP.HTTP.Get, layer, layerFormat, isKvp);
11791199
//将需要的参数补上
11801200
layerInfo.extent = extent;
11811201
layerInfo.matrixSet = matrixSet;
@@ -1203,22 +1223,23 @@ export class WebMap extends Observable {
12031223
* @function ol.supermap.WebMap.prototype.getTileUrl
12041224
* @description 获取wmts的图层参数。
12051225
* @param {array} getTileArray - 图层信息。
1206-
* @param {boolean} isKvp - 是否是kvp方式
12071226
* @param {string} layer - 选择的图层
1208-
* @param {string} matrixSet -选择比例尺
1209-
*/
1210-
getTileUrl(getTileArray, isKvp, layer, matrixSet) {
1211-
let url, type = isKvp ? 'KVP' : 'RESTful';
1212-
getTileArray.forEach(data => {
1213-
if(data.Constraint[0].AllowedValues.Value[0].toUpperCase() === type.toUpperCase()) {
1214-
url = data.href;
1215-
}
1216-
})
1217-
if(!isKvp) {
1218-
//Restful格式
1219-
url = `${url}${layer}/default/${matrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png`;
1220-
//supermap iserver发布的restful会有一个?
1221-
url = url.replace('?', '/');
1227+
* @param {string} format - 选择的出图方式
1228+
* @param {boolean} isKvp - 是否是kvp方式
1229+
*/
1230+
getTileUrl(getTileArray, layer, format, isKvp) {
1231+
let url;
1232+
if(isKvp) {
1233+
getTileArray.forEach(data => {
1234+
if(data.Constraint[0].AllowedValues.Value[0].toUpperCase() === 'KVP') {
1235+
url = data.href;
1236+
}
1237+
})
1238+
} else {
1239+
const reuslt = layer.ResourceURL.filter(resource => {
1240+
return resource.format === format;
1241+
})
1242+
url = reuslt[0].template;
12221243
}
12231244
return url;
12241245
}
@@ -2410,28 +2431,41 @@ export class WebMap extends Observable {
24102431
*/
24112432
getLabelStyle(parameters, layerInfo) {
24122433
let style = layerInfo.style || layerInfo.pointStyle;
2413-
let radius = style.radius || 0;
2414-
let strokeWidth = style.strokeWidth || 0;
2415-
let offsetY = -1.8 * radius - strokeWidth;
2416-
if (offsetY > -20) {
2417-
offsetY = -20;
2434+
const {radius=0, strokeWidth=0} = style,
2435+
beforeOffsetY = -(radius + strokeWidth);
2436+
const {
2437+
fontSize = '14px',
2438+
fontFamily,
2439+
fill,
2440+
backgroundFill,
2441+
offsetX = 0,
2442+
offsetY = beforeOffsetY,
2443+
placement = "point",
2444+
textBaseline = "bottom",
2445+
textAlign='center',
2446+
outlineColor = "#000000",
2447+
outlineWidth = 0
2448+
} = parameters;
2449+
const option = {
2450+
font: `${fontSize} ${fontFamily}`,
2451+
placement,
2452+
textBaseline,
2453+
textAlign,
2454+
fill: new FillStyle({ color: fill }),
2455+
backgroundFill: new FillStyle({ color: backgroundFill }),
2456+
padding: [3, 3, 3, 3],
2457+
offsetX: layerInfo.featureType === 'POINT' ? offsetX : 0,
2458+
offsetY: layerInfo.featureType === 'POINT' ? offsetY : 0
2459+
};
2460+
if (outlineWidth > 0) {
2461+
option.stroke = new StrokeStyle({
2462+
color: outlineColor,
2463+
width: outlineWidth
2464+
});
24182465
}
2419-
parameters.offsetY = offsetY;
24202466

24212467
return new Style({
2422-
text: new Text({
2423-
font: `${parameters.fontSize || '14px'} ${parameters.fontFamily}`,
2424-
placement: 'point',
2425-
textAlign: 'center',
2426-
fill: new FillStyle({
2427-
color: parameters.fill
2428-
}),
2429-
backgroundFill: new FillStyle({
2430-
color: parameters.backgroundFill || [255, 255, 255, 0.7]
2431-
}),
2432-
padding: [3, 3, 3, 3],
2433-
offsetY: parameters.offsetY
2434-
})
2468+
text: new Text(option)
24352469
});
24362470
}
24372471

0 commit comments

Comments
 (0)