diff --git a/requirements.txt b/requirements.txt index b3bfae1..f45f77f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,11 @@ +CairoSVG~=2.5.0 colour~=0.1.5 +logging~=0.4.9.6 numpy>=1.18.1 portolan~=1.0.1 pycairo -pytest -pyyaml>=4.2b1 -shapely +pytest~=6.2.2 +PyYAML>=4.2b1 +Shapely svgwrite~=1.4 -urllib3>=1.25.6 +urllib3>=1.25.6 \ No newline at end of file diff --git a/roentgen/raster.py b/roentgen/raster.py deleted file mode 100644 index db16baa..0000000 --- a/roentgen/raster.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -Rasterize vector graphics using Inkscape. -""" -import logging -import os -import subprocess -from pathlib import Path - -__author__ = "Sergey Vartanov" -__email__ = "me@enzet.ru" - -INKSCAPE_BIN: str = "INKSCAPE_BIN" - - -def rasterize(from_: Path, to_: Path, area: str = "", dpi: float = 90) -> None: - """ - Make PNG image out of SVG using Inkscape. - - See https://inkscape.org/ - """ - if "INKSCAPE_BIN" not in os.environ: - logging.fatal( - f"Environment variable {INKSCAPE_BIN} not set. Please, install " - f"Inkscape and set the variable to be able to rasterize SVG files." - ) - - commands: list[str] = [os.environ[INKSCAPE_BIN]] - commands += ["--export-png", to_.absolute()] - commands += ["--export-dpi", str(dpi)] - if area: - commands += ["--export-area", area] - commands += [from_.absolute()] - - logging.info(f"Rasterize SVG file to {to_}...") - subprocess.run(commands) diff --git a/roentgen/server.py b/roentgen/server.py index 81b33e3..00c44f5 100644 --- a/roentgen/server.py +++ b/roentgen/server.py @@ -6,7 +6,8 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from pathlib import Path from typing import Optional -from roentgen.raster import rasterize +import cairosvg + from roentgen.tile import Tile from roentgen.workspace import workspace @@ -42,7 +43,8 @@ class Handler(BaseHTTPRequestHandler): if not svg_path.exists(): tile = Tile(x, y, zoom) tile.draw(tile_path, self.cache) - rasterize(svg_path, png_path) + cairosvg.svg2png(file_obj=svg_path, write_to=str(png_path)) + logging.info(f"SVG file is rasterized to {png_path}.") if zoom != 18: return if png_path.exists(): diff --git a/roentgen/tile.py b/roentgen/tile.py index 36be2a9..5e14cc6 100644 --- a/roentgen/tile.py +++ b/roentgen/tile.py @@ -9,6 +9,7 @@ from dataclasses import dataclass from pathlib import Path from typing import Optional +import cairosvg import numpy as np import svgwrite @@ -18,7 +19,6 @@ from roentgen.icon import ShapeExtractor from roentgen.mapper import Map from roentgen.osm_getter import NetworkError, get_osm from roentgen.osm_reader import OSMData, OSMReader -from roentgen.raster import rasterize from roentgen.scheme import Scheme from roentgen.ui import BoundaryBox from roentgen.util import MinMax @@ -84,7 +84,8 @@ class Tiles: output_path: Path = file_path.with_suffix(".png") if not output_path.exists(): - rasterize(file_path, output_path) + cairosvg.svg2png(file_obj=file_path, write_to=str(output_path)) + logging.info(f"SVG file is rasterized to {output_path}.") else: logging.info(f"File {output_path} already exists.") @@ -135,7 +136,8 @@ class Tiles: png_path: Path = cache_path / f"{self.boundary_box.get_format()}.png" if not png_path.exists(): - rasterize(output_path, png_path) + cairosvg.svg2png(file_obj=output_path, write_to=str(png_path)) + logging.info(f"SVG file is rasterized to {png_path}.") else: logging.info(f"File {png_path} already exists.")