2

I have a shapefile with a column that have project references, I want to create a different shapefile for every project reference there is.

Note that the project references are not unique in the sense that there are multiple lines with the same reference.

Input example:

ID ref clmnC
1 001 abcd
2 002 addd
3 001 dsdd
4 003 sdsd
5 003 rzer
6 002 qdqd
7 001 ssqz

Output wanted (3 tables in this example):

  ref clmnC          ref clmnC           ref clmnC
  001 abcd           002 addd            003 sdsd
  001 dsdd           002 qdqd            003 rzer
  001 ssqz

I tried listing unique values but that only return a table with only unique project references values, any solution to solve this problem using the graphical modeler in QGIS 2.18?

Taras
  • 32,823
  • 4
  • 66
  • 137
Ayogo
  • 221
  • 4
  • 18

2 Answers2

2

I found the solution, using the 'Split Vector Layer' tool, I was able to split my datasets based on the attribute I wanted.

split_in_GM

Taras
  • 32,823
  • 4
  • 66
  • 137
Ayogo
  • 221
  • 4
  • 18
1

As an alternative using PyQGIS where Graphical Modeler is not required.

Let's assume there is a point layer 'points' with its corresponding attribute table accordingly, see image below.

input

Task: Split layer into several layers by "value" attribute and delete "id" field for each of it.

Proceed with Plugins > Python Console > Show Editor > New Editor and paste the script below

# imports
import os, sys

class SplitLayerToMany: # initiating layer's and field's names provided from user def init(self, layer_name, field_name): self.layer_name = layer_name self.field_name = field_name

# setting up the input layer and defining required parameters
def split_layer(self):
    # loading layer
    layer = QgsProject.instance().mapLayersByName(self.layer_name)[0]
    layer_fields = layer.fields()
    idx_of_that_field = layer_fields.indexFromName(self.field_name)

    if not layer.isValid():
        raise ValueError("Layer failed to load!")
        sys.exit()

    # getting a set with unique key attributes
    list_attributes = []
    for feat in layer.getFeatures():
        list_attributes.append(feat.attributes()[idx_of_that_field])
    list_attributes = set(list_attributes)

    # checking a number of features in the input layer
    if layer.featureCount() < 2:
        raise ValueError("Layer contains only one feature. No reason to double save it")

    else:
        # getting a working dir, where the input layer is stored
        path_to_file = layer.dataProvider().dataSourceUri()
        working_dir = os.path.split(path_to_file)[0]

        # looping over attributes from the set, selecting features and saving them as a new single layer
        for i in list_attributes:
            selection = layer.selectByExpression('{0}=\'{1}\''.format(self.field_name, i))
            writer = QgsVectorFileWriter.writeAsVectorFormat(layer, working_dir + "/{}.shp".format(i), "UTF-8", layer.crs(), driverName = "ESRI Shapefile", onlySelected = True)

    layer.removeSelection()

my_class = SplitLayerToMany('points', 'value') my_class.split_layer()

Press Run script run script and get the output

output_folder

output_qgis


References:

Taras
  • 32,823
  • 4
  • 66
  • 137