模板驱动输出

作者

贺兹逊

联系

Chodgson at Refractions.net网站

作者

赛斯·吉文

联系

Sethg在gegraph ika.co.uk网站上

最后更新

2019-10-04

介绍

RFC36增加了对定义模板驱动的输出格式以用于功能查询的支持,包括WMS GetFeatureInfo和WFS GetFeature。这允许定制面向文本的输出,如geojson、kml或xml。这些模板基本上与标准的mapserver查询相同。 模板法 但是,还有一些附加标记允许在单个文件中定义模板,而不是标准的页眉/模板/页脚。

备注

有其他更简单的方法可以使用MapServer输出其中的一些格式。然而,模板驱动的输出提供了最大的灵活性和输出的定制,代价是增加了复杂性和配置。

备注

TEMPLATE

TEMPLATE "dummy"

备注

WEB LAYER

"wfs_getfeature_formatlist" "gml,geojson"

输出格式声明

模板驱动的输出格式的详细信息由 OUTPUTFORMAT 宣言。声明定义了要使用的模板文件以及其他标准的outputformat选项。

实例:

OUTPUTFORMAT
  NAME "kayml"
  DRIVER "TEMPLATE"
  MIMETYPE "application/vnd.google-earth.kml+xml"
  FORMATOPTION "FILE=myTemplate.kml"
  FORMATOPTION "ATTACHMENT=queryResults.kml"
END

OUTPUTFORMAT
  NAME "geojson"
  DRIVER "TEMPLATE"
  FORMATOPTION "FILE=myTemplate.js"
END

OUTPUTFORMAT
  NAME "customxml"
  DRIVER "TEMPLATE"
  FORMATOPTION "FILE=myTemplate.xml"
END

FORMATOPTION.

备注

模板文件的有效后缀是:.xml,.wml,.html,.htm,.svg,.kml,.gml,.js,.tmpl。

mimetype和formatOption“attachment=…”参数对于控制Web浏览器如何处理输出文件非常有用。

模板替换标记

这些标记只在查询结果模板中工作,它们的目的主要是为了简化为自定义输出格式的单个文件的模板化。

[包括src=“othertemplate.txt”]

包括另一个模板文件;模板文件的路径相对于映射文件路径。

属性:

  • SRC:要包含的文件。

[结果集层=层名称]…[/resultset]

定义给定层的结果位置。

属性:

  • 图层:要使用的图层

  • nodata:(可选)如果没有返回结果,则返回的字符串。

[feature]...[/feature]

定义围绕为给定层返回的要素的循环。

属性:

  • limit:(可选)指定要为此层输出的功能的最大数目。

  • trimlast:(可选)指定要从输出的最终功能的末尾修剪的字符串。这是为了允许删除尾随记录分隔符。见下面的例子。

[加入名称=join1]…[/join]

定义从另一层连接的要素周围的循环。

参见

模板法

实例

下面是使用模板可以实现的不同类型输出的一些示例。

GML3

<!-- mapserver template -->
<?xml version="1.0" encoding="ISO-8859-1"?>
[resultset layer=mums]
<MapServerUserMeetings xmlns="http://localhost/ms_ogc_workshop"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:gml="http://www.opengis.net/gml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://localhost/ms_ogc_workshop ./mums.xsd">
 <gml:description>This is a GML document which provides locations of
    all MapServer User Meeting that have taken place</gml:description>
 <gml:name>MapServer User Meetings</gml:name>
 <gml:boundedBy>
  <gml:Envelope>
   <gml:coordinates>-93.093055556,44.944444444 -75.7,45.4166667</gml:coordinates>
  </gml:Envelope>
 </gml:boundedBy>
 [feature]
 <gml:featureMember>
  <Meeting>
   <gml:description>[desc]</gml:description>
   <gml:name>[name]</gml:name>
   <gml:location>
    <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
     <gml:pos>[x] [y]</gml:pos>
    </gml:Point>
   </gml:location>
   <year>[year]</year>
   <venue>[venue]</venue>
   <website>[url]</website>
  </Meeting>
 </gml:featureMember>
 [/feature]
 <dateCreated>2007-08-13T17:17:32Z</dateCreated>
</MapServerUserMeetings>
[resultset]

GeoJSON

可以使用…&layer=mums&mode=nquery&qformat=geojson调用

或者将&outputformat=geojson添加到WFS GetFeature请求中::

// mapserver template
[resultset layer=mums]
{
  "type": "FeatureCollection",
  "features": [
    [feature trimlast=","]
    {
      "type": "Feature",
      "id": "[myuniqueid]",
      "geometry": {
        "type": "PointLineString",
        "coordinates": [
          {
            "type": "Point",
            "coordinates": [[x], [y]]
          }
        ]
      },
      "properties": {
        "description": "[description]",
        "venue": "[venue]",
        "year": "[year]"
      }
    },
    [/feature]
  ]
}
[/resultset]

KML

更复杂的KML例子。注意使用[SHPxy]来支持带孔的多元体,并且使用[SHPLABEL]在每个特征中包含一个点放置标记:

<!--MapServer Template-->
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
     xmlns:gx="http://www.google.com/kml/ext/2.2"
     xmlns:kml="http://www.opengis.net/kml/2.2"
     xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
  <Style id="parks_highlight">
    <IconStyle>
      <scale>1.4</scale>
      <Icon>
        <href>http://maps.google.com/mapfiles/kml/shapes/parks.png</href>
      </Icon>
      <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
    </IconStyle>
    <LineStyle>
      <color>ffff5500</color>
      <width>4.2</width>
    </LineStyle>
    <PolyStyle>
      <color>aaaaaaaa</color>
    </PolyStyle>
    <BalloonStyle>
      <text>
        <![CDATA[
          <p ALIGN="center"><b>$[name]</b></p>
          $[description]
        ]]>
      </text>
    </BalloonStyle>
  </Style>
  <Style id="parks_normal">
    <IconStyle>
      <scale>1.2</scale>
      <Icon>
        <href>http://maps.google.com/mapfiles/kml/shapes/parks.png</href>
      </Icon>
      <hotSpot x="0.5" y="0" xunits="fraction" yunits="fraction"/>
    </IconStyle>
    <LineStyle>
      <color>ffff5500</color>
      <width>4.2</width>
    </LineStyle>
    <PolyStyle>
      <color>ff7fff55</color>
    </PolyStyle>
    <BalloonStyle>
      <text>
        <![CDATA[
          <p ALIGN="center"><b>$[name]</b></p>
          $[description]
        ]]>
      </text>
    </BalloonStyle>
  </Style>
  <StyleMap id="parks_map">
    <Pair>
      <key>normal</key>
      <styleUrl>#parks_normal</styleUrl>
    </Pair>
    <Pair>
      <key>highlight</key>
      <styleUrl>#parks_highlight</styleUrl>
    </Pair>
  </StyleMap>
[resultset layer=parks]
  <Folder>
    <name>Parks</name>
[feature trimlast="," limit=1]
    <Placemark>
      <name>[NAME]</name>
      <Snippet/>
      <description>
        <![CDATA[
          <p>Year Established: [YEAR_ESTABLISHED]</p>
          <p>Area: [AREA_KILOMETERS_SQUARED] sq km</p>
        ]]>
      </description>
      <styleUrl>#parks_map</styleUrl>
      <ExtendedData>
        <Data name="Year Established">[YEAR_ESTABLISHED]</Data>
        <Data name="Area">[AREA_KILOMETERS_SQUARED]</Data>
      </ExtendedData>
      <MultiGeometry>
        <Point>
          <coordinates>[shplabel proj=epsg:4326 precision=10],0</coordinates>
        </Point>
[shpxy     ph="<Polygon><tessellate>1</tessellate>" pf="</Polygon>"
           xf="," xh=" " yh=" " yf=",0 "
           orh="<outerBoundaryIs><LinearRing><coordinates>"
           orf="</coordinates></LinearRing></outerBoundaryIs>"
           irh="<innerBoundaryIs><LinearRing><coordinates>"
           irf="</coordinates></LinearRing></innerBoundaryIs>"
           proj=epsg:4326 precision=10]
      </MultiGeometry>
    </Placemark>
[/feature]
  </Folder>
[/resultset]
</Document>
</kml>

重新构造的文本表格

本示例从要素记录创建一个reStrutiredText表,使用 padding 格式化选项。要从命令行进行测试,请使用类似于下面的语法:

mapserv -nh "QUERY_STRING=map=./msautotest/query/query.map&mode=nquery&qlayer=bdry_counpy2&qformat=formattmpl"
# MapServer Template
[resultset layer=bdry_counpy2]
==================== ============
City Name            Abbreviation
==================== ============
[feature][item name="cty_name" padding="20"] [item name="cty_abbr" uc="true" padding="12"]
[/feature]
==================== ============
[/resultset]

然后,可以将RST输出呈现为表格。

城市名称

缩略语

森林之湖

LOTW

基特森

KITT

罗索

ROSE

库奇钦

KOOC

马歇尔

MARS

圣路易斯

STLO

警告

对于模板( 模板法 )有许多保留字。如果要公开名称等于保留字的属性,则不能使用速记[属性名称],而是必须使用construct[项名称=属性名称]。例如,在模板中,[id]是系统生成的唯一会话id(请参见 模板法 )因此,如果要公开一个名为“id”的属性,则必须重命名它或使用构造[项名称=id]。