我正在使用下面的代码来查找数百万个GPS点的国家(有时是州)。该代码当前每点大约需要一秒钟,这非常慢。shapefile为6 MB。
我读到geopandas使用rtree进行空间连接,这使它们效率极高,但这在这里似乎不起作用。我究竟做错了什么?我希望每秒能获得一千个积分。
可以在此处(5MB)下载shapefile和csv:https ://www.dropbox.com/s/gdkxtpqupj0sidm/SpatialJoin.zip ? dl =0
import pandas as pd
import geopandas as gpd
from geopandas import GeoDataFrame, read_file
from geopandas.tools import sjoin
from shapely.geometry import Point, mapping,shape
import time
#parameters
shapefile="K:/.../Shapefiles/Used/World.shp"
df=pd.read_csv("K:/.../output2.csv",index_col=None,nrows=20)# Limit to 20 rows for testing
if __name__=="__main__":
start=time.time()
df['geometry'] = df.apply(lambda z: Point(z.Longitude, z.Latitude), axis=1)
PointsGeodataframe = gpd.GeoDataFrame(df)
PolygonsGeodataframe = gpd.GeoDataFrame.from_file(shapefile)
PointsGeodataframe.crs = PolygonsGeodataframe.crs
print time.time()-start
merged=sjoin(PointsGeodataframe, PolygonsGeodataframe, how='left')
print time.time()-start
merged.to_csv("K:/01. Personal/04. Models/10. Location/output.csv",index=None)
print time.time()-start