模板驱动输出¶
- 作者
贺兹逊
- 联系
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浏览器如何处理输出文件非常有用。
实例¶
下面是使用模板可以实现的不同类型输出的一些示例。
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 |