Commit f0d81cc1 authored by Daniel Scheffler's avatar Daniel Scheffler

Fixed a race condition within geotransform2mapinfo() and...

Fixed a race condition within geotransform2mapinfo() and mapinfo2geotransform() when running in multiprocessing.
Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent c7c5f58d
Pipeline #11988 failed with stages
in 72 minutes and 51 seconds
......@@ -2,7 +2,13 @@
History
=======
0.15.1 (2020-08-26)
0.15.2 (2020-08-27)
-------------------
* Fixed a race condition within geotransform2mapinfo() and mapinfo2geotransform() when running in multiprocessing.
0.15.1 (2020-08-27)
-------------------
* Fixed exceptions caused by WKT2 strings passed to GDAL<3.
......
......@@ -26,6 +26,7 @@ import math
import warnings
import os
from typing import Union # noqa F401 # flake8 issue
from tempfile import TemporaryDirectory
from pyproj import CRS
import numpy as np
......@@ -140,6 +141,7 @@ class Geocoding(object):
'Geographic Lat/Lon' if proj4['proj'] == 'longlat' else \
'UTM' if proj4['proj'] == 'utm' else proj4['proj']
# FIXME there is no 'datum' key in case of, e.g., LAEA projection # still true?
# -> use CRS.datum.name instead?
self.datum = 'WGS-84' if proj4['datum'] == 'WGS84' else proj4['datum'] # proj4['ellps']?
self.units = proj4['unit'] if 'unit' in proj4 else self.units
......@@ -248,24 +250,22 @@ def geotransform2mapinfo(gt, prj):
return Geocoding(gt=gt, prj=prj).to_mapinfo()
except KeyError: # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
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))
if int(gdal.__version__[0]) < 3:
# noinspection PyTypeChecker
prj = CRS(prj).to_wkt(version="WKT1_GDAL")
try:
if int(gdal.__version__[0]) < 3:
# noinspection PyTypeChecker
prj = CRS(prj).to_wkt(version="WKT1_GDAL")
with TemporaryDirectory() as fdir:
fn = "py_tools_ds__geotransform2mapinfo_temp"
fP_bsq = os.path.join(fdir, fn + '.bsq')
fP_hdr = os.path.join(fdir, fn + '.hdr')
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1, gdal.GDT_Int32)
ds_out = gdal.GetDriverByName('ENVI').Create(fP_bsq, 2, 2, 1, gdal.GDT_Int32)
ds_out.SetGeoTransform(gt)
ds_out.SetProjection(prj)
ds_out.FlushCache()
del ds_out
# noinspection PyUnusedLocal
ds_out = None
with open(fn_hdr, 'r') as inF:
with open(fP_hdr, 'r') as inF:
content = inF.read()
if 'map info' in content:
res = re.search("map info = {(.*?)}", content, re.I).group(1)
......@@ -279,11 +279,6 @@ def geotransform2mapinfo(gt, prj):
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
......@@ -298,36 +293,29 @@ def mapinfo2geotransform(map_info):
return Geocoding(mapinfo=map_info).to_geotransform()
except (KeyError, ValueError): # KeyError: 'datum' - in case of, e.g., ETRS/LAEA projection
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))
with TemporaryDirectory() as fdir:
fn = "py_tools_ds__mapinfo2geotransform_temp"
fP_bsq = os.path.join(fdir, fn + '.bsq')
fP_hdr = os.path.join(fdir, fn + '.hdr')
try:
ds_out = gdal.GetDriverByName('ENVI').Create(fn_bsq, 2, 2, 1)
ds_out = gdal.GetDriverByName('ENVI').Create(fP_bsq, 2, 2, 1)
ds_out.GetRasterBand(1).WriteArray(np.array([[1, 2], [2, 3]]))
ds_out.FlushCache()
del ds_out
# noinspection PyUnusedLocal
ds_out = None
with open(fn_hdr, 'r') as InHdr:
with open(fP_hdr, 'r') as InHdr:
lines = InHdr.readlines()
lines.append('map info = { %s }\n' % ', '.join([str(i) for i in map_info]))
with open(fn_hdr, 'w') as OutHdr:
with open(fP_hdr, 'w') as OutHdr:
OutHdr.writelines(lines)
ds = gdal.Open(fn_bsq)
ds = gdal.Open(fP_bsq)
gt = ds.GetGeoTransform()
del ds
return gt
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 gt
def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None):
......
......@@ -19,5 +19,5 @@
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = '0.15.1'
__versionalias__ = '20200827_01'
__version__ = '0.15.2'
__versionalias__ = '20200827_02'
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