尝试使用国家/地区信息从坐标获取子区域

问题描述 投票:0回答:1

对……一切都很陌生。我正在尝试使用几何图形在 getcountry 中使用 latlng 查找子区域。也许点/坐标/几何形状与 latlng 的格式不同?

# getting data
df = pd.read_csv("earthquakes.csv")

# creating coordinates (POINTS)
df_geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]
gdf = gpd.GeoDataFrame(df, geometry = df_geometry)
gdf.head()

# setting sns default theme
sns.set_theme()

# defining subregion
def subregion(value):
    for _, country_info in CountryInfo().all().items():
        if country_info.__contains__("latlng") and country_info.__getitem__("latlng") == value: 
            return country_info.__getitem__("subregion")

# adding the subregion to gdf 
gdf["subregion"] = gdf["geometry"].apply(subregion)

# creating a scatter graph of longitude / magnitude
# colour coded to show continent
sns.jointplot(data=gdf, x="latitude", y="mag", hue="subregion")
geometry coordinates latitude-longitude geopandas
1个回答
0
投票

由于您已经在使用 Geopandas,您可以使用一些具有 subregion 属性的空间数据集,并将其

sjoin()
(空间连接)到您的位置。

使用

.latlng()
,您只会在该国某处获得一个点,恐怕这对于将您自己的位置与国家/次区域进行匹配不会太有用。

import pandas as pd
import geopandas as gpd
import seaborn as sns

# Natural Earth 10m dataset, includes SUBREGION
# https://www.naturalearthdata.com/downloads/10m-cultural-vectors/
ne_gdf = gpd.read_file("https://naciscdn.org/naturalearth/10m/cultural/ne_10m_admin_0_countries.zip")

# csv feed, https://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php
df = pd.read_csv("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.csv")

# create Geopandas frame, spatial join to add SUBREGION from ne_gdf
gdf = (
    gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude), crs="WGS84")
    .sjoin(ne_gdf.loc[:, ["SUBREGION", "geometry"]], how="left")
    # fill NA values (outside of coutry polygons) to plot those as well
    # .fillna({"SUBREGION" : "N/A"})
)

g = sns.jointplot(data=gdf, x="latitude", y="mag", hue="SUBREGION")
sns.move_legend(g.figure.axes[0],"upper left", bbox_to_anchor=(1.25, 1))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.