CSS Style是GeoServer的一個擴充插件,使用CSS寫起來的地圖渲染政策檔案相比較SLD而言,非常的簡潔,本文根據GeoServer使用者手冊,稍微改寫,便于該知識點的推廣。
一 CSS Style安裝
1 從geoserver下載下傳頁面下載下傳 對應版本的geoserver-A.B.C-css-plugin.zip。A.B.C對應的是GeoServer的版本号。
2 解壓geoserver-A.B.C-css-plugin.zip,将解壓後的jar檔案,複制到對應geoserver版本的WEB-INF/lib目錄中。
3 重新開機GeoServer即可。
在建立Style頁面看到Format有CSS選項即代表可以正常使用了。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcukjNwYjMihjYhZTOhNTN1U2NwgzNiJTMhZmYkZDOidTZfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
建立CSS Style.png
二 CSS應用基礎
CSS Style和SLD一樣是一個地圖渲染政策檔案,建立樣式,綁定圖層等操作和sld是一模一樣的,隻是寫起來更加簡潔。
SLD的政策檔案舉例如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor
version="1.0.0"
xmlns="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
xsi:schemaLocation="http://www.opengis.net/sld
http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd
">
<NamedLayer>
<Name>USA states population</Name>
<UserStyle>
<Name>population</Name>
<Title>Population in the United States</Title>
<Abstract>A sample filter that filters the United States into three
categories of population, drawn in different colors</Abstract>
<FeatureTypeStyle>
<Rule>
<Title>< 2M</Title>
<ogc:Filter>
<ogc:PropertyIsLessThan>
<ogc:PropertyName>PERSONS</ogc:PropertyName>
<ogc:Literal>2000000</ogc:Literal>
</ogc:PropertyIsLessThan>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#4DFF4D</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>2M - 4M</Title>
<ogc:Filter>
<ogc:PropertyIsBetween>
<ogc:PropertyName>PERSONS</ogc:PropertyName>
<ogc:LowerBoundary>
<ogc:Literal>2000000</ogc:Literal>
</ogc:LowerBoundary>
<ogc:UpperBoundary>
<ogc:Literal>4000000</ogc:Literal>
</ogc:UpperBoundary>
</ogc:PropertyIsBetween>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#FF4D4D</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>> 4M</Title>
<!-- like a linesymbolizer but with a fill too -->
<ogc:Filter>
<ogc:PropertyIsGreaterThan>
<ogc:PropertyName>PERSONS</ogc:PropertyName>
<ogc:Literal>4000000</ogc:Literal>
</ogc:PropertyIsGreaterThan>
</ogc:Filter>
<PolygonSymbolizer>
<Fill>
<!-- CssParameters allowed are fill (the color) and fill-opacity -->
<CssParameter name="fill">#4D4DFF</CssParameter>
<CssParameter name="fill-opacity">0.7</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
<Rule>
<Title>Boundary</Title>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke-width">0.2</CssParameter>
</Stroke>
</LineSymbolizer>
<TextSymbolizer>
<Label>
<ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
</Label>
<Font>
<CssParameter name="font-family">Times New Roman</CssParameter>
<CssParameter name="font-style">Normal</CssParameter>
<CssParameter name="font-size">14</CssParameter>
</Font>
<LabelPlacement>
<PointPlacement>
<AnchorPoint>
<AnchorPointX>0.5</AnchorPointX>
<AnchorPointY>0.5</AnchorPointY>
</AnchorPoint>
</PointPlacement>
</LabelPlacement>
</TextSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
改寫CSS樣式如下:
[PERSONS < 2000000] {
fill: #4DFF4D;
fill-opacity: 0.7;
stroke-width: 0.2;
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
[PERSONS >= 2000000] [PERSONS < 4000000] {
fill: #FF4D4D;
fill-opacity: 0.7;
stroke-width: 0.2;
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
[PERSONS >= 4000000] {
fill: #4D4DFF;
fill-opacity: 0.7;
stroke-width: 0.2;
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
一個rule對應css的一個{},注意{}後面沒有;,等符号。
注意觀察可知,每個規則,僅僅fill是不同的,其他的參數都是一樣的,可以考慮将公共的樣式部分,放到通用規則裡,通用規則是 *{},改寫如下:
[PERSONS < 2000000] {
fill: #4DFF4D;
}
[PERSONS > 2000000] [PERSONS < 4000000] {
fill: #FF4D4D;
}
[PERSONS > 4000000] {
fill: #4D4DFF;
}
* {
fill-opacity: 0.7;
stroke-width: 0.2;
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
每一個完整的規則,都是規則+*(通用)規則組成完整的樣式政策。
對比sld可知,檔案寫起來更簡單,可讀性更強。
三 CSS應用提高
3.1 使用Scale
sld中,常常比如說某個樣式,在scale大于某個比例尺下才顯示,在小于某個比例尺下不顯示。CSS Style中使用@scale來标志,例子如下:
[@scale >= 20000000]{
label:'';
}
[@scale < 20000000] {
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
該例子說明,在scale >= 20000000不顯示地圖示注,在scale < 20000000顯示設定的标注。
3.2 使用圖例
圖例的描述性資訊以/*@title */說明,如下:
/* @title Population < 2M */
[PERSONS < 2000000] {
fill: #4DFF4D;
}
Legend.png
描述的資訊就會作用在Legend上。
3.3 規則嵌套
* {
stroke: black;
stroke-width: 0.2;
fill-opacity: 0.7;
/* @title Population < 2M */
[PERSONS < 2000000] {
fill: #4DFF4D;
};
/* @title 2M < Population < 4M */
[PERSONS >= 2000000] [PERSONS < 4000000] {
fill: #FF4D4D;
};
/* @title Population > 4M */
[PERSONS >= 4000000] {
fill: #4D4DFF;
};
/* Labelling */
[@scale < 20000000] {
label: [STATE_ABBR];
label-anchor: 0.5 0.5;
font-family: "Times New Roman";
font-fill: black;
font-style: normal;
font-size: 14;
}
}
長話短說,這裡使用了 正常樣式+條件過濾樣式+scale比例尺 三個規則組合嵌套。
3.4 條件篩選
3.4.1 or與and
條件中常常使用多個條件組合應用。
and應用如下:
[rainfall>12] [lakes>1] {
fill: black;
}
and時,條件之間是空格,無符号。
or應用如下:
[rainfall>12], [lakes>1] {
fill: blue;
}
or時,條件之間是逗号,也可以寫成如下:
[rainfall>12 or lakes>1] {
fill: blue;
}
3.4.2 運算符号
=,<>,>,<,>=,<=,LIKE等操作。
3.4.3 根據圖層名稱Filter
這個用的不多,除非把若幹個圖層的渲染政策寫到了一個檔案。舉例說明如下:
line1 {
stroke: black;
}
line2 {
stroke: red;
}
line3 {
stroke: blue;
}
line1,line2,line3是三個圖層的名字,這三個圖層都綁定了這個樣式檔案。那麼使用Filter圖層定義不同的圖層分别的渲染政策。
3.4.4 根據Feature的ID Filtering
#states.2 {
stroke: black;
}
選擇states圖層中,id為2的要素。
3.4.5 根據symbols Filtering
當圖形組合内聯時,有時需要對一些符号做些可選設定。
官網說明.png
舉例如下:
* {
stroke: #333333, symbol("shape://vertline");
stroke-width: 3px;
:nth-stroke(2) {
size: 12;
stroke: #333333;
stroke-width: 1px;
}
}
鐵路.png
該線是由 #333333, symbol("
shape://vertline")兩個線樣式組合而來,其中,選擇symbol("
")并進行設定,選擇symbol就是使用:nth-stroke這種格式來選擇symbol。
更多更豐富的使用詳細見官網,後續會有具體使用說明。