Commit e20e3ea5 authored by Daniel Scheffler's avatar Daniel Scheffler

Reveal KeyError in geotransform2mapinfo()

Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent 3ee2314c
Pipeline #11904 failed with stage
in 13 minutes and 45 seconds
...@@ -244,41 +244,43 @@ def geotransform2mapinfo(gt, prj): ...@@ -244,41 +244,43 @@ def geotransform2mapinfo(gt, prj):
:returns: ENVI geo info, e.g. [ UTM , 1 , 1 , 256785.0 , 4572015.0 , 30.0 , 30.0 , 43 , North , WGS-84 ] :returns: ENVI geo info, e.g. [ UTM , 1 , 1 , 256785.0 , 4572015.0 , 30.0 , 30.0 , 43 , North , WGS-84 ]
:rtype: list :rtype: list
""" """
try: # try:
return Geocoding(gt=gt, prj=prj).to_mapinfo() return Geocoding(gt=gt, prj=prj).to_mapinfo()
except KeyError: # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection # except KeyError: # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq" # fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq"
fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr' # fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr'
fdir = os.path.join(os.path.abspath(os.curdir)) # fdir = os.path.join(os.path.abspath(os.curdir))
#
try: # try:
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1, gdal.GDT_Int32) # ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1, gdal.GDT_Int32)
ds_out.SetGeoTransform(gt) # ds_out.SetGeoTransform(gt)
ds_out.SetProjection(prj) # ds_out.SetProjection(prj)
ds_out.FlushCache() # ds_out.FlushCache()
del ds_out #
# # noinspection PyUnusedLocal
with open(fn_hdr, 'r') as inF: # ds_out = None
content = inF.read() #
if 'map info' in content: # with open(fn_hdr, 'r') as inF:
res = re.search("map info = {(.*?)}", content, re.I).group(1) # content = inF.read()
map_info = [i.strip() for i in res.split(',')] # if 'map info' in content:
# res = re.search("map info = {(.*?)}", content, re.I).group(1)
for i, ele in enumerate(map_info): # map_info = [i.strip() for i in res.split(',')]
try: #
map_info[i] = float(ele) # for i, ele in enumerate(map_info):
except ValueError: # try:
pass # map_info[i] = float(ele)
else: # except ValueError:
map_info = ['Arbitrary', 1.0, 1.0, 0.0, 0.0, 1.0, 1.0] # pass
# else:
finally: # map_info = ['Arbitrary', 1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
for fn in [fn_bsq, fn_hdr]: #
if os.path.exists(os.path.join(fdir, fn)): # finally:
gdal.Unlink(os.path.join(fdir, fn)) # for fn in [fn_bsq, fn_hdr]:
# if os.path.exists(os.path.join(fdir, fn)):
return map_info # gdal.Unlink(os.path.join(fdir, fn))
#
# return map_info
def mapinfo2geotransform(map_info): def mapinfo2geotransform(map_info):
...@@ -300,7 +302,9 @@ def mapinfo2geotransform(map_info): ...@@ -300,7 +302,9 @@ def mapinfo2geotransform(map_info):
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1) ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1)
ds_out.GetRasterBand(1).WriteArray(np.array([[1, 2], [2, 3]])) ds_out.GetRasterBand(1).WriteArray(np.array([[1, 2], [2, 3]]))
ds_out.FlushCache() ds_out.FlushCache()
del ds_out
# noinspection PyUnusedLocal
ds_out = None
with open(fn_hdr, 'r') as InHdr: with open(fn_hdr, 'r') as InHdr:
lines = InHdr.readlines() lines = InHdr.readlines()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment