[GIS] How to specify fields to export from QGIS

exportfields-attributesqgis

I'm using QGIS 2.0.1 and am connected to a MS SQL Server spatially enabled database (not ESRI). I am able to connect to the layer and load it into my TOC just fine. I have hidden a number of fields that are not important for visibility sake.

Now I want to export that data to another data type (we'll use shapefile for this example). When I right-click the layer, choose "Save As" and define the output file, I get the following error:

Export to vector file failed. Error: unsupported type for field GlobalID

Note 1: If I check "Skip attribute creation" in the dialog box, the shapefile is created with no errors.

Note 2: ArcGIS does not have this problem, it just converts the GUID field to "TEXT" type.

Looking at the table, one of the fields that I have hidden is a GUID of SQL type <uniqueidentifier>, which is in all likelyhood the culprit. In an attempt to resolve this, I made the field "hidden". I would have assumed that because the field was hidden, QGIS would not try to export this field, but that turned out to not be the case.

Can you define the fields you want to export from QGIS?

Is it possible for QGIS to export just the "Visible" fields? Or is there another alternative way of doing this from QGIS (maybe define the fields via an ogr2ogr query)? This seems like a shortcoming of QGIS if I cannot define which fields to export.

UPDATE: I did a test using a sample shapefile to see if I could limit the output fields. The following ogr2ogr command line worked and eliminated all fields except for "name":

ogr2ogr -f "ESRI Shapefile" test.shp city.shp -sql "select name from city"

but using QGIS and adding:

-sql "select name from city"

to the "layer" section of the "Save As" dialog did not result in a shapefile with just the single field. So I don't really see how I can manipulate the file output to show just a select few fields. Any help is appreciated.

Best Answer

If it works for you in command line with ogr2ogr, I think it is the best solution to use that. You can write a simple bat file to create a shapefile from your MSSQL database. Obviously you can not edit the data and save back to the server that way.

QGIS has the Data Source and Layer input boxes, but those are only for -dsco and -lco parameters of ogr2ogr. The OGR shapefile driver has no useful dsco or lco for your needs:

http://www.gdal.org/ogr2ogr.html

http://www.gdal.org/ogr/drv_shapefile.html

Alternatively, it should be possible to write a python plugin that requests the data from the server with a SQL SELECT command at your needs. This would even enable data editing.

Related Question