[GIS] Why don’t the polygons from MSSQL2008 display in QGIS

qgissql server

I have imported a polygon shape file in MSSQL2008 using Shape2SQL loader without any issue. When I import the loaded polygon file in QGIS, I can't see it ! When I import other point or line features (previously loaded in MSSQL2008), I can see a few polygons from my file but not all of them.

At first I thought that the geometry of the poly file was not complete, so I repaired the geometry using ArcGIS 10.1 and reloaded the ""fixed geometry" file in MSSQL2008, and still am running in the same problem when I import the layer in QGIS.

One thing I have noticed in QGIS: when I connect to my MSQL2008 database, the points and line features have the geometry and SRID already determined but the polygon file requires the user to define the geometry type and SRID. I have tried selecting polygon and multipolygon type and set the SRID to 0 (like the other features in the DB) and still am running into problems viewing my poly file in QGIS. My original poly file has 1285 polygons in it.

I have tried using a subset of my original file and still am running into problems in QGIS.

Best Answer

I had the same problem and repairing invalid geometries within SQL Server (I'm using 2012) fixed it for me. Qgis was then also able to auto-detect the geometry type and srid

If running this query:

select * from yourgeotable
where geom.STIsValid() = 0;

returns any records, then you have invalid records.

Fix them with this query:

update yourgeotable
set geom = geom.MakeValid()
output inserted.id
where geom.STIsValid() = 0;

The Spatial Results tab in SSMS automatically uses MakeValid to repair geometries for display, so that can cause confusion as well. Since SQL Server is displaying the results, you may think they are valid when in fact SQL Server is repairing them for you.

Check out part 2 of the 2nd link posted below for more information on valid shapes and MakeValid.

Sources: