Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set bounding box to map? #30

Open
eruiz67 opened this issue Feb 2, 2023 · 2 comments
Open

How to set bounding box to map? #30

eruiz67 opened this issue Feb 2, 2023 · 2 comments

Comments

@eruiz67
Copy link

eruiz67 commented Feb 2, 2023

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)
@lowtower
Copy link
Contributor

lowtower commented Feb 3, 2023

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

@wyhinton
Copy link

def render_cairo(self, width: int, height: int, bounds: LatLngRect) -> typing.Any:
    """Render area using cairo

    :param width: width of static map
    :type width: int
    :param height: height of static map
    :type height: int
    :return: cairo image
    :rtype: cairo.ImageSurface
    :raises RuntimeError: raises runtime error if cairo is not available
    :raises RuntimeError: raises runtime error if map has no center and zoom
    """

    center, zoom = self.determine_center_zoom(width, height)
    if center is None or zoom is None:
        raise RuntimeError("Cannot render map without center/zoom.")

    trans = Transformer(width, height, zoom, center, self._tile_provider.tile_size())
    x, y = trans.ll2pixel(bounds.lo())
    a, b = trans.ll2pixel(bounds.hi())

    renderer = CairoRenderer(trans)
    renderer.render_background(self._background_color)
    renderer.render_tiles(self._fetch_tile)
    renderer.render_objects(self._objects)
    renderer.render_attribution(self._tile_provider.attribution())
    return crop_image_surface(renderer.image_surface(), int(x), int(b), int(a-x), int(y-b) )

staticmaps.Context.render_cairo = render_cairo

This monkey patch worked for me for the cairo renderer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants