flopp / py-staticmaps

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

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

Closed Lohorunk closed 1 year ago

Lohorunk commented 1 year ago

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

lowtower commented 1 year ago

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()
Lohorunk commented 1 year ago

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?

lowtower commented 1 year ago

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 ...

Lohorunk commented 1 year ago

it works, thanks.

lowtower commented 1 year ago

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