| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_geo_distance |
| 5 |
|
| 6 |
|
| 7 |
``geo_distance`` |
| 8 |
================ |
| 9 |
|
| 10 |
Summary |
| 11 |
------- |
| 12 |
|
| 13 |
``geo_distance`` calculates the value of distance between specified two points. |
| 14 |
|
| 15 |
Syntax |
| 16 |
------ |
| 17 |
|
| 18 |
``geo_distance`` requires two point. |
| 19 |
The parameter ``approximate_type`` is optional:: |
| 20 |
|
| 21 |
geo_distance(point1, point2) |
| 22 |
geo_distance(point1, point2, approximate_type) |
| 23 |
|
| 24 |
The default value of ``approximate_type`` is ``"rectangle"``. |
| 25 |
If you omit ``approximate_type``, ``geo_distance`` calculates the value of |
| 26 |
distance as if ``"rectangle"`` was specified. |
| 27 |
|
| 28 |
Usage |
| 29 |
----- |
| 30 |
|
| 31 |
``geo_distance`` is one of the Groonga builtin functions. |
| 32 |
|
| 33 |
You can call a builtin function in :doc:`/reference/grn_expr` |
| 34 |
|
| 35 |
``geo_distance`` function calculates the value of distance (approximate value) |
| 36 |
between the coordinate of ``point1`` and the coordinate of ``point2``. |
| 37 |
|
| 38 |
.. note:: |
| 39 |
|
| 40 |
Groonga provides three built in functions for calculating the value of distance. |
| 41 |
There are ``geo_distance()``, ``geo_distance2()`` and ``geo_distance3()``. |
| 42 |
The difference of them is the algorithm of calculating distance. |
| 43 |
``geo_distance2()`` and ``geo_distance3()`` were deprecated since version 1.2.9. |
| 44 |
Use ``geo_distance(point1, point2, "sphere")`` instead of ``geo_distance2(point1, point2)``. |
| 45 |
Use ``geo_distance(point1, point2, "ellipsoid")`` instead of ``geo_distance3(point1, point2)``. |
| 46 |
|
| 47 |
Lets's learn about ``geo_distance`` usage with examples. |
| 48 |
This section shows simple usages. |
| 49 |
|
| 50 |
Here are two schema definition and sample data to show the difference according to the usage. |
| 51 |
Those samples show how to calculate the value of distance between New York City and London. |
| 52 |
|
| 53 |
#. Using the column value of location for calculating the distance (``Cities`` table) |
| 54 |
#. Using the explicitly specified coordinates for calculating the distance (``Geo`` table) |
| 55 |
|
| 56 |
Using the column value of location |
| 57 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 58 |
|
| 59 |
Here are a schema definition of ``Cities`` table and sample data to show usage. |
| 60 |
|
| 61 |
:: |
| 62 |
|
| 63 |
table_create Cities TABLE_HASH_KEY ShortText |
| 64 |
column_create Cities location COLUMN_SCALAR WGS84GeoPoint |
| 65 |
load --table Cities |
| 66 |
[ |
| 67 |
["_key", "location"], |
| 68 |
["New York City", "146566000x-266422000"] |
| 69 |
] |
| 70 |
|
| 71 |
.. groonga-command |
| 72 |
.. include:: ../../example/reference/functions/geo_distance_setup_location.log |
| 73 |
.. table_create Cities TABLE_HASH_KEY ShortText |
| 74 |
.. column_create Cities location COLUMN_SCALAR WGS84GeoPoint |
| 75 |
.. load --table Cities |
| 76 |
.. [ |
| 77 |
.. ["_key", "location"], |
| 78 |
.. ["New York City", "146566000x-266422000"] |
| 79 |
.. ] |
| 80 |
|
| 81 |
|
| 82 |
This execution example creates a table named ``Cities`` which has one column named ``location``. |
| 83 |
``location`` column stores the value of coordinate. |
| 84 |
The coordinate of Tokyo is stored as sample data. |
| 85 |
|
| 86 |
.. groonga-command |
| 87 |
.. include:: ../../example/reference/functions/geo_distance_location_rectangle.log |
| 88 |
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "rectangle")' |
| 89 |
|
| 90 |
This sample shows that ``geo_distance`` use the value of ``location`` column |
| 91 |
and the value of coordinate to calculate distance. |
| 92 |
|
| 93 |
The value ("185428000x-461000") passed to ``geo_distance`` as the second argument is |
| 94 |
the coordinate of London. |
| 95 |
|
| 96 |
|
| 97 |
Using the explicitly specified value of location |
| 98 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 99 |
|
| 100 |
Here are a schema definition of ``Geo`` table and sample data to show usage. |
| 101 |
|
| 102 |
:: |
| 103 |
|
| 104 |
table_create Geo TABLE_HASH_KEY ShortText |
| 105 |
column_create Geo distance COLUMN_SCALAR Int32 |
| 106 |
load --table Geo |
| 107 |
[ |
| 108 |
{ |
| 109 |
"_key": "the record for geo_distance() result" |
| 110 |
} |
| 111 |
] |
| 112 |
|
| 113 |
|
| 114 |
.. groonga-command |
| 115 |
.. include:: ../../example/reference/functions/geo_distance_setup_distance.log |
| 116 |
.. table_create Geo TABLE_HASH_KEY ShortText |
| 117 |
.. column_create Geo distance COLUMN_SCALAR Int32 |
| 118 |
.. load --table Geo |
| 119 |
.. [ |
| 120 |
.. { |
| 121 |
.. "_key": "the record for geo_distance() result" |
| 122 |
.. } |
| 123 |
.. ] |
| 124 |
|
| 125 |
This execution example creates a table named ``Geo`` which has one column named ``distance``. |
| 126 |
``distance`` column stores the value of distance. |
| 127 |
|
| 128 |
.. groonga-command |
| 129 |
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle.log |
| 130 |
.. select Geo --output_columns distance --scorer 'distance = geo_distance("146566000x-266422000", "185428000x-461000", "rectangle")' |
| 131 |
|
| 132 |
This sample shows that ``geo_distance`` use the coordinate of London |
| 133 |
and the coordinate of New York to calculate distance. |
| 134 |
|
| 135 |
Parameters |
| 136 |
---------- |
| 137 |
|
| 138 |
Required parameters |
| 139 |
^^^^^^^^^^^^^^^^^^^ |
| 140 |
|
| 141 |
There are two required parameter, ``point1`` and ``point2``. |
| 142 |
|
| 143 |
``point1`` |
| 144 |
"""""""""" |
| 145 |
|
| 146 |
Specifies the start point that |
| 147 |
you want to calculate the value of distance between two points. |
| 148 |
|
| 149 |
You can specify the value of GeoPoint type. [#]_ |
| 150 |
|
| 151 |
See :doc:`/reference/types` about GeoPoint. |
| 152 |
|
| 153 |
``point2`` |
| 154 |
"""""""""" |
| 155 |
|
| 156 |
Specifies the end point that |
| 157 |
you want to calculate the value of distance between two points. |
| 158 |
|
| 159 |
You can specify the value of GeoPoint type or |
| 160 |
the string indicating the coordinate. |
| 161 |
|
| 162 |
See :doc:`/reference/types` about GeoPoint and the coordinate. |
| 163 |
|
| 164 |
Optional parameter |
| 165 |
^^^^^^^^^^^^^^^^^^ |
| 166 |
|
| 167 |
There is a optional parameter, ``approximate_type``. |
| 168 |
|
| 169 |
``approximate_type`` |
| 170 |
"""""""""""""""""""" |
| 171 |
|
| 172 |
Specifies how to approximate the geographical features for calculating |
| 173 |
the value of distance. |
| 174 |
|
| 175 |
You can specify the value of ``approximate_type`` by one of the followings. |
| 176 |
|
| 177 |
* ``rectangle`` |
| 178 |
* ``sphere`` |
| 179 |
* ``ellipsoid`` |
| 180 |
|
| 181 |
.. note:: |
| 182 |
|
| 183 |
There is a limitation about ``geo_distance``. ``geo_distance`` can not |
| 184 |
calculate the value of distance between two points across meridian, |
| 185 |
equator or the date line if you use ``sphere`` or ``ellipsoid`` as |
| 186 |
approximate type. There is not such a limitation for ``rectangle``. |
| 187 |
This is temporary limitation according to the implementation of Groonga, |
| 188 |
but it will be fixed in the future release. |
| 189 |
|
| 190 |
``rectangle`` |
| 191 |
............... |
| 192 |
|
| 193 |
This parameter require to approximate the geographical features |
| 194 |
by square approximation for calculating the distance. |
| 195 |
|
| 196 |
Since the value of distance is calculated by simple formula, |
| 197 |
you can calculate the value of distance fast. |
| 198 |
But, the error of distance increases as it approaches the pole. |
| 199 |
|
| 200 |
You can also specify ``rect`` as abbrev expression. |
| 201 |
|
| 202 |
Here is a sample about calculating the value of distance with column value. |
| 203 |
|
| 204 |
.. include:: ../../example/reference/functions/geo_distance_location_rectangle.log |
| 205 |
|
| 206 |
Here is a sample about calculating the value of distance with explicitly specified point. |
| 207 |
|
| 208 |
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle.log |
| 209 |
|
| 210 |
Here are samples about calculating the value of distance with explicitly specified point across meridian, equator, the date line. |
| 211 |
|
| 212 |
.. groonga-command |
| 213 |
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_meridian.log |
| 214 |
.. select Geo --output_columns distance --scorer 'distance = geo_distance("175904000x8464000", "145508000x-13291000", "rectangle")' |
| 215 |
|
| 216 |
This sample shows the value of distance across meridian. |
| 217 |
The return value of ``geo_distance("175904000x8464000", "145508000x-13291000", "rectangle")`` is the value of distance from Paris, Flance to Madrid, Spain. |
| 218 |
|
| 219 |
.. groonga-command |
| 220 |
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_equator.log |
| 221 |
.. select Geo --output_columns distance --scorer 'distance = geo_distance("146566000x-266422000", "-56880000x-172310000", "rectangle")' |
| 222 |
|
| 223 |
This sample shows the value of distance across equator. |
| 224 |
The return value of ``geo_distance("146566000x-266422000", "-56880000x-172310000", "rectangle")`` is the value of distance from New York, The United Status to Brasillia, Brasil. |
| 225 |
|
| 226 |
.. groonga-command |
| 227 |
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_the_date_line.log |
| 228 |
.. select Geo --output_columns distance --scorer 'distance = geo_distance("143660000x419009000", "135960000x-440760000", "rectangle")' |
| 229 |
|
| 230 |
This sample shows the value of distance across the date line. |
| 231 |
The return value of ``geo_distance("143660000x419009000", "135960000x-440760000", "rectangle")`` is the value of distance from Beijin, China to San Francisco, The United States. |
| 232 |
|
| 233 |
.. note:: |
| 234 |
|
| 235 |
``geo_distance`` uses square approximation as default. If you omit ``approximate_type``, ``geo_distance`` behaves like ``rectangle`` was specified. |
| 236 |
|
| 237 |
.. note:: |
| 238 |
|
| 239 |
``geo_distance`` accepts the string indicating the coordinate as |
| 240 |
the value of ``point1`` when the value of ``approximate_type`` is |
| 241 |
``"rectangle"``. |
| 242 |
If you specified the string indicating the coordinate as the value |
| 243 |
of ``point1`` with ``sphere`` or ``ellipsoid``, ``geo_distance`` |
| 244 |
returns 0 as the value of distance. |
| 245 |
|
| 246 |
|
| 247 |
``sphere`` |
| 248 |
............... |
| 249 |
|
| 250 |
This parameter require to approximate the geographical features |
| 251 |
by spherical approximation for calculating the distance. |
| 252 |
|
| 253 |
It is slower than ``rectangle``, but the error of distance becomes |
| 254 |
smaller than ``rectangle``. |
| 255 |
|
| 256 |
You can also specify ``sphr`` as abbrev expression. |
| 257 |
|
| 258 |
Here is a sample about calculating the value of distance with column value. |
| 259 |
|
| 260 |
.. groonga-command |
| 261 |
.. include:: ../../example/reference/functions/geo_distance_location_sphere.log |
| 262 |
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "sphere")' |
| 263 |
|
| 264 |
``ellipsoid`` |
| 265 |
............. |
| 266 |
|
| 267 |
This parameter require to approximate the geographical features |
| 268 |
by ellipsoid approximation for calculating the distance. |
| 269 |
|
| 270 |
It uses the calculation of distance by the formula of Hubeny. |
| 271 |
It is slower than ``sphere``, but the error of distance becomes |
| 272 |
smaller than ``sphere``. |
| 273 |
|
| 274 |
You can also specify ``ellip`` as abbrev expression. |
| 275 |
|
| 276 |
Here is a sample about calculating the value of distance with column value. |
| 277 |
|
| 278 |
.. groonga-command |
| 279 |
.. include:: ../../example/reference/functions/geo_distance_location_ellipsoid.log |
| 280 |
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "ellipsoid")' |
| 281 |
|
| 282 |
Return value |
| 283 |
------------ |
| 284 |
|
| 285 |
``geo_distance`` returns the value of distance in float type. |
| 286 |
The unit of return value is meter. |
| 287 |
|
| 288 |
.. rubric:: Footnote |
| 289 |
|
| 290 |
.. [#] You can specify whether TokyoGeoPoint or WGS84GeoPoint. |