directly compute a geohash with django without creating a model

ghz 8months ago ⋅ 100 views

is it possible to use this GeoHash class directly without creating a model on a db?

e.g.:

from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import GeoHash
p = Point(x=lon, y=lat, srid=4326)
geohash_str = GeoHash(p).???   # i want the actual geohash string here

Answers

Yes, it's possible to use the GeoHash class directly without creating a model in the database. You can create a Point object using the Point class from django.contrib.gis.geos and then use the GeoHash function to generate the GeoHash string directly from the Point object.

Here's how you can do it:

from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import GeoHash

# Create a Point object with the longitude and latitude
p = Point(x=lon, y=lat, srid=4326)

# Use the GeoHash function to generate the GeoHash string
geohash_str = GeoHash(p)

This will give you the GeoHash string for the given longitude and latitude coordinates stored in the p Point object. You can then use this GeoHash string as needed in your application.