2

Is it possible in PyQGIS to have in one layer more polygons and some would have one set of attributes and another different?

E.g. Polygon_1(name, size, average_age), Polygon_2(name, size, average_height), Polygon3(same as Polygon1)...

I imagine that I would create two tables and when I create a new polygon I somehow link it together.
Is anything like that possible? Or do I have to have one layer for each set of attributes?

2

You can have multiple layers (or shapefiles) with different attributes. It sounds like from your description that you want to do a JOIN - which is basically connecting a number of layers together by a common attribute.

So you could, for example, have a single layer which is joined to a number of other layers by using the name attribute. Then that layer will contain all attributes corresponding to the name (it is ideal to use a joining attribute that is unique, otherwise you may not get the correct average height for the correct name).

Joining options

Hope this helps!


EDIT:

@MatthiasKuhn posted an answer to a question I asked using PyQGIS to join layers together. Since you included a pyqgis tag, I thought the following post might help in that regard.

QGIS 2.2 - possible to do a JOINS from the Action list?

  • 2
    Just to clarify because I think this is what you mean: one shapefile with only the name attribute, and then a few other a-spatial tables that each have the name attribute but different sets of information. Then add the shapefile a few times to create separate layers and join each a-spatial table to a different layer. So: one shapefile, many tables. – mr.adam Jul 30 '15 at 13:17
  • @mr.adam - Thanks for your comment buddy! Just want to ask, why create separate layers for the shapefile and then join each of those layers with each a-spatial table? Wouldn't it be easier to have a single shapefile joined to multiple a-spatial tables? – Joseph Jul 30 '15 at 13:35
  • I guess it just comes down to what the OP wants, I may have misinterpreted the desired outcome. I was just thinking of a way that the same geometry could be used with layers that have different sets of fields. – mr.adam Jul 30 '15 at 14:40
  • @mr.adam - You should post your suggestion as an answer (I will upvote it) as the method you described could probably be used for PyQGIS :) – Joseph Jul 30 '15 at 14:48
  • Thanks, @mr.adam got it right. Could you also give me a hint how to easily create a-spatial table in PyQGis? – Aerov Jul 30 '15 at 17:48
2

Here's what I was thinking, and it's based on the way a lot of attribute-heavy government data is official disseminated (like US soil surveys, etc.):

--Create a single shapefile with an attribute Name to hold your geometry.

--Create series of a-spatial tables, each with different fields but all with the Name field (sorry, not familiar with how to do this in pyqgis, but you could just write .dbfs with a Python dbf module)

--Add the shapefile multiple times to the project (so you have multiple layers referencing the same shapefile)

--Join a different a-spatial table to each layer.

Now you have multiple layers, each with a different set of attributes, all using the same geometry. So if you edit the geometry of features in one layer, it will be reflected in all the others.

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.