I want to export spatial data from SQL Server to ESRI shapefile and I have an issue :
ogr2ogr -f "ESRI Shapefile" " C:\Users\sqlexport.shp "
"MSSQL:server=PORT_7FMW8H2;database=testdbspatial;trusted_connection=yes;
"-sql "select * from DRShape" -overwrite
ERROR 1: Failed to create directory C:\Users\sqlexport.shp for shapefile datastore.
ERROR 1: ESRI Shapefile driver failed to create C:\Users\sqlexport.shp
-
You have written the -sql parameter wrong so it is omitted. When it is omitted ogr2ogr believes that you want to convert all the tables that it finds from your server and for that it wants to create a new directory that would get one shapefile per table.user30184– user301842018年04月06日 14:34:03 +00:00Commented Apr 6, 2018 at 14:34
-
so how can i write it ?amine bak– amine bak2018年04月10日 14:50:40 +00:00Commented Apr 10, 2018 at 14:50
-
Put a space chartacter before the dash of -sql.user30184– user301842018年04月11日 18:16:20 +00:00Commented Apr 11, 2018 at 18:16
2 Answers 2
If that is the exact command copied from your terminal, you simply need to be more careful with the spelling; there are spaces between quotation marks and the output file string. Try and replace your command with this one:
ogr2ogr -f "ESRI Shapefile" "C:\Users\sqlexport.shp" "MSSQL:server=PORT_7FMW8H2;database=testdbspatial;trusted_connection=yes;" -sql "select * from DRShape" -overwrite
If you have multiple geometry types in the table then you need to see Selecting feature types when using ogr2ogr to convert to shapefile?
-
1This error means that your table "DRShape" seems to contain both polygons and linestrings. They can't be saved into same shapefile. You must either sort the geometries by geometry type or use some other format that supports mixed geometries like GML or GeoPackage.user30184– user301842018年04月06日 14:37:26 +00:00Commented Apr 6, 2018 at 14:37
-
1@aminebak well, solve 1, get 3 is actually not the worst ratio in problem solving...,). you could try adding
-nlt POLYGON -skipfailures
to the above command (or LINESTRING if that is what you want). this will only process the given geometry type and skips all those that do not match (and other failures, make sure you double check that shapefile). in the end, your table has mixed geometries and cannot be converted into one shapefile as it is.geozelot– geozelot2018年04月06日 14:40:37 +00:00Commented Apr 6, 2018 at 14:40 -
1@aminebak those values are just hexadecimal representations of your geometries; the geom column can hold different geometry types, like LINEs and POLYGONs together, and the error messages tell you that there are indeed two types together. a shapefile does not allow different geometry types together. with
-nlt POLYGON
added to theogr2ogr
command, you specify that your shapefile will be of type POLYGON, and-skipfailures
will ignore all LINEs (which would otherwise lead to above errors).geozelot– geozelot2018年04月06日 14:52:53 +00:00Commented Apr 6, 2018 at 14:52 -
1Tbh this question is basically screwed beyond fixingIan Turton– Ian Turton2018年04月20日 19:01:27 +00:00Commented Apr 20, 2018 at 19:01
-
1@ThingumaBob similar but not the same. ogr2ogr -nlt polygon won't handle a geometryCollection correctly(at least in my experience). I suspect his union aggregate is merging a polygon and a linestring into a single GeometryCollection and not handling the metadata correctly when it breaks that out. He may need to explode collections.Brian W.– Brian W.2018年04月20日 19:57:59 +00:00Commented Apr 20, 2018 at 19:57
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 -- Add Alias!!
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
-
1Can you load the data into QGIS? You can always export it from there to SHP...Inactivated Account– Inactivated Account2018年04月06日 17:37:42 +00:00Commented Apr 6, 2018 at 17:37
-
1ladies and gentlemen, this is so much not the place to ask and discuss this...just you wait until a moderator comes along ,) @aminebak please ask this as a new question, let's keep this board like it's supposed to work. you'l get answers then, I promisegeozelot– geozelot2018年04月06日 19:53:05 +00:00Commented Apr 6, 2018 at 19:53
-
@ThingumaBob I'll be waiting!Inactivated Account– Inactivated Account2018年04月06日 21:52:27 +00:00Commented Apr 6, 2018 at 21:52
Explore related questions
See similar questions with these tags.