Created
October 17, 2016 12:37
-
-
Save gacarrillor/233567ca4b407bfd4e92ca66c0fe01df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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