Example #1
0
def test_bad_creation() -> None:
    with pytest.raises(ValueError):
        staticmaps.Line([])

    with pytest.raises(ValueError):
        staticmaps.Line([staticmaps.create_latlng(48, 8)])

    with pytest.raises(ValueError):
        staticmaps.Line(
            [staticmaps.create_latlng(48, 8),
             staticmaps.create_latlng(49, 9)],
            width=-123)
def find_route(endpoint, data, color, staticmaps_context=None):
    node = nodelist[endpoint]
    while True:
        old_node = node
        node_index = [i for i, x in enumerate(data) if x['index'] == node][0]
        try:
            node = data[node_index]['pre']
        except:
            break
        if staticmaps_context is not None:
            staticmaps_context.add_object(
                staticmaps.Line([
                    staticmaps.create_latlng(
                        nodepl[nodelist.index(old_node)][0],
                        nodepl[nodelist.index(old_node)][1]),
                    staticmaps.create_latlng(nodepl[nodelist.index(node)][0],
                                             nodepl[nodelist.index(node)][1])
                ],
                                color=staticmaps.parse_color(color),
                                width=4))
        plt.plot([
            nodepl[nodelist.index(old_node)][1],
            nodepl[nodelist.index(node)][1]
        ], [
            nodepl[nodelist.index(old_node)][0],
            nodepl[nodelist.index(node)][0]
        ],
                 c=color)
Example #3
0
def test_creation() -> None:
    staticmaps.Line(
        [
            staticmaps.create_latlng(48, 8),
            staticmaps.create_latlng(49, 9),
            staticmaps.create_latlng(50, 8)
        ],
        color=staticmaps.YELLOW,
    )
Example #4
0
def test_bounds() -> None:
    line = staticmaps.Line(
        [
            staticmaps.create_latlng(48, 8),
            staticmaps.create_latlng(49, 9),
            staticmaps.create_latlng(50, 8)
        ],
        color=staticmaps.YELLOW,
    )
    assert not line.bounds().is_point()
Example #5
0
 def create_thumbnail(self, output_dir: str) -> None:
     if not self.track:
         return
     os.makedirs(output_dir, exist_ok=True)
     file_name = os.path.join(output_dir, f"{self.strava_id}.png")
     context = staticmaps.Context()
     context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)
     line = [staticmaps.create_latlng(*point) for point in self.track]
     context.add_object(staticmaps.Line(line))
     image = context.render_cairo(800, 500)
     image.write_to_png(file_name)
Example #6
0
import sys

import gpxpy  # type: ignore
import staticmaps

context = staticmaps.Context()
context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)

with open(sys.argv[1], "r") as file:
    gpx = gpxpy.parse(file)

for track in gpx.tracks:
    for segment in track.segments:
        line = [staticmaps.create_latlng(p.latitude, p.longitude) for p in segment.points]
        context.add_object(staticmaps.Line(line))

# add an image marker to the first track point
for p in gpx.walk(only_points=True):
    pos = staticmaps.create_latlng(p.latitude, p.longitude)
    marker = staticmaps.ImageMarker(pos, "start.png", origin_x=27, origin_y=35)
    context.add_object(marker)
    break

# render png via pillow
image = context.render_pillow(800, 500)
image.save("running.pillow.png")

# render png via cairo
if staticmaps.cairo_is_supported():
    image = context.render_cairo(800, 500)
Example #7
0
#!/usr/bin/env python

# py-staticmaps
# Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information

import staticmaps

context = staticmaps.Context()
context.set_tile_provider(staticmaps.tile_provider_StamenToner)

frankfurt = staticmaps.create_latlng(50.110644, 8.682092)
newyork = staticmaps.create_latlng(40.712728, -74.006015)

context.add_object(staticmaps.Line([frankfurt, newyork], color=staticmaps.BLUE, width=4))
context.add_object(staticmaps.Marker(frankfurt, color=staticmaps.GREEN, size=12))
context.add_object(staticmaps.Marker(newyork, color=staticmaps.RED, size=12))

# render png via pillow
image = context.render_pillow(800, 500)
image.save("frankfurt_newyork.pillow.png")

# render png via cairo
if staticmaps.cairo_is_supported():
    image = context.render_cairo(800, 500)
    image.write_to_png("frankfurt_newyork.cairo.png")

# render svg
svg_image = context.render_svg(800, 500)
with open("frankfurt_newyork.svg", "w", encoding="utf-8") as f:
    svg_image.write(f, pretty=True)
Example #8
0
def main() -> None:
    args_parser = argparse.ArgumentParser(prog="createstaticmap")
    args_parser.add_argument(
        "--center",
        metavar="LAT,LNG",
        type=str,
    )
    args_parser.add_argument(
        "--zoom",
        metavar="ZOOM",
        type=int,
    )
    args_parser.add_argument(
        "--width",
        metavar="WIDTH",
        type=int,
        required=True,
    )
    args_parser.add_argument(
        "--height",
        metavar="HEIGHT",
        type=int,
        required=True,
    )
    args_parser.add_argument(
        "--background",
        metavar="COLOR",
        type=str,
    )
    args_parser.add_argument(
        "--marker",
        metavar="LAT,LNG",
        type=str,
        action="append",
    )
    args_parser.add_argument(
        "--line",
        metavar="LAT,LNG LAT,LNG ...",
        type=str,
        action="append",
    )
    args_parser.add_argument(
        "--area",
        metavar="LAT,LNG LAT,LNG ...",
        type=str,
        action="append",
    )
    args_parser.add_argument(
        "--bounds",
        metavar="LAT,LNG LAT,LNG",
        type=str,
    )
    args_parser.add_argument(
        "--tiles",
        metavar="TILEPROVIDER",
        type=str,
        choices=staticmaps.default_tile_providers.keys(),
        default=staticmaps.tile_provider_OSM.name(),
    )
    args_parser.add_argument(
        "--file-format",
        metavar="FORMAT",
        type=FileFormat,
        choices=FileFormat,
        default=FileFormat.GUESS,
    )
    args_parser.add_argument(
        "filename",
        metavar="FILE",
        type=str,
        nargs=1,
    )

    args = args_parser.parse_args()

    context = staticmaps.Context()

    context.set_tile_provider(staticmaps.default_tile_providers[args.tiles])

    if args.center is not None:
        context.set_center(staticmaps.parse_latlng(args.center))
    if args.zoom is not None:
        context.set_zoom(args.zoom)
    if args.background is not None:
        context.set_background_color(staticmaps.parse_color(args.background))
    if args.area:
        for coords in args.area:
            context.add_object(
                staticmaps.Area(staticmaps.parse_latlngs(coords)))
    if args.line:
        for coords in args.line:
            context.add_object(
                staticmaps.Line(staticmaps.parse_latlngs(coords)))
    if args.marker:
        for coords in args.marker:
            context.add_object(
                staticmaps.Marker(staticmaps.parse_latlng(coords)))
    if args.bounds is not None:
        context.add_bounds(staticmaps.parse_latlngs2rect(args.bounds))

    file_name = args.filename[0]
    if determine_file_format(args.file_format, file_name) == FileFormat.PNG:
        image = context.render_cairo(args.width, args.height)
        image.write_to_png(file_name)
    else:
        svg_image = context.render_svg(args.width, args.height)
        with open(file_name, "w", encoding="utf-8") as f:
            svg_image.write(f, pretty=True)
    print(f"wrote result image to {file_name}")