Skip to content
Permalink
Browse files

Revert "Fix extraction of ogr LayerName from multi-layer dataset URIs"

This reverts commit 6c53641.

As it breaks all OGR geoprocessing algoroithms.
  • Loading branch information
alexbruy committed Nov 12, 2016
1 parent 5991ecc commit a6bd9f020706fa99ee5bb863b5894a2dcb4682ab
Showing with 12 additions and 95 deletions.
  1. +0 −56 python/plugins/processing/tests/ToolsTest.py
  2. +12 −39 python/plugins/processing/tools/vector.py
@@ -31,72 +31,17 @@
from qgis.core import (QgsVectorLayer, QgsFeatureRequest)
from processing.core.ProcessingConfig import ProcessingConfig

import os.path
import errno
import shutil

dataFolder = os.path.join(os.path.dirname(__file__), '../../../../tests/testdata/')
tmpBaseFolder = os.path.join(os.sep, 'tmp', 'qgis_test', str(os.getpid()))


def mkDirP(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise

start_app()


class VectorTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
mkDirP(tmpBaseFolder)

@classmethod
def tearDownClass(cls):
shutil.rmtree(tmpBaseFolder)
pass

# See http://hub.qgis.org/issues/15698
def test_ogrLayerName(self):
tmpdir = os.path.join(tmpBaseFolder, 'ogrLayerName')
os.mkdir(tmpdir)

def linkTestfile(f, t):
os.link(os.path.join(dataFolder, f), os.path.join(tmpdir, t))

# URI from OGR provider
linkTestfile('geom_data.csv', 'a.csv')
name = vector.ogrLayerName(tmpdir)
self.assertEqual(name, 'a')

# URI from OGR provider
linkTestfile('wkt_data.csv', 'b.csv')
name = vector.ogrLayerName(tmpdir + '|layerid=0')
self.assertEqual(name, 'a')
name = vector.ogrLayerName(tmpdir + '|layerid=1')
self.assertEqual(name, 'b')

# URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layerid=2')
self.assertEqual(name, 'invalid-layerid')

# URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layername=f')
self.assertEqual(name, 'f') # layername takes precedence

# URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layerid=0|layername=f2')
self.assertEqual(name, 'f2') # layername takes precedence

# URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layername=f2|layerid=0')
self.assertEqual(name, 'f2') # layername takes precedence

# URI from Sqlite provider
name = vector.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=')
@@ -105,7 +50,6 @@ def linkTestfile(f, t):
# URI from PostgreSQL provider
name = vector.ogrLayerName('port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=')
self.assertEqual(name, 'city_data.edge')

def testFeatures(self):
ProcessingConfig.initialize()

@@ -41,8 +41,6 @@

import psycopg2

from osgeo import ogr

from qgis.PyQt.QtCore import QVariant, QSettings
from qgis.core import (Qgis, QgsFields, QgsField, QgsGeometry, QgsRectangle, QgsWkbTypes,
QgsSpatialIndex, QgsMapLayerRegistry, QgsMapLayer, QgsVectorLayer,
@@ -558,46 +556,21 @@ def ogrConnectionString(uri):
# /tmp/x.gdb|layername=thelayer
#
def ogrLayerName(uri):

# handle URIs of database providers
if ' table=' in uri:
# Matches table="schema"."table"
re_table_schema = re.compile(' table="([^"]*)"\."([^"]*)"')
r = re_table_schema.search(uri)
if r:
return r.groups()[0] + '.' + r.groups()[1]
# Matches table="table"
re_table = re.compile(' table="([^"]*)"')
r = re_table.search(uri)
if r:
return r.groups()[0]

# handle URIs of OGR provider with explicit layername
if 'layername' in uri:
regex = re.compile('(layername=)([^|]*)')
if 'host' in uri:
regex = re.compile('(table=")(.+?)(\.)(.+?)"')
r = regex.search(uri)
return '"' + r.groups()[1] + '.' + r.groups()[3] + '"'
elif 'dbname' in uri:
regex = re.compile('(table=")(.+?)"')
r = regex.search(uri)
return r.groups()[1]
elif 'layername' in uri:
regex = re.compile('(layername=)(.*)')
r = regex.search(uri)
return r.groups()[1]
else:
return os.path.basename(os.path.splitext(uri)[0])

fields = uri.split('|')
ogruri = fields[0]
fields = fields[1:]
layerid = 0
for f in fields:
if f.startswith('layername='):
# Name encoded in uri, nothing more needed
return f.split('=')[1]
if f.startswith('layerid='):
layerid = int(f.split('=')[1])
# Last layerid= takes precedence, to allow of layername to
# take precedence
ds = ogr.Open(ogruri)
if not ds:
return "invalid-uri"
ly = ds.GetLayer(layerid)
if not ly:
return "invalid-layerid"
name = ly.GetName()
return name


class VectorWriter(object):

0 comments on commit a6bd9f0

Please sign in to comment.
You can’t perform that action at this time.