If placekey is not installed on your system you can install it and other dependencies for the notebooks in this repo by running
pip install -r requirements.txt
import placekey as pk
import h3 as h3
The most basic functionality of the library is converting between Placekeys and latitude/longitude
geo = (37.779351, -122.418655) # The front door of SF City Hall
placekey = pk.geo_to_placekey(*geo)
print('The Placekey for the location of SF City Hall is "{}".'.format(placekey))
centroid_lat, centroid_long = pk.placekey_to_geo(placekey)
print('The latitude and longitude for the center of "{}" is ({}, {}).'.format(placekey, centroid_lat, centroid_long))
Since the location portion (Where Part) of a Placekey is based on H3, there are also functions for converting back and forth between Plackeys and H3 indices. These H3 indices are always resolution 10. There is also support for working with integer representation of H3 indices as well as the string representation.
h3_for_placekey = pk.placekey_to_h3(placekey)
print('The H3 index corresponding to "{}" is "{}".'.format(placekey, h3_for_placekey))
print('"{}" has resolution {}.'.format(h3_for_placekey, h3.h3_get_resolution(h3_for_placekey)))
h3_int_for_placekey = pk.placekey_to_h3_int(placekey)
print('The integer H3 index corresponding to "{}" is {}.'.format(placekey, h3_int_for_placekey))
Often when working with Placekeys it is useful to be able to visualize the corresponding hexagon or to operate with that hexagon and other spatial geometries. To that end we've provided funtionality to convert placekeys into several formats for specifying geometric shapes:
There are also functions for converting geometric shapes in these formats into lists of Placekeys. See the advanced functionality notebook for examples
pk.placekey_to_hex_boundary(placekey)
pk.placekey_to_wkt(placekey)
pk.placekey_to_geojson(placekey)
pk.placekey_to_polygon(placekey)