Commit 95332aac authored by Rick Gruber-Riemer's avatar Rick Gruber-Riemer

Collect boundary tests in one place.

parent 5e439021
......@@ -2,25 +2,8 @@ import argparse
import logging
import parameters
import sys
from typing import List, Optional
import unittest
def parse_boundary(boundary_string: str) -> Optional[List[float]]:
boundary_parts = boundary_string.split("_")
if len(boundary_parts) != 4:
logging.error("Boundary must have four elements separated by '_': {} has only {} element(s) \
-> aborting!".format(args.boundary, len(boundary_parts)))
return None
boundary_float_list = list()
for i in range(len(boundary_parts)):
try:
boundary_float_list[i] = float(boundary_parts[i])
except ValueError:
logging.error("Boundary part {} cannot be parsed as float (decimal)".format(boundary_parts[i]))
return None
return boundary_float_list
from utils.utilities import BoundaryError, parse_boundary
if __name__ == '__main__':
......@@ -33,7 +16,8 @@ if __name__ == '__main__':
help="set loglevel. Valid levels are VERBOSE, DEBUG, INFO, WARNING, ERROR, CRITICAL",
required=False)
parser.add_argument("-b", "--boundary", dest="boundary",
help="set the boundary as WEST_SOUTH_EAST_NORTH like 9.1_47.0_11_48.8 (. as decimal)")
help="set the boundary as WEST_SOUTH_EAST_NORTH like 9.1_47.0_11_48.8 (. as decimal)",
required=True)
args = parser.parse_args()
......@@ -41,13 +25,8 @@ if __name__ == '__main__':
parameters.set_loglevel(args.loglevel) # -- must go after reading params file
parameters.USE_DATABASE = True # just to be sure
boundary_floats = parse_boundary(args.boundary)
if boundary_floats is None:
try:
boundary_floats = parse_boundary(args.boundary)
except BoundaryError as be:
logging.error(be.message)
sys.exit(1)
# ================ UNITTESTS =======================
class TestBuildTilesDB(unittest.TestCase):
def test_parse_boundary(self):
pass
......@@ -22,6 +22,7 @@ import types
import textures.road
from utils import vec2d as v
import utils.utilities as utils
# default_args_start # DO NOT MODIFY THIS LINE
# -*- coding: utf-8 -*-
......@@ -377,17 +378,12 @@ def set_boundary(boundary_west: float, boundary_south: float,
"""Overrides the geographical boundary values (either default values or read from file).
In most situations should be called after method read_from_file().
"""
boundary_ok = True
if boundary_west >= boundary_east:
boundary_ok = False
logging.error("Boundary West {} must be smaller than East {} -> aborting!".format(boundary_west,
boundary_east))
if boundary_south >= boundary_north:
boundary_ok = False
logging.error("Boundary -south {} must be smaller than North {} -> aborting!".format(boundary_south,
boundary_north))
if not boundary_ok:
try:
utils.check_boundary(boundary_west, boundary_south, boundary_east, boundary_north)
except utils.BoundaryError as be:
logging.error(be.message)
sys.exit(1)
global BOUNDARY_WEST
BOUNDARY_WEST = boundary_west
global BOUNDARY_SOUTH
......
......@@ -12,7 +12,8 @@ import pickle
import subprocess
import sys
import textwrap
from typing import Tuple
from typing import List, Optional, Tuple
import unittest
import numpy as np
import parameters
......@@ -424,3 +425,74 @@ def progress(i, max_i):
print("%i %i %5.1f%% \r" % (i+1, max_i, (float(i+1)/max_i) * 100), end='')
if i > max_i - 2:
print()
class BoundaryError(Exception):
"""Indicates wrong values to define the boundary of the scenery."""
def __init__(self, message: str) -> None:
self.message = message
def parse_boundary(boundary_string: str) -> Optional[List[float]]:
"""Parses the boundary argument provided as an underscore delimited string into 4 floats for lon/lat.
Raises BoundaryError if cannot be parsed into 4 floats.
"""
boundary_parts = boundary_string.split("_")
if len(boundary_parts) != 4:
message = "Boundary must have four elements separated by '_': {} has only {} element(s) \
-> aborting!".format(boundary_string, len(boundary_parts))
raise BoundaryError(message)
boundary_float_list = list()
for i in range(len(boundary_parts)):
try:
boundary_float_list.append(float(boundary_parts[i]))
except ValueError as my_value_error:
message = "Boundary part {} cannot be parsed as float (decimal)".format(boundary_parts[i])
raise BoundaryError(message) from my_value_error
return boundary_float_list
def check_boundary(boundary_west: float, boundary_south: float,
boundary_east: float, boundary_north: float) -> None:
"""Check whether the boundary values actually make sense.
Raise BoundaryError if there is a problem.
"""
if boundary_west >= boundary_east:
raise BoundaryError("Boundary West {} must be smaller than East {} -> aborting!".format(boundary_west,
boundary_east))
if boundary_south >= boundary_north:
raise BoundaryError("Boundary South {} must be smaller than North {} -> aborting!".format(boundary_south,
boundary_north))
# ================ UNITTESTS =======================
class TestUtilities(unittest.TestCase):
def test_parse_boundary_empty_string(self):
with self.assertRaises(BoundaryError):
parse_boundary("")
def test_parse_boundary_three_floats(self):
with self.assertRaises(BoundaryError):
parse_boundary("1.1_1.2_1.2")
def test_parse_boundary_one_not_float(self):
with self.assertRaises(BoundaryError):
parse_boundary("1.1_1.2_1.2_a")
def test_parse_boundary_pass(self):
self.assertEqual(parse_boundary("1.1_1.2_1.2_-1.2"), [1.1, 1.2, 1.2, -1.2])
def check_boundary_east_west_wrong(self):
with self.assertRaises(BoundaryError):
check_boundary(2, 1, 1, 2)
def check_boundary_south_north_wrong(self):
with self.assertRaises(BoundaryError):
check_boundary(-2, 1, 1, -2)
def check_boundary_pass(self):
self.assertEqual(None, check_boundary(-2, -3, 1, -2))
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