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

  • GeoServer Cookbook
  • Stefano Iacovella
  • 453字
  • 2021-08-05 17:09:57

Creating a parametric view

A parametric SQL view is based on a SQL query containing the named parameters with values provided dynamically in WMS and WFS requests.

How to do it…

  1. We will now create a new view by extending the code of the previous step. What if you want to have a dynamic view that works for each continent? You need to start the same way as we did previously: select Add a new resource from the Layer menu.
  2. Select the data store pointing to PostGIS or your preferred RDBMS. Instead of selecting a table from the list, select the Configure new SQL view… link:
  3. In the form, insert ContinentView as View Name and the following code as the SQL statement:
    SELECT 
      ADMIN,
      GEOM
    FROM 
      COUNTRIES
    WHERE 
      CONTINENT = '%continent%'
  4. Go to the SQL view parameters section and click on the Guess parameters from SQL link. Insert Africa as the default value.
  5. At the bottom of the page, select the Guess Geometry type and srid checkbox and click on Refresh. The 4326 EPSG code is properly detected, but you have to manually select MultiPolygon instead of the detect value of Polygon.
  6. Click on Save, and you'll be taken to the publish the layer form. Click on the button to calculate the native data extent, and then click on Publish. Move to Layer Preview and select the ContinentView layer. Your map now looks like this one:
  7. The preview map shows you the result of filtering with default values. Add &viewparams=continent:Europe to your request URL and reload the map. You should now see a different map:

How it works…

A parametric view lets you define one or more filtering parameters at request time. In this recipe, instead of building five different views (one for each continent), we have a single view, and the calling app can define which continent is relevant in the request.

Building a parametric view is not different from a standard SQL view. You can start creating the code without any parameters to check that you have no syntax or logical errors.

Once you are done with the code, you can select which field should become a parameter. To let GeoServer recognize the parameters, you simply enclose them within % characters, as shown in the following code:

WHERE CONTINENT = '%continent%'

GeoServer recognizes the parameters and lets you set an optional default value. Please note that if you don't set a default value, you always need to set a value for parameters when sending a request to GeoServer.

At run time, you set values for each parameter, adding the viewparams option to your request. As per the value of the option, you insert a set of params-value couples.

主站蜘蛛池模板: 柘荣县| 兰西县| 阿尔山市| 甘肃省| 东乡族自治县| 新民市| 永定县| 惠安县| 寻乌县| 扎赉特旗| 延川县| 遵义市| 禄丰县| 怀来县| 东源县| 莒南县| 修水县| 彩票| 自贡市| 北宁市| 渭南市| 呼和浩特市| 榕江县| 建瓯市| 通江县| 达拉特旗| 武穴市| 白沙| 阳曲县| 治县。| 西乡县| 长沙市| 精河县| 徐水县| 德州市| 曲阜市| 毕节市| SHOW| 长垣县| 荥阳市| 庄河市|