flopp / py-staticmaps

A python module to create static map images with markers, geodesic lines, etc.
MIT License
128 stars 17 forks source link

Enforce displayed map area #34

Open schneidr opened 11 months ago

schneidr commented 11 months ago

I'm trying to create a video from a GPX file where the track is drawn step by step. To do so I create a sequence of images that are used to create a video file in a second step. For a stable video all images need to show exactly the same map area.

I managed to constantly show the same map area by creating a transparent area with all the points before drawing the track, with one point added in every iteration of the loop.

context = staticmaps.Context()

count: int = 0
for track in gpx.tracks:
    for segment in track.segments:
        bounds = staticmaps.Area(
                [staticmaps.create_latlng(point.latitude, point.longitude) for point in segment.points],
                fill_color=staticmaps.TRANSPARENT,
                width=8,
                color=staticmaps.TRANSPARENT,
            )
        context.add_object(bounds)

        points: list = []
        for point in progress.iter_bar(points=segment.points):
            count += 1
            latlng = staticmaps.create_latlng(point.latitude, point.longitude)
            points.append(latlng)
            if count > 1:
                line = staticmaps.Line(points, color=color, width=args.trackwidth)
                context.add_object(line)
                marker = staticmaps.ImageMarker(latlng, "marker_dot.png", origin_x=5, origin_y=5)
                context.add_object(marker)
                filename = "{0}/{1:06d}.png".format(tmpdir.name, count)
                image = context.render_cairo(args.width, args.height)
                image.write_to_png(filename)
                context._objects.remove(line)
                context._objects.remove(marker)

This works when I only draw the track. As soon as I add a marker to the latest point of the drawn track the video created from the image sequence starts to jitter, meaning the drawn area varies slightly between the images.

Example videos:

This is only a snippet, the full script is available on Github.

Is there any way to consistently enforce the same map area?