I'm trying to create a join/relate in QGIS where I have a shapefile of buildings and to that I would like to join a non-spatial table (.csv) containing people who work in each building. So I have multiple records in my table that I would like to be able to join to my single features in my shapefile. I can only run a join in QGIS where the first record is joined to the shapefile feature (ie building) but subsequent records get removed.

I am having the same problem and indeed joining tables work. The trick is to join the tables with multiple values to the table with the single value (here, from person to building, rather than the other way around). The problem, as far as I experience it, is that by doing this, you cannot then search and export the result as a georeferenced file, which is a bugger (at least to me). Any way to bypass this issue? marc
– user3555Jul 5 '11 at 11:14

What is the end-result you want to achieve? Even if you manage to do a join, do you want to present it on a map? Do statistics? Depending on that, different workflows may be followed, but using a database is usually a good idea if you want to make joins.
– johanvdwSep 27 '11 at 13:25

6 Answers
6

I solved this problem using QGIS 1.7.0 by first saving the vector layer as a comma separated values (CSV) table with the geometry field converted to well known text. To do this, the layer field of the "Save vector layer as..." dialog must read GEOMETRY=AS_WKT to create a well known text field of the geometry with the column named WKT.

The CSV geometry file was then joined to the non geometry attribute table in a one-to-many join under the properties joins tab of the non geometry attribute table.

The resulting join was saved as another geometry CSV file.

When this table is opened in QGIS each attribute record was represented by its appropriate vector geometry.

In doing all of this I discovered that The Delimited Text Plugin is not needed to open a CSV geometry file if the delimiters are commas and the WKT geometry field is named WKT.

Good approach, I did a similar thing just using ogr2ogr to create the CSV files from the Shapefiles. Then a simple R script to merge the files on the common field I wanted and write back to CSV. Then ogr2ogr again to create the merged shapefile.
– vpipktJan 4 '18 at 13:31

1

This may be rated the highest, but the process meant for this is answered by @underdark below. This is a hack and not at all the best option, it is slow and inefficient and results in larger files and more steps.
– nittyjeeJul 21 '18 at 5:02

Starting at version 2.2 QGIS supports one-to-many relations. Define them in the Project Properties though, not Vector Layer Properties. And then related records will appear in the layer's Attribute Table or Identify Results via the form.

You can't overcome the fact that you are trying to manage a 1 to many relationship.

For non-spatial to non-spatial it is worth the effort to learn. I find the biggest trick is making sure the join attribute fields have exactly the same terms.

Open the spatial located data as a layer in QGIS, then you select all the records in the attribute table in QGIS (clear selection, invert selection) and copy these into a new sheet in LibreOffice Calc. You will find the first field is WKT, which is a description of the feature which can be universally used in GIS. Calc will give you a dialog to import the pasted table, choose tab delimited, ensure comma is not ticked as it will chew the WKT field to bits. Then highlight this table in Calc, switch to Base, highlight the Tables icon on the left and paste. Base will go through a dialog to import the table. The WKT field can be retained through the join and when the file is imported back into QGIS as a .csv these are used to relocate the features. Just remember the projection.

In that way you will have full control of the join and can do it in "reverse". To do joins in Base ensure the unique data field (the 1) is a primary key and then you point it at the "many" if you are familiar with a 1 to many or 1 to n concept. Joins are set up by making relationships |TOOLS|. As I mentioned you will probably want to kick someone when you find the join fields have the same information but have used different text or syntax to represent it.

I don't think this is possible to link tables with a 1 to many relationship in QGIS. As an alternative, you may want to try gvSIG. Here you can make 1 to many links. After creating the link, selecting a feature in the map will highlight all related records in the linked table.