Beispiel #1
0
def test_bounds() -> None:
    context = staticmaps.Context()
    assert context.object_bounds() is None

    context.add_object(staticmaps.Marker(staticmaps.create_latlng(48, 8)))
    bounds = context.object_bounds()
    assert bounds is not None
    assert bounds.is_point()

    context.add_object(staticmaps.Marker(staticmaps.create_latlng(47, 7)))
    assert context.object_bounds() is not None
    assert context.object_bounds() == s2sphere.LatLngRect(
        staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8))

    context.add_object(staticmaps.Marker(staticmaps.create_latlng(47.5, 7.5)))
    assert context.object_bounds() is not None
    assert context.object_bounds() == s2sphere.LatLngRect(
        staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8))

    context.add_bounds(
        s2sphere.LatLngRect(staticmaps.create_latlng(46, 6),
                            staticmaps.create_latlng(49, 9)))
    assert context.object_bounds() is not None
    assert context.object_bounds() == s2sphere.LatLngRect(
        staticmaps.create_latlng(46, 6), staticmaps.create_latlng(49, 9))

    context.add_bounds(
        s2sphere.LatLngRect(staticmaps.create_latlng(47.5, 7.5),
                            staticmaps.create_latlng(48, 8)))
    assert context.object_bounds() is not None
    assert context.object_bounds() == s2sphere.LatLngRect(
        staticmaps.create_latlng(47, 7), staticmaps.create_latlng(48, 8))
Beispiel #2
0
 def add_cluster(self,
                 cluster,
                 size=6,
                 color=None,
                 fill_color=None,
                 width=2,
                 colors=None):
     for lat, lon, id, day in zip(cluster.lats, cluster.lons, cluster.ids,
                                  cluster.days):
         if colors is not None:
             color = colors[id]
         point = staticmaps.create_latlng(lat, lon)
         self.add_object(staticmaps.Marker(point, color=color, size=size))
Beispiel #3
0
def test_creation() -> None:
    staticmaps.Marker(staticmaps.create_latlng(48, 8),
                      color=staticmaps.YELLOW,
                      size=8)
Beispiel #4
0
def test_bounds() -> None:
    marker = staticmaps.Marker(staticmaps.create_latlng(48, 8))
    assert marker.bounds().is_point()
                  mfc='#ff1515',
                  mec='#ff1515',
                  marker='o',
                  markersize=8,
                  label='Startpoint')
]

colors = [
    "#2db60a", "#0ab6b6", "#b60ab1", "#0a37b6", "#b6690a", "#0ab64f",
    "#0a6fb6", "#740ab6", "#2b6435", "#642b48", "#333"
]

context = staticmaps.Context()
context.add_object(
    staticmaps.Marker(staticmaps.create_latlng(startpoint[0], startpoint[1]),
                      color=staticmaps.parse_color("#ff1515"),
                      size=12))
context.add_object(
    staticmaps.Marker(staticmaps.create_latlng(min_lat, min_long),
                      color=staticmaps.parse_color("#ffffff00"),
                      size=0))
context.add_object(
    staticmaps.Marker(staticmaps.create_latlng(max_lat, max_long),
                      color=staticmaps.parse_color("#ffffff00"),
                      size=0))

for index, i in enumerate(goal_coordinates):
    color = colors[index % len(colors)]
    legend.append(
        mlines.Line2D([], [],
                      color=color,
Beispiel #6
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)
# Copyright (c) 2020 Florian Pigorsch; see /LICENSE for licensing information

import staticmaps

context = staticmaps.Context()

p1 = staticmaps.create_latlng(48.005774, 7.834042)
p2 = staticmaps.create_latlng(47.988716, 7.868804)
p3 = staticmaps.create_latlng(47.985958, 7.824601)

context.add_object(
    staticmaps.Area([p1, p2, p3, p1],
                    color=staticmaps.RED,
                    fill_color=staticmaps.TRANSPARENT,
                    width=2))
context.add_object(staticmaps.Marker(p1, color=staticmaps.BLUE))
context.add_object(staticmaps.Marker(p2, color=staticmaps.GREEN))
context.add_object(staticmaps.Marker(p3, color=staticmaps.YELLOW))

for name, provider in staticmaps.default_tile_providers.items():
    context.set_tile_provider(provider)

    # render png via pillow
    image = context.render_pillow(800, 500)
    image.save(f"provider_{name}.pillow.png")

    # render png via cairo
    if staticmaps.cairo_is_supported():
        image = context.render_cairo(800, 500)
        image.write_to_png(f"provider_{name}.cairo.png")
Beispiel #8
0
import json
import requests
import staticmaps

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

URL = (
    "https://gist.githubusercontent.com/jpriebe/d62a45e29f24e843c974/"
    "raw/b1d3066d245e742018bce56e41788ac7afa60e29/us_state_capitals.json"
)
response = requests.get(URL)
for _, data in json.loads(response.text).items():
    capital = staticmaps.create_latlng(float(data["lat"]), float(data["long"]))
    context.add_object(staticmaps.Marker(capital, size=5))

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

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

# render svg
svg_image = context.render_svg(800, 500)
with open("us_capitals.svg", "w", encoding="utf-8") as f:
    svg_image.write(f, pretty=True)
Beispiel #9
0
center1 = staticmaps.create_latlng(66, 0)
center2 = staticmaps.create_latlng(0, 0)

context.add_object(
    staticmaps.Circle(center1,
                      2000,
                      fill_color=staticmaps.TRANSPARENT,
                      color=staticmaps.RED,
                      width=2))
context.add_object(
    staticmaps.Circle(center2,
                      2000,
                      fill_color=staticmaps.TRANSPARENT,
                      color=staticmaps.GREEN,
                      width=2))
context.add_object(staticmaps.Marker(center1, color=staticmaps.RED))
context.add_object(staticmaps.Marker(center2, color=staticmaps.GREEN))

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

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

# render svg
svg_image = context.render_svg(800, 600)
with open("geodesic_circles.svg", "w", encoding="utf-8") as f:
    svg_image.write(f, pretty=True)
Beispiel #10
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}")
Beispiel #11
0
def plot(gps_txt, bt_txt, out_png):
    context = staticmaps.Context()
    context.set_tile_provider(staticmaps.tile_provider_OSM)

    gps_objs = []

    with open(gps_txt, 'r') as f:
        for line in f.readlines():
            loaded = json.loads(line)
            coords = loaded['coords']
            latitude = coords['latitude']
            longitude = coords['longitude']
            latlng = staticmaps.create_latlng(latitude, longitude)
            marker = staticmaps.Marker(latlng, size=5)
            context.add_object(marker)
            timestamp = loaded['timestamp']
            ts = timestamp_to_datetime_gps(timestamp)
            gps_objs.append({
                'ts': ts,
                'latitude': latitude,
                'longitude': longitude,
            })

    i = 0
    with open(bt_txt, 'r') as f:
        for line in f.readlines():
            i += 1
            print('progress:', i)
            loaded = json.loads(line)
            timestamp = loaded['Timestamp']
            ts = timestamp_to_datetime_bt(timestamp)
            last = None
            current = None
            found = None
            for gps_obj in gps_objs:
                if gps_obj['ts'] > ts:
                    if last is None:
                        print('bad gps: no previous')
                    else:
                        found = last
                        current = gps_obj
                    break
                else:
                    last = gps_obj
            if found is None:
                print('bad gps: not found!')
                break
            device_lat = (last['latitude'] + current['latitude']) / 2.0
            device_long = (last['longitude'] + current['longitude']) / 2.0
            lat_dist = abs(current['latitude'] - last['latitude'])
            long_dist = abs(current['longitude'] - last['longitude'])
            radius = (lat_dist + long_dist) / 2.0
            multiplier = 100

            latlng = staticmaps.create_latlng(device_lat, device_long)
            circle = staticmaps.Circle(latlng,
                                       radius * multiplier,
                                       fill_color=staticmaps.TRANSPARENT,
                                       color=staticmaps.BLUE,
                                       width=2)
            context.add_object(circle)

    image = context.render_cairo(1600, 900)
    image.write_to_png(out_png)