flopp/py-staticmaps

Failing to retrieve an image with set coordinates, zoom and size

Lohorunk opened this issue · 5 comments

Im trying to get a satellite image with specific center coordinates and zoom via staticmaps

import staticmaps
import s2sphere 

context = staticmaps.Context()

context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)

context.set_zoom(15)
context.set_center(s2sphere.LatLng(60.169320, 24.952011))

image = context.render_pillow(500, 500)
image.show()

But i get the following error:

File "/home/user/Projects/a/b/c/d/api.py", line 29, 
    image = context.render_pillow(500, 500)
  File "/home/user/Projects/a/b/lib/python3.10/site-packages/staticmaps/context.py", line 147, in render_pillow
    trans = Transformer(width, height, zoom, center, self._tile_provider.tile_size())
  File "/home/user/Projects/a/b/lib/python3.10/site-packages/staticmaps/transformer.py", line 22, in __init__
    self._tile_center_x, self._tile_center_y = self.ll2t(center)
  File "/home/user/Projects/a/b/lib/python3.10/site-packages/staticmaps/transformer.py", line 182, in ll2t
    x, y = self.mercator(latlng)
  File "/home/user/Projects/a/b/lib/python3.10/site-packages/staticmaps/transformer.py", line 156, in mercator
    return lng / (2 * math.pi) + 0.5, (1 - math.log(math.tan(lat) + (1 / math.cos(lat))) / math.pi) / 2
ValueError: math domain error

However if i use different coordinates "51.230372, 6.787148" (Düsseldorf)
It works, but It is a completely different place

Hello hiikion,

I don't know why, but when You add an object instead of setting the centre, it works, e.g.:

import staticmaps
import s2sphere 

context = staticmaps.Context()

context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)

helsinki = staticmaps.create_latlng(60.169320, 24.952011)
context.add_object(staticmaps.Marker(helsinki, color=staticmaps.GREEN, size=12))
context.set_zoom(15)

image = context.render_pillow(500, 500)
image.show()

Hello hiikion,

I don't know why, but when You add an object instead of setting the centre, it works, e.g.:

import staticmaps
import s2sphere 

context = staticmaps.Context()

context.set_tile_provider(staticmaps.tile_provider_ArcGISWorldImagery)

helsinki = staticmaps.create_latlng(60.169320, 24.952011)
context.add_object(staticmaps.Marker(helsinki, color=staticmaps.GREEN, size=12))
context.set_zoom(15)

image = context.render_pillow(500, 500)
image.show()

Any way of getting a clear image without markers?

Hello hiikion,

replace the line
context.set_center(s2sphere.LatLng(60.169320, 24.952011))
with
context.set_center(staticmaps.create_latlng(60.169320, 24.952011))
and it works ...

it works, thanks.

Just for completeness, the following would also work:
context.set_center(s2sphere.LatLng.from_degrees(60.169320, 24.952011))