Class H3Core

java.lang.Object
com.uber.h3core.H3Core

public class H3Core
extends Object
H3Core provides all functions of the H3 API.

This class is thread safe and can be used as a singleton.

Any function in this class may throw H3Exception.

  • Method Details

    • newInstance

      public static H3Core newInstance() throws IOException
      Create by unpacking the H3 native library to disk and loading it. The library will attempt to detect the correct operating system and architecture of native library to unpack.
      Throws:
      SecurityException - Loading the library was not allowed by the SecurityManager.
      UnsatisfiedLinkError - The library could not be loaded
      IOException - The library could not be extracted to disk.
    • newInstance

      public static H3Core newInstance​(H3CoreLoader.OperatingSystem os, String arch) throws IOException
      Create by unpacking the H3 native library to disk and loading it. The library will attempt to extract the native library matching the given arguments to disk.
      Throws:
      SecurityException - Loading the library was not allowed by the SecurityManager.
      UnsatisfiedLinkError - The library could not be loaded
      IOException - The library could not be extracted to disk.
    • newSystemInstance

      public static H3Core newSystemInstance()
      Create by using the H3 native library already installed on the system.
      Throws:
      SecurityException - The library could not be loaded
      UnsatisfiedLinkError - The library could not be loaded
    • isValidCell

      public boolean isValidCell​(long h3)
      Returns true if this is a valid H3 index.
    • isValidCell

      public boolean isValidCell​(String h3Address)
      Returns true if this is a valid H3 index.
    • getBaseCellNumber

      public int getBaseCellNumber​(long h3)
      Returns the base cell number for this index.
    • getBaseCellNumber

      public int getBaseCellNumber​(String h3Address)
      Returns the base cell number for this index.
    • isPentagon

      public boolean isPentagon​(long h3)
      Returns true if this index is one of twelve pentagons per resolution.
    • isPentagon

      public boolean isPentagon​(String h3Address)
      Returns true if this index is one of twelve pentagons per resolution.
    • latLngToCell

      public long latLngToCell​(double lat, double lng, int res)
      Find the H3 index of the resolution res cell containing the lat/lon (in degrees)
      Parameters:
      lat - Latitude in degrees.
      lng - Longitude in degrees.
      res - Resolution, 0 <= res <= 15
      Returns:
      The H3 index.
    • latLngToCellAddress

      public String latLngToCellAddress​(double lat, double lng, int res)
      Find the H3 index of the resolution res cell containing the lat/lon (in degrees)
      Parameters:
      lat - Latitude in degrees.
      lng - Longitude in degrees.
      res - Resolution, 0 <= res <= 15
      Returns:
      The H3 index.
    • cellToLatLng

      public LatLng cellToLatLng​(long h3)
      Find the latitude, longitude (both in degrees) center point of the cell.
    • cellToLatLng

      public LatLng cellToLatLng​(String h3Address)
      Find the latitude, longitude (degrees) center point of the cell.
    • cellToBoundary

      public List<LatLng> cellToBoundary​(long h3)
      Find the cell boundary in latitude, longitude (degrees) coordinates for the cell
    • cellToBoundary

      public List<LatLng> cellToBoundary​(String h3Address)
      Find the cell boundary in latitude, longitude (degrees) coordinates for the cell
    • gridDisk

      public List<String> gridDisk​(String h3Address, int k)
      Neighboring indexes in all directions.
      Parameters:
      h3Address - Origin index
      k - Number of rings around the origin
    • gridDisk

      public List<Long> gridDisk​(long h3, int k)
      Neighboring indexes in all directions.
      Parameters:
      h3 - Origin index
      k - Number of rings around the origin
    • gridDiskDistances

      public List<List<String>> gridDiskDistances​(String h3Address, int k)
      Neighboring indexes in all directions, ordered by distance from the origin index.
      Parameters:
      h3Address - Origin index
      k - Number of rings around the origin
      Returns:
      A list of rings, each of which is a list of addresses. The rings are in order from closest to origin to farthest.
    • gridDiskDistances

      public List<List<Long>> gridDiskDistances​(long h3, int k)
      Neighboring indexes in all directions, ordered by distance from the origin index.
      Parameters:
      h3 - Origin index
      k - Number of rings around the origin
      Returns:
      A list of rings, each of which is a list of addresses. The rings are in order from closest to origin to farthest.
    • gridDiskUnsafe

      public List<List<String>> gridDiskUnsafe​(String h3Address, int k)
      Returns in order neighbor traversal.
      Parameters:
      h3Address - Origin hexagon index
      k - Number of rings around the origin
      Returns:
      A list of rings, each of which is a list of addresses. The rings are in order from closest to origin to farthest.
    • gridDiskUnsafe

      public List<List<Long>> gridDiskUnsafe​(long h3, int k)
      Returns in order neighbor traversal.
      Parameters:
      h3 - Origin hexagon index
      k - Number of rings around the origin
      Returns:
      A list of rings, each of which is a list of addresses. The rings are in order from closest to origin to farthest.
    • gridRingUnsafe

      public List<String> gridRingUnsafe​(String h3Address, int k)
      Returns in order neighbor traversal, of indexes with distance of k.
      Parameters:
      h3Address - Origin index
      k - Number of rings around the origin
      Returns:
      All indexes k away from the origin
    • gridRingUnsafe

      public List<Long> gridRingUnsafe​(long h3, int k)
      Returns in order neighbor traversal, of indexes with distance of k.
      Parameters:
      h3 - Origin index
      k - Number of rings around the origin
      Returns:
      All indexes k away from the origin
    • gridDistance

      public long gridDistance​(String a, String b)
      Returns the distance between a and b. This is the grid distance, or distance expressed in number of H3 cells.

      In some cases H3 cannot compute the distance between two indexes. This can happen because:

      • The indexes are not comparable (difference resolutions, etc)
      • The distance is greater than the H3 core library supports
      • The H3 library does not support finding the distance between the two cells, because of pentagonal distortion.
      Parameters:
      a - An H3 index
      b - Another H3 index
      Returns:
      Distance between the two in grid cells
    • gridDistance

      public long gridDistance​(long a, long b)
      Returns the distance between a and b. This is the grid distance, or distance expressed in number of H3 cells.

      In some cases H3 cannot compute the distance between two indexes. This can happen because:

      • The indexes are not comparable (difference resolutions, etc)
      • The distance is greater than the H3 core library supports
      • The H3 library does not support finding the distance between the two cells, because of pentagonal distortion.
      Parameters:
      a - An H3 index
      b - Another H3 index
      Returns:
      Distance between the two in grid cells
    • cellToLocalIj

      public CoordIJ cellToLocalIj​(long origin, long h3)
      Converts h3 to IJ coordinates in a local coordinate space defined by origin .

      The local IJ coordinate space may have deleted regions and warping due to pentagon distortion. IJ coordinates are only comparable if they came from the same origin.

      This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.

      Parameters:
      origin - Anchoring index for the local coordinate space.
      h3 - Index to find the coordinates of.
      Returns:
      Coordinates for h3 in the local coordinate space.
    • cellToLocalIj

      public CoordIJ cellToLocalIj​(String originAddress, String h3Address)
      Converts h3Address to IJ coordinates in a local coordinate space defined by originAddress.

      The local IJ coordinate space may have deleted regions and warping due to pentagon distortion. IJ coordinates are only comparable if they came from the same origin.

      This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.

      Parameters:
      originAddress - Anchoring index for the local coordinate space.
      h3Address - Index to find the coordinates of.
      Returns:
      Coordinates for h3 in the local coordinate space.
    • localIjToCell

      public long localIjToCell​(long origin, CoordIJ ij)
      Converts the IJ coordinates to an index, using a local IJ coordinate space anchored by origin.

      The local IJ coordinate space may have deleted regions and warping due to pentagon distortion. IJ coordinates are only comparable if they came from the same origin.

      This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.

      Parameters:
      origin - Anchoring index for the local coordinate space.
      ij - Coordinates in the local IJ coordinate space.
      Returns:
      Index represented by ij
    • localIjToCell

      public String localIjToCell​(String originAddress, CoordIJ ij)
      Converts the IJ coordinates to an index, using a local IJ coordinate space anchored by origin.

      The local IJ coordinate space may have deleted regions and warping due to pentagon distortion. IJ coordinates are only comparable if they came from the same origin.

      This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.

      Parameters:
      originAddress - Anchoring index for the local coordinate space.
      ij - Coordinates in the local IJ coordinate space.
      Returns:
      Index represented by ij
    • gridPathCells

      public List<String> gridPathCells​(String startAddress, String endAddress)
      Given two H3 indexes, return the line of indexes between them (inclusive of endpoints).

      This function may fail to find the line between two indexes, for example if they are very far apart. It may also fail when finding distances for indexes on opposite sides of a pentagon.

      Notes:

      • The specific output of this function should not be considered stable across library versions. The only guarantees the library provides are that the line length will be `h3Distance(start, end) + 1` and that every index in the line will be a neighbor of the preceding index.
      • Lines are drawn in grid space, and may not correspond exactly to either Cartesian lines or great arcs.
      Parameters:
      startAddress - Start index of the line
      endAddress - End index of the line
      Returns:
      Indexes making up the line.
    • gridPathCells

      public List<Long> gridPathCells​(long start, long end)
      Given two H3 indexes, return the line of indexes between them (inclusive of endpoints).

      This function may fail to find the line between two indexes, for example if they are very far apart. It may also fail when finding distances for indexes on opposite sides of a pentagon.

      Notes:

      • The specific output of this function should not be considered stable across library versions. The only guarantees the library provides are that the line length will be `h3Distance(start, end) + 1` and that every index in the line will be a neighbor of the preceding index.
      • Lines are drawn in grid space, and may not correspond exactly to either Cartesian lines or great arcs.
      Parameters:
      start - Start index of the line
      end - End index of the line
      Returns:
      Indexes making up the line.
    • polygonToCellAddresses

      public List<String> polygonToCellAddresses​(List<LatLng> points, List<List<LatLng>> holes, int res)
      Finds indexes within the given geofence.
      Parameters:
      points - Outline geofence
      holes - Geofences of any internal holes
      res - Resolution of the desired indexes
    • polygonToCells

      public List<Long> polygonToCells​(List<LatLng> points, List<List<LatLng>> holes, int res)
      Finds indexes within the given geofence.
      Parameters:
      points - Outline geofence
      holes - Geofences of any internal holes
      res - Resolution of the desired indexes
      Throws:
      IllegalArgumentException - Invalid resolution
    • cellAddressesToMultiPolygon

      public List<List<List<LatLng>>> cellAddressesToMultiPolygon​(Collection<String> h3Addresses, boolean geoJson)
      Create polygons from a set of contiguous indexes
    • cellsToMultiPolygon

      public List<List<List<LatLng>>> cellsToMultiPolygon​(Collection<Long> h3, boolean geoJson)
      Create polygons from a set of contiguous indexes
    • getResolution

      public int getResolution​(String h3Address)
      Returns the resolution of the provided index
    • getResolution

      public int getResolution​(long h3)
      Returns the resolution of the provided index
    • cellToParent

      public long cellToParent​(long h3, int res)
      Returns the parent of the index at the given resolution.
      Parameters:
      h3 - H3 index.
      res - Resolution of the parent, 0 <= res <= h3GetResolution(h3)
      Throws:
      IllegalArgumentException - res is not between 0 and the resolution of h3, inclusive.
    • cellToParentAddress

      public String cellToParentAddress​(String h3Address, int res)
      Returns the parent of the index at the given resolution.
      Parameters:
      h3Address - H3 index.
      res - Resolution of the parent, 0 <= res <= h3GetResolution(h3)
    • cellToChildren

      public List<String> cellToChildren​(String h3Address, int childRes)
      Provides the children of the index at the given resolution.
      Parameters:
      childRes - Resolution of the children
    • cellToChildren

      public List<Long> cellToChildren​(long h3, int childRes)
      Provides the children of the index at the given resolution.
      Parameters:
      h3 - H3 index.
      childRes - Resolution of the children
      Throws:
      IllegalArgumentException - Invalid resolution
    • cellToCenterChild

      public String cellToCenterChild​(String h3, int childRes)
      Returns the center child at the given resolution.
      Parameters:
      h3 - Parent H3 index
      childRes - Resolution of the child
      Throws:
      IllegalArgumentException - Invalid resolution (e.g. coarser than the parent)
    • cellToChildrenSize

      public long cellToChildrenSize​(long cell, int childRes)
      Returns the number of children the cell index has at the given resolution.
    • cellToChildrenSize

      public long cellToChildrenSize​(String cellAddress, int childRes)
      Returns the number of children the cell index has at the given resolution.
    • cellToCenterChild

      public long cellToCenterChild​(long h3, int childRes)
      Returns the center child at the given resolution.
      Parameters:
      h3 - Parent H3 index
      childRes - Resolution of the child
      Throws:
      IllegalArgumentException - Invalid resolution (e.g. coarser than the parent)
    • isResClassIII

      public boolean isResClassIII​(String h3Address)
      Determines if an index is Class III or Class II.
      Returns:
      true if the index is Class III
    • isResClassIII

      public boolean isResClassIII​(long h3)
      Determines if an index is Class III or Class II.
      Parameters:
      h3 - H3 index.
      Returns:
      true if the index is Class III
    • compactCellAddresses

      public List<String> compactCellAddresses​(Collection<String> h3Addresses)
      Returns a compacted set of indexes, at possibly coarser resolutions.
    • compactCells

      public List<Long> compactCells​(Collection<Long> h3)
      Returns a compacted set of indexes, at possibly coarser resolutions.
    • uncompactCellAddresses

      public List<String> uncompactCellAddresses​(Collection<String> h3Addresses, int res)
      Uncompacts all the given indexes to resolution res.
    • uncompactCells

      public List<Long> uncompactCells​(Collection<Long> h3, int res)
      Uncompacts all the given indexes to resolution res.
    • h3ToString

      public String h3ToString​(long h3)
      Converts from long representation of an index to String representation.
    • stringToH3

      public long stringToH3​(String h3Address)
      Converts from String representation of an index to long representation.
    • cellArea

      public double cellArea​(String h3Address, AreaUnit unit)
      Calculates the area of the given H3 cell.
      Parameters:
      h3Address - Cell to find the area of.
      unit - Unit to calculate the area in.
      Returns:
      Cell area in the given units.
    • cellArea

      public double cellArea​(long h3, AreaUnit unit)
      Calculates the area of the given H3 cell.
      Parameters:
      h3 - Cell to find the area of.
      unit - Unit to calculate the area in.
      Returns:
      Cell area in the given units.
    • greatCircleDistance

      public double greatCircleDistance​(LatLng a, LatLng b, LengthUnit unit)
      Return the distance along the sphere between two points.
      Parameters:
      a - First point
      b - Second point
      unit - Unit to return the distance in.
      Returns:
      Distance from point a to point b
    • edgeLength

      public double edgeLength​(String edgeAddress, LengthUnit unit)
      Calculate the edge length of the given H3 edge.
      Parameters:
      edgeAddress - Edge to find the edge length of.
      unit - Unit of measure to use.
      Returns:
      Length of the given edge.
    • edgeLength

      public double edgeLength​(long edge, LengthUnit unit)
      Calculate the edge length of the given H3 edge.
      Parameters:
      edge - Edge to find the edge length of.
      unit - Unit of measure to use.
      Returns:
      Length of the given edge.
    • getHexagonAreaAvg

      public double getHexagonAreaAvg​(int res, AreaUnit unit)
      Returns the average area in unit for indexes at resolution res.
      Throws:
      IllegalArgumentException - Invalid parameter value
    • getHexagonEdgeLengthAvg

      public double getHexagonEdgeLengthAvg​(int res, LengthUnit unit)
      Returns the average edge length in unit for indexes at resolution res .
      Throws:
      IllegalArgumentException - Invalid parameter value
    • getNumCells

      public long getNumCells​(int res)
      Returns the number of unique H3 indexes at resolution res.
      Throws:
      IllegalArgumentException - Invalid resolution
    • getRes0CellAddresses

      public Collection<String> getRes0CellAddresses()
      Returns a collection of all base cells (H3 indexes are resolution 0).
    • getRes0Cells

      public Collection<Long> getRes0Cells()
      Returns a collection of all base cells (H3 indexes are resolution 0).
    • getPentagonAddresses

      public Collection<String> getPentagonAddresses​(int res)
      Returns a collection of all topologically pentagonal cells at the given resolution.
      Throws:
      IllegalArgumentException - Invalid resolution.
    • getPentagons

      public Collection<Long> getPentagons​(int res)
      Returns a collection of all topologically pentagonal cells at the given resolution.
      Throws:
      IllegalArgumentException - Invalid resolution.
    • areNeighborCells

      public boolean areNeighborCells​(long a, long b)
      Returns true if the two indexes are neighbors.
    • areNeighborCells

      public boolean areNeighborCells​(String a, String b)
      Returns true if the two indexes are neighbors.
    • cellsToDirectedEdge

      public long cellsToDirectedEdge​(long a, long b)
      Returns a unidirectional edge index representing a towards b.
      Throws:
      IllegalArgumentException - The indexes are not neighbors.
    • cellsToDirectedEdge

      public String cellsToDirectedEdge​(String a, String b)
      Returns a unidirectional edge index representing a towards b.
      Throws:
      IllegalArgumentException - The indexes are not neighbors.
    • isValidDirectedEdge

      public boolean isValidDirectedEdge​(long h3)
      Returns true if the given index is a valid unidirectional edge.
    • isValidDirectedEdge

      public boolean isValidDirectedEdge​(String h3)
      Returns true if the given index is a valid unidirectional edge.
    • getDirectedEdgeOrigin

      public long getDirectedEdgeOrigin​(long h3)
      Returns the origin index of the given unidirectional edge.
    • getDirectedEdgeOrigin

      public String getDirectedEdgeOrigin​(String h3)
      Returns the origin index of the given unidirectional edge.
    • getDirectedEdgeDestination

      public long getDirectedEdgeDestination​(long h3)
      Returns the destination index of the given unidirectional edge.
    • getDirectedEdgeDestination

      public String getDirectedEdgeDestination​(String h3)
      Returns the destination index of the given unidirectional edge.
    • directedEdgeToCells

      public List<Long> directedEdgeToCells​(long h3)
      Returns the origin and destination indexes (in that order) of the given unidirectional edge.
    • directedEdgeToCells

      public List<String> directedEdgeToCells​(String h3)
      Returns the origin and destination indexes (in that order) of the given unidirectional edge.
    • originToDirectedEdges

      public List<Long> originToDirectedEdges​(long h3)
      Returns all unidirectional edges originating from the given index.
    • originToDirectedEdges

      public List<String> originToDirectedEdges​(String h3)
      Returns all unidirectional edges originating from the given index.
    • directedEdgeToBoundary

      public List<LatLng> directedEdgeToBoundary​(long h3)
      Returns a list of coordinates representing the given edge.
    • directedEdgeToBoundary

      public List<LatLng> directedEdgeToBoundary​(String h3)
      Returns a list of coordinates representing the given edge.
    • getIcosahedronFaces

      public Collection<Integer> getIcosahedronFaces​(String h3)
      Find all icosahedron faces intersected by a given H3 index, represented as integers from 0-19.
      Parameters:
      h3 - Index to find icosahedron faces for.
      Returns:
      A collection of faces intersected by the index.
    • getIcosahedronFaces

      public Collection<Integer> getIcosahedronFaces​(long h3)
      Find all icosahedron faces intersected by a given H3 index, represented as integers from 0-19.
      Parameters:
      h3 - Index to find icosahedron faces for.
      Returns:
      A collection of faces intersected by the index.
    • cellToVertex

      public long cellToVertex​(long h3, int vertexNum)
    • cellToVertex

      public String cellToVertex​(String h3Address, int vertexNum)
    • cellToVertexes

      public List<Long> cellToVertexes​(long h3)
    • cellToVertexes

      public List<String> cellToVertexes​(String h3Address)
    • vertexToLatLng

      public LatLng vertexToLatLng​(long h3)
    • vertexToLatLng

      public LatLng vertexToLatLng​(String h3Address)
    • isValidVertex

      public boolean isValidVertex​(long h3)
    • isValidVertex

      public boolean isValidVertex​(String h3Address)
    • cellToChildPos

      public long cellToChildPos​(String childAddress, int parentRes)
      Returns the position of the child cell within an ordered list of all children of the cell's parent at the specified resolution parentRes.
    • cellToChildPos

      public long cellToChildPos​(long child, int parentRes)
      Returns the position of the child cell within an ordered list of all children of the cell's parent at the specified resolution parentRes.
    • childPosToCell

      public long childPosToCell​(long childPos, long parent, int childRes)
      Returns the child cell at a given position within an ordered list of all children of parent at the specified resolution childRes.
    • childPosToCell

      public String childPosToCell​(long childPos, String parentAddress, int childRes)
      Returns the child cell at a given position within an ordered list of all children of parent at the specified resolution childRes.