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 @@ ...@@ -2,7 +2,6 @@
import re import re
import math import math
import warnings import warnings
from tempfile import NamedTemporaryFile
import os import os
from typing import Union # noqa F401 # flake8 issue from typing import Union # noqa F401 # flake8 issue
...@@ -17,7 +16,6 @@ except ImportError: ...@@ -17,7 +16,6 @@ except ImportError:
from .coord_trafo import transform_any_prj from .coord_trafo import transform_any_prj
from .projection import get_proj4info, proj4_to_dict, isLocal from .projection import get_proj4info, proj4_to_dict, isLocal
from ..io.raster.gdal import get_GDAL_ds_inmem
__author__ = "Daniel Scheffler" __author__ = "Daniel Scheffler"
...@@ -224,31 +222,35 @@ def geotransform2mapinfo(gt, prj): ...@@ -224,31 +222,35 @@ def geotransform2mapinfo(gt, prj):
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
with NamedTemporaryFile(prefix='py_tools_ds__', suffix='.bsq') as tf_bsq: fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq"
path_hdr = os.path.splitext(tf_bsq.name)[0] + '.hdr' fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr'
fdir = os.path.join(os.path.abspath(os.curdir))
try:
ds_inmem = get_GDAL_ds_inmem(np.array([[1, 2], [2, 3]]), gt, prj) try:
ds_out = gdal.GetDriverByName('ENVI').CreateCopy(tf_bsq.name, ds_inmem) ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1, gdal.GDT_Int32)
ds_out.FlushCache() ds_out.SetGeoTransform(gt)
del ds_inmem, ds_out ds_out.SetProjection(prj)
ds_out.FlushCache()
with open(path_hdr, 'r') as inF: del ds_out
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]
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))
return map_info return map_info
...@@ -264,31 +266,34 @@ def mapinfo2geotransform(map_info): ...@@ -264,31 +266,34 @@ def mapinfo2geotransform(map_info):
return Geocoding(mapinfo=map_info).to_geotransform() return Geocoding(mapinfo=map_info).to_geotransform()
except (KeyError, ValueError): # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection except (KeyError, ValueError): # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
with NamedTemporaryFile(prefix='py_tools_ds__', suffix='.bsq') as tf_bsq: fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq"
path_hdr = os.path.splitext(tf_bsq.name)[0] + '.hdr' fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr'
fdir = os.path.join(os.path.abspath(os.curdir))
try: try:
ds_out = gdal.GetDriverByName('ENVI').Create(tf_bsq.name, 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 del ds_out
with open(path_hdr, 'r') as InHdr: with open(fn_hdr, 'r') as InHdr:
lines = InHdr.readlines() 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: with open(fn_hdr, 'w') as OutHdr:
OutHdr.writelines(lines) OutHdr.writelines(lines)
ds = gdal.Open(tf_bsq.name) ds = gdal.Open(fn_bsq)
gt = ds.GetGeoTransform() gt = ds.GetGeoTransform()
del ds del ds
return gt return gt
finally: finally:
os.remove(path_hdr) 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): def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None):
......
__version__ = '0.14.11' __version__ = '0.14.12'
__versionalias__ = '20190219_02' __versionalias__ = '20190227_01'
Supports Markdown
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