【问题标题】:How to read firestore GeoPoint in python script?如何在 python 脚本中读取 Firestore GeoPoint?
【发布时间】:2020-05-07 09:26:29
【问题描述】:

我正在编写一个 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 位置?

【问题讨论】:

  • Geopoint 具有纬度和经度参数。我猜像u'latitude': self.location.latitude 这样的东西应该可以工作。
  • @EmilGi 感谢您的评论。你能把你的建议写成“城市班”吗?您可以在我分享的示例中找到“城市类”。

标签: python firebase google-cloud-firestore geopoints


【解决方案1】:

假设 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2019-05-16
    • 1970-01-01
    相关资源
    最近更新 更多