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

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 @@ ...@@ -2,7 +2,13 @@
History 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. * Fixed exceptions caused by WKT2 strings passed to GDAL<3.
......
...@@ -26,6 +26,7 @@ import math ...@@ -26,6 +26,7 @@ import math
import warnings import warnings
import os import os
from typing import Union # noqa F401 # flake8 issue from typing import Union # noqa F401 # flake8 issue
from tempfile import TemporaryDirectory
from pyproj import CRS from pyproj import CRS
import numpy as np import numpy as np
...@@ -140,6 +141,7 @@ class Geocoding(object): ...@@ -140,6 +141,7 @@ class Geocoding(object):
'Geographic Lat/Lon' if proj4['proj'] == 'longlat' else \ 'Geographic Lat/Lon' if proj4['proj'] == 'longlat' else \
'UTM' if proj4['proj'] == 'utm' else proj4['proj'] 'UTM' if proj4['proj'] == 'utm' else proj4['proj']
# FIXME there is no 'datum' key in case of, e.g., LAEA projection # still true? # 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.datum = 'WGS-84' if proj4['datum'] == 'WGS84' else proj4['datum'] # proj4['ellps']?
self.units = proj4['unit'] if 'unit' in proj4 else self.units self.units = proj4['unit'] if 'unit' in proj4 else self.units
...@@ -248,24 +250,22 @@ def geotransform2mapinfo(gt, prj): ...@@ -248,24 +250,22 @@ 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
fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq" if int(gdal.__version__[0]) < 3:
fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr' # noinspection PyTypeChecker
fdir = os.path.join(os.path.abspath(os.curdir)) prj = CRS(prj).to_wkt(version="WKT1_GDAL")
try: with TemporaryDirectory() as fdir:
if int(gdal.__version__[0]) < 3: fn = "py_tools_ds__geotransform2mapinfo_temp"
# noinspection PyTypeChecker fP_bsq = os.path.join(fdir, fn + '.bsq')
prj = CRS(prj).to_wkt(version="WKT1_GDAL") 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.SetGeoTransform(gt)
ds_out.SetProjection(prj) ds_out.SetProjection(prj)
ds_out.FlushCache() ds_out.FlushCache()
del ds_out
# noinspection PyUnusedLocal with open(fP_hdr, 'r') as inF:
ds_out = None
with open(fn_hdr, 'r') as inF:
content = inF.read() content = inF.read()
if 'map info' in content: if 'map info' in content:
res = re.search("map info = {(.*?)}", content, re.I).group(1) res = re.search("map info = {(.*?)}", content, re.I).group(1)
...@@ -279,11 +279,6 @@ def geotransform2mapinfo(gt, prj): ...@@ -279,11 +279,6 @@ def geotransform2mapinfo(gt, prj):
else: else:
map_info = ['Arbitrary', 1.0, 1.0, 0.0, 0.0, 1.0, 1.0] 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 return map_info
...@@ -298,36 +293,29 @@ def mapinfo2geotransform(map_info): ...@@ -298,36 +293,29 @@ 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
fn_bsq = "py_tools_ds__geotransform2mapinfo_temp.bsq" with TemporaryDirectory() as fdir:
fn_hdr = os.path.splitext(fn_bsq)[0] + '.hdr' fn = "py_tools_ds__mapinfo2geotransform_temp"
fdir = os.path.join(os.path.abspath(os.curdir)) fP_bsq = os.path.join(fdir, fn + '.bsq')
fP_hdr = os.path.join(fdir, fn + '.hdr')
try: ds_out = gdal.GetDriverByName('ENVI').Create(fP_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 with open(fP_hdr, 'r') as InHdr:
ds_out = None
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(fn_hdr, 'w') as OutHdr: with open(fP_hdr, 'w') as OutHdr:
OutHdr.writelines(lines) OutHdr.writelines(lines)
ds = gdal.Open(fn_bsq) ds = gdal.Open(fP_bsq)
gt = ds.GetGeoTransform() gt = ds.GetGeoTransform()
del ds del ds
return gt 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))
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):
......
...@@ -19,5 +19,5 @@ ...@@ -19,5 +19,5 @@
# You should have received a copy of the GNU Lesser General Public License along # 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/>. # with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = '0.15.1' __version__ = '0.15.2'
__versionalias__ = '20200827_01' __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