为什么Geolocate不给我正确的地址?

发布于 2025-02-09 13:34:37 字数 676 浏览 0 评论 0原文

因此,我正在分析宾夕法尼亚州费城的地址的数据集。现在,为了使用这些,我想获得确切的经度和纬度,以便以后在地图上显示它们。

我已经获得了列的独特条目作为清单,并实施了一个循环,以使我具有经度和纬度,尽管这给了我每个城市,有时甚至是费城以外的城市的坐标。

这是我到目前为止所做的:

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="my_user_agent")
geocode = lambda query: geolocator.geocode("%s, Philadelphia PA" % query)

cities = list(philly["station_name"].unique())
for city in cities:
    address = city
    location = geolocator.geocode(address)
    if(location != None):
        philly["longitude"] = location.longitude
        philly["latitude"] = location.latitude

philly["coordinates"] = list(zip(philly["latitude"], philly["longitude"]))

So I was analyzing a data set with addresses in Philadelphia, PA. Now, in order to make use of these, I wanted to get the exact longitude and latitude to later show them on a map.

I have gotten the unique entries of the column as a list and have implemented a loop to get me the longitude and latitude, though it's giving me the same coordinates for every city and sometimes even ones that are outside of Philadelphia.

Here's what I did so far:

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="my_user_agent")
geocode = lambda query: geolocator.geocode("%s, Philadelphia PA" % query)

cities = list(philly["station_name"].unique())
for city in cities:
    address = city
    location = geolocator.geocode(address)
    if(location != None):
        philly["longitude"] = location.longitude
        philly["latitude"] = location.latitude

philly["coordinates"] = list(zip(philly["latitude"], philly["longitude"]))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-02-16 13:34:38

如果“ Philly”是字典对象列表,则可以在列表上迭代并将位置属性添加到每个记录中。

from geopy.geocoders import Nominatim

philly = [{'station_name': '30th Street Station'}]

geolocator = Nominatim(user_agent="my_user_agent")
for row in philly:
    address = row["station_name"]
    location = geolocator.geocode(f"{address}, Philadelphia, PA", country_codes="us")
    if location:
        print(address)
        print(">>", location.longitude, location.latitude)
        row["longitude"] = location.longitude
        row["latitude"] = location.latitude
        row["coordinates"] = (location.longitude, location.latitude)
print(philly)

输出:

30th Street Station
>> -75.1821442 39.9552836
[{'station_name': '30th Street Station', 'longitude': -75.1821442, 'latitude': 39.9552836, 'coordinates': (-75.1821442, 39.9552836)}]

如果使用Pandas DataFrame,则可以在数据框架中的每个记录上迭代,然后在其中设置纬度,经度和协调字段。

您可以执行这样的操作:

from geopy.geocoders import Nominatim
import pandas as pd

geolocator = Nominatim(user_agent="my_user_agent")

philly = [{'station_name': '30th Street Station'}]
df = pd.DataFrame(philly)

# add empty location columns to data frame
df["latitude"] = ""
df["longitude"] = ""
df["coordinates"] = ""

for _, row in df.iterrows():
    address = row.station_name
    location = geolocator.geocode(f"{address}, Philadelphia, PA", country_codes="us")
    if location:
        row["latitude"] = location.latitude
        row["longitude"] = location.longitude
        row["coordinates"] = (location.longitude, location.latitude)
print(df)

输出:

          station_name   latitude  longitude                coordinates
0  30th Street Station  39.955284 -75.182144  (-75.1821442, 39.9552836)

如果您有一个具有重复站名称的列表,则应缓存结果,以免重复地进行地理位置请求。

If "philly" is a list of dictionary objects then you can iterate over the list and add the location properties to each record.

from geopy.geocoders import Nominatim

philly = [{'station_name': '30th Street Station'}]

geolocator = Nominatim(user_agent="my_user_agent")
for row in philly:
    address = row["station_name"]
    location = geolocator.geocode(f"{address}, Philadelphia, PA", country_codes="us")
    if location:
        print(address)
        print(">>", location.longitude, location.latitude)
        row["longitude"] = location.longitude
        row["latitude"] = location.latitude
        row["coordinates"] = (location.longitude, location.latitude)
print(philly)

Output:

30th Street Station
>> -75.1821442 39.9552836
[{'station_name': '30th Street Station', 'longitude': -75.1821442, 'latitude': 39.9552836, 'coordinates': (-75.1821442, 39.9552836)}]

If working with a Pandas dataframe then you can iterate over each record in the dataframe then set the latitude, longitude and coordinates fields in it.

You can do something like this:

from geopy.geocoders import Nominatim
import pandas as pd

geolocator = Nominatim(user_agent="my_user_agent")

philly = [{'station_name': '30th Street Station'}]
df = pd.DataFrame(philly)

# add empty location columns to data frame
df["latitude"] = ""
df["longitude"] = ""
df["coordinates"] = ""

for _, row in df.iterrows():
    address = row.station_name
    location = geolocator.geocode(f"{address}, Philadelphia, PA", country_codes="us")
    if location:
        row["latitude"] = location.latitude
        row["longitude"] = location.longitude
        row["coordinates"] = (location.longitude, location.latitude)
print(df)

Output:

          station_name   latitude  longitude                coordinates
0  30th Street Station  39.955284 -75.182144  (-75.1821442, 39.9552836)

If you have a list with duplicate station names then you should cache the results so you don't make duplicate geolocation requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文