2017-02-14 55 views
3

ジオメトリを使用してデータファイルをシェープファイルとして保存すると、次のエラーが発生します。pythonでgeopandasを使用してデータファイルをシェープファイルに保存すると、boolのValueErrorが発生します

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 

このエラーの意味を知ることはできませんでした。

+0

どのバージョンのpython/geopandas/fionaをお使いですか?ジオメトリフィールドに空のデータがありますか? Python 3.4、geopandas 0.2.1、fiona 1.7.1のランダムフロートデータでエラーを再現できません。 –

+0

'df.info()'の出力を表示できますか?特定の列のサブセットのみを書き込むときに同じエラーが発生するかどうかを確認できますか? – joris

+0

@joris実際にはいくつかの列にnull値が存在しているので、私はcsvに保存できません。だから私は必要な列をnewdfを作成し、それは私の問題を解決しました。 – user99

答えて

3

TL; DR:DTYPE boolintへとリキャスト列。


このエラーは、設計により、ESRIシェープファイルは、「ブール」のデータ型が何であるかを知らないという事実から来ています。 It just knows what integers are instead。ほとんどの人は、単にデータ型を整数、つまりTrue - > 1とFalse - > 0に変更するだけです。

boolデータ型に割り当てられた列を調べるには、

タイプ boolの列 cでデータフレーム df考える
geopandas.io.file.infer_schema(df) 

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

、私がやるだろう:

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 

geopandas repo on Githubで説明したようにまた、この問題を見てみることができます。

関連する問題