i did a join between 2 tables departements2 which has all department with their id and geometry with BM_REGIONFR with id of department and region ( i have 5 region in each region many departement )

 select b.[Region],geometry::UnionAggregate(geom.MakeValid()) AS Geo
 into dbo.DRShape from [dbo].[departements2] a 
 join [dbo].[BM_REGIONFR] b on a.[code_insee] = b.[dep_2] group by b.Region

After that i wanted to export the shapefile with OSGeo4w in order to have the map with the right decomposition and thats the query i used

ogr2ogr -f "ESRI Shapefile" "C:\Users\sqlexport.shp" "MSSQL:server=PORT_7FMW8H2;database=testdbspatial;trusted_connection=yes;" -sql "select * from DRShape" -overwrite

And i got these ERRORS

ERROR 1: Attempt to write non-polygon (LINESTRING) geometry to POLYGON type shapefile. ERROR 1: Unable to write feature 1 from layer dbo.DRShape. ERROR 1: Terminating translation prematurely after failed


You're somehow ending up with different types of geometry inside that table. Run this to determine what types are in your table:

select distinct(geom.STGeometryType()) from DRShape

If you have something called a GeometryCollection you're probably creating it when you do your merge(but not necessarily). A GeometryCollection is valid in SQL but not in a shapefile and mixes geometry types within a single row. If you have these they will need to be exploded into individual parts before you can make your shape file.

If you only have Polygons or Linestrings(anything not a GeometryCollection) returned by the above query you just need to add a where clause to your sql query. (and possibly export two shapefiles if you need both datatypes). Your sql query should look like this:

-sql "select * from DRShape where geom.STGeometryType() = 'Polygon'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.