Geohash.h 5.7 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#pragma once
/**
 * \file NETGeographicLib/Geohash.h
 * \brief Header for NETGeographicLib::Geohash class
 *
 * NETGeographicLib is copyright (c) Scott Heiman (2013)
 * GeographicLib is Copyright (c) Charles Karney (2010-2012)
 * <charles@karney.com> and licensed under the MIT/X11 License.
 * For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/

namespace NETGeographicLib
{
  /**
   * \brief .NET wrapper for GeographicLib::Geohash.
   *
   * Geohashes are described in
   * - https://en.wikipedia.org/wiki/Geohash
   * - http://geohash.org/ (this link is broken as of 2012-12-11)
   * .
   * They provide a compact string representation of a particular geographic
   * location (expressed as latitude and longitude), with the property that if
   * trailing characters are dropped from the string the geographic location
   * remains nearby.
   *
   * C# Example:
   * \include example-Geohash.cs
   * Managed C++ Example:
   * \include example-Geohash.cpp
   * Visual Basic Example:
   * \include example-Geohash.vb
   **********************************************************************/
    public ref class Geohash
    {
        private:
        // hide the constructor since all members of this class are static.
        Geohash() {}
    public:

        /**
         * Convert from geographic coordinates to a geohash.
         *
         * @param[in] lat latitude of point (degrees).
         * @param[in] lon longitude of point (degrees).
         * @param[in] len the length of the resulting geohash.
         * @param[out] geohash the geohash.
         * @exception GeographicErr if \e lat is not in [&minus;90&deg;,
         *   90&deg;].
         * @exception std::bad_alloc if memory for \e geohash can't be allocated.
         *
         * Internally, \e len is first put in the range [0, 18].
         *
         * If \e lat or \e lon is NaN, the returned geohash is "nan".
         **********************************************************************/
        static void Forward(double lat, double lon, int len,
            [System::Runtime::InteropServices::Out] System::String^% geohash);

        /**
         * Convert from a geohash to geographic coordinates.
         *
         * @param[in] geohash the geohash.
         * @param[out] lat latitude of point (degrees).
         * @param[out] lon longitude of point (degrees).
         * @param[out] len the length of the geohash.
         * @param[in] centerp if true (the default) return the center of the
         *   geohash location, otherwise return the south-west corner.
         * @exception GeographicErr if \e geohash contains illegal characters.
         *
         * Only the first 18 characters for \e geohash are considered.  The case of
         * the letters in \e geohash is ignored.
         *
         * If the first three characters in \e geohash are "nan", then \e lat and
         * \e lon are set to NaN.
         **********************************************************************/
        static void Reverse(System::String^ geohash,
            [System::Runtime::InteropServices::Out] double% lat,
            [System::Runtime::InteropServices::Out] double% lon,
            [System::Runtime::InteropServices::Out] int% len,
            bool centerp);

        /**
         * The latitude resolution of a geohash.
         *
         * @param[in] len the length of the geohash.
         * @return the latitude resolution (degrees).
         *
         * Internally, \e len is first put in the range [0, 18].
         **********************************************************************/
        static double LatitudeResolution(int len);

        /**
         * The longitude resolution of a geohash.
         *
         * @param[in] len the length of the geohash.
         * @return the longitude resolution (degrees).
         *
         * Internally, \e len is first put in the range [0, 18].
         **********************************************************************/
        static double LongitudeResolution(int len);

        /**
         * The geohash length required to meet a given geographic resolution.
         *
         * @param[in] res the minimum of resolution in latitude and longitude
         *   (degrees).
         * @return geohash length.
         *
         * The returned length is in the range [0, 18].
         **********************************************************************/
        static int GeohashLength(double res);

        /**
         * The geohash length required to meet a given geographic resolution.
         *
         * @param[in] latres the resolution in latitude (degrees).
         * @param[in] lonres the resolution in longitude (degrees).
         * @return geohash length.
         *
         * The returned length is in the range [0, 18].
         **********************************************************************/
        static int GeohashLength(double latres, double lonres);

        /**
         * The decimal geographic precision required to match a given geohash
         * length.  This is the number of digits needed after decimal point in a
         * decimal degrees representation.
         *
         * @param[in] len the length of the geohash.
         * @return the decimal precision (may be negative).
         *
         * Internally, \e len is first put in the range [0, 18].  The returned
         * decimal precision is in the range [&minus;2, 12].
         **********************************************************************/
        static int DecimalPrecision(int len);
    };
} // namespace NETGeographicLib