Commit a800708f authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Fixed issue #7 (only appearing on Windows).

parent f65c5039
Pipeline #3747 failed with stages
in 1 minute and 48 seconds
......@@ -2,7 +2,6 @@
import re
import math
import warnings
from tempfile import NamedTemporaryFile
import os
from typing import Union # noqa F401 # flake8 issue
......@@ -17,7 +16,6 @@ except ImportError:
from .coord_trafo import transform_any_prj
from .projection import get_proj4info, proj4_to_dict, isLocal
from ..io.raster.gdal import get_GDAL_ds_inmem
__author__ = "Daniel Scheffler"
......@@ -224,31 +222,35 @@ def geotransform2mapinfo(gt, prj):
return Geocoding(gt=gt, prj=prj).to_mapinfo()
except KeyError: # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
with NamedTemporaryFile(prefix='py_tools_ds__', suffix='.bsq') as tf_bsq:
path_hdr = os.path.splitext(tf_bsq.name)[0] + '.hdr'
try:
ds_inmem = get_GDAL_ds_inmem(np.array([[1, 2], [2, 3]]), gt, prj)
ds_out = gdal.GetDriverByName('ENVI').CreateCopy(tf_bsq.name, ds_inmem)
ds_out.FlushCache()
del ds_inmem, ds_out
with open(path_hdr, 'r') as inF:
content = inF.read()
if 'map info' in content:
res = re.search("map info = {(.*?)}", content, re.I).group(1)
map_info = [i.strip() for i in res.split(',')]
for i, ele in enumerate(map_info):
try:
map_info[i] = float(ele)
except ValueError:
pass
else:
map_info = ['Arbitrary', 1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
finally:
os.remove(path_hdr)
fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq"
fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr'
fdir = os.path.join(os.path.abspath(os.curdir))
try:
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1, gdal.GDT_Int32)
ds_out.SetGeoTransform(gt)
ds_out.SetProjection(prj)
ds_out.FlushCache()
del ds_out
with open(fn_hdr, 'r') as inF:
content = inF.read()
if 'map info' in content:
res = re.search("map info = {(.*?)}", content, re.I).group(1)
map_info = [i.strip() for i in res.split(',')]
for i, ele in enumerate(map_info):
try:
map_info[i] = float(ele)
except ValueError:
pass
else:
map_info = ['Arbitrary', 1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
finally:
for fn in [fn_bsq, fn_hdr]:
if os.path.exists(os.path.join(fdir, fn)):
gdal.Unlink(os.path.join(fdir, fn))
return map_info
......@@ -264,31 +266,34 @@ def mapinfo2geotransform(map_info):
return Geocoding(mapinfo=map_info).to_geotransform()
except (KeyError, ValueError): # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
with NamedTemporaryFile(prefix='py_tools_ds__', suffix='.bsq') as tf_bsq:
path_hdr = os.path.splitext(tf_bsq.name)[0] + '.hdr'
fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq"
fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr'
fdir = os.path.join(os.path.abspath(os.curdir))
try:
ds_out = gdal.GetDriverByName('ENVI').Create(tf_bsq.name, 2, 2, 1)
ds_out.GetRasterBand(1).WriteArray(np.array([[1, 2], [2, 3]]))
ds_out.FlushCache()
del ds_out
try:
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1)
ds_out.GetRasterBand(1).WriteArray(np.array([[1, 2], [2, 3]]))
ds_out.FlushCache()
del ds_out
with open(path_hdr, 'r') as InHdr:
lines = InHdr.readlines()
with open(fn_hdr, 'r') as InHdr:
lines = InHdr.readlines()
lines.append('map info = { %s }\n' % ', '.join([str(i) for i in map_info]))
lines.append('map info = { %s }\n' % ', '.join([str(i) for i in map_info]))
with open(path_hdr, 'w') as OutHdr:
OutHdr.writelines(lines)
with open(fn_hdr, 'w') as OutHdr:
OutHdr.writelines(lines)
ds = gdal.Open(tf_bsq.name)
gt = ds.GetGeoTransform()
del ds
ds = gdal.Open(fn_bsq)
gt = ds.GetGeoTransform()
del ds
return gt
return gt
finally:
os.remove(path_hdr)
finally:
for fn in [fn_bsq, fn_hdr]:
if os.path.exists(os.path.join(fdir, fn)):
gdal.Unlink(os.path.join(fdir, fn))
def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None):
......
__version__ = '0.14.11'
__versionalias__ = '20190219_02'
__version__ = '0.14.12'
__versionalias__ = '20190227_01'
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