postgis.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. .. _data_postgis:
  2. PostGIS
  3. =======
  4. `PostGIS <http://postgis.net>`_ is an open source spatial database based on `PostgreSQL <http://postgresql.com/>`_, and is currently one of the most popular open source spatial databases today.
  5. Adding a PostGIS database
  6. -------------------------
  7. As with all formats, adding a shapefile to GeoServer involves adding a new store to the existing :ref:`data_webadmin_stores` through the :ref:`web_admin`.
  8. Using default connection
  9. ````````````````````````
  10. To begin, navigate to :menuselection:`Stores --> Add a new store --> PostGIS NG`.
  11. Fill in the *Basic Store Info* used to identify the database when managing layers.
  12. .. figure:: images/postgis-basic-info.png
  13. *Adding a PostGIS database*
  14. .. list-table::
  15. :widths: 20 80
  16. :header-rows: 1
  17. * - Basic Store Info
  18. - Description
  19. * - :guilabel:`Workspace`
  20. - Name of the workspace to contain the database. This will also be the prefix of any layer names created from tables in the database.
  21. * - :guilabel:`Data Source Name`
  22. - Name of the database. This can be different from the name as known to PostgreSQL/PostGIS.
  23. * - :guilabel:`Description`
  24. - Description of the database/store.
  25. * - :guilabel:`Enabled`
  26. - Enables the store. If disabled, no data in the database will be served.
  27. Move on to the connection parameters used to connect and interact with the database.
  28. .. figure:: images/postgis.png
  29. :align: center
  30. *PostGIS connection parameters*
  31. The ``dbtype`` and ``namespace`` connection parameters are not directly editable. The :guilabel:`dbtype` parameter is for internal use only (and only accessible via the REST API).
  32. .. list-table::
  33. :widths: 20 80
  34. :header-rows: 1
  35. * - Connection Parameter
  36. - Description
  37. * - :guilabel:`dbtype`
  38. - Type of database. Internal value, leave this value as the default.
  39. * - :guilabel:`namespace`
  40. - Namespace to be associated with the database. This field is altered by changing the workspace name.
  41. Connection parameters establishing a database connection (see :doc:`connection-pooling`):
  42. .. list-table::
  43. :widths: 20 80
  44. :header-rows: 1
  45. * - Connection Parameter
  46. - Description
  47. * - :guilabel:`host`
  48. - Host name where the database exists.
  49. * - :guilabel:`port`
  50. - Port number to connect to the above host.
  51. * - :guilabel:`database`
  52. - Name of the database as known on the host.
  53. * - :guilabel:`schema`
  54. - Schema in the above database.
  55. * - :guilabel:`user`
  56. - Username to connect to the database.
  57. * - :guilabel:`passwd`
  58. - Password associated with the above user.
  59. * - :guilabel:`max connections`
  60. - Maximum amount of open connections to the database.
  61. * - :guilabel:`min connections`
  62. - Minimum number of pooled connections.
  63. * - :guilabel:`fetch size`
  64. - Number of records read with each interaction with the database.
  65. * - :guilabel:`Connection timeout`
  66. - Time (in seconds) the connection pool will wait before timing out.
  67. * - :guilabel:`validate connections`
  68. - Checks the connection is alive before using it.
  69. * - :guilabel:`Evictor run periodicity`
  70. - Number of seconds between idle object evictor runs.
  71. * - :guilabel:`Max connection idle time`
  72. - Number of seconds a connection needs to stay idle before the evictor starts to consider closing it.
  73. * - :guilabel:`Evictor tests per run`
  74. - Number of connections checked by the idle connection evictor for each of its runs.
  75. Connection parameters managing SQL generation:
  76. .. list-table::
  77. :widths: 20 80
  78. :header-rows: 1
  79. * - Connection Parameter
  80. - Description
  81. * - :guilabel:`Expose primary keys`
  82. - Expose primary key columns as values suitable for filtering.
  83. * - :guilabel:`Primary key metadata table`
  84. - Provide table defining how primary keys values are generated (see :doc:`primarykey`)
  85. * - :guilabel:`Session startup SQL`
  86. - SQL applied to connection before use (see :doc:`sqlsession`)
  87. * - :guilabel:`Session close-up SQL`
  88. - SQL applied to connection after use (see :doc:`sqlsession`)
  89. * - :guilabel:`preparedStatements`
  90. - Enables prepared statements for SQL generation, rather than text substitution.
  91. * - :guilabel:`Max open prepared statements`
  92. - Number of prepared statements available.
  93. Connection parameters managing database interaction:
  94. .. list-table::
  95. :widths: 20 80
  96. :header-rows: 1
  97. * - Connection Parameter
  98. - Description
  99. * - :guilabel:`Loose bbox`
  100. - Performs only the primary filter on the bounding box. See the section on :ref:`postgis_loose_bbox` for details.
  101. * - :guilabel:`Estimated extends`
  102. - Use spatial index to quickly estimate bounds, rather than check every row.
  103. * - :guilabel:`Encode functions`
  104. - Generate supported filter functions into their SQL equivalent.
  105. * - :guilabel:`Support on the fly geometry simplification`
  106. - Enables use of PostGIS geometry simplification
  107. Connection parameters supporting initial database creation:
  108. .. list-table::
  109. :widths: 20 80
  110. :header-rows: 1
  111. * - Connection Parameter
  112. - Description
  113. * - :guilabel:`create database`
  114. - Enable to define a new database on connection
  115. * - :guilabel:`create database params`
  116. - Additional CREATE DATABASE definition, example :kbd:`WITH TEMPLATE=postgis`
  117. When finished, click :guilabel:`Save`.
  118. Using JNDI
  119. ``````````
  120. GeoServer can also connect to a PostGIS database using `JNDI <https://docs.oracle.com/javase/tutorial/jndi/overview/index.html>`_ (Java Naming and Directory Interface). This is used to allow a PostGIS DataStore to share a named connection pool of PostGIS connections configured in your application server.
  121. To begin, navigate to :menuselection:`Stores --> Add a new store --> PostGIS NG (JNDI)`.
  122. .. figure:: images/postgisjndi.png
  123. :align: center
  124. *Adding a PostGIS database (using JNDI)*
  125. .. list-table::
  126. :widths: 20 80
  127. :header-rows: 1
  128. * - Option
  129. - Description
  130. * - :guilabel:`Workspace`
  131. - Name of the workspace to contain the store. This will also be the prefix of all of the layer names created from the store.
  132. * - :guilabel:`Data Source Name`
  133. - Name of the database. This can be different from the name as known to PostgreSQL/PostGIS.
  134. * - :guilabel:`Description`
  135. - Description of the database/store.
  136. * - :guilabel:`Enabled`
  137. - Enables the store. If disabled, no data in the database will be served.
  138. * - :guilabel:`dbtype`
  139. - Type of database. Leave this value as the default.
  140. * - :guilabel:`jndiReferenceName`
  141. - JNDI path to the database.
  142. * - :guilabel:`schema`
  143. - Schema for the above database.
  144. * - :guilabel:`namespace`
  145. - Namespace to be associated with the database. This field is altered by changing the workspace name.
  146. When finished, click :guilabel:`Save`.
  147. Configuring PostGIS layers
  148. --------------------------
  149. When properly loaded, all tables in the database will be visible to GeoServer, but they will need to be individually configured before being served by GeoServer. See the section on :ref:`data_webadmin_layers` for how to add and edit new layers.
  150. .. _postgis_loose_bbox:
  151. Using loose bounding box
  152. ------------------------
  153. When the option :guilabel:`loose bbox` is enabled, only the bounding box of a geometry is used. This can result in a significant performance gain, but at the expense of total accuracy; some geometries may be considered inside of a bounding box when they are technically not.
  154. If primarily connecting to this data via WMS, this flag can be set safely since a loss of some accuracy is usually acceptable. However, if using WFS and especially if making use of BBOX filtering capabilities, this flag should not be set.
  155. Publishing a PostGIS view
  156. -------------------------
  157. Publishing a view follows the same process as publishing a table. The only additional step is to manually ensure that the view has an entry in the ``geometry_columns`` table.
  158. For example consider a table with the schema::
  159. my_table( id int PRIMARY KEY, name VARCHAR, the_geom GEOMETRY )
  160. Consider also the following view::
  161. CREATE VIEW my_view as SELECT id, the_geom FROM my_table;
  162. Before this view can be served by GeoServer, the following step is necessary to manually create the ``geometry_columns`` entry::
  163. INSERT INTO geometry_columns VALUES ('','public','my_view','my_geom', 2, 4326, 'POINT' );
  164. Performance considerations
  165. --------------------------
  166. GEOS
  167. ````
  168. `GEOS <http://trac.osgeo.org/geos/>`_ (Geometry Engine, Open Source) is an optional component of a PostGIS installation. It is recommended that GEOS be installed with any PostGIS instance used by GeoServer, as this allows GeoServer to make use of its functionality when doing spatial operations. When GEOS is not available, these operations are performed internally which can result in degraded performance.
  169. Spatial indexing
  170. ````````````````
  171. It is strongly recommended to create a spatial index on tables with a spatial component (i.e. containing a geometry column). Any table of which does not have a spatial index will likely respond slowly to queries.
  172. Common problems
  173. ---------------
  174. Primary keys
  175. ````````````
  176. In order to enable transactional extensions on a table (for transactional WFS), the table must have a primary key. A table without a primary key is considered read-only to GeoServer.
  177. GeoServer has an option to expose primary key values (to make filters easier). Please keep in mind that these values are only exposed for your convenience - any attempted to modify these values using WFS-T update will be silently ignored. This restriction is in place as the primary key value is used to define the FeatureId. If you must change the FeatureId you can use WFS-T delete and add in a single Transaction request to define a replacement feature.
  178. Multi-line
  179. ``````````
  180. To insert multi-line text (for use with labeling) remember to use escaped text::
  181. INSERT INTO place VALUES (ST_GeomFromText('POINT(-71.060316 48.432044)', 4326), E'Westfield\nTower');
  182. JsonPointer Function support
  183. ----------------------------
  184. GeoServer is able to translate the ``jsonPointer`` function to a query using PostgreSQL support for JSON types.
  185. The following are the main characteristics of the implementation:
  186. * The jsonPointer function syntax is like the following: ``jsonPointer(attributeName,'/path/to/json/attribute')``.
  187. * The function is able to select attributes inside json arrays by specifying the index of the target element in the json path eg. ``'/path/to/array/element/0'``.
  188. * When accessing a JSON property it is implicitly assumed that the same property will have the same type on all features, otherwise a cast exception will be thrown by the database.
  189. * GeoServer will perform a cast automatically to the expect type from the evaluation; the cast is completely delegated to the database.
  190. * If the property doesn't exists no errors will be issued, but the features that have that property will be excluded; hence the property we wish to query is not mandatory in all features.
  191. Examples
  192. ````````
  193. Having a json column storing jsonvalues like the following,
  194. .. code-block :: json
  195. { "name": "city name",
  196. "description": "the city description",
  197. "districts": [
  198. {
  199. "name":"district1",
  200. "population": 2000
  201. },
  202. {
  203. "name":"district2",
  204. "population": 5000
  205. }
  206. ]
  207. "population":{
  208. "average_age": 35,
  209. "toal": 50000
  210. }
  211. }
  212. and assuming an attribute name as ``city``, valid jsonPointer functions would be:
  213. * ``jsonPointer(city, '/name')``.
  214. * ``jsonPointer(city, '/population/average_age')``.
  215. * ``jsonPointer(city, '/districts/0/name')``.
  216. An example cql_filter would then be ``jsonPointer(city, '/population/average_age') > 30``.
  217. While an example rule in a sld style sheet could be:
  218. .. code-block:: xml
  219. <Rule>
  220. <Name>Cities</Name>
  221. <ogc:Filter>
  222. <ogc:PropertyIsEqualTo>
  223. <ogc:Function name="jsonPointer">
  224. <ogc:PropertyName>city</ogc:PropertyName>
  225. <ogc:Literal>/population/average_age</ogc:Literal>
  226. </ogc:Function>
  227. <ogc:Literal>35</ogc:Literal>
  228. </ogc:PropertyIsEqualTo>
  229. </ogc:Filter>
  230. <PointSymbolizer>
  231. <Graphic>
  232. <Mark>
  233. <WellKnownName>square</WellKnownName>
  234. <Fill>
  235. <CssParameter name="fill">#FF0000</CssParameter>
  236. </Fill>
  237. </Mark>
  238. <Size>16</Size>
  239. </Graphic>
  240. </PointSymbolizer>
  241. </Rule>
  242. DataTypes
  243. `````````
  244. PostgreSQL defines two JSON datatypes:
  245. * ``json`` that stores an exact copy of the input text.
  246. * ``jsonb`` which store the value in a decomposed binary format.
  247. The jsonPointer function supports both, as well as the text format if it contains a valid json representation.
  248. Anyways, the PostgreSQL documentation recommends usage of jsonb, as it is faster to process.
  249. PostgreSQL supports also indexing on json types. And index on a specific json attribute can be created as follow:
  250. ``CREATE INDEX description_index ON table_name
  251. ((column_name -> path -> to ->> json_attribute ))``.
  252. Index can also be specified in partial way:
  253. ``CREATE INDEX description_index ON table_name
  254. ((column_name -> path -> to ->> json_attribute ))
  255. WHERE (column_name -> path -> to ->> json_attribute) IS NOT NULL``.