@@ -5,151 +5,49 @@ import Feature from "ol/Feature";
5
5
import { Circle , Fill , RegularShape , Stroke , Style } from "ol/style" ;
6
6
import Text from "ol/style/Text" ;
7
7
8
- function setImage (
9
- symbol : string ,
10
- stroke : Stroke ,
11
- fill : Fill ,
12
- size : number
13
- ) : Circle | RegularShape {
14
- switch ( symbol . toLowerCase ( ) ) {
15
- case "circle" :
16
- default :
17
- return new Circle ( {
18
- fill : fill ,
19
- stroke : stroke ,
20
- radius : size ,
21
- } ) ;
22
- case "cross" :
23
- return new RegularShape ( {
24
- fill : fill ,
25
- stroke : stroke ,
26
- points : 4 ,
27
- radius : size ,
28
- radius2 : 0 ,
29
- angle : Math . PI / 4 ,
30
- } ) ;
31
- case "square" :
32
- return new RegularShape ( {
33
- fill : fill ,
34
- stroke : stroke ,
35
- points : 4 ,
36
- radius : size ,
37
- angle : Math . PI / 4 ,
38
- } ) ;
39
- case "star" :
40
- return new RegularShape ( {
41
- fill : fill ,
42
- stroke : stroke ,
43
- points : 5 ,
44
- radius : size ,
45
- radius2 : size - 5 ,
46
- angle : 0 ,
47
- } ) ;
48
- case "triangle" :
49
- return new RegularShape ( {
50
- fill : fill ,
51
- stroke : stroke ,
52
- points : 3 ,
53
- radius : size ,
54
- angle : 0 ,
55
- } ) ;
56
- }
57
- }
8
+ import stylePoint from "./style/point" ;
9
+ import styleLine from "./style/line" ;
10
+ import stylePolygon from "./style/polygon" ;
11
+ import text from "./style/text" ;
58
12
59
- export default function style (
13
+ export default function (
60
14
feature : Feature ,
61
15
labelColumn : string ,
62
- color : string | Color ,
16
+ layerColor : string | Color | null ,
63
17
resolution : number
64
18
) {
65
19
const type = feature . getGeometry ( ) . getType ( ) ;
66
20
const properties = feature . getProperties ( ) ;
67
21
68
- const fill = new Fill ( {
69
- color : "rgba(0,0,0,0.4)" ,
70
- } ) ;
71
- const stroke = new Stroke ( {
72
- color : "#000" ,
73
- width : 1.25 ,
74
- } ) ;
75
-
76
- // Color defined in layers settings.
77
- if ( color !== null ) {
78
- stroke . setColor ( color ) ;
79
-
80
- const fillColor = colorAsArray ( color ) ;
81
- fillColor [ 3 ] = type === "Point" || type === "MultiPoint" ? 0.7 : 0.4 ;
82
- fill . setColor ( fillColor ) ;
83
- }
84
- // Color from Feature properties.
85
- else if (
86
- typeof properties . color !== "undefined" &&
87
- properties . color !== null
88
- ) {
89
- stroke . setColor ( colorAsArray ( properties . color ) ) ;
90
-
91
- const fillColor = colorAsArray ( properties . color ) ;
92
- fillColor [ 3 ] = type === "Point" || type === "MultiPoint" ? 0.7 : 0.4 ;
93
- fill . setColor ( fillColor ) ;
94
- }
95
-
96
- const symbol = properties [ "marker-symbol" ] || "circle" ;
97
- const image = setImage ( symbol , stroke , fill , properties [ "marker-size" ] || 5 ) ;
98
-
99
- return [
100
- new Style ( {
101
- image,
102
- fill : fill ,
103
- stroke : stroke ,
104
- text : text ( feature , labelColumn ) ,
105
- } ) ,
106
- ] ;
107
- }
108
-
109
- function text ( feature : Feature , labelColumn : string ) {
110
- const type = feature . getGeometry ( ) . getType ( ) ;
111
- const properties = feature . getProperties ( ) ;
112
-
113
- const label =
114
- labelColumn !== null && typeof properties [ labelColumn ] !== "undefined"
115
- ? properties [ labelColumn ]
22
+ const color =
23
+ layerColor !== null
24
+ ? layerColor
25
+ : typeof properties . color !== "undefined"
26
+ ? properties . color
116
27
: null ;
117
28
118
- if ( label !== null ) {
119
- const textOptions = {
120
- stroke : new Stroke ( {
121
- color : "#fff" ,
122
- width : 2 ,
123
- } ) ,
124
- text : label . toString ( ) ,
125
- } ;
126
-
127
- switch ( type ) {
128
- case "Point" :
129
- case "MultiPoint" :
130
- Object . assign ( textOptions , {
131
- offsetY : 12 ,
132
- } ) ;
133
- break ;
29
+ let style = new Style ( ) ;
134
30
135
- case "LineString" :
136
- case "MultiLineString" :
137
- Object . assign ( textOptions , {
138
- overflow : true ,
139
- placement : "line" ,
140
- } ) ;
141
- break ;
31
+ switch ( type ) {
32
+ case "Point" :
33
+ case "MultiPoint" : {
34
+ const symbol = properties [ "marker-symbol" ] || null ;
35
+ const size = properties [ "marker-size" ] || null ;
142
36
143
- case "Polygon" :
144
- case "MultiPolygon" :
145
- Object . assign ( textOptions , {
146
- overflow : true ,
147
- } ) ;
148
- break ;
37
+ style = stylePoint ( color , size , symbol ) ;
38
+ break ;
149
39
}
150
-
151
- return new Text ( textOptions ) ;
40
+ case "LineString" :
41
+ case "MultiLineString" :
42
+ style = styleLine ( color , null , null ) ;
43
+ break ;
44
+ case "Polygon" :
45
+ case "MultiPolygon" :
46
+ style = stylePolygon ( color , null , null , color , null ) ;
47
+ break ;
152
48
}
153
49
154
- return null ;
50
+ style . setText ( text ( feature , labelColumn ) ) ;
51
+
52
+ return style ;
155
53
}
0 commit comments