Home | Docs | Issue Tracker | FAQ | Download | |
Author: | Tamas Szekeres |
---|---|
Contact: | szekerest at gmail.com |
Author: | Jeff McKenna |
Contact: | jmckenna at gatewaygeomatics.com |
Last Updated: | 2012-09-26 |
Contents
Microsoft SQL Server 2008+ supports storing spatial data by using the built-in geometry/geography data types. MapServer can connect to MSSQL through either: 1) an OGR connectiontype, or 2) a driver that accesses these tables containing spatial columns, which is compiled as a plugin (“msplugin_mssql2008.dll”).
There are several ways to create spatial data tables in MSSQL 2008. You can easily upload existing data to an MSSQL table by using the ogr2ogr commandline tool and the OGR’s MSSQL Spatial driver Here is an example that uploads a shapefile (province.shp) into an MSSQL 2008 instance:
ogr2ogr -f MSSQLSpatial -a_srs EPSG:4326 "MSSQL:server=.\SQLEXPRESS;database=geo;trusted_connection=yes" province.shp
In order to connect to the MSSQL 2008 spatial database you should set up a valid connection string to the database like the following examples:
Server=.\MSSQLSERVER2008;Database=Maps;Integrated Security=true
Server=55.55.55.55,1433;uid=a_user;pwd=a_password;database=a_database;
Integrated Security=True
Server=55.55.55.55\SQLEXPRESS,1433;uid=a_user;pwd=a_password;
database=a_database;Integrated Security=True
GDAL/OGR (and therefore MapServer) can read spatial tables in MSSQL 2008 through the MSSQLSpatial driver.
Use the command “ogrinfo –formats” to verify that your local GDAL is built with support for MSSQL; the response should contain “MSSQLSpatial” such as:
Supported Formats:
-> "OCI" (read/write)
-> "ESRI Shapefile" (read/write)
-> "MapInfo File" (read/write)
...
-> "MSSQLSpatial" (read/write)
...
Use the ogrinfo commandline utility to test your connection through the MSSQLSpatial driver, such as:
ogrinfo "MSSQL:server=.\SQLEXPRESS;database=geo;trusted_connection=yes" province -summary
Your layer should contain a CONNECTIONTYPE OGR statement, as well as a CONNECTION. The connection should also contact a “tables=” parameter, and also the name of the geometry column in brackets. You do not need to specify the DATA parameter unless you define an sql select statement starting with the ‘WHERE’ keyword. For example:
LAYER
NAME "provinces"
TYPE POLYGON
STATUS ON
####
CONNECTIONTYPE OGR
CONNECTION "MSSQL:server=.\SQLEXPRESS;uid=xx;pwd=xxx;database=geo;trusted_connection=yes;tables=province(ogr_geometry)"
####
PROJECTION
"init=epsg:4326"
END
CLASS
NAME "Land"
STYLE
COLOR 240 240 240
OUTLINECOLOR 199 199 199
END
END
PROCESSING 'CLOSE_CONNECTION=DEFER'
END # layer
Note
The usual CONNECTIONTYPE terms ‘using unique’ and ‘using srid’ are not meaningful for the OGR driver in this case, as these parameters are automatically retrieved from the ‘geometry_columns’ metadata table.
Once the connection can be established to the server the layer can be configured to access MSSQL2008 as follows:
LAYER
NAME "rivers_mssql_spatial"
TYPE POLYGON
STATUS DEFAULT
CONNECTIONTYPE PLUGIN
PLUGIN "msplugin_mssql2008.dll"
CONNECTION "Server=.\MSSQLSERVER2008;Database=Maps;Integrated Security=true"
DATA "ogr_geometry from rivers USING UNIQUE ogr_fid USING SRID=4326"
...
END
The DATA parameter is used to perform the SQL select statement to access your table in MSSQL. The geometry column is required in the select statement; in the above example the ogr_geometry column is the geometry column in the rivers table. The table should also have an unique column (ogr_fid) which is provided for random access to the features in the feature query operations.
The DATA section should also contain the spatial reference id (SRID) of the features in the data table The SRID is used when specifying the search shapes during the intersect operations which should match with the SRID of the features otherwise no features are returned in a particular query. if you omit specifying the SRID value in the DATA section the diver will use SRID=0 when defining the search shapes.
For the geometry columns MSSQL supports 2 data types: “geometry” and “geography”. By default the driver considers the type of the geometry column is “geometry”. In case if the type of the geometry column is “geography” we must specify the data type in the DATA section explicitly, like:
DATA "ogr_geometry(geography) from rivers USING UNIQUE ogr_fid USING SRID=4326"
On Windows platforms the DLLs needed by the program are searched for in the following order:
Currently the following binary distributions contain msplugin_mssql2008.dll:
In order to speed up the access to the features a spatial index should be created to the geometry column which could easily be done with the OGR MSSQL Spatial driver like:
ogrinfo -sql "create spatial index on rivers"
"MSSQL:server=.\MSSQLSERVER2008;database=Maps;
Integrated Security=true"
In general we can safely rely on the query optimizer to select the most appropriate index in the sql query operations. In some cases - however - we should force the optimizer to use the spatial index by specifying the index hint in the DATA section like:
DATA "ogr_geometry from rivers using index ogr_geometry_sidx
USING UNIQUE ogr_fid USING SRID=4326"
We can control the behaviour of the MSSQL driver by using the following PROCESSING options: