Example #1
0
        /**
         * h3ToCenterChild produces the center child index for a given H3 index at
         * the specified resolution
         *
         * @param h H3Index to find center child of
         * @param childRes The resolution to switch to
         *
         * @return H3Index of the center child, or 0 if you actually asked for a parent
         */
        public static H3Index h3ToCenterChild(H3Index h, int childRes)
        {
            int parentRes = H3_GET_RESOLUTION(h);

            if (!_isValidChildRes(parentRes, childRes))
            {
                return(H3_INVALID_INDEX);
            }
            else if (childRes == parentRes)
            {
                return(h);
            }
            H3Index child = H3_SET_RESOLUTION(ref h, childRes);

            for (int i = parentRes + 1; i <= childRes; i++)
            {
                H3_SET_INDEX_DIGIT(ref child, i, 0);
            }
            return(child);
        }
Example #2
0
        /**
         * h3ToParent produces the parent index for a given H3 index
         *
         * @param h H3Index to find parent of
         * @param parentRes The resolution to switch to (parent, grandparent, etc)
         *
         * @return H3Index of the parent, or 0 if you actually asked for a child
         */
        public static H3Index h3ToParent(H3Index h, int parentRes)
        {
            int childRes = H3_GET_RESOLUTION(h);

            if (parentRes > childRes)
            {
                return(H3_INVALID_INDEX);
            }
            else if (parentRes == childRes)
            {
                return(h);
            }
            else if (parentRes < 0 || parentRes > Constants.MAX_H3_RES)
            {
                return(H3_INVALID_INDEX);
            }
            H3Index parentH = H3_SET_RESOLUTION(ref h, parentRes);

            for (int i = parentRes + 1; i <= childRes; i++)
            {
                H3_SET_INDEX_DIGIT(ref parentH, i, (Direction)H3_DIGIT_MASK);
            }
            return(parentH);
        }
Example #3
0
 /**
  * Gets the highest bit of the H3 index.
  */
 public static int H3_GET_HIGH_BIT(H3Index h3)
 {
     return((int)((((h3) & H3_HIGH_BIT_MASK) >> H3_MAX_OFFSET)));
 }
Example #4
0
 /**
  * Converts an H3 index into a string representation.
  * @param h The H3 index to convert.
  * @param str The string representation of the H3 index.
  * @param sz Size of the buffer `str`
  */
 public static string h3ToString(H3Index h)
 {
     return(h.value.ToString("x"));
 }
Example #5
0
        /**
         * Returns whether or not an H3 index is a valid cell (hexagon or pentagon).
         * @param h The H3 index to validate.
         * @return 1 if the H3 index if valid, and 0 if it is not.
         */
        public static bool h3IsValid(H3Index h)
        {
            if (H3_GET_HIGH_BIT(h) != 0)
            {
                return(false);
            }

            if (H3_GET_MODE(h) != Constants.H3_HEXAGON_MODE)
            {
                return(false);
            }

            if (H3_GET_RESERVED_BITS(h) != 0)
            {
                return(false);
            }

            int baseCell = H3_GET_BASE_CELL(h);

            if (baseCell < 0 || baseCell >= Constants.NUM_BASE_CELLS)
            {
                return(false);
            }

            int res = H3_GET_RESOLUTION(h);

            if (res < 0 || res > Constants.MAX_H3_RES)
            {
                return(false);
            }

            bool foundFirstNonZeroDigit = false;

            for (int r = 1; r <= res; r++)
            {
                Direction digit = H3_GET_INDEX_DIGIT(h, r);

                if (!foundFirstNonZeroDigit && digit != Direction.CENTER_DIGIT)
                {
                    foundFirstNonZeroDigit = true;
                    if (BaseCellData._isBaseCellPentagon(baseCell) && digit == Direction.K_AXES_DIGIT)
                    {
                        return(false);
                    }
                }

                if (digit < Direction.CENTER_DIGIT || digit >= Direction.NUM_DIGITS)
                {
                    return(false);
                }
            }

            for (int r = res + 1; r <= Constants.MAX_H3_RES; r++)
            {
                Direction digit = H3_GET_INDEX_DIGIT(h, r);
                if (digit != Direction.INVALID_DIGIT)
                {
                    return(false);
                }
            }

            return(true);
        }
Example #6
0
 /**
  * Gets the integer base cell of h3.
  */
 public static int H3_GET_BASE_CELL(H3Index h3)
 {
     return((int)((((h3) & H3_BC_MASK) >> H3_BC_OFFSET)));
 }
Example #7
0
 /**
  * Returns the H3 base cell "number" of an H3 cell (hexagon or pentagon).
  *
  * Note: Technically works on H3 edges, but will return base cell of the
  * origin cell.
  *
  * @param h The H3 cell.
  * @return The base cell "number" of the H3 cell argument.
  */
 public static int h3GetBaseCell(H3Index h)
 {
     return(H3_GET_BASE_CELL(h));
 }
Example #8
0
 /**
  * Gets a value in the reserved space. Should always be zero for valid indexes.
  */
 public static int H3_GET_RESERVED_BITS(H3Index h3)
 {
     return((int)((((h3) & H3_RESERVED_MASK) >> H3_RESERVED_OFFSET)));
 }
Example #9
0
 /**
  * Sets the resolution res digit of h3 to the integer digit (0-7)
  */
 public static void H3_SET_INDEX_DIGIT(ref H3Index h3, int res, Direction digit)
 {
     h3 = (((h3) & ~((H3_DIGIT_MASK << ((Constants.MAX_H3_RES - (res)) * H3_PER_DIGIT_OFFSET)))) |
           (((ulong)(digit)) << ((Constants.MAX_H3_RES - (res)) * H3_PER_DIGIT_OFFSET)));
 }
Example #10
0
 /**
  * Gets the resolution res integer digit (0-7) of h3.
  */
 public static Direction H3_GET_INDEX_DIGIT(H3Index h3, int res)
 {
     return((Direction)((((h3) >> ((Constants.MAX_H3_RES - (res)) * H3_PER_DIGIT_OFFSET)) & H3_DIGIT_MASK)));
 }
Example #11
0
 /**
  * Sets a value in the reserved space. Setting to non-zero may produce invalid
  * indexes.
  */
 public static void H3_SET_RESERVED_BITS(ref H3Index h3, int v)
 {
     h3 = (((h3) & H3_RESERVED_MASK_NEGATIVE) | (((ulong)(v)) << H3_RESERVED_OFFSET));
 }
Example #12
0
 /**
  * Sets the integer resolution of h3.
  */
 public static H3Index H3_SET_RESOLUTION(ref H3Index h3, int res)
 {
     h3 = (((h3) & H3_RES_MASK_NEGATIVE) | (((ulong)(res)) << H3_RES_OFFSET));
     return(h3);
 }
Example #13
0
 /**
  * Gets the integer resolution of h3.
  */
 public static int H3_GET_RESOLUTION(H3Index h3)
 {
     return((int)((((h3) & H3_RES_MASK) >> H3_RES_OFFSET)));
 }
Example #14
0
 /**
  * Sets the integer base cell of h3 to bc.
  */
 public static void H3_SET_BASE_CELL(ref H3Index h3, int bc)
 {
     h3 = (((h3) & H3_BC_MASK_NEGATIVE) | (((ulong)(bc)) << H3_BC_OFFSET));
 }
Example #15
0
 /**
  * Sets the highest bit of the h3 to v.
  */
 public static void H3_SET_HIGH_BIT(ref H3Index h3, int v)
 {
     h3 = (((h3) & H3_HIGH_BIT_MASK_NEGATIVE) | (((ulong)(v)) << H3_MAX_OFFSET));
 }
Example #16
0
 /**
  * Returns the H3 resolution of an H3 index.
  * @param h The H3 index.
  * @return The resolution of the H3 index argument.
  */
 public static int h3GetResolution(H3Index h)
 {
     return(H3_GET_RESOLUTION(h));
 }
Example #17
0
 /**
  * Gets the integer mode of h3.
  */
 public static int H3_GET_MODE(H3Index h3)
 {
     return((int)((((h3) & H3_MODE_MASK) >> H3_MODE_OFFSET)));
 }
Example #18
0
 /**
  * Sets the integer mode of h3 to v.
  */
 public static void H3_SET_MODE(ref H3Index h3, int v)
 {
     h3 = (((h3) & H3_MODE_MASK_NEGATIVE) | (((ulong)(v)) << H3_MODE_OFFSET));
 }