Пример #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))
Пример #2
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)
Пример #3
0
def create_area(*coords):
    context = staticmaps.Context()
    context.set_tile_provider(staticmaps.tile_provider_OSM)
    rectangle = [(coords[0], coords[1]), (coords[2], coords[1]),
                 (coords[2], coords[3]), (coords[0], coords[3]),
                 (coords[0], coords[1])]

    context.add_object(
        staticmaps.Area(
            [staticmaps.create_latlng(lat, lng) for lat, lng in rectangle],
            fill_color=staticmaps.parse_color("#FFFFFF00"),
            width=2,
            color=staticmaps.BLUE,
        ))
    return context
Пример #4
0
        path.push(f" l {w / 2 - self._arrow / 2} 0")
        path.push("Z")
        renderer.group().add(path)

        renderer.group().add(renderer.drawing().text(
            self._text,
            text_anchor="middle",
            dominant_baseline="central",
            insert=(x, y - self._arrow - h / 2),
            font_family="sans-serif",
            font_size=f"{self._font_size}px",
            fill="#000000",
        ))


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(TextLabel(p1, "X"))
context.add_object(TextLabel(p2, "Label"))
context.add_object(TextLabel(p3, "This is a very long text label"))

context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels)

# render png via pillow
image = context.render_pillow(800, 500)
image.save("custom_objects.pillow.png")
Пример #5
0
def test_render_center_zoom() -> None:
    context = staticmaps.Context()
    context.set_tile_downloader(MockTileDownloader())
    context.set_center(staticmaps.create_latlng(48, 8))
    context.set_zoom(15)
    context.render_svg(200, 100)
Пример #6
0
def test_render_empty() -> None:
    context = staticmaps.Context()
    with pytest.raises(RuntimeError):
        context.render_svg(200, 100)
Пример #7
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}")
Пример #8
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)