Skip to content

Instantly share code, notes, and snippets.

@gacarrillor
Created October 17, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gacarrillor/233567ca4b407bfd4e92ca66c0fe01df to your computer and use it in GitHub Desktop.
Save gacarrillor/233567ca4b407bfd4e92ca66c0fe01df to your computer and use it in GitHub Desktop.
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