python - 如何在 python 脚本中读取 Firestore GeoPoint?
问题描述
我正在编写一个 Python 脚本来读取 Firebase Firestore 数据。不知何故 documentSnapshop.to_dict() 对我不起作用。所以,我写了一个类,参考https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/firestore/cloud-client/snippets.py#L103这个资源。
就我而言,城市的位置存储为 GeoPoint firestore 字段。我无法在字典中找到它。我们如何在上面提到的参考示例中提到的 City 类中添加 GeoPoint 位置?
解决方案
假设 GeoLocation 属性名为location
,您应该像这样添加它:
class City(object):
def __init__(self, name, state, country, capital=False, population=0,
regions=[], location):
self.name = name
self.state = state
self.country = country
self.capital = capital
self.population = population
self.regions = regions
self.location = location
然后,当从数据库中获取数据时,您可以location
像这样设置属性:
source = db.collection(u'collection').document(u'docId').get().to_dict()
city = City(source[u'name'], source[u'state'], source[u'country'], source[u'country'], source[u'location'])
如果要获取纬度或经度,可以使用以下代码进行:
city.location.latitude
city.location.longitude
推荐阅读
- javascript - 如何使用 CSS 或 Javascript 创建一个看起来无限的选取框
- python - str.endswith(suffix[, start, end]) 的机制是什么?
- xml - 如何在 XML 文件中提取 connection.username 并替换
- java - 如何在 Eclipse 中打开和运行 .java 文件?
- javascript - 如何在 chromium 开发者控制台中使用 console.log、console.warn 等?
- r - 在R中找到函数的最大值
- ios - 音频播放器包导入后无法运行颤振
- r - 如何编写 R 代码来删除重复行,其中一个观察值是重复项的负值?
- c++ - 替换 C++ 字符串中的标点符号
- r - int_abline 中的错误....plot.new 尚未调用,如何放置最佳拟合线?