geojson.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. .. _tutorials_getfeatureinfo_geojson:
  2. GeoJSON output format
  3. ======================
  4. The default GeoJSON output uses the WFS GeoJSON encoding mechanism, producing a fixed output, it is however possible to customize the output using FreeMarker templates.
  5. GeoServer will lookup for json templates following the same rules defined for the html output, but the template files have to be named appending ``_json`` to the usual name, as below:
  6. * ``header_json.ftl``
  7. * ``content_json.ftl``
  8. * ``footer_json.ftl``
  9. Moreover, unlike the html case, all three template files must be provided.
  10. In case of a multi-layer request, GeoServer will act in the following way:
  11. * content template will be searched following the usual rules;
  12. * since there are no default templates for GeoJSON, header and footer will be looked up in the ``templates`` directory;
  13. * features with no content template will be encoded with the normal GeoJSON encoding, along with the customized ones.
  14. Follow examples of json template for each type.
  15. The *header json template*::
  16. {
  17. "header":"this is the header",
  18. "type":"FeatureCollection",
  19. "features":[
  20. The *footer json template*::
  21. ],
  22. "footer" : "this is the footer"
  23. }
  24. The *content json template*::
  25. <#list features as feature>
  26. {
  27. "content" : "this is the content",
  28. "type": "Feature",
  29. "id" : "${feature.fid}",
  30. <#list feature.attributes as attribute>
  31. <#if attribute.isGeometry>
  32. "geometry": ${geoJSON.geomToGeoJSON(attribute.rawValue)},
  33. </#if>
  34. </#list>
  35. "properties": {
  36. <#list feature.attributes?filter(a -> !a.isGeometry) as attribute>
  37. "${attribute.name}": "${attribute.value}"
  38. <#if attribute_has_next>
  39. ,
  40. </#if>
  41. </#list>
  42. }
  43. }
  44. <#if feature_has_next>
  45. ,
  46. </#if>
  47. </#list>
  48. As it is possible to see from the content template, a new static model ``geoJSON``, which has the ``geomToGeoJSON`` method has been provided. It can be used to encode a valid geoJSON geometry by passing to it the raw value of the geometry attribute in the following way: ``${geoJSON.geomToGeoJSON(attribute.rawValue)}``.
  49. Placing the above templates in the directory of layer tiger_roads and issuing this request, ::
  50. 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
  51. will produce the output:
  52. .. code-block:: json
  53. {
  54. "header":"this is the header",
  55. "type":"FeatureCollection",
  56. "features":[
  57. {
  58. "content":"this is the content",
  59. "type":"Feature",
  60. "id":"tiger_roads.7752",
  61. "geometry":{
  62. "type":"MultiLineString",
  63. "coordinates":[
  64. [
  65. [
  66. -73.9674,
  67. 40.7844
  68. ],
  69. [
  70. -73.9642,
  71. 40.7832
  72. ],
  73. [
  74. -73.9628,
  75. 40.7813
  76. ]
  77. ]
  78. ]
  79. },
  80. "properties":{
  81. "CFCC":"A41",
  82. "NAME":"85th St Transverse"
  83. }
  84. }
  85. ],
  86. "footer":"this is the footer"
  87. }
  88. While taking care of moving header_json.ftl and footer_json.ftl into the templates directory and performing the following request against the layer group tiger-ny ::
  89. 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
  90. will return the following result:
  91. .. code-block:: json
  92. {
  93. "header":"this is the header",
  94. "type":"FeatureCollection",
  95. "features":[
  96. {
  97. "type":"Feature",
  98. "id":"giant_polygon.1",
  99. "geometry":{
  100. "type":"MultiPolygon",
  101. "coordinates":[
  102. [
  103. [
  104. [
  105. -180,
  106. -90
  107. ],
  108. [
  109. -180,
  110. 90
  111. ],
  112. [
  113. 180,
  114. 90
  115. ],
  116. [
  117. 180,
  118. -90
  119. ],
  120. [
  121. -180,
  122. -90
  123. ]
  124. ]
  125. ]
  126. ]
  127. },
  128. "properties":{
  129. "@featureType":"giant_polygon",
  130. "the_geom":{
  131. "type":"MultiPolygon",
  132. "coordinates":[
  133. [
  134. [
  135. [
  136. -180,
  137. -90
  138. ],
  139. [
  140. -180,
  141. 90
  142. ],
  143. [
  144. 180,
  145. 90
  146. ],
  147. [
  148. 180,
  149. -90
  150. ],
  151. [
  152. -180,
  153. -90
  154. ]
  155. ]
  156. ]
  157. ]
  158. }
  159. }
  160. },
  161. {
  162. "content":"this is the content",
  163. "type":"Feature",
  164. "id":"tiger_roads.7672",
  165. "geometry":{
  166. "type":"MultiLineString",
  167. "coordinates":[
  168. [
  169. [
  170. -74.0108,
  171. 40.7093
  172. ],
  173. [
  174. -74.0105,
  175. 40.7096
  176. ]
  177. ]
  178. ]
  179. },
  180. "properties":{
  181. "CFCC":"A41",
  182. "NAME":"Broadway"
  183. }
  184. },
  185. {
  186. "type":"Feature",
  187. "id":"poi.3",
  188. "geometry":{
  189. "type":"Point",
  190. "coordinates":[
  191. -74.01053,
  192. 40.709387
  193. ]
  194. },
  195. "properties":{
  196. "@featureType":"poi",
  197. "the_geom":{
  198. "type":"Point",
  199. "coordinates":[
  200. -74.01053,
  201. 40.709387
  202. ]
  203. },
  204. "NAME":"art",
  205. "THUMBNAIL":"pics/22037856-Ti.jpg",
  206. "MAINPAGE":"pics/22037856-L.jpg"
  207. }
  208. }
  209. ],
  210. "footer":"this is the footer"
  211. }
  212. As it is possible to see, the json output comprises a mix of the output mediated by a content_json.ftl for the tiger_roads feature, and the normal output for the other features, while header and footer have been kept respectively at the top and at the bottom.