Gryd.geodesy

base32

base32(secret)

Return a 32-length of unique bytes from secret hash.

Geodesic Objects

class Geodesic(ctypes.Structure)

ctypes structure for geodesic coordinates

Attributes:

  • longitude float - longitude value of geodesic coordinates in degrees
  • latitude float - latitude value of geodesic coordinates in degrees
  • altitude float - elevation of the geodesic coordinates in meters
>>> dublin = Gryd.Geodesic(-6.272877, 53.344606, 105.)
>>> dublin
<lon=-006°16'22.357" lat=+053°20'40.582" alt=105.000>

encrypt

 | encrypt(digit, secret)

Encrypt geodesic coordinates. It uses geohash with a custom 32-length base initialized by Gryd.geodesy.base32.

>>> g = Gryd.geodesy.Geodesic(-5.412300, 45.632100)
>>> g.encrypt(23, "secret")
b'\xbda\xe0\xa3\xe9\xbd\x1d\x86\xe0_a1\x8bV2\xe0aV\xbd2\xcd\xe0\xe0'

Arguments:

  • digit int - result bytes-length
  • secret bytes or str - secret used to encrypt geodesic coordinates

Returns:

bytes data

decrypt

 | @staticmethod
 | decrypt(encrypted, secret)

Decrypt geodesic from encrypted. It uses geohash with a custem 32-length base initialized by Gryd.geodesy.base32.

>>> enc = b'\xbda\xe0\xa3\xe9\xbd\x1d\x86\xe0_a1\x8bV2\xe0aV\xbd2\xcd'\
...       b'\xe0\xe0'
>>> geo.Geodesic.decrypt(enc, "secret")
<lon=-5.412300 lat=45.632100 alt=0.000>

Arguments:

  • encrypted bytes - encrypted geodesic coordinates
  • secret bytes or str - secret used to encrypt geodesic coordinates

Returns:

Gryd.geodesy.Geodesic coordinates

geohash

 | geohash(digit=10)

Convert coordinates to geohash.

>>> dublin.geohash() # by default on 10 digit for metric precision
'gc7x3r04z7'
>>> dublin.geohash(14) # why not on 14 digit for millimetric precision
'gc7x3r04z77csw'

Arguments:

  • digit int - total digit to use in the geohash

Returns:

Geohash str

from_geohash

 | @staticmethod
 | from_geohash(geoh)

Return Geodesic object geohash.

>>> Gryd.Geodesic.from_geohash('gc7x3r04z7')
<lon=-006°16'22.347" lat=+053°20'40.590" alt=0.000>
>>> Gryd.Geodesic.from_geohash('gc7x3r04z77csw')
<lon=-006°16'22.357" lat=+053°20'40.582" alt=0.000>

Arguments:

  • geoh str - georef string

Returns:

Gryd.geodesy.Geodesic coordinates

maidenhead

 | maidenhead(level=4)

Convert coordinates to maidenhead.

>>> dublin.maidenhead()
'IO63ui72gq'
>>> dublin.maidenhead(level=6)
'IO63ui72gq19dh'

Arguments:

  • level int - precision level of maidenhead

Returns:

Maidenhead str

from_maidenhead

 | @staticmethod
 | from_maidenhead(maidenhead)

Return Geodesic object from maidenhead string.

Arguments:

  • maidenhead str - maidenhead string

Returns:

Gryd.geodesy.Geodesic coordinates

georef

 | georef(digit=8)

Convert coordinates to georef.

>>> dublin.georef()
'MKJJ43322037'
>>> dublin.georef(digit=6)
'MKJJ433203'

Arguments:

  • digit int - digit number of georef (can be 4, 6 or 8)

Returns:

Georef str

from_georef

 | @staticmethod
 | from_georef(georef)

Return Geodesic object from georef.

>>> Gryd.Geodesic.from_georef('MKJJ43322037')
<lon=-006°16'21.900" lat=+053°20'41.100" alt=0.000>
>>> Gryd.Geodesic.from_georef('MKJJ433220')
<lon=-006°15'57.000" lat=+053°22'45.000" alt=0.000>

Arguments:

  • georef str - georef string

Returns:

Gryd.geodesy.Geodesic coordinates

gars

 | gars()

Get the associated GARS Area (5minx5min tile).

>>> dublin.gars()
'348MY16'

from_gars

 | @staticmethod
 | from_gars(gars, anchor="")

Return Geodesic object from gars. Optional anchor value to define where to handle 5minx5min tile.

>>> Gryd.Geodesic.from_gars('348MY16', anchor="nw")
<lon=-006°20'0.000" lat=+053°25'0.000" alt=0.000>
>>> Gryd.Geodesic.from_gars('348MY16')
<lon=-006°17'30.000" lat=+053°22'30.000" alt=0.000>

Arguments:

  • gars str - gars string
  • anchor str - tile anchor (nesw)

Returns:

Gryd.geodesy.Geodesic coordinates

url_load_location

 | url_load_location(url, **kwargs)

Return a static map image data from map provider.

>>> # below a mapbox-static-map url centered on [lon, lat] with a red
>>> # pin, width, height and zoom to be specified on call
>>> url = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/static"
... "/pin-s+f74e4e(%(lon)f,%(lat)f)/%(lon)f,%(lat)f,%(zoom)d,0"
... "/%(width)dx%(height)d?access_token=%(token)s"
>>> data = dublin.url_load_location(
...    url, zoom=15, width=600, height=400, token="xx-xxxxxx-xx"
... )
>>> # see `Gryd.geodesy.Geodesic.dump_location`
>>> with io.open("dump.png", "wb") as f:
...    f.write(data)

Arguments:

  • url str - map provider url containing %(lon)f and %(lat)f format expression to be replaced by longitude and latitude found in GPS data
  • **kwargs dict - key-value pairs to match entries in url according to python string formatting

Returns:

Image data as bytes (py3) or str (py2)

dump_location

 | dump_location(name, url, **kwargs)

Dump a static map image from map provider into filesystem.

Arguments:

  • name str - a valid filepath
  • url str - map provider url containing %(lon)f and %(lat)f format expression to be replaced by longitude and latitude found in GPS data
  • **kwargs dict - key-value pairs to match entries in url according to python string formatting

Gryd.geohash

Efficient geohash computing library based on bitwise operation with python integer.

>>> from Gryd import geohash
>>> dublin = geohash.geoh(-6.272877, 53.344606, bits=50)
>>> dublin
<01111010110011111101000111011100000001001111100111>
>>> geohash.as_str(dublin)
'gc7x3r04z7'

Geohash can be encoded with a custom 32-element-sized base.

>>> import random
>>> base = list("0123456789bcdefghjkmnpqrstuvwxyz")
>>> random.shuffle(base)
>>> base = "".join(base)
>>> base
'tjcbwq2uev8n7r9gmdf1sy05kzxh4p63'
>>> geohash.as_str(dublin, base=base)
'gnupb5tw3u'
>>> geohash.as_int('gnupb5tw3u', base=base)
<01111010110011111101000111011100000001001111100111>
>>> dublin
<01111010110011111101000111011100000001001111100111>

EARTH_RADIUS

Popular Visualisation Spheroid radius (epsg 7059 ellipsoid)

GeoH Objects

class GeoH(int)

Integer that keeps info about leading zero bits.

precision

 | precision()

Return meter precision tuple for longitude and latitude based on Popular Visualisation Spheroid radius (epsg 7059 ellipsoid).

geoh

geoh(lon, lat, bits=25)

Return a python integer representing geohashed coordinates longitude and latitude with a given precision.

Arguments:

  • lon float - longitude
  • lat float - latitude
  • bits int - length of the geohash in bit

Returns:

Gryd.geohash.GeoH

lonlat

lonlat(value, centered=False)

Return longitude and latitude and precision from a geohash integer.

Arguments:

  • value Gryd.geohash.GeoH or int - geohash value
  • centered bool - returns bottom-left corner (if False) or center (if True) of geohash surface

Returns:

longitude, latitude and precision as (dlon, dlat) tuple

to_geohash

backward compatibility

from_geohash

backward compatibility