所以,我有一个包含 12.5 亿个特征的地理包。该文件实际上不包含几何图形,只有一个属性“id”,它是一个唯一的 id。有很多重复项,我想删除重复的“id”并只保留唯一值。由于存在大量数据(geopackage 包含 19 GB),我选择了切片。我尝试了多处理,但这没有用,而且会出现问题,因为我必须跟踪唯一的“id”,而多处理不允许这样做(至少据我所知)。
是)我有的:
import fiona
import geopandas as gpd
import pandas as pd
# import numpy as np
slice_count = 200
start = 0
end = slice_count
fname = "path/Output.gpkg"
file_gpd = gpd.read_file(fname, rows=slice(start, end))
chunk = pd.DataFrame(file_gpd)
chunks = pd.DataFrame()
only_ids = pd.DataFrame(columns=['id'])
loop = True
while loop:
try:
# Dropping duplicates in current dataset
chunk = chunk.drop_duplicates(subset=['id'])
# Extract only unique IDS from chunk variable to save memory
only_ids_in_chunk = pd.DataFrame()
only_ids_in_chunk['id'] = chunk['id']
only_ids = only_ids.append(only_ids_in_chunk)
only_ids = only_ids.drop_duplicates(subset=['id'])
# If we want to make another file which have all values unique
# we must store somewhere what we have in chunk variable, to be able to load new chunk
# Because we must not have all chunks in memory at the same time
del chunk
# Load next chunk
start += slice_count
end += slice_count
file_gpd = gpd.read_file(fname, rows=slice(start, end))
chunk = pd.DataFrame(file_gpd)
if len(chunk) == 0:
print(len(only_ids))
loop = False
else:
pass
except Exception:
loop = False
print("Iteration is stopped")
我得到一个无限循环。我认为使用 if 语句会发现块的长度何时等于 0 或切片何时结束。