Fix tile generation test.

This commit is contained in:
Sergey Vartanov 2021-08-07 23:30:28 +03:00
parent 3cb462721f
commit eb48600e9c
5 changed files with 37 additions and 24 deletions

View file

@ -37,4 +37,8 @@ echo "Test element generation"
python3 roentgen.py element --node amenity=bench,material=wood \ python3 roentgen.py element --node amenity=bench,material=wood \
|| { echo "FAIL"; exit 1; } || { echo "FAIL"; exit 1; }
echo "Test tile generation"
python3 roentgen.py tile --coordinates 46.09498,10.06879 --cache test/data \
|| { echo "FAIL"; exit 1; }
exit 0 exit 0

View file

@ -38,7 +38,7 @@ def get_osm(
content: Optional[bytes] = get_data( content: Optional[bytes] = get_data(
"api.openstreetmap.org/api/0.6/map", "api.openstreetmap.org/api/0.6/map",
{"bbox": boundary_box}, {"bbox": boundary_box.get_format()},
is_secure=True, is_secure=True,
).decode("utf-8") ).decode("utf-8")

View file

@ -55,7 +55,8 @@ class Tiles:
extended_boundary_box: BoundaryBox = BoundaryBox( extended_boundary_box: BoundaryBox = BoundaryBox(
lon_1, lat_1, lon_2, lat_2 lon_1, lat_1, lon_2, lat_2
) ).round()
return cls(tiles, tile_1, tile_2, scale, extended_boundary_box) return cls(tiles, tile_1, tile_2, scale, extended_boundary_box)
def draw(self, directory: Path, cache_path: Path) -> None: def draw(self, directory: Path, cache_path: Path) -> None:
@ -68,7 +69,7 @@ class Tiles:
get_osm(self.boundary_box, cache_path) get_osm(self.boundary_box, cache_path)
map_ = OSMReader().parse_osm_file( map_ = OSMReader().parse_osm_file(
cache_path / (self.boundary_box.get_format_rounded() + ".osm") cache_path / (self.boundary_box.get_format() + ".osm")
) )
for tile in self.tiles: for tile in self.tiles:
file_path: Path = tile.get_file_name(directory) file_path: Path = tile.get_file_name(directory)
@ -86,7 +87,7 @@ class Tiles:
:param cache_path: directory for temporary SVG file and OSM files :param cache_path: directory for temporary SVG file and OSM files
""" """
output_path: Path = cache_path / ( output_path: Path = cache_path / (
self.boundary_box.get_format_rounded() + ".svg" self.boundary_box.get_format() + ".svg"
) )
if not output_path.exists(): if not output_path.exists():
content = get_osm(self.boundary_box, cache_path) content = get_osm(self.boundary_box, cache_path)
@ -95,7 +96,7 @@ class Tiles:
return None return None
map_: Map = OSMReader().parse_osm_file( map_: Map = OSMReader().parse_osm_file(
cache_path / (self.boundary_box.get_format_rounded() + ".osm") cache_path / (self.boundary_box.get_format() + ".osm")
) )
lat_2, lon_1 = self.tile_1.get_coordinates() lat_2, lon_1 = self.tile_1.get_coordinates()
lat_1, lon_2 = Tile( lat_1, lon_2 = Tile(
@ -129,9 +130,7 @@ class Tiles:
with output_path.open("w+") as output_file: with output_path.open("w+") as output_file:
svg.write(output_file) svg.write(output_file)
png_path: Path = ( png_path: Path = cache_path / f"{self.boundary_box.get_format()}.png"
cache_path / f"{self.boundary_box.get_format_rounded()}.png"
)
if not png_path.exists(): if not png_path.exists():
rasterize(output_path, png_path) rasterize(output_path, png_path)
@ -189,14 +188,12 @@ class Tile:
self.x + 1, self.y + 1, self.scale self.x + 1, self.y + 1, self.scale
).get_coordinates() ).get_coordinates()
# FIXME: check negative values lat_2 = point_1[0]
lon_1 = point_1[1]
lat_1 = point_2[0]
lon_2 = point_2[1]
lat_2 = int(point_1[0] * 1000) / 1000 + 0.002 return BoundaryBox(lon_1, lat_1, lon_2, lat_2).round()
lon_1 = int(point_1[1] * 1000) / 1000 - 0.001
lat_1 = int(point_2[0] * 1000) / 1000 - 0.001
lon_2 = int(point_2[1] * 1000) / 1000 + 0.002
return BoundaryBox(lon_1, lat_1, lon_2, lat_2)
def load_map(self, cache_path: Path) -> Map: def load_map(self, cache_path: Path) -> Map:
""" """
@ -208,7 +205,7 @@ class Tile:
get_osm(boundary_box, cache_path) get_osm(boundary_box, cache_path)
return OSMReader().parse_osm_file( return OSMReader().parse_osm_file(
cache_path / f"{boundary_box.format()}.osm" cache_path / f"{boundary_box.get_format()}.osm"
) )
def get_file_name(self, directory_name: Path) -> Path: def get_file_name(self, directory_name: Path) -> Path:

View file

@ -257,16 +257,18 @@ class BoundaryBox:
"""Get right bottom corner of the boundary box.""" """Get right bottom corner of the boundary box."""
return self.bottom, self.right return self.bottom, self.right
def get_format_rounded(self) -> str: def round(self) -> "BoundaryBox":
""" """
Get text representation of the boundary box: Round boundary box.
<longitude 1>,<latitude 1>,<longitude 2>,<latitude 2>. Coordinates are
rounded to three digits after comma.
""" """
return ( # FIXME: check negative values
f"{self.left - 0.001:.3f},{self.bottom - 0.001:.3f},"
f"{self.right + 0.001:.3f},{self.top + 0.001:.3f}" self.left = int(self.left * 1000) / 1000 - 0.001
) self.bottom = int(self.bottom * 1000) / 1000 - 0.001
self.right = int(self.right * 1000) / 1000 + 0.002
self.top = int(self.top * 1000) / 1000 + 0.002
return self
def get_format(self) -> str: def get_format(self) -> str:
""" """

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.8.5 (2521311 spike-06.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<bounds minlat="46.0930000" minlon="10.0660000" maxlat="46.0970000" maxlon="10.0700000"/>
<node id="534246185" visible="true" version="1" changeset="2872960" timestamp="2009-10-17T13:34:50Z" user="kaitu" uid="69717" lat="46.0949830" lon="10.0687940">
<tag k="ele" v="2743"/>
<tag k="name" v="Cima Tresciana"/>
<tag k="natural" v="peak"/>
<tag k="source" v="Regione Lombardia - La banca dati territoriali CTR10000 e' prodotta da Regione Lombardia - Infrastruttura per l'Informazione Territoriale"/>
</node>
</osm>