简体   繁体   English

在python中使用geopandas将数据帧保存到shapefile会引发bool的ValueError

[英]Saving dataframe to shapefile using geopandas in python raises ValueError for bool

When I am saving the dataframe as a shapefile with geometry I am getting the following error. 当我将数据帧保存为具有几何的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

I havn't been able to find out what this error means. 我无法找出这个错误的含义。

TL;DR : Recast columns with dtype bool to int . TL; DR :重新生成dtype boolint列。


This error comes from the fact that by design, the ESRI Shapefile doesn't know what a "boolean" data type is. 这个错误来自这样一个事实,即在设计上,ESRI Shapefile不知道“布尔”数据类型是什么。 It just knows what integers are instead . 它只知道整数是什么 What most people end up doing is simply to change the datatype back to integer, ie True -> 1 and False -> 0. 大多数人最终做的只是将数据类型更改回整数,即True - > 1和False - > 0。

To find out what column(s) have been assigned a bool datatype, go with: 要找出已为bool数据类型指定了哪些列,请执行以下操作:

geopandas.io.file.infer_schema(df)

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

Given dataframe df with a column c of type bool , I'd do: 给定具有bool类型的列c的数据帧df ,我会这样做:

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

We can write a simple function that takes care of all this for us: 我们可以编写一个简单的函数来为我们处理所有这些:

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

You can also take a look at this issue, as discussed in the geopandas repo on Github . 您还可以查看此问题,如Github上geopandas repo中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM