1313:Contact: jmckenna at gatewaygeomatics.com
1414:Author: Seth Girvin
1515:Contact: sethg at geographika.co.uk
16- :Last Updated: 2021-05-05
16+ :Last Updated: 2024-09-22
1717
1818.. contents::
1919 :depth: 4
@@ -32,17 +32,17 @@ columns, which is compiled as a plugin ("msplugin_mssql2008.dll").
3232
3333More Information:
3434
35- * `OGR MSSQL Spatial driver page <https://gdal.org/drivers/vector/mssqlspatial.html>`__ (describes the OGR MSSQL support)
36- * `ogr2ogr application <https://gdal.org/programs/ogr2ogr.html>`__ (describes the ogr2ogr commandline application)
35+ * `OGR MSSQL Spatial driver page <https://gdal.org/en/latest/ drivers/vector/mssqlspatial.html>`__ (describes the OGR MSSQL support)
36+ * `ogr2ogr application <https://gdal.org/en/latest/ programs/ogr2ogr.html>`__ (describes the ogr2ogr commandline application)
3737* :ref:`vector` (MapServer Vector Data Access Guide)
3838
3939Creating Spatial Data Tables in MSSQL
4040=====================================
4141
4242There are several ways to create spatial data tables in MSSQL. You can easily upload existing data to an MSSQL
43- table by using the `ogr2ogr <https://gdal.org/programs/ogr2ogr.html>`__
44- commandline tool and the OGR's `MSSQL Spatial driver
45- <https://gdal.org/drivers/vector/mssqlspatial.html>`__. Here is an example that uploads
43+ table by using the `ogr2ogr <https://gdal.org/en/latest/ programs/ogr2ogr.html>`__
44+ command line tool and the OGR's `MSSQL Spatial driver
45+ <https://gdal.org/en/latest/ drivers/vector/mssqlspatial.html>`__. Here is an example that uploads
4646a shapefile (province.shp) into an MSSQL instance:
4747
4848::
@@ -69,28 +69,28 @@ examples:
6969
7070 Server=55.55.55.55\SQLEXPRESS,1433;uid=a_user;pwd=a_password;
7171 database=a_database;Integrated Security=True
72-
72+
7373If you don't specify Driver in the connection string, it uses the "SQL Server" driver (sqlsrv32.dll)
7474which was last updated in 2010. To use a newer driver you can specify this in the connection string:
7575
7676::
7777
7878 driver={ODBC Driver 17 for SQL Server};server=55.55.55.55\SQLEXPRESS,1433;uid=a_user;pwd=a_password;
7979 database=a_database;Integrated Security=False
80-
81- The "SQL Server Native Client" drivers are also supported.
8280
83-
81+ The "SQL Server Native Client" drivers are also supported.
82+
83+
8484OPTION 1: Connect Through OGR
8585-----------------------------
8686
8787GDAL/OGR (and therefore MapServer) can read spatial tables in MSSQL 2008 through the
88- `MSSQLSpatial driver <https://gdal.org/drivers/vector/mssqlspatial.html>`__.
88+ `MSSQLSpatial driver <https://gdal.org/en/latest/ drivers/vector/mssqlspatial.html>`__.
8989
9090Verify Local Support for MSSQLSpatial
9191*************************************
9292
93- Use the command * ogrinfo --formats* to verify that your local GDAL is built with support for MSSQL;
93+ Use the command `` ogrinfo --formats`` to verify that your local GDAL is built with support for MSSQL;
9494the response should contain "MSSQLSpatial" such as:
9595
9696::
@@ -102,23 +102,23 @@ the response should contain "MSSQLSpatial" such as:
102102 ...
103103 -> "MSSQLSpatial" (read/write)
104104 ...
105-
105+
106106Test OGR Connection Parameters
107107******************************
108108
109- Use the *ogrinfo* commandline utility to test your connection through the MSSQLSpatial driver, such as:
109+ Use the *ogrinfo* command line utility to test your connection through the MSSQLSpatial driver, such as:
110110
111111::
112112
113113 ogrinfo "MSSQL:server=.\SQLEXPRESS;database=geo;trusted_connection=yes" province -summary
114-
114+
115115Create MapServer Layer using CONNECTIONTYPE OGR
116116***********************************************
117117
118118Your layer should contain a CONNECTIONTYPE OGR statement, as well as a CONNECTION.
119119The connection should also contact a "tables=" parameter, and also the name of the
120120geometry column in brackets. You do not need to specify the DATA parameter unless you
121- define an sql select statement starting with the 'WHERE' keyword. For example:
121+ define an SQL select statement starting with the 'WHERE' keyword. For example:
122122
123123.. code-block:: mapfile
124124
@@ -142,9 +142,9 @@ define an sql select statement starting with the 'WHERE' keyword. For example:
142142 END
143143 PROCESSING 'CLOSE_CONNECTION=DEFER'
144144 END # layer
145-
146145
147- .. NOTE::
146+ .. note::
147+
148148 The usual CONNECTIONTYPE terms 'using unique' and 'using srid' are not meaningful
149149 for the OGR driver in this case, as these parameters are automatically retrieved
150150 from the 'geometry_columns' metadata table.
@@ -154,6 +154,22 @@ define an sql select statement starting with the 'WHERE' keyword. For example:
154154OPTION 2: Connect Through MapServer Plugin
155155------------------------------------------
156156
157+ As of MapServer 8.0 plugins can only be used when referenced in the MapServer :ref:`CONFIG <config>`
158+ file. The ``PLUGINS`` section needs to contain a key - a name for the driver, and a the path to the DLL.
159+ For example:
160+
161+ .. code-block:: mapfile
162+
163+ CONFIG
164+ ...
165+ PLUGINS
166+ "mssql" "C:\MapServer\bin\ms\plugins\mssql2008\msplugin_mssql2008.dll"
167+ END
168+ ...
169+
170+ The key ``mssql`` can then be used in any Mapfiles to refer to this DLL. Controlling which DLLs are loaded by MapServer
171+ in the ``CONFIG`` file prevents Mapfiles from loading potentially dangerous DLLs.
172+
157173Create MapServer Layer
158174**********************
159175
@@ -167,32 +183,32 @@ configured to access MSSQL as follows:
167183 TYPE POLYGON
168184 STATUS DEFAULT
169185 CONNECTIONTYPE PLUGIN
170- PLUGIN "msplugin_mssql2008.dll "
186+ PLUGIN "mssql "
171187 CONNECTION "Server=.\MSSQLSERVER2008;Database=Maps;Integrated Security=true"
172188 DATA "ogr_geometry from rivers USING UNIQUE ogr_fid USING SRID=4326"
173189 ...
174190 END
175191
176192The DATA parameter is used to perform the SQL select statement to
177- access your table in MSSQL. The geometry column is required in the
178- select statement; in the above example the ogr_geometry column is the
193+ access your table in MSSQL. The geometry column is required in the
194+ select statement; in the above example the `` ogr_geometry`` column is the
179195geometry column in the rivers table. The table should also have an
180- unique column (ogr_fid) which is provided for random access to the
196+ unique column (`` ogr_fid`` ) which is provided for random access to the
181197features in the feature query operations.
182198
183199The DATA section should also contain the spatial reference id (SRID)
184200of the features in the data table The SRID is used when specifying the
185201search shapes during the intersect operations which should match with
186202the SRID of the features otherwise no features are returned in a
187- particular query. if you omit specifying the SRID value in the DATA
203+ particular query. If you omit specifying the SRID value in the DATA
188204section the diver will use SRID=0 when defining the search shapes.
189205
190206Selecting the Type of the Geometry Column
191207*****************************************
192208
193209For the geometry columns MSSQL supports 2 data types: "geometry" and
194- "geography". By default the driver considers the type of the geometry
195- column is "geometry". In case if the type of the geometry column is
210+ "geography". By default the driver considers the type of the geometry
211+ column is "geometry". In case if the type of the geometry column is
196212"geography" we must specify the data type in the DATA section
197213explicitly, like::
198214
@@ -205,19 +221,14 @@ On Windows platforms the DLLs needed by the program are searched for
205221in the following order:
206222
2072231) The directory from which the application loaded.
208-
2092242) The current directory.
210-
2112253) The system directory. Use the `GetSystemDirectory
212226 <http://msdn.microsoft.com/en-us/library/ms724373.aspx>`__ function
213227 to get the path of this directory.
214-
2152284) The 16-bit system directory.
216-
2172295) The Windows directory. Use the `GetWindowsDirectory
218230 <http://msdn.microsoft.com/en-us/library/ms724454.aspx>`__ function
219231 to get the path of this directory.
220-
2212326) The directories that are listed in the PATH environment variable.
222233
223234Binaries Containing the MSSQL Plugin
@@ -226,7 +237,7 @@ Binaries Containing the MSSQL Plugin
226237Currently the following binary distributions contain
227238msplugin_mssql2008.dll:
228239
229- - `GISInternals <http ://www.gisinternals.com>`__
240+ - `GISInternals <https ://www.gisinternals.com/ >`__
230241- `MS4W <https://ms4w.com>`__
231242
232243Using Spatial Indexes
@@ -237,16 +248,14 @@ be created to the geometry column which could easily be done with the
237248OGR MSSQL Spatial driver like::
238249
239250 ogrinfo -sql "create spatial index on rivers"
240- "MSSQL:server=.\MSSQLSERVER2008;database=Maps;
241- Integrated Security=true"
242-
251+ "MSSQL:server=.\MSSQLSERVER2008;database=Maps;Integrated Security=true;"
252+
243253In general we can safely rely on the query optimizer to select the
244- most appropriate index in the sql query operations. In some cases -
254+ most appropriate index in the SQL query operations. In some cases -
245255however - we should force the optimizer to use the spatial index by
246256specifying the index hint in the DATA section like::
247257
248- DATA "ogr_geometry from rivers using index ogr_geometry_sidx
249- USING UNIQUE ogr_fid USING SRID=4326"
258+ DATA "ogr_geometry from rivers using index ogr_geometry_sidx USING UNIQUE ogr_fid USING SRID=4326"
250259
251260Layer Processing Options
252261------------------------
0 commit comments