import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
# 读取CSV文件(文件A)并逐块处理
csv_file = "file_a.csv"
chunk_size = 10000 # 调整适当的块大小
result_gdf = gpd.GeoDataFrame(columns=["geometry"])
for chunk_a in pd.read_csv(csv_file, chunksize=chunk_size):
# 读取GeoJSON文件(文件B)并逐块处理
geojson_file = "file_b.geojson"
gdf_b = gpd.read_file(geojson_file, chunksize=chunk_size)
for index_a, row_a in chunk_a.iterrows():
# 提取CSV文件中的经纬度信息
lon, lat = row_a['longitude'], row_a['latitude']
# 创建点对象
point = Point(lon, lat)
# 遍历GeoJSON文件中的每个块
for chunk_b in gdf_b:
# 找到GeoJSON文件中附近的点
nearby_points = chunk_b[chunk_b.geometry.distance(point) < 0.1] # 调整距离阈值
# 将找到的附近点添加到结果GeoDataFrame中
result_gdf = pd.concat([result_gdf, nearby_points])
# 将结果保存为新的GeoJSON文件
result_gdf.to_file("result.geojson", driver="GeoJSON")
print("处理完成,结果保存在 result.geojson")