繁体   English   中英

类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 GeojsonFile

[英]TypeError: expected str, bytes or os.PathLike object, not GeojsonFile

数据文件 = 打开(文件名,“r”); 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 GeojsonFile

有人可以帮助我为什么会发生这种情况吗? 我想以 geojson 格式读取空间数据。

我想将 geojson 数据存储到数据库中。 我已经连接了 MongoDB 数据库。 我使用 gridfs 来克服限制,但首先我需要读取数据并存储到数据库中。

非常感谢

    #read in the spatial data
filename = "/dami_data/data1/countries.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open(filename, "r")
thedata = datafile.read()

#create a gridFS object 
filesystem = gridfs.GridFS(db, collection='data')

#write data to GridFS
myfile = filesystem.put(thedata, filename = "countries")
print ("Store to database :)")

#retrieve what was just stored/document
filesystem.get(myfile).read()

在您的代码中, filename指的是 GeojsonFile 对象而不是文件名。

如果您要读取的原始文件与 pygeoj 读取的文件相同,那么您应该执行以下操作:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open(filename, "r")
thedata = datafile.read()
datafile.close()

尽管您可以做到,但您应该考虑使用geojfile而不是再次读取文件。 无论如何,它已经包含您需要从该文件中获取的信息(正如@Karl Knechtel在评论中指出的那样)。

如果没有,意味着你想读取不同的文件,你应该传递文件名:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open("my_other_file_name", "r")
thedata = datafile.read()
datafile.close()

最后一种情况,如果您想获取该 GeojsonFile 的原始数据,请执行以下操作:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
thedata = str(geojfile)

暂无
暂无

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

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