Commit 064d1108 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

mapinfo2geotransform(): Fix for asserting a map_info with 8 elements in case...

mapinfo2geotransform(): Fix for asserting a map_info with 8 elements in case of arbitrary coordinates. Added test_coord_grid module.
parent 0897ce29
......@@ -2,6 +2,7 @@
import numpy as np
from shapely.geometry import box
from typing import Tuple # noqa F401 # flake8 issue
from ..numeric.vector import find_nearest
from .coord_calc import get_corner_coordinates
......@@ -76,7 +77,7 @@ def find_nearest_grid_coord(valXY, gt, rows, cols, direction='NW', extrapolate=T
def move_shapelyPoly_to_image_grid(shapelyPoly, gt, rows, cols, moving_dir='NW', extrapolate=True):
polyULxy = (min(shapelyPoly.exterior.coords.xy[0]), max(shapelyPoly.exterior.coords.xy[1]))
polyLRxy = (max(shapelyPoly.exterior.coords.xy[0]), min(shapelyPoly.exterior.coords.xy[1]))
UL, LL, LR, UR = get_corner_coordinates(gt=gt, rows=rows, cols=cols) # (x,y) tuples
UL, LL, LR, UR = get_corner_coordinates(gt=gt, rows=rows, cols=cols) # type: Tuple[Tuple] # (x,y)
round_x = {'NW': 'off', 'NO': 'on', 'SW': 'off', 'SE': 'on'}[moving_dir]
round_y = {'NW': 'on', 'NO': 'on', 'SW': 'off', 'SE': 'off'}[moving_dir]
tgt_xgrid = np.arange(UL[0], UR[0] + gt[1], gt[1])
......
......@@ -60,12 +60,12 @@ def mapinfo2geotransform(map_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']
:returns: GDAL GeoTransform, e.g. [249885.0, 30.0, 0.0, 4578615.0, 0.0, -30.0]
"""
if map_info and map_info[0] != 'Arbitrary':
if map_info:
# 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
exp_len = 10 if map_info[0] == 'UTM' else 9 if map_info[0] == 'Arbitrary' 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)
"The map info argument has to be a list of length %s. Got %s." % (exp_len, len(map_info))
if map_info[1] == 1 and map_info[2] == 1:
ULmapX, ULmapY = float(map_info[3]), float(map_info[4])
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_coord_grid
---------------
Tests for `py_tools_ds.geo.coord_grid` module.
"""
import unittest
from shapely.geometry import Polygon
from py_tools_ds.geo.coord_grid import move_shapelyPoly_to_image_grid
poly_local = Polygon([(5708.2, -3006), (5708, -3262), (5452, -3262), (5452, -3006), (5708, -3006)])
class Test_move_shapelyPoly_to_image_grid(unittest.TestCase):
def test_image_coord_grid(self):
print(move_shapelyPoly_to_image_grid(poly_local, (0, 1, 0, 0, 0, -1), rows=6281, cols=11162))
......@@ -74,11 +74,16 @@ class Test_geotransform2mapinfo(unittest.TestCase):
class Test_mapinfo2geotransform(unittest.TestCase):
def test_UTM(self):
def test_map_info_is_valid(self):
gt = mapinfo2geotransform(map_info_utm)
self.assertTrue(isinstance(gt, list))
self.assertEqual(gt, geotransform_utm)
# test
gt = mapinfo2geotransform(['Arbitrary', 1, 1, 5, -7, 1, 1, 0, 'North'])
self.assertTrue(isinstance(gt, list))
self.assertEqual(gt, [5, 1, 0, -7, 0, -1])
def test_map_info_is_empty(self):
gt = mapinfo2geotransform(None)
self.assertTrue(isinstance(gt, list))
......
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