3

当我将数据框保存为带有几何形状的 shapefile 时,出现以下错误。

geometry  = [Point(xy) for xy in zip(df.longitude, df.latitude)]
dfout = geopandas.GeoDataFrame(df,  geometry=geometry)    
dfout.to_file(outputpath, driver='ESRI Shapefile')

Traceback (most recent call last):
  File "test.py", line 230, in <module>
    main()
  File "test.py", line 223, in main
    createSHP(df,outputpath)
  File "test.py", line 150, in createSHP
    dfout.to_file(outputpath, driver='ESRI Shapefile')
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/geopandas/geodataframe.py", line 343, in to_file
    to_file(self, filename, driver, schema, **kwargs)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/geopandas/io/file.py", line 61, in to_file
    schema=schema, **kwargs) as c:
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/fiona/__init__.py", line 178, in open
    enabled_drivers=enabled_drivers, crs_wkt=crs_wkt)
  File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/fiona/collection.py", line 155, in __init__
    self.session.start(self, **kwargs)
  File "fiona/ogrext.pyx", line 961, in fiona.ogrext.WritingSession.start (fiona/ogrext.c:16015)
ValueError: 'bool' is not in list

我无法找出这个错误的含义。

4

1 回答 1

3

TL;DR:将具有 dtype 的列重铸boolint.


这个错误来自这样一个事实,即 ESRI Shapefile 不知道什么是“布尔”数据类型。它只知道整数是什么。大多数人最终所做的只是将数据类型更改回整数,即 True -> 1 和 False -> 0。

要找出已为哪些列分配了bool数据类型,请使用:

geopandas.io.file.infer_schema(df)

>>> {'geometry': 'Point',
     'properties': OrderedDict([('integer', 'int'),
                                ('c', 'bool')])

给定具有typedf列的数据框,我会这样做:cbool

df['c'] = df['c'].astype('int')

我们可以编写一个简单的函数来为我们处理所有这些:

def gdf_bool_to_int(gdf):
    """For a given GeoDataFrame, returns a copy that
    recasts all `bool`-type columns as `int`.

    GeoDataFrame -> GeoDataFrame"""
    df = gdf.copy()
    coltypes = gpd.io.file.infer_schema(df)['properties']
    for colname, coltype in coltypes.items():
        if coltype == 'bool':
            df[colname] = df[colname].astype('int')
    return df

你也可以看看这个问题,正如Github 上的 geopandas repo中所讨论的那样。

于 2017-11-01T17:42:15.657 回答