【3】面

150 阅读12分钟

简单多边形

此示例显示用蓝色填充的多边形。

../../../_images/polygon_simplepolygon.png

简单多边形

法典

View and download the full "Simple polygon" SLD

1    <FeatureTypeStyle>
2      <Rule>
3        <PolygonSymbolizer>
4          <Fill>
5            <CssParameter name="fill">#000080</CssParameter>
6          </Fill>
7        </PolygonSymbolizer>
8      </Rule>
9    </FeatureTypeStyle>

这种风格有一合一,这是最简单的情况。(除非另有说明,否则所有后续示例都将共享此特征。设置多边形的样式是通过  (第 3-7 行) 完成的。第 5 行指定深蓝色 () 作为多边形的填充颜色。<Rule>``<FeatureTypeStyle>``<PolygonSymbolizer>``#000080

注意

 图中多边形

周围的浅色边框是由相邻多边形引起的渲染器的伪影。这种样式没有边框。

带描边的简单多边形

本示例向“简单多边形”示例添加一个 2 像素的白色描边。

../../../_images/polygon_simplepolygonwithstroke.png

带描边的简单多边形

法典

View and download the full "Simple polygon with stroke" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <CssParameter name="fill">#000080</CssParameter>
 6          </Fill>
 7          <Stroke>
 8            <CssParameter name="stroke">#FFFFFF</CssParameter>
 9            <CssParameter name="stroke-width">2</CssParameter>
10          </Stroke>
11        </PolygonSymbolizer>
12      </Rule>
13    </FeatureTypeStyle>

此示例类似于上面的简单多边形示例,但添加了标记(第 7-10 行)。第 8 行将描边的颜色设置为白色 (),第 9 行将描边的宽度设置为 2 像素。<Stroke>``#FFFFFF

透明多边形

本示例以“带描边的简单多边形”示例为基础,通过将不透明度设置为 50% 使填充部分透明。

../../../_images/polygon_transparentpolygon.png

Transparent polygon

Code

View and download the full "Transparent polygon" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <CssParameter name="fill">#000080</CssParameter>
 6            <CssParameter name="fill-opacity">0.5</CssParameter>
 7          </Fill>
 8          <Stroke>
 9            <CssParameter name="stroke">#FFFFFF</CssParameter>
10            <CssParameter name="stroke-width">2</CssParameter>
11          </Stroke>
12        </PolygonSymbolizer>
13      </Rule>
14    </FeatureTypeStyle>

Details

This example is similar to the Simple polygon with stroke example, save for defining the fill’s opacity in line 6. The value of 0.5 results in partially transparent fill that is 50% opaque. An opacity value of 1 would draw the fill as 100% opaque, while an opacity value of 0 would result in a completely transparent (0% opaque) fill. In this example, since the background is white, the dark blue looks lighter. Were the points imposed on a dark background, the resulting color would be darker.

Offset inner lines

Shows how to draw inner buffer lines inside a polygon.

../../../_images/polygon_offset.png

Offset buffer

Code

View and download the full "Inner offset lines" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Stroke>
 5            <CssParameter name="stroke">#000000</CssParameter>
 6            <CssParameter name="stroke-width">2</CssParameter>
 7          </Stroke>
 8        </PolygonSymbolizer>
 9        <LineSymbolizer>
10          <Stroke>
11            <CssParameter name="stroke">#AAAAAA</CssParameter>
12            <CssParameter name="stroke-width">3</CssParameter>
13          </Stroke>
14          <PerpendicularOffset>-2</PerpendicularOffset>
15        </LineSymbolizer>
16      </Rule>
17    </FeatureTypeStyle>

Details

This example is similar to the Simple polygon with stroke example, save for defining adding a at line 9, where a light gray (line 11) 3 pixels wide (line 12) line is drawn as a inner buffer inside the polygon. Line 14 controls the buffering distance, setting a inner buffer of 2 pixels.<LineSymbolizer>>

Graphic fill

This example fills the polygons with a tiled graphic.

../../../_images/polygon_graphicfill.png

Graphic fill

Code

View and download the full "Graphic fill" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <GraphicFill>
 6              <Graphic>
 7                <ExternalGraphic>
 8                  <OnlineResource
 9                    xlink:type="simple"
10                    xlink:href="colorblocks.png" />
11                  <Format>image/png</Format>
12                </ExternalGraphic>
13              <Size>93</Size>
14              </Graphic>
15            </GraphicFill>
16          </Fill>
17        </PolygonSymbolizer>
18      </Rule>
19    </FeatureTypeStyle>

Details

This style fills the polygon with a tiled graphic. This is known as an in SLD, to distinguish it from commonly-used shapes such as squares and circles that are “internal” to the renderer. Lines 7-12 specify details for the graphic, with line 10 setting the path and file name of the graphic and line 11 indicating the file format (MIME type) of the graphic (). Although a full URL could be specified if desired, no path information is necessary in line 11 because this graphic is contained in the same directory as the SLD. Line 13 determines the height of the displayed graphic in pixels; if the value differs from the height of the graphic then it will be scaled accordingly while preserving the aspect ratio.<ExternalGraphic>``image/png

../../../_images/colorblocks.png

Graphic used for fill

Hatching fill

This example fills the polygons with a hatching pattern.

Note

 

This example leverages an SLD extension in GeoServer. Hatching is not part of the standard SLD 1.0 specification.

../../../_images/polygon_hatchingfill.png

Hatching fill

Code

View and download the full "Hatching fill" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <GraphicFill>
 6              <Graphic>
 7                <Mark>
 8                  <WellKnownName>shape://times</WellKnownName>
 9                  <Stroke>
10                    <CssParameter name="stroke">#990099</CssParameter>
11                    <CssParameter name="stroke-width">1</CssParameter>
12                  </Stroke>
13                </Mark>
14                <Size>16</Size>
15              </Graphic>
16            </GraphicFill>
17          </Fill>
18        </PolygonSymbolizer>
19      </Rule>
20    </FeatureTypeStyle>

Details

In this example, there is a tag as in the Graphic fill example, but a (lines 7-13) is used instead of an . Line 8 specifies a “times” symbol (an “x”) be tiled throughout the polygon. Line 10 sets the color to purple (), line 11 sets the width of the hatches to 1 pixel, and line 14 sets the size of the tile to 16 pixels. Because hatch tiles are always square, the sets both the width and the height.<GraphicFill>``<Mark>``<ExternalGraphic>``#990099``<Size>

Polygon with default label

This example shows a text label on the polygon. In the absence of any other customization, this is how a label will be displayed.

../../../_images/polygon_polygonwithdefaultlabel.png

Polygon with default label

Code

View and download the full "Polygon with default label" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <CssParameter name="fill">#40FF40</CssParameter>
 6          </Fill>
 7          <Stroke>
 8            <CssParameter name="stroke">#FFFFFF</CssParameter>
 9            <CssParameter name="stroke-width">2</CssParameter>
10          </Stroke>
11        </PolygonSymbolizer>
12        <TextSymbolizer>
13          <Label>
14            <ogc:PropertyName>name</ogc:PropertyName>
15          </Label>
16        </TextSymbolizer>
17      </Rule>
18    </FeatureTypeStyle>

Details

In this example there is a and a . Lines 3-11 comprise the . The fill of the polygon is set on line 5 to a light green () while the stroke of the polygon is set on lines 8-9 to white () with a thickness of 2 pixels. The label is set in the on lines 12-16, with line 14 determining what text to display, in this case the value of the “name” attribute. (Refer to the attribute table in the Example polygons layer section if necessary.) All other details about the label are set to the renderer default, which here is Times New Roman font, font color black, and font size of 10 pixels.<PolygonSymbolizer>``<TextSymbolizer>``<PolygonSymbolizer>``#40FF40``#FFFFFF``<TextSymbolizer>

Label halo

This example alters the look of the Polygon with default label by adding a white halo to the label.

../../../_images/polygon_labelhalo.png

Label halo

Code

View and download the full "Label halo" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <CssParameter name="fill">#40FF40</CssParameter>
 6          </Fill>
 7          <Stroke>
 8            <CssParameter name="stroke">#FFFFFF</CssParameter>
 9            <CssParameter name="stroke-width">2</CssParameter>
10          </Stroke>
11        </PolygonSymbolizer>
12        <TextSymbolizer>
13          <Label>
14            <ogc:PropertyName>name</ogc:PropertyName>
15          </Label>
16          <Halo>
17            <Radius>3</Radius>
18            <Fill>
19              <CssParameter name="fill">#FFFFFF</CssParameter>
20            </Fill>
21          </Halo>
22        </TextSymbolizer>
23      </Rule>
24    </FeatureTypeStyle>

Details

This example is similar to the Polygon with default label, with the addition of a halo around the labels on lines 16-21. A halo creates a color buffer around the label to improve label legibility. Line 17 sets the radius of the halo, extending the halo 3 pixels around the edge of the label, and line 19 sets the color of the halo to white (). Since halos are most useful when set to a sharp contrast relative to the text color, this example uses a white halo around black text to ensure optimum readability.#FFFFFF

Polygon with styled label

This example improves the label style from the Polygon with default label example by centering the label on the polygon, specifying a different font name and size, and setting additional label placement optimizations.

Note

 

The label placement optimizations discussed below (the <VendorOption> tags) are SLD extensions that are custom to GeoServer. They are not part of the SLD 1.0 specification.

../../../_images/polygon_polygonwithstyledlabel.png

Polygon with styled label

Code

View and download the full "Polygon with styled label" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <PolygonSymbolizer>
 4          <Fill>
 5            <CssParameter name="fill">#40FF40</CssParameter>
 6          </Fill>
 7          <Stroke>
 8            <CssParameter name="stroke">#FFFFFF</CssParameter>
 9            <CssParameter name="stroke-width">2</CssParameter>
10          </Stroke>
11        </PolygonSymbolizer>
12        <TextSymbolizer>
13          <Label>
14            <ogc:PropertyName>name</ogc:PropertyName>
15          </Label>
16          <Font>
17            <CssParameter name="font-family">Arial</CssParameter>
18            <CssParameter name="font-size">11</CssParameter>
19            <CssParameter name="font-style">normal</CssParameter>
20            <CssParameter name="font-weight">bold</CssParameter>
21          </Font>
22          <LabelPlacement>
23            <PointPlacement>
24              <AnchorPoint>
25                <AnchorPointX>0.5</AnchorPointX>
26                <AnchorPointY>0.5</AnchorPointY>
27              </AnchorPoint>
28            </PointPlacement>
29          </LabelPlacement>
30          <Fill>
31            <CssParameter name="fill">#000000</CssParameter>
32          </Fill>
33          <VendorOption name="autoWrap">60</VendorOption>
34          <VendorOption name="maxDisplacement">150</VendorOption>
35        </TextSymbolizer>
36      </Rule>
37    </FeatureTypeStyle>

Details

This example is similar to the Polygon with default label example, with additional styling options within the on lines 12-35Lines 16-21 set the font styling. Line 17 sets the font family to be Arial, line 18 sets the font size to 11 pixels, line 19 sets the font style to “normal” (as opposed to “italic” or “oblique”), and line 20 sets the font weight to “bold” (as opposed to “normal”).<TextSymbolizer>

The tag on lines 22-29 affects where the label is placed relative to the centroid of the polygon. Line 21 centers the label by positioning it 50% (or 0.5) of the way horizontally along the centroid of the polygon. Line 22 centers the label vertically in exactly the same way.<LabelPlacement>

Finally, there are two added touches for label placement optimization: line 33 ensures that long labels are split across multiple lines by setting line wrapping on the labels to 60 pixels, and line 34 allows the label to be displaced by up to 150 pixels. This ensures that labels are compacted and less likely to spill over polygon boundaries. Notice little Massive County in the corner, whose label is now displayed.”

Attribute-based polygon

This example styles the polygons differently based on the “pop” (Population) attribute.

../../../_images/polygon_attributebasedpolygon.png

Attribute-based polygon

Code

View and download the full "Attribute-based polygon" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <Name>SmallPop</Name>
 4        <Title>Less Than 200,000</Title>
 5        <ogc:Filter>
 6          <ogc:PropertyIsLessThan>
 7            <ogc:PropertyName>pop</ogc:PropertyName>
 8            <ogc:Literal>200000</ogc:Literal>
 9          </ogc:PropertyIsLessThan>
10        </ogc:Filter>
11        <PolygonSymbolizer>
12          <Fill>
13            <CssParameter name="fill">#66FF66</CssParameter>
14          </Fill>
15        </PolygonSymbolizer>
16      </Rule>
17      <Rule>
18        <Name>MediumPop</Name>
19        <Title>200,000 to 500,000</Title>
20        <ogc:Filter>
21          <ogc:And>
22            <ogc:PropertyIsGreaterThanOrEqualTo>
23              <ogc:PropertyName>pop</ogc:PropertyName>
24              <ogc:Literal>200000</ogc:Literal>
25            </ogc:PropertyIsGreaterThanOrEqualTo>
26            <ogc:PropertyIsLessThan>
27              <ogc:PropertyName>pop</ogc:PropertyName>
28              <ogc:Literal>500000</ogc:Literal>
29            </ogc:PropertyIsLessThan>
30          </ogc:And>
31        </ogc:Filter>
32        <PolygonSymbolizer>
33          <Fill>
34            <CssParameter name="fill">#33CC33</CssParameter>
35          </Fill>
36        </PolygonSymbolizer>
37      </Rule>
38      <Rule>
39        <Name>LargePop</Name>
40        <Title>Greater Than 500,000</Title>
41        <ogc:Filter>
42          <ogc:PropertyIsGreaterThan>
43            <ogc:PropertyName>pop</ogc:PropertyName>
44            <ogc:Literal>500000</ogc:Literal>
45          </ogc:PropertyIsGreaterThan>
46        </ogc:Filter>
47        <PolygonSymbolizer>
48          <Fill>
49            <CssParameter name="fill">#009900</CssParameter>
50          </Fill>
51        </PolygonSymbolizer>
52      </Rule>
53    </FeatureTypeStyle>

Details

Note

 

Refer to the Example polygons layer to see the attributes for the layer. This example has eschewed labels in order to simplify the style, but you can refer to the example Polygon with styled label to see which attributes correspond to which polygons.

Each polygon in our fictional country has a population that is represented by the population (“pop”) attribute. This style contains three rules that alter the fill based on the value of “pop” attribute, with smaller values yielding a lighter color and larger values yielding a darker color.

The three rules are designed as follows:

Rule orderRule namePopulation (“pop”)Color
1SmallPopLess than 200,000#66FF66
2MediumPop200,000 to 500,000#33CC33
3LargePopGreater than 500,000#009900

The order of the rules does not matter in this case, since each shape is only rendered by a single rule.

The first rule, on lines 2-16, specifies the styling of polygons whose population attribute is less than 200,000. Lines 5-10 set this filter, with lines 6-9 setting the “less than” filter, line 7 denoting the attribute (“pop”), and line 8 the value of 200,000. The color of the polygon fill is set to a light green () on line 13.#66FF66

The second rule, on lines 17-37, is similar, specifying a style for polygons whose population attribute is greater than or equal to 200,000 but less than 500,000. The filter is set on lines 20-31. This filter is longer than in the first rule because two criteria need to be specified instead of one: a “greater than or equal to” and a “less than” filter. Notice the on line 21 and line 30. This mandates that both filters need to be true for the rule to be applicable. The color of the polygon fill is set to a medium green on () on line 34.And``#33CC33

The third rule, on lines 38-52, specifies a style for polygons whose population attribute is greater than or equal to 500,000. The filter is set on lines 41-46. The color of the polygon fill is the only other difference in this rule, which is set to a dark green () on line 49.#009900

Zoom-based polygon

This example alters the style of the polygon at different zoom levels.

../../../_images/polygon_zoombasedpolygonlarge.png

Zoom-based polygon: Zoomed in

../../../_images/polygon_zoombasedpolygonmedium.png

Zoom-based polygon: Partially zoomed

../../../_images/polygon_zoombasedpolygonsmall.png

Zoom-based polygon: Zoomed out

Code

View and download the full "Zoom-based polygon" SLD

 1    <FeatureTypeStyle>
 2      <Rule>
 3        <Name>Large</Name>
 4        <MaxScaleDenominator>100000000</MaxScaleDenominator>
 5        <PolygonSymbolizer>
 6          <Fill>
 7            <CssParameter name="fill">#0000CC</CssParameter>
 8          </Fill>
 9          <Stroke>
10            <CssParameter name="stroke">#000000</CssParameter>
11            <CssParameter name="stroke-width">7</CssParameter>
12          </Stroke>
13        </PolygonSymbolizer>
14        <TextSymbolizer>
15          <Label>
16            <ogc:PropertyName>name</ogc:PropertyName>
17          </Label>
18          <Font>
19            <CssParameter name="font-family">Arial</CssParameter>
20            <CssParameter name="font-size">14</CssParameter>
21            <CssParameter name="font-style">normal</CssParameter>
22            <CssParameter name="font-weight">bold</CssParameter>
23          </Font>
24          <LabelPlacement>
25            <PointPlacement>
26              <AnchorPoint>
27                <AnchorPointX>0.5</AnchorPointX>
28                <AnchorPointY>0.5</AnchorPointY>
29              </AnchorPoint>
30            </PointPlacement>
31          </LabelPlacement>
32          <Fill>
33            <CssParameter name="fill">#FFFFFF</CssParameter>
34          </Fill>
35        </TextSymbolizer>
36      </Rule>
37      <Rule>
38        <Name>Medium</Name>
39        <MinScaleDenominator>100000000</MinScaleDenominator>
40        <MaxScaleDenominator>200000000</MaxScaleDenominator>
41        <PolygonSymbolizer>
42          <Fill>
43            <CssParameter name="fill">#0000CC</CssParameter>
44          </Fill>
45          <Stroke>
46            <CssParameter name="stroke">#000000</CssParameter>
47            <CssParameter name="stroke-width">4</CssParameter>
48          </Stroke>
49        </PolygonSymbolizer>
50      </Rule>
51      <Rule>
52        <Name>Small</Name>
53        <MinScaleDenominator>200000000</MinScaleDenominator>
54        <PolygonSymbolizer>
55          <Fill>
56            <CssParameter name="fill">#0000CC</CssParameter>
57          </Fill>
58          <Stroke>
59            <CssParameter name="stroke">#000000</CssParameter>
60            <CssParameter name="stroke-width">1</CssParameter>
61          </Stroke>
62        </PolygonSymbolizer>
63      </Rule>
64    </FeatureTypeStyle>

Details

It is often desirable to make shapes larger at higher zoom levels when creating a natural-looking map. This example varies the thickness of the lines according to the zoom level. Polygons already do this by nature of being two dimensional, but another way to adjust styling of polygons based on zoom level is to adjust the thickness of the stroke (to be larger as the map is zoomed in) or to limit labels to only certain zoom levels. This is ensures that the size and quantity of strokes and labels remains legible and doesn’t overshadow the polygons themselves.

缩放级别(或更准确地说,比例分母)是指地图的比例。比例分母为 10,000 表示地图的比例为 1:10,000(以地图投影为单位)。

注意

 

确定要使用的适当比例分母(缩放级别)超出了此示例的范围。

此样式包含三个规则,定义如下:

规则顺序规则名称刻度分母描边宽度标签显示?
11:100,000,000以下7是的
2中等1:100,000,000 至 1:200,000,0004
3大于 1:200,000,0002

第一条规则,在第 2-36 行,用于最小比例分母,对应于视图“放大”的时间。比例规则设置在第 40 行,以便仅当比例分母为 100,000,000 或更小时,该规则才适用。第 7 行将填充定义为蓝色 ()。请注意,无论比例分母如何,填充在所有规则中都保持不变。与具有默认标签的多边形或具有样式标注的多边形示例一样,该规则还包含第 14-35 行,用于在多边形顶部绘制文本标签。第 19-22 行将字体信息设置为 Arial、14 像素和粗体,不带斜体。通过设置 0-5 行上的 50.27(或 28%),标签沿多边形的质心水平和垂直居中。最后,字体的颜色在第 33 行设置为白色 ()。#0000CC``<TextSymbolizer>``<AnchorPointX>``<AnchorPointY>``#FFFFFF

第二条规则在第 37-50 行,用于中间比例分母,对应于视图“部分缩放”的时间。第 39-40 行上的比例规则设置了该规则,使其将应用于比例分母介于 100,000,000 和 200,000,000 之间的任何地图。(包含和排除,因此正好 200,000,000 的缩放级别在这里适用。除了比例之外,此规则与第一个规则之间还有两个区别:第 4 行的描边宽度设置为 47 像素,并且不存在 a,因此不会显示任何标签。<MinScaleDenominator>``<MaxScaleDenominator>``<TextSymbolizer>

第三条规则位于第 51-63 行,用于最大比例分母,对应于地图“缩小”的时间。比例规则设置在第 53 行,以便该规则将应用于比例分母为 200,000,000 或更大的任何地图。同样,此规则与其他规则之间的唯一区别是行的宽度(在第 1 行上设置为 60 像素)以及缺少 以便不显示标签。<TextSymbolizer>

生成的样式将生成一个多边形描边,该描边会随着放大而变大,并且标签仅在放大到足够级别时才显示。