Skip to content

Commit

Permalink
[processing] moved function to resolve source uri to tools module
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed May 24, 2019
1 parent dd8bf30 commit b41598f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 4 additions & 6 deletions python/plugins/processing/algs/qgis/Datasources2Vrt.py
Expand Up @@ -38,6 +38,7 @@
QgsProcessingException,
QgsProviderRegistry)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools.vector import resolveSourceToFilepath


class Datasources2Vrt(QgisAlgorithm):
Expand Down Expand Up @@ -127,14 +128,11 @@ def mergeDataSources2Vrt(self, dataSources, outFile, union=False, relative=False

feedback.setProgress(int(current * total))

inFile = layer.source()
inFile = layer.source()
srcDS = ogr.Open(inFile, 0)
if srcDS is None:
#might be a shapefile loaded as a directory of shapefiles. We try to resolve the uri to a filepath
sourceParts = QgsProviderRegistry.instance().decodeUri('ogr', inFile)
if sourceParts.get("layerName"):
inFile = os.path.join(sourceParts.get("path"), sourceParts.get("layerName") + ".shp")
srcDS = ogr.Open(inFile, 0)
inFile = resolveSourceToFilepath(inFile)
srcDS = ogr.Open(inFile, 0)
if srcDS is None:
raise QgsProcessingException(
self.tr('Invalid datasource: {}'.format(inFile)))
Expand Down
22 changes: 21 additions & 1 deletion python/plugins/processing/tools/vector.py
Expand Up @@ -21,8 +21,11 @@
__date__ = 'February 2013'
__copyright__ = '(C) 2013, Victor Olaya'

import os
from qgis.core import (NULL,
QgsFeatureRequest)
QgsFeatureRequest,
QgsProviderRegistry,
QgsVectorFileWriter)


def resolveFieldIndex(source, attr):
Expand Down Expand Up @@ -111,3 +114,20 @@ def checkMinDistance(point, index, distance, points):
return False

return True


def resolveSourceToFilepath(source):
#Tries to resolve a uri-like source (i.e /path/|layername=points) to a valid filepath (/path/points.shp)
sourceParts = QgsProviderRegistry.instance().decodeUri('ogr', source)
folder = sourceParts.get("path")
layerName = sourceParts.get("layerName")
if layerName:
if os.path.exists(folder):
if os.path.isdir(folder):
for ext in QgsVectorFileWriter.supportedFormatExtensions():
f = os.path.join(folder, "%s.%s" % (layerName, ext))
if os.path.exists(f):
return f
else:
return folder
return source

0 comments on commit b41598f

Please sign in to comment.