csparpa / pyowm

A Python wrapper around the OpenWeatherMap web API
https://pyowm.readthedocs.io
MIT License
789 stars 171 forks source link

UnicodeEncodeError errors related to a corrupted location name ? #296

Closed xla99 closed 4 years ago

xla99 commented 4 years ago

Hi there, Everything was working fine for our weather forecast routine until we’ve started to get UnicodeEncodeError errors. If you run the following piece of code to get forecast for Tolagnaro (or Fort Dauphin) in Madagascar it return the error. I checked l.get_name() and it returns ‘Tôlanaro’ with the accent. I’m not sure that accent was there before. I’m not a developer and I couldn’t fix that error (tried all possible encode-decode, it's Chinese for me) Is that possible to get a forecast based on lat-long coordinates, instead of providing the name of the station to the owm.three_hours_forecast function ? Thanks. Alex.

Code :

from pyowm import OWM

API_key = 'xxxxxxxxxxxxxxxxxxxxxxx'
owm = OWM(API_key,language='fr')

obs = owm.weather_at_id(1055433)

l = obs.get_location()

fc = owm.three_hours_forecast(l.get_name())

csparpa commented 4 years ago

Hello Alex

Is that possible to get a forecast based on lat-long coordinates, instead of providing the name of the station to the owm.three_hours_forecast function ?

Sure it is!

Use the three_hours_forecast_at_coords function, it's what you're looking for. Eg:

from pyowm import OWM
API_key = 'xxxxxxxxxxxxxxxxxxxxxxx'
owm = OWM(API_key,language='fr')
obs = owm.weather_at_id(1055433)
l = obs.get_location()
fc = owm.three_hours_forecast_at_coords(l.get_lat(), l.get_lon())

As you already use a city ID, your code can be further simplified this way by using the three_hours_forecast_at_id function Eg:

from pyowm import OWM
API_key = 'xxxxxxxxxxxxxxxxxxxxxxx'
owm = OWM(API_key,language='fr')
fc = owm.three_hours_forecast_at_id(1055433)

Take a look at the source code bits I've linked above for details

xla99 commented 4 years ago

Hi Claudio, That's great, I'll use the three_hours_forecast_at_id function, thank's for the help. Alex.