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_lands(cursor, bbox, precision): assert isinstance(bbox, BoundingBox) cursor.execute( ''' SELECT land_id, d_path FROM lands NATURAL JOIN lands_shapes WHERE precision_in_km=%s AND NOT( %s < lands.min_x OR lands.max_x < %s OR %s < lands.min_y OR lands.max_y < %s ) ''', (precision, bbox.max_x, bbox.x, bbox.max_y, bbox.y)) return [ Land(row[0], [MapistoShape(row[1], precision)]) for row in cursor.fetchall() ]
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 post_land(): land = Land.from_dict(request.json) return {"added_land": LandTag.post_land(land)}
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_post_too_small(): too_small_land = Land.from_dict({'d_path' : 'M 0 0 L 0.00000001 0.0000002 L 0 0.0000002 Z'}) with pytest.raises(BadRequest): LandTag.post_land(too_small_land)
from land_tag import LandTag from crud.db import get_cursor from crud.Land_CRUD import LandCRUD from maps_geometry.compression import compress_land from resources.MapistoShape import MapistoShape from resources.Land import Land from werkzeug.exceptions import BadRequest, NotFound import conf import pytest from resources.BoundingBox import BoundingBox 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.from_dict({ 'd_path' : test_path }) def test_post_and_get_land(): land_id = LandTag.post_land(example_land) land = LandTag.get_land(land_id) assert land.land_id==land_id assert land.representations[0].equals(example_land.representations[0]) def test_post_too_small(): too_small_land = Land.from_dict({'d_path' : 'M 0 0 L 0.00000001 0.0000002 L 0 0.0000002 Z'}) with pytest.raises(BadRequest): LandTag.post_land(too_small_land) def test_not_found(): with pytest.raises(NotFound): LandTag.get_land(-1)
def compress_land(land): assert isinstance(land, Land) return Land(land.land_id, _get_compressed_shapes(land.representations[0]), land.bounding_box)