繁体   English   中英

从不同目录导入文件后,Geopandas 不工作

[英]Geopandas not working after importing a file from a different directory

我正在尝试使用从bbike.org下载的 shapefile 在 python 中制作地图。 这是我的代码:

import geopandas as gpd
import os
import sys
import matplotlib.pyplot as plt

bos_files_list = ['buildings.shx', 'landuse.shx', 'natural.shx', 'places.shx', 'points.shx', 'railways.shx', 'roads.shx']
cur_path = os.path.dirname(__file__)

def maps_of_bos(files):
    for x in range(len(files)):
        os.chdir(f'location/of/file')
        f = open(f'{files[x]}', 'r')
        gpd.read_file(f)


z = maps_of_bos(bos_files_list)

z.plot()

plt.show()

但是,我的错误输出如下:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    z = maps_of_bos(bos_files_list)
  File "test.py", line 13, in maps_of_bos
    gpd.read_file(f)

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/geopandas/io/f
ile.py", line 76, in read_file
    with reader(path_or_bytes, **kwargs) as features:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 113, in
 __enter__
    return next(self.gen)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fiona/__init__
.py", line 206, in fp_reader
    dataset = memfile.open()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fiona/io.py",
line 63, in open
    return Collection(vsi_path, 'w', crs=crs, driver=driver,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/fiona/collecti
on.py", line 126, in __init__
    raise DriverError("no driver")
fiona.errors.DriverError: no driver

我对 python 比较陌生,并不真正理解我的错误。 有人可以帮帮我吗?

根据文档read_file应该采用文件的路径而不是对象。

gpd.read_file(f'{files[x]}')

你不需要

f = open(f'{files[x]}', 'r')

暂无
暂无

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

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