3

Here's what I'm trying to achieve:

  1. Load the required GIS layer (*.shp file)
  2. Load the .csv file in QGIS
  3. Perform the join based on common attributes in the two files
  4. Export the mapped .csv file as a .shp file

This is where I'm having trouble. The error that I'm getting is

"Invalid Data Source: C:/GIS Projects/Services.shp is not a valid or recognized data source"

I am so used to doing this easily in ArcGIS and MapInfo I don't understand why I cannot get this to work on QGIS. I have write permissions and everything and I can see 3/4 files being created in the specified folder just the .shp file is missing in the output folder.

  • Do not problematic space in the folder name? – Samane Aug 20 '15 at 6:46
12

You don't want to export csv, you should Save as... the shapefile which you joined the csv to. If you export csv table (which was not loaded with points geometry), there won't be any geometry and thus no shapefile.

So it should go like this:

  1. drag and drop shapefile and csv into QGIS
  2. Double click on shapefile in layers list, switch to joins tab
  3. join csv to shapefile table through common attribute
  4. right click on shapefile and Open attribute table to make sure it is joined as expected (now actually this might cause potentionally troubles because shapefiles/respectively dbf tables have only 10 characters limitation for column names and also limit for absolute length - good idea is rename csv to some very short name - only one character or so before starting the whole operation, or use the custom field name prefix when doing join)
  5. once all look good right click and Save as... on shapefile, by default QGIS will automatically load the new file after save
    • If you want to save only features with joined information from csv - meaning your csv has IDs only of some smaller subset of features:
    • Select only the ones joined through expression: "joined_column_name" is not NULL
    • Then click on Save as... and check Save only selected features

enter image description here

(If you try to save csv it will create only dbf (and projection files proj, qpj)) but won't create shapefile because csv is only table, which can be joined to shapefile but not the other way around - thus it will come up with error because there won't be any shp to load)

  • @Miro- Sorry, my bad. That's exactly what I was trying to do. Right click and click save as... It gives me that error message which I have shared in my question above. – VGu Aug 20 '15 at 11:24
  • @VGu just to make sure, when you right click - Save as... you are doing it on the existing shapefile, not the csv, correct? – Miro Aug 20 '15 at 11:30
  • @VGu I have added a detailed description, hope that helps to solve it for you, in my case saving shapefile with joined csv is smooth and the new shapefile is automatically loaded into QGIS with all the joined columns – Miro Aug 20 '15 at 12:09
  • @Miro- Thank you for your detailed response. It worked. But I am very surprised it works the other way around in QGIS. This is so much easier to perform in MapInfo and ArcGIS. Because I am trying to link a spreadsheet with just 7000 rows to a shapefile with 100,000 rows. This would mean I would have to export the new shapefile with the link to the csv and then subject it to further data cleansing. Am I correct in my thinking? – VGu Aug 24 '15 at 5:10
  • Thanks for your assistance. I have now run into another issue with this approach. My csv file has multiple rows for the same unique column ID. Let me try and explain the issue: 'ID Name Service Join Value' 'A AB 122 98' 'A AB 123 98' 'A AB 125 98' 'B AA 122 100' I want to join to these multiple records with the same ID. If I do it the other way around then join value is getting linked to the GIS layer with the first instance in the csv file. How do I go about sorting that out? – VGu Aug 24 '15 at 7:05
4

If you need to join csv to geometry file like shapefile (with attributes) in 1:N connection there is no simple way to do it as far as I am aware at this moment. One of the ways which I'm not going to describe in detail here is to import your csv as table into database together with your geometry features where you can do SQL queries and sure 1:N connections. But there is also other interesting workaround based on Joining multiple records to single feature using QGIS?

The answer there is not completely clear so I brake it into these steps:

  1. drag and drop shapefile and csv into QGIS
  2. save shapefile as new csv with Layer Options - GEOMETRY: AS_WKT and let it add back to map (it will ask for CRS - Coordinate reference system which you should use the same as your shapefile has)
  3. double click on your original csv in layers list, switch to joins tab
  4. join the csv with WKT geometry (which you created from shapefile) table through common attribute (ideally if it won't cause duplicates use custom field name prefix as empty field which will keep WKT column name only as WKT)
  5. save your original csv with joined columns as new csv, this time with Layer Options - GEOMETRY: default
  6. it should now load as new layer (it will ask for CRS, if you could not have the prefix empty and thus your column name in saved csv is not exactly named WKT, rename the column name in some text editor to WKT)
  7. Now you can finally save this final layer as shapefile and it will have geometry and attributes from both orginal csv and shapefile, before or after that you can delete the WKT column which is not needed anymore because the WKT geometry column was already used to create features geometry in this file

There is a feature request on QGIS hub to make this as simple as the standard 1:1 join but for some reason it is not done and is closed here: https://hub.qgis.org/issues/11505

  • Thanks a lot Miro. This worked perfectly. I think I overestimated the ability of QGIS to handle these tasks that I am routinely used to doing so easily on the fly using ArcGIS and MapInfo. Whilst it is a workaround and a bit challenging it is fun and rewarding nonetheless. I am sure this process can be automated using Python. That will take a bit of time for me though. Thank you once again... – VGu Aug 25 '15 at 0:13
  • @VGu no worries, yes you can use python. Also for many things graphical modeler under Processing if you are less experienced with programming. – Miro Aug 25 '15 at 0:37

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.