Fix rasterization.

This commit is contained in:
Sergey Vartanov 2021-08-21 22:11:34 +03:00
parent c20baa1554
commit 970046aa86
2 changed files with 12 additions and 5 deletions

View file

@ -2,7 +2,7 @@
Röntgen tile server for sloppy maps.
"""
import logging
from http.server import BaseHTTPRequestHandler, HTTPServer
from http.server import SimpleHTTPRequestHandler, HTTPServer
from pathlib import Path
from typing import Optional
@ -15,7 +15,7 @@ __author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
class Handler(BaseHTTPRequestHandler):
class Handler(SimpleHTTPRequestHandler):
"""
HTTP request handler that process sloppy map tile requests.
"""
@ -43,7 +43,10 @@ class Handler(BaseHTTPRequestHandler):
if not svg_path.exists():
tile = Tile(x, y, zoom)
tile.draw(tile_path, self.cache)
cairosvg.svg2png(file_obj=svg_path, write_to=str(png_path))
with svg_path.open() as input_file:
cairosvg.svg2png(
file_obj=input_file, write_to=str(png_path)
)
logging.info(f"SVG file is rasterized to {png_path}.")
if zoom != 18:
return

View file

@ -84,7 +84,10 @@ class Tiles:
output_path: Path = file_path.with_suffix(".png")
if not output_path.exists():
cairosvg.svg2png(file_obj=file_path, write_to=str(output_path))
with file_path.open() as input_file:
cairosvg.svg2png(
file_obj=input_file, write_to=str(output_path)
)
logging.info(f"SVG file is rasterized to {output_path}.")
else:
logging.info(f"File {output_path} already exists.")
@ -136,7 +139,8 @@ class Tiles:
png_path: Path = cache_path / f"{self.boundary_box.get_format()}.png"
if not png_path.exists():
cairosvg.svg2png(file_obj=output_path, write_to=str(png_path))
with output_path.open() as input_file:
cairosvg.svg2png(file_obj=input_file, write_to=str(png_path))
logging.info(f"SVG file is rasterized to {png_path}.")
else:
logging.info(f"File {png_path} already exists.")