Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def ogrLayerName(uri):
""" Returns layer name suitable for Gdal/OGR commands
"""
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:
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 re.match('^(multi)?(point|linestring|polygon)(25d|(z?m?))?(\?|$)', uri, re.I):
return 'memory_layer'
elif os.path.isfile(uri):
return os.path.basename(os.path.splitext(uri)[0])
return "invalid-uri"
ly = ds.GetLayer(layerid)
if not ly:
return "invalid-layerid"
name = ly.GetName()
return name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.