首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用地质公园和地形图绘制纽约地图上的位置

用地质公园和地形图绘制纽约地图上的位置
EN

Stack Overflow用户
提问于 2021-03-05 19:12:45
回答 1查看 2K关注 0票数 1

我有一个商店的位置文件,我试图在纽约地图上绘制。到目前为止,我一直使用以下两个链接作为我的指南,但我一直无法使代码工作。

代码语言:javascript
复制
import geopandas
import geoplot
import pandas as pd
from shapely.geometry import Point, Polygon

store_locDF=pd.read_csv('stores.csv') #Import file
cols_to_keep=['store_longitude','store_latitude']
store_locDF = store_locDF[store_locDF.columns.intersection(cols_to_keep)] #Keep only long and lat

#Convert long and lat to "points"
geometry = [Point(xy) for xy in zip( store_locDF["store_longitude"],store_locDF["store_latitude"])]
#Store geometry in dataframe
store_locDF['geometry']=geometry

#print(store_locDF.head())

boroughs = geopandas.read_file(geoplot.datasets.get_path('nyc_boroughs'))

ax=geoplot.polyplot(boroughs,edgecolor='white', facecolor='lightgray',figsize=(12, 8))

geoplot.polyplot(geometry, ax=ax) #<--Didnt work
#geoplt.pointplot(geometry, ax=ax) <--Also didnt work
''''


The error I am getting is: 
ValueError                                Traceback (most recent call last)
<ipython-input-12-3c7d64229a6c> in <module>
     19 ax=geoplot.polyplot(boroughs,edgecolor='white', facecolor='lightgray',figsize=(12, 8))
     20 
---> 21 geoplot.polyplot(geometry, ax=ax)

~\anaconda3\envs\IST 652\lib\site-packages\geoplot\geoplot.py in polyplot(df, projection, extent, figsize, ax, **kwargs)
    917             return ax
    918 
--> 919     plot = PolyPlot(df, figsize=figsize, ax=ax, extent=extent, projection=projection, **kwargs)
    920     return plot.draw()
    921 

~\anaconda3\envs\IST 652\lib\site-packages\geoplot\geoplot.py in __init__(self, df, **kwargs)
    881     class PolyPlot(Plot):
    882         def __init__(self, df, **kwargs):
--> 883             super().__init__(df, **kwargs)
    884 
    885         def draw(self):

~\anaconda3\envs\IST 652\lib\site-packages\geoplot\geoplot.py in __init__(self, df, **kwargs)
    614             # self when it is asked for its geometry property, and so it will never be the source
    615             # of this error.
--> 616             raise ValueError(
    617                 'The input GeoDataFrame does not have a "geometry" column set.'
    618             )

ValueError: The input GeoDataFrame does not have a "geometry" column set.


Any help is appreciated
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-07 19:51:39

正如评论者保罗·H所说的那样,问题是我提供的是一份清单,而不是地理数据。以下是工作守则:

代码语言:javascript
复制
import geopandas
import geoplot
import pandas as pd
from shapely.geometry import Point, Polygon
import matplotlib.pyplot as plt

store_locDF=pd.read_csv('stores.csv')
cols_to_keep=['store_longitude','store_latitude']

crs={'init':'epsg:4326'}

boroughs = geopandas.read_file(geoplot.datasets.get_path('nyc_boroughs'))

geo_df=geopandas.GeoDataFrame(store_locDF,crs=crs,geometry=geopandas.points_from_xy(store_locDF["store_longitude"], store_locDF["store_latitude"]))



fig,ax=plt.subplots(figsize=(15,15))
boroughs.plot(ax=ax,alpha=0.4,color="grey")
geo_df[geo_df['active_yn']=="Y"].plot(ax=ax,markersize=200, alpha=0.4,color="green", label="Active Store")
geo_df[geo_df['active_yn']=="N"].plot(ax=ax,markersize=200, alpha=0.4,color="red", label="Inactive Stores")
plt.legend()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66498316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档