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

This reverts commit 6c5364186dd8d45ac51e5bd1a72c6a542f032cb1.

As it breaks all OGR geoprocessing algoroithms.
This commit is contained in:
Alexander Bruy 2016-11-01 10:25:59 +02:00
parent 5991eccb47
commit a6bd9f0207
2 changed files with 12 additions and 95 deletions

View File

@ -31,72 +31,17 @@ from processing.tools import vector
from qgis.core import (QgsVectorLayer, QgsFeatureRequest) from qgis.core import (QgsVectorLayer, QgsFeatureRequest)
from processing.core.ProcessingConfig import ProcessingConfig 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() start_app()
class VectorTest(unittest.TestCase): 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 # URI from OGR provider
linkTestfile('geom_data.csv', 'a.csv')
name = vector.ogrLayerName(tmpdir)
self.assertEqual(name, 'a')
# URI from OGR provider # 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 # URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layerid=2')
self.assertEqual(name, 'invalid-layerid')
# URI from OGR provider # URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layername=f')
self.assertEqual(name, 'f') # layername takes precedence
# URI from OGR provider # URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layerid=0|layername=f2')
self.assertEqual(name, 'f2') # layername takes precedence
# URI from OGR provider # URI from OGR provider
name = vector.ogrLayerName(tmpdir + '|layername=f2|layerid=0')
self.assertEqual(name, 'f2') # layername takes precedence
# URI from Sqlite provider # URI from Sqlite provider
name = vector.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=') name = vector.ogrLayerName('dbname=\'/tmp/x.sqlite\' table="t" (geometry) sql=')
@ -105,7 +50,6 @@ class VectorTest(unittest.TestCase):
# URI from PostgreSQL provider # URI from PostgreSQL provider
name = vector.ogrLayerName('port=5493 sslmode=disable key=\'edge_id\' srid=0 type=LineString table="city_data"."edge" (geom) sql=') 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') self.assertEqual(name, 'city_data.edge')
def testFeatures(self): def testFeatures(self):
ProcessingConfig.initialize() ProcessingConfig.initialize()

View File

@ -41,8 +41,6 @@ import io
import psycopg2 import psycopg2
from osgeo import ogr
from qgis.PyQt.QtCore import QVariant, QSettings from qgis.PyQt.QtCore import QVariant, QSettings
from qgis.core import (Qgis, QgsFields, QgsField, QgsGeometry, QgsRectangle, QgsWkbTypes, from qgis.core import (Qgis, QgsFields, QgsField, QgsGeometry, QgsRectangle, QgsWkbTypes,
QgsSpatialIndex, QgsMapLayerRegistry, QgsMapLayer, QgsVectorLayer, QgsSpatialIndex, QgsMapLayerRegistry, QgsMapLayer, QgsVectorLayer,
@ -558,46 +556,21 @@ def ogrConnectionString(uri):
# /tmp/x.gdb|layername=thelayer # /tmp/x.gdb|layername=thelayer
# #
def ogrLayerName(uri): def ogrLayerName(uri):
if 'host' in uri:
# handle URIs of database providers regex = re.compile('(table=")(.+?)(\.)(.+?)"')
if ' table=' in uri: r = regex.search(uri)
# Matches table="schema"."table" return '"' + r.groups()[1] + '.' + r.groups()[3] + '"'
re_table_schema = re.compile(' table="([^"]*)"\."([^"]*)"') elif 'dbname' in uri:
r = re_table_schema.search(uri) regex = re.compile('(table=")(.+?)"')
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=)([^|]*)')
r = regex.search(uri) r = regex.search(uri)
return r.groups()[1] 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): class VectorWriter(object):