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

Cannot get city info for any US city #376

Closed
project-owner opened this issue May 15, 2021 · 11 comments
Closed

Cannot get city info for any US city #376

project-owner opened this issue May 15, 2021 · 11 comments

Comments

@project-owner
Copy link

Hi, I'm trying to use the following function to get the city info:

list_of_locations = city_registry.locations_for("New York", country="US")

It returns the empty list.
The following function returns non-empty list:

list_of_locations = city_registry.locations_for("Paris", country="FR")

What could be the reason for the empty list in the first case? I tried many different US cities and all of them return empty list.
Thanks!

@csparpa
Copy link
Owner

csparpa commented May 16, 2021

Hello @project-owner please take a look here: #309 you basically should specify the 2 letter name of the US country for your city instead of using a generic 'US'. In your example, if you look for 'New York' belonging to the the New York state, you must use use country='NY' instead of 'US'. please tell me if this works ok! thanks

@project-owner
Copy link
Author

It works, thank you!
But how it will distinguish cities in California (CA) from the cities in Canada (CA)?
I've tried to search on OpenWeather site: https://openweathermap.org/find
and it works fine for query 'New York, US' and for 'New York, NY, US' but doesn't work for 'New York, NY'. Don't you use the same API for search?
Thanks!

@csparpa
Copy link
Owner

csparpa commented May 16, 2021 via email

@project-owner
Copy link
Author

project-owner commented May 16, 2021

There are some of them (https://en.wikipedia.org/wiki/List_of_U.S._places_named_after_non-U.S._places):
Ontario
Galt
Inglewood
probably more.
Also there are 50 states in US and some of them can coincide with another country codes. For example Delaware has the same code as Germany - DE.
Is there OpenWeather forum where I can ask this question?

Yahoo Weather is using both parameters: state + country. I'm not sure if it's possible somehow here.

Thanks!

@csparpa
Copy link
Owner

csparpa commented May 16, 2021

This is their support center URL: https://openweathermap.force.com/s/contactsupport

Anyway, just for you to know: when you call the locations_for function on the cityIDregistry object, pyOWM performs a local query against text files that were bundled from OpenWeatherMap at release time. If you take a look at those files, they tell "state" from "country" when country=US
That's the best we can do with the info OpenWeatherMap provides.

The mismatch between Californian and Canadian names could happen when you try to query e.g. the actual weather on them via pyOWM - which will invoke the remote OpenWeatherMap API. This is the case that needs to be investigated

Thanks, pls keep me posted

@project-owner
Copy link
Author

project-owner commented May 16, 2021

I'm not sure which file you use to look up the city info. I've had a look to this one:
city.list.json.gz
which I found here:
https://bulk.openweathermap.org/sample/

Here is the example for the city Ontario in Canada and California:

    {
        "id": 5379439,
        "name": "Ontario",
        "state": "CA",
        "country": "US",
        "coord": {
            "lon": -117.650887,
            "lat": 34.063339
        }
    }
	
    {
        "id": 6093943,
        "name": "Ontario",
        "state": "",
        "country": "CA",
        "coord": {
            "lon": -84.499832,
            "lat": 49.250141
        }
    }

It would be possible to avoid ambiguity if you would query first by country and then by state.
I use the one call API so I get longitude/latitude from the city info and provide in that API, so I don't call any other API with city/country info. It's only for lookup.

I believe the correct API would be:

city_registry.locations_for("Ontario", country="CA")
city_registry.locations_for("Ontario", country="US", state="CA")

where 'state' is optional.

Thanks!

@project-owner
Copy link
Author

project-owner commented May 16, 2021

I also cannot get the info for Washington:

city_registry.locations_for("Washington", country="DC")

returns empty list.

@csparpa
Copy link
Owner

csparpa commented May 19, 2021

Thanks @project-owner for notifying.

Yes, the API you propose is the way to go and would be a breaking change with respect to today's usage of the country field

I'd like to implement it - and maybe take the chance to innovate the way PyOWM stores city IDs internally (4 separate bz2 compressed files - here)

I was thinking about shipping only 1 sqlite db file which can be natively queried from Python, but that should need to come with an acceptable size - ideally less than 10 MBs

@project-owner
Copy link
Author

I'm migrating from the Yahoo Weather API which will reach end-of-life by June 1st. I hope to replace it by that time. If your new API won't be ready I will ask users to provide latitude/longitude instead of city name and country.

You could create new function for example either 'locations_for_2' or 'locations_for_city' to avoid the breaking change in the API.
Thanks!

@csparpa
Copy link
Owner

csparpa commented Sep 2, 2021

@project-owner Hello, just to let you know that the refactoring is in the works (783cb8b) and will be shipped on the next release!

@csparpa csparpa closed this as completed Sep 2, 2021
@project-owner
Copy link
Author

Good news. I hope it will be able to coexist with my app which is also using SQLite.

@csparpa csparpa mentioned this issue Feb 14, 2022
csparpa added a commit that referenced this issue Feb 14, 2022
# New features
[380](#380) Implemented [National Weather Alerts](https://openweathermap.org/api/one-call-api#listsource) support
[376](#376) Now PyOWM uses SQLite instead of files to internally store city data. `CityIDRegistry` interface has changed but in a retrocompatible way


# Chores
[381](#381) Now it is possible to specify how many times to retry an API call


# Bugfixes
[379](#379) Experimental fix for `404` errors on Agromonitor API satellite image search 
[387](#387) Fixed lat/lon swap bug on Airpollution API
[389](#389) Fixed wrong city name in City ID database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants