def test_compress_territory(): example_territory = Territory(12, representations=[MapistoShape(test_path, 0)], bounding_box=BoundingBox(0, 0, 100, 100), state_id=14, validity_start=datetime.now(), validity_end=datetime.now(), color='#FF0000', name="Algeria") compressed = compress_territory(example_territory) assert isinstance(compressed, Territory) # 0 is raw precision assert compressed.representations[0].precision_in_km == 0 for i, rep in enumerate(compressed.representations): assert isinstance(rep, MapistoShape) if i > 0: previous = compressed.representations[i - 1] # Assumes precision_levels is sorted asc # representations are sorted by precision asc assert rep.precision_in_km > previous.precision_in_km # Precision asc means size desc (as more precise = bigger definition) assert previous.precision_in_km == 0 or len(rep.d_path) <= len( previous.d_path) compressed.representations = [compressed.representations[0]] assert compressed.equals(example_territory)
def get_within_bbox_at_time(cursor, bbox, date, precision_level): assert isinstance(bbox, BoundingBox) assert isinstance(date, datetime) cursor.execute( ''' SELECT territories.territory_id, state_id, min_x, min_y, max_x-min_x, max_y-min_y, validity_start, validity_end, color, name, d_path FROM territories INNER JOIN territories_shapes ON territories.territory_id=territories_shapes.territory_id WHERE territories.validity_start <= %s AND territories.validity_end > %s AND precision_in_km=%s AND NOT( %s < territories.min_x OR territories.max_x < %s OR %s < territories.min_y OR territories.max_y < %s ) ''', (date, date, precision_level, bbox.max_x, bbox.x, bbox.max_y, bbox.y)) return [ Territory(territory_id, [MapistoShape(d_path, precision_level)], state_id, BoundingBox(x, y, width, height), validity_start, validity_end, color, name) for (territory_id, state_id, x, y, width, height, validity_start, validity_end, color, name, d_path) in cursor.fetchall() ]
def test_get_bbox_only_1_retrieved(): with get_db_with_state_and_territories_cursor(france_1912_2018, algeria, corsica) as cursor: bbox = BoundingBox(-20, 17, 16, 4) # algeria goes till y=16 date = year_to_date(1918) res = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, 0) assert any(t.equals(corsica) for t in res) assert not any(t.equals(algeria) for t in res)
def extract_map_request(): precision = float(request.args.get('precision_in_km')) bbmin_x = int(float(request.args.get('min_x'))) bbmax_x = int(float(request.args.get('max_x'))) bbmin_y = int(float(request.args.get('min_y'))) bbmax_y = int(float(request.args.get('max_y'))) bbox = BoundingBox(bbmin_x, bbmin_y, bbmax_x - bbmin_x, bbmax_y - bbmin_y) return (precision, bbox)
def test_get_bbox_at_date_limit(): with get_db_with_state_and_territories_cursor(france_1912_2018, algeria, corsica) as cursor: bbox = BoundingBox(-20, -2, 16, 14) date = year_to_date(1919) res = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, 0) assert not any(t.equals(corsica) or t.equals(algeria) for t in res) date = year_to_date(1918) res = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, 0) assert any(t.equals(corsica) for t in res) assert any(t.equals(algeria) for t in res)
def test_get_bbox_in_period_nominal(): with get_db_with_state_and_territories_cursor(france_1912_2018, algeria, corsica) as cursor: bbox = BoundingBox(-20, -2, 16, 14) res = TerritoryCRUD.get_within_bbox_in_period(cursor, bbox, year_to_date(1900), year_to_date(1920), 0) assert isinstance(res, list) assert len(res) >= 2 assert any(t.equals(corsica) for t in res) assert any(t.equals(algeria) for t in res)
def test_get_bbox_at_nominal(): with get_db_with_state_and_territories_cursor(france_1912_2018, algeria, corsica) as cursor: bbox = BoundingBox(-20, -2, 16, 14) print('alegeria bbox : ', algeria.bounding_box) print('corsica bbox : ', corsica.bounding_box) date = year_to_date(1918) res = TerritoryCRUD.get_within_bbox_at_time(cursor, bbox, date, 0) assert isinstance(res, list) assert len(res) >= 2 assert any(t.equals(corsica) for t in res) assert any(t.equals(algeria) for t in res)
def get_by_state(cursor, state_id): assert isinstance(state_id, int) cursor.execute( ''' SELECT territory_id, min_x, min_y, max_x-min_x, max_y-min_y, validity_start, validity_end, color, name FROM territories WHERE state_id=%s ''', (state_id, )) res = [] for row in cursor.fetchall(): (territory_id, x, y, width, height, validity_start, validity_end, color, name) = row res.append( Territory(territory_id, [], state_id, BoundingBox(x, y, width, height), validity_start, validity_end, color, name)) return res
def get_land(cursor, land_id): assert isinstance(land_id, int) cursor.execute( ''' SELECT min_x, max_x, min_y, max_y, d_path, precision_in_km FROM lands NATURAL JOIN lands_shapes WHERE land_id=%s ''', (land_id, )) rows = cursor.fetchall() if not len(rows): raise NotFound(f"Cannot find land no {land_id}") (minx, maxx, miny, maxy, _, _) = rows[0] res = Land(land_id, [], BoundingBox(minx, miny, maxx - minx, maxy - miny)) for row in rows: (_, _, _, _, d_path, precision) = row res.representations.append(MapistoShape(d_path, precision)) return res
def get_bbox(cursor, id, date): assert isinstance(id, int) assert isinstance(date, datetime) cursor.execute( ''' SELECT MIN(min_x), MIN(min_y), MAX(max_x) , MAX(max_y) FROM territories WHERE state_id=%s AND validity_start <= %s AND validity_end > %s ''', (id, date, date)) row = cursor.fetchone() if row[0] is None: raise NotFound( f'No map for state {id} at {date.isoformat()} : no territories found at date or non existent state' ) min_x, min_y, max_x, max_y = row return BoundingBox(min_x, min_y, max_x - min_x, max_y - min_y)
def test_compress_land(): example_land = Land(12, [MapistoShape(test_path, 0)], BoundingBox(0, 0, 100, 100)) compressed = compress_land(example_land) assert isinstance(compressed, Land) assert isinstance(compressed.representations, list) assert len(compressed.representations) > 0 # 0 is raw precision assert compressed.representations[0].precision_in_km == 0 for i, rep in enumerate(compressed.representations): assert isinstance(rep, MapistoShape) if i > 0: previous = compressed.representations[i - 1] # Assumes precision_levels is sorted asc # representations are sorted by precision asc assert rep.precision_in_km > previous.precision_in_km # Precision asc means size desc (as more precise = bigger definition) assert previous.precision_in_km == 0 or len(rep.d_path) <= len( previous.d_path)
def get(cursor, id): cursor.execute( ''' SELECT territories.territory_id, state_id, min_x, min_y, max_x-min_x, max_y-min_y, validity_start, validity_end, color, name, d_path, precision_in_km FROM territories INNER JOIN territories_shapes ON territories.territory_id=territories_shapes.territory_id WHERE territories.territory_id=%s ORDER BY precision_in_km ''', (id, )) rows = cursor.fetchall() if not len(rows): raise NotFound(f'Territory not found : {id}') (territory_id, state_id, x, y, width, height, validity_start, validity_end, color, name, _, _) = rows[0] bounding_box = BoundingBox(x, y, width, height) representations = [MapistoShape(row[10], row[11]) for row in rows] return Territory(territory_id, representations, state_id, bounding_box, validity_start, validity_end, color, name)
from .Land_CRUD import LandCRUD from .db import get_cursor from werkzeug.exceptions import NotFound import pytest from resources.MapistoShape import MapistoShape from resources.BoundingBox import BoundingBox from resources.Land import Land import copy test_path = "M 0.1 0 L 10.12 10 L 0 15.7 L -2 7 L -3 6 L -5 0 Z" example_land = Land(None, [MapistoShape(test_path, 0)], BoundingBox(-5,0,15,16)) def test_get_land_not_found(): with get_cursor() as curs: with pytest.raises(NotFound): LandCRUD.get_land(curs, -12) def test_add_land(): with get_cursor() as curs: old_count = LandCRUD.count(curs) land_id = LandCRUD.add_land(curs, example_land) print("ADDED ID : ", land_id) with get_cursor() as curs: retrieved = LandCRUD.get_land(curs, land_id) assert isinstance(retrieved, Land) retrieved.land_id=None # example land is retrieved but with none assert retrieved.equals(example_land) assert LandCRUD.count(curs) == old_count+1 def test_count_land():
def test_get_map(): bbox = BoundingBox(0, 0, 1116, 11114) res = MapTag.get(bbox, year_to_date(1918), max(conf.PRECISION_LEVELS)) ensure_is_map_data(res)
def test_get_lands_wrong_precision(): with pytest.raises(BadRequest): LandTag.get_lands(BoundingBox(0,0,4,4), max(conf.PRECISION_LEVELS)+1)
def test_get_lands(): res = LandTag.get_lands(BoundingBox(0,0,4,4), max(conf.PRECISION_LEVELS)) assert isinstance(res, list)
def get_bounding_box(path): minx, miny, maxx, maxy = Polygon(parse_path(path).to_polygons()[0]).bounds # As the coordinates of the polygon will be rounded, the bounding box is stretched to the next integer-coordinates bounding box minx, miny, maxx, maxy = floor(minx), floor(miny), ceil(maxx), ceil(maxy) return BoundingBox(minx, miny, maxx - minx, maxy - miny)