Importing .shp files

Importing .shp files to MS SQL Server can be done in a number of ways.

In this article, we'll do it using ogr2ogr. We've chosen this command based tool because it's free and you likely have it installed on your system if you've done work with .shp files before.

If you need to install ogr2ogr, you can download OSGeo4W or QGIS and ogr2ogr will be a tool included in either package.

OSGeo4W is a basic set of spatial tools while QGIS is a full GUI application. QGIS may be a better choice because you can use it to answer many other spatial questions.

Using ogr2ogr

To use ogr2ogr, open the windows command window and issue a similar command to the one below.

"<where ogr2ogr is installed>\ogr2ogr.exe" -overwrite -f MSSQLSpatial "MSSQL:server=<your SQL Server>;database=<database to shp file is uploaded to>;trusted_connection=yes" -lco "GEOM_NAME=geom" "<location of the .shp file to be uploaded>" -nln <name of the table to be created>

An example of my upload script to upload a State.shp to a local instance of SQL Server Express in the database shp_northamerica, schema unitedstates andname the table unsiplified_state looks something like this:

"C:\Program Files\PostgreSQL\9.5\bin\ogr2ogr.exe" -overwrite -f MSSQLSpatial "MSSQL:server=localhost\SQLEXPRESS;database=shp_northamerica;trusted_connection=yes" -lco "GEOM_NAME=geom" "c:\Users\Trey\Desktop\Shp Files\State\Unsimplified\Source\State.shp" -nln unitedstates.unsimplified_state