13
13
:Contact: jmckenna at gatewaygeomatics.com
14
14
:Author: Seth Girvin
15
15
:Contact: sethg at geographika.co.uk
16
- :Last Updated: 2021-05-05
16
+ :Last Updated: 2024-09-22
17
17
18
18
.. contents::
19
19
:depth: 4
@@ -32,17 +32,17 @@ columns, which is compiled as a plugin ("msplugin_mssql2008.dll").
32
32
33
33
More Information:
34
34
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)
37
37
* :ref:`vector` (MapServer Vector Data Access Guide)
38
38
39
39
Creating Spatial Data Tables in MSSQL
40
40
=====================================
41
41
42
42
There 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
46
46
a shapefile (province.shp) into an MSSQL instance:
47
47
48
48
::
@@ -69,28 +69,28 @@ examples:
69
69
70
70
Server=55.55.55.55\SQLEXPRESS,1433;uid=a_user;pwd=a_password;
71
71
database=a_database;Integrated Security=True
72
-
72
+
73
73
If you don't specify Driver in the connection string, it uses the "SQL Server" driver (sqlsrv32.dll)
74
74
which was last updated in 2010. To use a newer driver you can specify this in the connection string:
75
75
76
76
::
77
77
78
78
driver={ODBC Driver 17 for SQL Server};server=55.55.55.55\SQLEXPRESS,1433;uid=a_user;pwd=a_password;
79
79
database=a_database;Integrated Security=False
80
-
81
- The "SQL Server Native Client" drivers are also supported.
82
80
83
-
81
+ The "SQL Server Native Client" drivers are also supported.
82
+
83
+
84
84
OPTION 1: Connect Through OGR
85
85
-----------------------------
86
86
87
87
GDAL/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>`__.
89
89
90
90
Verify Local Support for MSSQLSpatial
91
91
*************************************
92
92
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;
94
94
the response should contain "MSSQLSpatial" such as:
95
95
96
96
::
@@ -102,23 +102,23 @@ the response should contain "MSSQLSpatial" such as:
102
102
...
103
103
-> "MSSQLSpatial" (read/write)
104
104
...
105
-
105
+
106
106
Test OGR Connection Parameters
107
107
******************************
108
108
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:
110
110
111
111
::
112
112
113
113
ogrinfo "MSSQL:server=.\SQLEXPRESS;database=geo;trusted_connection=yes" province -summary
114
-
114
+
115
115
Create MapServer Layer using CONNECTIONTYPE OGR
116
116
***********************************************
117
117
118
118
Your layer should contain a CONNECTIONTYPE OGR statement, as well as a CONNECTION.
119
119
The connection should also contact a "tables=" parameter, and also the name of the
120
120
geometry 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:
122
122
123
123
.. code-block:: mapfile
124
124
@@ -142,9 +142,9 @@ define an sql select statement starting with the 'WHERE' keyword. For example:
142
142
END
143
143
PROCESSING 'CLOSE_CONNECTION=DEFER'
144
144
END # layer
145
-
146
145
147
- .. NOTE::
146
+ .. note::
147
+
148
148
The usual CONNECTIONTYPE terms 'using unique' and 'using srid' are not meaningful
149
149
for the OGR driver in this case, as these parameters are automatically retrieved
150
150
from the 'geometry_columns' metadata table.
@@ -154,6 +154,22 @@ define an sql select statement starting with the 'WHERE' keyword. For example:
154
154
OPTION 2: Connect Through MapServer Plugin
155
155
------------------------------------------
156
156
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
+
157
173
Create MapServer Layer
158
174
**********************
159
175
@@ -167,32 +183,32 @@ configured to access MSSQL as follows:
167
183
TYPE POLYGON
168
184
STATUS DEFAULT
169
185
CONNECTIONTYPE PLUGIN
170
- PLUGIN "msplugin_mssql2008.dll "
186
+ PLUGIN "mssql "
171
187
CONNECTION "Server=.\MSSQLSERVER2008;Database=Maps;Integrated Security=true"
172
188
DATA "ogr_geometry from rivers USING UNIQUE ogr_fid USING SRID=4326"
173
189
...
174
190
END
175
191
176
192
The 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
179
195
geometry 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
181
197
features in the feature query operations.
182
198
183
199
The DATA section should also contain the spatial reference id (SRID)
184
200
of the features in the data table The SRID is used when specifying the
185
201
search shapes during the intersect operations which should match with
186
202
the 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
188
204
section the diver will use SRID=0 when defining the search shapes.
189
205
190
206
Selecting the Type of the Geometry Column
191
207
*****************************************
192
208
193
209
For 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
196
212
"geography" we must specify the data type in the DATA section
197
213
explicitly, like::
198
214
@@ -205,19 +221,14 @@ On Windows platforms the DLLs needed by the program are searched for
205
221
in the following order:
206
222
207
223
1) The directory from which the application loaded.
208
-
209
224
2) The current directory.
210
-
211
225
3) The system directory. Use the `GetSystemDirectory
212
226
<http://msdn.microsoft.com/en-us/library/ms724373.aspx>`__ function
213
227
to get the path of this directory.
214
-
215
228
4) The 16-bit system directory.
216
-
217
229
5) The Windows directory. Use the `GetWindowsDirectory
218
230
<http://msdn.microsoft.com/en-us/library/ms724454.aspx>`__ function
219
231
to get the path of this directory.
220
-
221
232
6) The directories that are listed in the PATH environment variable.
222
233
223
234
Binaries Containing the MSSQL Plugin
@@ -226,7 +237,7 @@ Binaries Containing the MSSQL Plugin
226
237
Currently the following binary distributions contain
227
238
msplugin_mssql2008.dll:
228
239
229
- - `GISInternals <http ://www.gisinternals.com>`__
240
+ - `GISInternals <https ://www.gisinternals.com/ >`__
230
241
- `MS4W <https://ms4w.com>`__
231
242
232
243
Using Spatial Indexes
@@ -237,16 +248,14 @@ be created to the geometry column which could easily be done with the
237
248
OGR MSSQL Spatial driver like::
238
249
239
250
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
+
243
253
In 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 -
245
255
however - we should force the optimizer to use the spatial index by
246
256
specifying the index hint in the DATA section like::
247
257
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"
250
259
251
260
Layer Processing Options
252
261
------------------------
0 commit comments