Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a shape file into SQL Server?

In the past I have used shape2sql to import shape files into SQL Server.

I tried that route again on a newer Windows 10 box, and nothing seems to happen when trying to use that application.

Any suggestions for either getting shape2sql to work, or alternative ways to upload shape files into SQL Server?

like image 638
Jereme Avatar asked Apr 23 '26 11:04

Jereme


2 Answers

So, it turns out that ogr2ogr has the ability to import directly into a SQL Server database now. I created a temporary database and did the following:

  • Install ogr2ogr via the GDAL core MSI installer from http://www.gisinternals.com/release.php
  • Open a command line to the installed path
  • Run the following:

ogr2ogr -overwrite -f MSSQLSpatial -lco “GEOM_TYPE=geography” -a_srs "EPSG:4326" "MSSQL:server=.;database=temp_import;trusted_connection=yes" "[path to shape file]"

like image 60
Jereme Avatar answered Apr 30 '26 04:04

Jereme


I've managed to import successfully on remote server. Here goes procedure

  1. Install QGIS Desktop 3.10.5..or whatever version is actual.
  2. How to find out what goes to parameter -a_srs, in my casa that is : -a_srs "EPSG:3765":

Open QGIS Desktop 3.10.5, open new project and drag-drop your shape file to Layers left sidebar. Right click on that layer and go to Properties. There you will be able to read your -a_srs information.

enter image description here

  1. Run command in CMD:

    C:\Program Files\QGIS 3.10\bin>ogr2ogr -progress -f "MSSQLSpatial" "MSSQL:server=xxx.xxx.xxx.xxx,yyyy;database=yourDB;UID=yourUse;PWD=yourPassword" "C:\Users\Username\Desktop\ShapeFiles\Name_of_shape_file.shp" -a_srs "EPSG:3765" -lco PRECISION=NO

Where xxx.xxx.xxx.xxx - IP address of your server yyyy - port number of your server if you are running SQL Server on different port than standard (I think you can omit that in case you are)

Output: 0...10...20...30...40...50...Warning 1: Ring Self-intersection at or near point 664451.64629999956 5053409.8562000087 60...70...80...90...100 - done.

like image 27
Hrvoje Avatar answered Apr 30 '26 05:04

Hrvoje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!