1

I am working in QGIS. I have a shapefile and its table and I need to export each row as a new file called "Fecha" field:

attribute_table

Finally, I will get 6 shapefiles such as 20130612.shp, 20130714.shp, ...etc

1
  • 6
    you can use split vector layer treatment in the toolbox ... – Snaileater Mar 26 at 13:36
2

A solution using the PyQGIS.

Let's assume there is a point layer called 'random_points_test' with its corresponding attribute table, see image below

input

Do not forget to change the layer and field names at the bottom of the code, i.e. split_layer('CMillor_ldcRefAuto_2013_WGSUTMH31','Fecha')

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

# imports
import os, sys

# setting up the input layer and defining required parameters
def split_layer(layer_name, field_name):
    # loading layer
    layer = QgsProject.instance().mapLayersByName(layer_name)[0]
    layer_fields = layer.fields()
    idx_of_that_field = layer_fields.indexFromName(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(field_name, i))
            writer = QgsVectorFileWriter.writeAsVectorFormat(layer, working_dir + "/{}.shp".format(i), "UTF-8", layer.crs(), driverName = "ESRI Shapefile", onlySelected = True)

    layer.removeSelection()

split_layer('random_points_test','Info')

Press Run script run script and get the output like this

result

A name of each output will match a pattern like '<attribute_value>.shp' e.g. 'Test 2', and should not '<attribute_field_name><attribute_value>.shp' i.e. 'Fecha_20130612.shp' etc.

If you want to change the directory where the output should be saved, please create this folder beforehand, see this thread for more details. And do not forget to apply changed in the writer = ....


References:

2
  • oh ... u really seem hungry ... – Snaileater Mar 26 at 15:09
  • but why? ... sorry i did not get this joke =( – Taras Mar 26 at 15:23
4

Open the Processing Toolbox and try the 'Split vector layer' tool.

Set the Unique "ID" field to Fecha and the Output file type to shp (or another format if you wish).

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.