Google Geo Type API Client Library
The `google-geo-type` library provides Python protobuf definitions for common geographic types used across Google APIs, such as LatLng, Viewport, and PostalAddress. It is part of the larger `google-cloud-python` monorepo. Currently at version 0.6.0, it follows the release cadence of the Google Cloud client libraries, with updates typically bundled with other related packages.
Common errors
-
ModuleNotFoundError: No module named 'google.geo.type.latlng'
cause Incorrect import path. The protobuf definitions are found in modules ending with `_pb2`.fixChange the import statement to include `_pb2` at the end: `from google.geo.type import latlng_pb2`. -
ERROR: Package 'google-geo-type' requires a different Python; <your_version> not in '>=3.9'
cause Attempting to install or run the library with a Python version older than 3.9.fixUpgrade your Python environment to version 3.9 or newer. Check your Python version with `python --version`. -
AttributeError: 'module' object has no attribute 'LatLng'
cause Attempting to access a protobuf message directly from the `google.geo.type` module, or using the wrong casing/name.fixEnsure you are importing the specific protobuf definition module (e.g., `latlng_pb2`) and accessing the message class (e.g., `LatLng`) from *that* module: `from google.geo.type import latlng_pb2; point = latlng_pb2.LatLng(...)`.
Warnings
- gotcha As a pre-GA (versions < 1.0.0) library, protobuf definitions within `google-geo-type` may introduce backward-incompatible changes without a major version bump. Always review release notes when upgrading.
- breaking The library explicitly requires Python 3.9 or newer. Installing or running on older Python versions will result in an `ERROR: Package 'google-geo-type' requires a different Python; <your_version> not in '>=3.9'` during installation or runtime errors.
- gotcha This library provides only protobuf *definitions* (like `LatLng`, `Viewport`). It does not include client methods for interacting with specific Google Geo APIs (e.g., Geocoding API, Places API). For those, you'll need the respective Google Cloud client library.
Install
-
pip install google-geo-type
Imports
- latlng_pb2
from google.geo.type.v1 import latlng_pb2
from google.geo.type import latlng_pb2
- viewport_pb2
from google.geo.type import viewport_pb2
- postal_address_pb2
from google.geo.type import postal_address_pb2
Quickstart
from google.geo.type import latlng_pb2
from google.geo.type import viewport_pb2
# Create a LatLng object
latitude = 34.052235
longitude = -118.243683
point = latlng_pb2.LatLng(latitude=latitude, longitude=longitude)
print(f"Point: ({point.latitude}, {point.longitude})")
# Create a Viewport object
viewport = viewport_pb2.Viewport(
low=latlng_pb2.LatLng(latitude=33.9, longitude=-118.3),
high=latlng_pb2.LatLng(latitude=34.1, longitude=-118.1)
)
print(f"Viewport Low: ({viewport.low.latitude}, {viewport.low.longitude})")
print(f"Viewport High: ({viewport.high.latitude}, {viewport.high.longitude})")