-1

I am having difficulty retrieving the latitude and longitude from a Firestore Geopoint object in Python. I've tried multiple ways but none of them seem to work. Any suggestions? The geopoint object is at alertLocation in the dict.

Code:

docs = db.collection(u'users').where(u'first', u'==', u'MyFirstName').stream()

for doc in docs:
    logger.log_text(f'{doc.id} => {doc.to_dict()}')

Output:

HHA5pGRN1rBtGtHRgDS7 => {'alertLocation': <google.cloud.firestore_v1._helpers.GeoPoint object at 0x3e1877599e50>, 'first': 'MyFirstName', 'email': 'me@email.com', 'last': 'mylastname'}
Filip
  • 898
  • 1
  • 8
  • 24
Jeff
  • 21
  • 5
  • Do you know how to retrieve the object from the dictionary? – AMC Aug 26 '20 at 23:48
  • This works, but it doesn't show any part of the object. `code` list_alertLocation = doc.to_dict()["alertLocation"] – Jeff Aug 26 '20 at 23:50
  • 2
    Using doc.to_dict() returns a dictionary of the doc; what you want is access to the GeoPoint object (i.e., you want doc.to_dict()['alertLocation'] ). The latitude and longitude are stored on the GeoPoint object as properties, which are accessed via dot-notation. Replacing your format string with `f'{doc.id} => {doc.to_dict()['alertLocation'].latitude}'` will give you the latitude, for example. The default GeoPoint object representation only gives you the object id (in memory), rather than a pretty representation showing the latitude and longitude. – KDN Aug 26 '20 at 23:59
  • That sounds like an answer @KDN :) – Frank van Puffelen Aug 27 '20 at 01:20

2 Answers2

1

Using doc.to_dict() returns a dictionary of the doc; what you want is access to the GeoPoint object (i.e., you want doc.to_dict()['alertLocation'] ). The latitude and longitude are stored on the GeoPoint object as properties, which are accessed via dot-notation. Replacing your format string with f'{doc.id} => {doc.to_dict()['alertLocation'].latitude}' will give you the latitude, for example. The default GeoPoint object representation only gives you the object id (in memory), rather than a pretty representation showing the latitude and longitude.

KDN
  • 1,349
  • 2
  • 14
  • 17
0

Apparently, my problem was with the logger function I was trying to use and less so with accessing the property in the geopoint object. I needed to convert the latitude into a string for the log_text method to work correctly.

logger.log_text(str(doc.to_dict()["alertLocation"].latitude))
Jeff
  • 21
  • 5
  • 1
    While the use of `str` may improve consistency of behavior in some cases, it shouldn't be necessary here. The output from your example is printing the object representation (from the object's `__repr__` method), and since the latitude and longitudes are just `float`s, they will be automatically converted to suitable values of strings. Calling `str()` on these objects ensures that the `__str__` method is called instead of `__repr__`. However, the behaviors are the same for floats. – KDN Aug 27 '20 at 20:01
  • 1
    If you want more control over the numerical representation, you could use `f'{doc.id} => {doc.to_dict()['alertLocation'].latitude: 6.2f}`, for example, where the `: +6.2f` indicates a float with 2 decimal places, 6 total spaces wide, padded with spaces, and with a leading + or - sign. For details, see https://docs.python.org/3.8/library/string.html – KDN Aug 27 '20 at 20:01