GeoServer使用CSS渲染地图

简介: CSS Style是GeoServer的一个扩展插件,使用CSS写起来的地图渲染策略文件相比较SLD而言,非常的简洁,本文根据GeoServer用户手册,稍微改写,便于该知识点的推广。

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


img_e7b86dbfa12b7807e553a96ab8b26069.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;
}
img_7142f6475006ebcb316990dd7eaae9f9.png
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

当图形组合内联时,有时需要对一些符号做些可选设置。


img_0a059bccf2e447baa79df675096c2068.png
官网说明.png

举例如下:

 * {
   stroke: #333333, symbol("shape://vertline");
   stroke-width: 3px;
   :nth-stroke(2) {
     size: 12;
     stroke: #333333;
     stroke-width: 1px;
   }
 }

img_3ff0a2d6ed4df2c69fc88b87975e5067.png
铁路.png

该线是由 #333333, symbol(" shape://vertline")两个线样式组合而来,其中,选择symbol(" shape://vertline")并进行设置,选择symbol就是使用:nth-stroke这种格式来选择symbol。
更多更丰富的使用详细见官网,后续会有具体使用说明。

相关文章
|
9月前
|
前端开发 JavaScript Serverless
前端工程化的前端性能的性能优化方案的渲染层面优化之CSS/JS优化
渲染是一种非常重要的前端性能优化方案,因为它可以在不同的环境中提高网页的响应速度和可接受性。
64 2
|
7月前
|
Web App开发 前端开发 iOS开发
CSS3 字体抗锯齿渲染
CSS3 字体抗锯齿渲染
37 0
|
8月前
|
JavaScript 前端开发
html和css中的图片加载与渲染规则是什么样的?
html和css中的图片加载与渲染规则是什么样的?
95 0
|
11月前
|
JavaScript 前端开发 API
轻松明白 CSS 和 JS 对页面渲染的阻塞
前言 前面有分析过页面的渲染逻辑(老生常谈之从输入URL到页面呈现的过程(全)),从上至下解析 HTML,构建 DOM 树和 Style Rules,其中构建 DOM 和解析 Style 是并行的,之后 DOM 树和 Style Rules 结合成 Render Tree。
|
前端开发 JavaScript Go
浏览器原理 22 # 渲染流水线:CSS如何影响首次加载时的白屏时间?
浏览器原理 22 # 渲染流水线:CSS如何影响首次加载时的白屏时间?
94 0
浏览器原理 22 # 渲染流水线:CSS如何影响首次加载时的白屏时间?
|
JavaScript 前端开发 数据可视化
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?2
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?
193 0
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?2
|
Web App开发 JavaScript 前端开发
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?
147 0
浏览器原理 05 # 渲染流程:HTML、CSS和JavaScript,是如何变成页面的?
|
前端开发 JavaScript 测试技术
两行CSS 提升渲染性能7倍
两行CSS 提升渲染性能7倍
95 0
|
移动开发 前端开发 HTML5
前端静态页面html5样式设置css页面渲染
前端静态页面html5样式设置css页面渲染
277 0
前端静态页面html5样式设置css页面渲染
|
前端开发 JavaScript 测试技术
1---两行CSS提升渲染性能7倍
两行CSS提升渲染性能7倍
142 0