无法剪辑之前从不同形状文件剪辑过的 GeoDataFrame

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

我有一个使用 geopandas 读取的 shapefile(~500 mb)。我通过国家的边界形状文件来剪辑它(这是没有问题的)。接下来,我从结果中删除一些无用的列,并根据我想要的时间段选择一些数据。但是,当我尝试再次使用状态边界(与地理数据帧相同的 crs)剪辑文件时,它给出了错误:

TypeError: 'gdf' should be GeoDataFrame or GeoSeries, got <class 'geopandas.geodataframe.GeoDataFrame'>

clip 的输入是一个 GeoDataFrame,但它没有被裁剪。

type(fire_file2024_proc2) Out: geopandas.geodataframe.GeoDataFrame

我以前使用过这些形状文件,所以这不是问题。我提供了一小部分数据样本(从国家/地区范围内截取)。

整个代码有点像这样:

# reading big, raw data
file1_raw = gpd.read_file("/home/ubuntu/file_raw.shp")
# reading outer shp
ind_shp = gpd.read_file("/home/ubuntu/ind_bdy.shp")

# first clip with country's extent
file1_proc1 = gpd.clip(file1_raw,ind_shp)
file1_proc2 = file_proc1.drop(['some unwanted columns'],axis=1)

# reading another big raw file
file2_raw = gpd.read_file("/home/ubuntu/file_raw2.shp")
# same process as above
# ------ #
# concat both files
file_proc2 = pd.concat([file1_proc2,file2_proc2])

# subset based on year
mask = (file_proc2['DATE_IST']>='2014-01-01) & (file_proc2['DATE_IST']<'2015-01-01')
file_proc2_2014 = file_proc2.loc(mask).reset_index(drop=True)

# reading states shapefile
ind_st = gpd.read_file("/home/ubuntu/ind_st.shp")

state_names = ind_st['Name'].to_list()
clipped_dfs_holder = []

# clipping all states
for i in range(len(state_names)):
    current_state = ind_st[ind_st['Name']==state_names[i]]
    df_st = gpd.clip(file_proc2_2014,current_state)
    clipped_dfs_holder.append(df_st)
    print("Clip complete for: "+state_names[i])

代码在第

df_st = gpd.clip(file_proc2_2014,current_state)
行失败。谢谢。

python geopandas shapefile
1个回答
0
投票

不确定在哪里可以找到您的数据样本文件?

无论如何,如果测试数据仅包含在测试脚本中会更容易,所以我尝试用一些内联测试数据替换您的读取命令,但我无法重现您的错误。

测试脚本:

import geopandas as gpd
import pandas as pd
from shapely import box

# reading big, raw data
file1_raw = gpd.GeoDataFrame(
    data={"DATE_IST": [1, 2]}, geometry=[box(0, 0, 10, 10), box(50, 0, 60, 10)]
)
# reading outer shp
ind_shp = gpd.GeoDataFrame(geometry=[box(0, 0, 55, 55)])

# clip with country's extent
file1_proc1 = gpd.clip(file1_raw, ind_shp)
file1_proc2 = file1_proc1  # .drop(["some unwanted columns"], axis=1)

# reading another big raw file
file2_raw = gpd.GeoDataFrame(
    data={"DATE_IST": [1, 2]}, geometry=[box(0, 20, 10, 30), box(50, 50, 60, 60)]
)
# clip with country's extent
file2_proc1 = gpd.clip(file2_raw, ind_shp)
file2_proc2 = file2_proc1  # .drop(["some unwanted columns"], axis=1)
# concat both files
file_proc2 = pd.concat([file1_proc2, file2_proc2])

# subset based on year
mask = (file_proc2["DATE_IST"] > 0)
file_proc2_2014 = file_proc2.loc[mask].reset_index(drop=True)

# reading states shapefile
ind_st = gpd.GeoDataFrame(
    data={"Name": ["John", "Doe"]}, geometry=[box(0, 0, 2, 2), box(2, 2, 4, 4)]
)

state_names = ind_st["Name"].to_list()
clipped_dfs_holder = []

# clipping all states
for i in range(len(state_names)):
    current_state = ind_st[ind_st["Name"] == state_names[i]]
    df_st = gpd.clip(file_proc2_2014, current_state)
    clipped_dfs_holder.append(df_st)
    print("Clip complete for: " + state_names[i])
    print(df_st)
© www.soinside.com 2019 - 2024. All rights reserved.