GeoJSON输出格式¶
默认的GeoJSON输出使用WFS GeoJSON编码机制,生成固定的输出,但是可以使用FreeMarker模板自定义输出。Geoserver将查找遵循为html输出定义的相同规则的json模板,但模板文件必须命名为appending _json
更改为通常的名称,如下所示:
header_json.ftl
content_json.ftl
footer_json.ftl
此外,与html不同的是,必须提供所有三个模板文件。在多层请求的情况下,Geoserver将按照以下方式运行:
内容模板将按照通常的规则进行搜索;
因为GeoJSON没有默认模板,所以页眉和页脚将在
templates
目录;没有内容模板的特性将使用常规GeoJSON编码以及自定义编码进行编码。
以下是每种类型的json模板示例。
这个 头json模板 ::
{
"header":"this is the header",
"type":"FeatureCollection",
"features":[
这个 页脚json模板 ::
],
"footer" : "this is the footer"
}
这个 内容json模板 ::
<#list features as feature>
{
"content" : "this is the content",
"type": "Feature",
"id" : "${feature.fid}",
<#list feature.attributes as attribute>
<#if attribute.isGeometry>
"geometry": ${geoJSON.geomToGeoJSON(attribute.rawValue)},
</#if>
</#list>
"properties": {
<#list feature.attributes?filter(a -> !a.isGeometry) as attribute>
"${attribute.name}": "${attribute.value}"
<#if attribute_has_next>
,
</#if>
</#list>
}
}
<#if feature_has_next>
,
</#if>
</#list>
从内容模板中可以看到,一个新的静态模型 geoJSON
,其中包含 geomToGeoJSON
方法已提供。它可以通过以下方式将geometry属性的原始值传递给有效的geoJSON几何体来对其进行编码: ${{geoJSON.geomToGeoJSON(attribute.rawValue)}}
.
将上述模板放置在tigerúu roads图层的目录中并发出此请求:
http://localhost:8080/geoserver/tiger/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=application/json&TRANSPARENT=true&QUERY_LAYERS=tiger:tiger_roads&LAYERS=tiger:tiger_roads&exceptions=application/vnd.ogc.se_inimage&INFO_FORMAT=application/json&FEATURE_COUNT=50&X=50&Y=50&SRS=EPSG:4326&STYLES=&WIDTH=101&HEIGHT=101&BBOX=-73.96894311918004,40.78191518783569,-73.96460866941197,40.78624963760376
将产生输出:
{
"header":"this is the header",
"type":"FeatureCollection",
"features":[
{
"content":"this is the content",
"type":"Feature",
"id":"tiger_roads.7752",
"geometry":{
"type":"MultiLineString",
"coordinates":[
[
[
-73.9674,
40.7844
],
[
-73.9642,
40.7832
],
[
-73.9628,
40.7813
]
]
]
},
"properties":{
"CFCC":"A41",
"NAME":"85th St Transverse"
}
}
],
"footer":"this is the footer"
}
在移动收割台时_json.ftl格式和页脚_json.ftl格式进入templates目录并对tiger ny层组执行以下请求:
http://localhost:8080/geoserver/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=application/json&TRANSPARENT=true&QUERY_LAYERS=tiger-ny&LAYERS=tiger-ny&exceptions=application/vnd.ogc.se_inimage&INFO_FORMAT=application/json&FEATURE_COUNT=50&X=50&Y=50&SRS=EPSG:4326&STYLES=&WIDTH=101&HEIGHT=101&BBOX=-74.01161170018896,40.70833468424098,-74.00944447530493,40.710501909125014
将返回以下结果:
{
"header":"this is the header",
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"id":"giant_polygon.1",
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
-180,
-90
],
[
-180,
90
],
[
180,
90
],
[
180,
-90
],
[
-180,
-90
]
]
]
]
},
"properties":{
"@featureType":"giant_polygon",
"the_geom":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
-180,
-90
],
[
-180,
90
],
[
180,
90
],
[
180,
-90
],
[
-180,
-90
]
]
]
]
}
}
},
{
"content":"this is the content",
"type":"Feature",
"id":"tiger_roads.7672",
"geometry":{
"type":"MultiLineString",
"coordinates":[
[
[
-74.0108,
40.7093
],
[
-74.0105,
40.7096
]
]
]
},
"properties":{
"CFCC":"A41",
"NAME":"Broadway"
}
},
{
"type":"Feature",
"id":"poi.3",
"geometry":{
"type":"Point",
"coordinates":[
-74.01053,
40.709387
]
},
"properties":{
"@featureType":"poi",
"the_geom":{
"type":"Point",
"coordinates":[
-74.01053,
40.709387
]
},
"NAME":"art",
"THUMBNAIL":"pics/22037856-Ti.jpg",
"MAINPAGE":"pics/22037856-L.jpg"
}
}
],
"footer":"this is the footer"
}
可以看到,json输出由Tiger_Roads特性的Content_json.ftl传递的输出和其他特性的正常输出混合而成,而页眉和页脚分别位于顶部和底部。
Previous: HTML输出格式
Next: 栅格GetFeatureInfo响应自定义