py-staticmaps icon indicating copy to clipboard operation
py-staticmaps copied to clipboard

How to set bounding box to map?

Open eruiz67 opened this issue 1 year ago • 1 comments

Im trayin to represent a polygon in the map. But when I do so, The size of the polygon in the map is so little. I need a way to define a bounding box in the map, or a zoom level, so I can the the polygon bigger. polygon pillow Here is the code:


import staticmaps

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

polygon = [
    (11.946898955783189, -86.1092862482307 ),
    (11.947196373156675, -86.10915988584337 ),
    (11.947196373156675, -86.10896210123707),
    (11.946897164111272, -86.1088650402729),
    (11.946898955783189, -86.1092862482307)
]

context.add_object(
    staticmaps.Area(
        [staticmaps.create_latlng(lat, lng) for lat, lng in polygon],
        fill_color=staticmaps.parse_color("#00FF003F"),
        width=2,
        color=staticmaps.BLUE,
    )
)

# render non-anti-aliased png
image = context.render_pillow(800, 500)
image.save("polygon.pillow.png")

# render anti-aliased png (this only works if pycairo is installed)
image = context.render_cairo(800, 500)
image.write_to_png("polygon.cairo.png")

# render svg
svg_image = context.render_svg(800, 500)
with open("polygon.svg", "w", encoding="utf-8") as f:
    svg_image.write(f, pretty=True)

eruiz67 avatar Feb 02 '23 15:02 eruiz67

Hello @eruiz67

the zoom should be determined automatically - in Your case it is calculated to 15. Instead, you can set the zoom "manually" with context.set_zoom(), e.g.

...
staticmaps.context.add_object(
    staticmaps.Area(
        [staticmaps.create_latlng(lat, lng) for lat, lng in polygon],
        fill_color=staticmaps.parse_color("#00FF003F"),
        width=2,
        color=staticmaps.BLUE,
    )
)
staticmaps.context.set_zoom(17)
...

Cheers, LT

lowtower avatar Feb 03 '23 10:02 lowtower