日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

GeoServer使用CSS渲染地图

發布時間:2025/3/17 CSS 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GeoServer使用CSS渲染地图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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選項即代表可以正常使用了。


新建CSS Style.png

二 CSS應用基礎

CSS Style和SLD一樣是一個地圖渲染策略文件,新建樣式,綁定圖層等操作和sld是一模一樣的,只是寫起來更加簡潔。
SLD的策略文件舉例如下:

<?xml version="1.0" encoding="ISO-8859-1"?> <StyledLayerDescriptorversion="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/sldhttp://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 threecategories 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("shape://vertline")并進行設置,選擇symbol就是使用:nth-stroke這種格式來選擇symbol。
更多更豐富的使用詳細見官網,后續會有具體使用說明。

總結

以上是生活随笔為你收集整理的GeoServer使用CSS渲染地图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。