官术网_书友最值得收藏!

  • GeoServer Cookbook
  • Stefano Iacovella
  • 567字
  • 2021-08-05 17:09:56

Filtering data with CQL

Another vendor parameter is cql_filter. It allows users to add filters to requests using Extended Common Query Language (ECQL).

In the previous recipes, you created filters using the OGC filter XML standard. ECQL lets you create filters in an easier text format and in a much more compact way. A CQL filter is a list of phrases similar to the where clauses in SQL, each separated by the combiner words AND or OR.

Note

CQL was originally used for catalog systems. GeoServer uses an extension for CQL, allowing the full representation of OGC filters in the text form. This extension is called ECQL.

ECQL lets you use several operators and functions. For more information, you can read the documents available at the following URLs:

In this recipe, we will create a map with a filter on income_grp, which is very similar to the previous one. Your result should look like the following screenshot:

Tip

You can find the full source code for this recipe in the ch01_wmsCQLFilter.html file.

How to do it…

  1. Copy the file used in the first recipe to a new file and name it wfsCQLFilter.html in the same folder. Insert a new sld variable and populate it as shown in the following lines:
      <script type="text/javascript" src="http://openlayers.org/api/2.13.1/OpenLayers.js"></script>
      <script type="text/javascript">
        function init() {
          var sld = '<StyledLayerDescriptor version="1.0.0">';
          sld+= '<NamedLayer>';
          sld+= '<Name>NaturalEarth:countries</Name>';
          sld+= '<UserStyle>';
          sld+= '<IsDefault>1</IsDefault>';
          sld+= '<FeatureTypeStyle>';
          sld+= '<Rule>';
          sld+= '<PolygonSymbolizer>';
          sld+= '<Stroke>';
          sld+= '<CssParameter name="stroke">#000000</CssParameter>';
          sld+= '<CssParameter name="stroke-width">1</CssParameter>';
          sld+= '</Stroke>';
          sld+= '<Fill>';
          sld+= '<CssParameter name="fill">#FFFFCC</CssParameter>';
          sld+= '<CssParameter name="fill-opacity">0.65</CssParameter>';
          sld+= '</Fill>';
          sld+= '</PolygonSymbolizer>';
          sld+= '</Rule>';
          sld+= '</FeatureTypeStyle>';
          sld+= '</UserStyle>';
          sld+= '</NamedLayer>';
          sld+= '</StyledLayerDescriptor>';
  2. After the Blue Marble layer, add a new WMS layer:
          new OpenLayers.Layer.WMS("countries",
            "http://localhost/geoserver/wms",{
              layers: "NaturalEarth:countries",
              format: "image/png",
              transparent: true,
              CQL_FILTER: "income_grp = '1. High income: OECD' OR income_grp ='2. High income: nonOECD'",
              sld_body: sld
              })
  3. Save the file and point your browser to it. You should get a map that looks like the one shown in the introduction to this recipe.

How it works…

As with the vendor parameter for reprojection, the use of CQL filters is really easy. You can add one when you create the Layer object and insert the textual representation of the filter, that is, a string. In this filter, we want to select high income countries; two different values match the condition, so we use the logical operator OR to join them:

CQL_FILTER: "income_grp = '1. High income: OECD' OR income_grp ='2. High income: nonOECD'",

Then, we add an sld_body parameter and assign the content of the variable sld to it. This is not a filter requirement; we just want to override the default style for the countries' layers, so we use the WMS option to send a Styled Layer Descriptor (SLD) description of drawing rules to GeoServer:

sld_body: sld

Of course, we need to create an actual SLD document and insert it into the sld variable before creating the countries layer. We do this by adding a well-formed XML code line by line to the sld variable. You will note that we're creating exactly the same symbology used in the Using WFS spatial filters recipe:

var sld = '<StyledLayerDescriptor version="1.0.0">';
…
sld+= '<PolygonSymbolizer>';
sld+= '<Stroke>';
sld+= '<CssParameter name="stroke">#000000</CssParameter>';
sld+= '<CssParameter name="stroke-width">1</CssParameter>';
sld+= '</Stroke>';
sld+= '<Fill>';
sld+= '<CssParameter name="fill">#FFFFCC</CssParameter>';
sld+= '<CssParameter name="fill-opacity">0.65</CssParameter>';
sld+= '</Fill>';
sld+= '</PolygonSymbolizer>';
…

This is just a small peek at SLD. If you are curious about the standard, you can find the official papers for SLD at schemas at http://schemas.opengis.net/sld/.

主站蜘蛛池模板: 隆德县| 北辰区| 靖边县| 西乌| 平潭县| 兰州市| 山丹县| 益阳市| 高清| 桃源县| 离岛区| 光泽县| 天门市| 阿勒泰市| 和平县| 吉水县| 淅川县| 辉县市| 新巴尔虎右旗| 吴堡县| 密山市| 永昌县| 金秀| 舒兰市| 泗阳县| 报价| 合水县| 陆川县| 新巴尔虎左旗| 铜梁县| 集安市| 朝阳区| 静宁县| 五峰| 西昌市| 曲麻莱县| 曲周县| 五华县| 永福县| 浪卡子县| 新竹县|