3

Using QGIS 3.6.3, I have a layer with ten polygons. I want to use split vector layer to split the file into ten shapefiles, But the result output file is a Geopackage.

How can I get ten shapefiles by using split vector layer?

enter image description here

enter image description here

enter image description here

6

If you look at the Split vector layer tool script, you will see that .gpkg is hard-coded inside the script. This means there is no way to change the output format of the file directly from the GUI of script tool.

However, if you want the output to be shapefile not Geopakage, you need to change that inside the script. The script is called VectorSplit which is located - inside my machine - in the following path:

C:\OSGeo4W64\apps\qgis-ltr\python\plugins\processing\algs\qgis\VectorSplit.py

When you open the script in the text editor like Notepad++ look at the last defined function which is the following:

def processAlgorithm(self, parameters, context, feedback):
    source = self.parameterAsSource(parameters, self.INPUT, context)
    if source is None:
        raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

    fieldName = self.parameterAsString(parameters, self.FIELD, context)
    directory = self.parameterAsString(parameters, self.OUTPUT, context)

    mkdir(directory)

    fieldIndex = source.fields().lookupField(fieldName)
    uniqueValues = source.uniqueValues(fieldIndex)
    baseName = os.path.join(directory, '{0}'.format(fieldName))

    fields = source.fields()
    crs = source.sourceCrs()
    geomType = source.wkbType()

    total = 100.0 / len(uniqueValues) if uniqueValues else 1
    output_layers = []

    for current, i in enumerate(uniqueValues):
        if feedback.isCanceled():
            break
        fName = '{0}_{1}.gpkg'.format(baseName, str(i).strip()) # Change gpkg to shp for shapefile
        feedback.pushInfo(self.tr('Creating layer: {}').format(fName))

        sink, dest = QgsProcessingUtils.createFeatureSink(fName, context, fields, geomType, crs)

        filter = '{} = {}'.format(QgsExpression.quotedColumnRef(fieldName), QgsExpression.quotedValue(i))
        req = QgsFeatureRequest().setFilterExpression(filter)

        count = 0
        for f in source.getFeatures(req):
            if feedback.isCanceled():
                break
            sink.addFeature(f, QgsFeatureSink.FastInsert)
            count += 1
        feedback.pushInfo(self.tr('Added {} features to layer').format(count))
        output_layers.append(fName)
        del sink

        feedback.setProgress(int(current * total))

    return {self.OUTPUT: directory, self.OUTPUT_LAYERS: output_layers}

Change extension in the following line

fName = '{0}_{1}.gpkg'.format(baseName, str(i).strip())

To

fName = '{0}_{1}.shp'.format(baseName, str(i).strip())

Then save the script.

Finally, close QGIS and open it again and run Split vector layer tool. The output should be shapefiles not geopakage.

As a good practice, make a backup of the script tool before doing the above process.

enter image description here

  • 1
    It works,thanks very much. – Zhipeng Zheng May 29 at 6:19
  • You are welcome. Glad it worked :) – ahmadhanb May 29 at 6:30
  • 1
    looks fantastic! smart approach – Taras May 29 at 6:32

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.