def exportVectorLayer(layer, supported=None): """Takes a QgsVectorLayer and returns the filename to refer to it, which allows external apps which support only file-based layers to use it. It performs the necessary export in case the input layer is not in a standard format suitable for most applications, it is a remote one or db-based (non-file based) one, or if there is a selection and it should be used, exporting just the selected features. Currently, the output is restricted to shapefiles, so anything that is not in a shapefile will get exported. It also export to a new file if the original one contains non-ascii characters. """ supported = supported or ["shp"] settings = QSettings() systemEncoding = settings.value('/UI/encoding', 'System') output = getTempFilename('shp') basename = removeInvalidChars(os.path.basename(layer.source())) if basename: if not basename.endswith("shp"): basename = basename + ".shp" output = getTempFilenameInTempFolder(basename) else: output = getTempFilename("shp") provider = layer.dataProvider() useSelection = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: writer = QgsVectorFileWriter(output, systemEncoding, layer.fields(), layer.wkbType(), layer.crs()) selection = layer.selectedFeatures() for feat in selection: writer.addFeature(feat) del writer return output else: isASCII = True try: str(layer.source()).decode('ascii') except UnicodeEncodeError: isASCII = False if not os.path.splitext(layer.source())[1].lower() in supported or not isASCII: writer = QgsVectorFileWriter( output, systemEncoding, layer.fields(), layer.wkbType(), layer.crs() ) for feat in layer.getFeatures(): writer.addFeature(feat) del writer return output else: return str(layer.source())
def exportVectorLayer(layer, supported=None): """Takes a QgsVectorLayer and returns the filename to refer to it, which allows external apps which support only file-based layers to use it. It performs the necessary export in case the input layer is not in a standard format suitable for most applications, it is a remote one or db-based (non-file based) one, or if there is a selection and it should be used, exporting just the selected features. Currently, the output is restricted to shapefiles, so anything that is not in a shapefile will get exported. It also export to a new file if the original one contains non-ascii characters. """ supported = supported or ["shp"] settings = QSettings() systemEncoding = settings.value('/UI/encoding', 'System') output = getTempFilename('shp') basename = removeInvalidChars(os.path.basename(layer.source())) if basename: if not basename.endswith("shp"): basename = basename + ".shp" output = getTempFilenameInTempFolder(basename) else: output = getTempFilename("shp") provider = layer.dataProvider() useSelection = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: writer = QgsVectorFileWriter(output, systemEncoding, layer.fields(), layer.wkbType(), layer.crs()) selection = layer.selectedFeatures() for feat in selection: writer.addFeature(feat) del writer return output else: isASCII = True try: str(layer.source()) except UnicodeEncodeError: isASCII = False if not os.path.splitext(layer.source())[1].lower() in supported or not isASCII: writer = QgsVectorFileWriter( output, systemEncoding, layer.fields(), layer.wkbType(), layer.crs() ) for feat in layer.getFeatures(): writer.addFeature(feat) del writer return output else: return str(layer.source())