模板驱动输出

作者

Chris Hodgson

联系方式

chodgson at refractions.net

最后更新

2011-04-13

介绍

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

注解

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

注解

为了使模板驱动的输出工作,要输出的层需要包含“template”关键字::

TEMPLATE "dummy"

注解

为了使模板驱动的输出能够通过WFS工作,格式需要在“web``metatdata`部分或“layer``metatdata`部分(下面示例中的geojson格式)中的WFS“getFeature”格式列表中列出:

"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”]

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

属性:

  • src:要包含的文件。

[resultset layer=layername]...[/resultset]

定义给定层的结果位置。

属性:

  • 图层:要使用的图层

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

[feature]...[/feature]

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

属性:

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

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

[join name=join1]...[/join]

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

参见

模板

实例

此示例演示如何使用新系统模拟旧的3文件系统,以比较使用情况:

<!-- mapserver template -->
[include src="templates/header.html"]
[resultset layer=lakes]
  ... old layer HEADER stuff goes here, if a layer has no results
      this block disappears...
  [feature]
    ...repeat this block for each feature in the result set...
    [join name=join1]
      ...repeat this block for each joined row...
    [/join]
  [/feature]
  ...old layer FOOTER stuff goes here...
[/resultset]
[resulset layer=streams]
  ... old layer HEADER stuff goes here, if a layer has no results
      this block disappears...
  [feature]
    ...repeat this block for each feature in the result set...
  [/feature]
  ...old layer FOOTER stuff goes here...
[/resultset]
[include src="templates/footer.html"]

一个特定的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例子。注意使用[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>

警告

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