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

Fix mapinfo2geotransform for asserting wrong length of map info in case of geographic coordinates.

parent 1a518582
...@@ -27,6 +27,7 @@ def geotransform2mapinfo(gt, prj): ...@@ -27,6 +27,7 @@ def geotransform2mapinfo(gt, prj):
if gt[2] != 0 or gt[4] != 0: # TODO if gt[2] != 0 or gt[4] != 0: # TODO
raise NotImplementedError('Currently rotated datasets are not supported.') raise NotImplementedError('Currently rotated datasets are not supported.')
srs = osr.SpatialReference() srs = osr.SpatialReference()
srs.ImportFromWkt(prj) srs.ImportFromWkt(prj)
Proj4 = [i[1:] for i in srs.ExportToProj4().split()] Proj4 = [i[1:] for i in srs.ExportToProj4().split()]
...@@ -49,20 +50,23 @@ def geotransform2mapinfo(gt, prj): ...@@ -49,20 +50,23 @@ def geotransform2mapinfo(gt, prj):
def mapinfo2geotransform(map_info): def mapinfo2geotransform(map_info):
# type: (list) -> _T_mapinfo_out # type: (list) -> _T_mapinfo_out
"""Builds GDAL GeoTransform tuple from an ENVI geo info. """Builds GDAL GeoTransform tuple from an ENVI geo info.
:param map_info: ENVI geo info (list), e.g. ['UTM', 1, 1, 192585.0, 5379315.0, 30.0, 30.0, 41, 'North', 'WGS-84']
:param map_info: ENVI geo info (list), e.g., ['UTM', 1, 1, 192585.0, 5379315.0, 30.0, 30.0, 41, 'North', 'WGS-84']
:returns: GDAL GeoTransform, e.g. [249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0] :returns: GDAL GeoTransform, e.g. [249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0]
:rtype: list
""" """
if map_info: if map_info:
# FIXME rotated datasets are currently not supported -> function must return rotation at gt[2] and gt[4] # FIXME rotated datasets are currently not supported -> function must return rotation at gt[2] and gt[4]
# validate input map info
exp_len = 10 if map_info[0] == 'UTM' else 8
assert isinstance(map_info, list) and len(map_info) == exp_len, \
"The map info argument has to be a list of length %s. Got %s." % (map_info, exp_len)
if map_info[1] == 1 and map_info[2] == 1: if map_info[1] == 1 and map_info[2] == 1:
ULmapX, ULmapY = float(map_info[3]), float(map_info[4]) ULmapX, ULmapY = float(map_info[3]), float(map_info[4])
else: else:
ULmapX = float(map_info[3]) - (float(map_info[1]) * float(map_info[5]) - float(map_info[5])) ULmapX = float(map_info[3]) - (float(map_info[1]) * float(map_info[5]) - float(map_info[5]))
ULmapY = float(map_info[4]) + (float(map_info[2]) * float(map_info[6]) - float(map_info[6])) ULmapY = float(map_info[4]) + (float(map_info[2]) * float(map_info[6]) - float(map_info[6]))
assert isinstance(map_info, list) and len(map_info) == 10, \
"The geo info argument has to be a list of length 10. Got %s." % map_info
return [ULmapX, float(map_info[5]), 0., ULmapY, 0., -float(map_info[6])] return [ULmapX, float(map_info[5]), 0., ULmapY, 0., -float(map_info[6])]
...@@ -70,10 +74,12 @@ def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None): ...@@ -70,10 +74,12 @@ def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None):
"""Returns (ULxy, LLxy, LRxy, URxy) in the same coordinate units like the given geotransform.""" """Returns (ULxy, LLxy, LRxy, URxy) in the same coordinate units like the given geotransform."""
assert gdal_ds or (gt and cols and rows), \ assert gdal_ds or (gt and cols and rows), \
"GEOP.get_corner_coordinates: Missing argument! Please provide either 'gdal_ds' or 'gt', 'cols' AND 'rows'." "GEOP.get_corner_coordinates: Missing argument! Please provide either 'gdal_ds' or 'gt', 'cols' AND 'rows'."
gdal_ds_GT = gdal_ds.GetGeoTransform() if gdal_ds else gt gdal_ds_GT = gdal_ds.GetGeoTransform() if gdal_ds else gt
ext = [] ext = []
xarr = [0, gdal_ds.RasterXSize if gdal_ds else cols] xarr = [0, gdal_ds.RasterXSize if gdal_ds else cols]
yarr = [0, gdal_ds.RasterYSize if gdal_ds else rows] yarr = [0, gdal_ds.RasterYSize if gdal_ds else rows]
for px in xarr: for px in xarr:
for py in yarr: for py in yarr:
x = gdal_ds_GT[0] + (px * gdal_ds_GT[1]) + (py * gdal_ds_GT[2]) x = gdal_ds_GT[0] + (px * gdal_ds_GT[1]) + (py * gdal_ds_GT[2])
...@@ -81,4 +87,5 @@ def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None): ...@@ -81,4 +87,5 @@ def get_corner_coordinates(gdal_ds=None, gt=None, cols=None, rows=None):
ext.append([x, y]) ext.append([x, y])
yarr.reverse() yarr.reverse()
del gdal_ds_GT del gdal_ds_GT
return ext return ext
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