File: geo_distance.rst

package info (click to toggle)
groonga 15.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 163,080 kB
  • sloc: ansic: 770,564; cpp: 48,925; ruby: 40,447; javascript: 10,250; yacc: 7,045; sh: 5,602; python: 2,821; makefile: 1,672
file content (290 lines) | stat: -rw-r--r-- 10,036 bytes parent folder | download | duplicates (3)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
.. -*- rst -*-

.. groonga-command
.. database: functions_geo_distance


``geo_distance``
================

Summary
-------

``geo_distance`` calculates the value of distance between specified two points.

Syntax
------

``geo_distance`` requires two point.
The parameter ``approximate_type`` is optional::

    geo_distance(point1, point2)
    geo_distance(point1, point2, approximate_type)

The default value of ``approximate_type`` is ``"rectangle"``.
If you omit ``approximate_type``, ``geo_distance`` calculates the value of
distance as if ``"rectangle"`` was specified.

Usage
-----

``geo_distance`` is one of the Groonga builtin functions.

You can call a builtin function in :doc:`/reference/grn_expr`

``geo_distance`` function calculates the value of distance (approximate value)
between the coordinate of ``point1`` and the coordinate of ``point2``.

.. note::

    Groonga provides three built in functions for calculating the value of distance.
    There are ``geo_distance()``, ``geo_distance2()`` and ``geo_distance3()``.
    The difference of them is the algorithm of calculating distance.
    ``geo_distance2()`` and ``geo_distance3()`` were deprecated since version 1.2.9.
    Use ``geo_distance(point1, point2, "sphere")`` instead of ``geo_distance2(point1, point2)``.
    Use ``geo_distance(point1, point2, "ellipsoid")`` instead of ``geo_distance3(point1, point2)``.

Lets's learn about ``geo_distance`` usage with examples.
This section shows simple usages.

Here are two schema definition and sample data to show the difference according to the usage.
Those samples show how to calculate the value of distance between New York City and London.

#. Using the column value of location for calculating the distance (``Cities`` table)
#. Using the explicitly specified coordinates for calculating the distance (``Geo`` table)

Using the column value of location
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here are a schema definition of ``Cities`` table and sample data to show usage.

::

  table_create Cities TABLE_HASH_KEY ShortText
  column_create Cities location COLUMN_SCALAR WGS84GeoPoint
  load --table Cities
  [
  ["_key", "location"],
  ["New York City", "146566000x-266422000"]
  ]

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_setup_location.log
.. table_create Cities TABLE_HASH_KEY ShortText
.. column_create Cities location COLUMN_SCALAR WGS84GeoPoint
.. load --table Cities
.. [
.. ["_key", "location"],
.. ["New York City", "146566000x-266422000"]
.. ]


This execution example creates a table named ``Cities`` which has one column named ``location``.
``location`` column stores the value of coordinate.
The coordinate of Tokyo is stored as sample data.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_location_rectangle.log
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "rectangle")'

This sample shows that ``geo_distance`` use the value of ``location`` column
and the value of coordinate to calculate distance.

The value ("185428000x-461000") passed to ``geo_distance`` as the second argument is
the coordinate of London.


Using the explicitly specified value of location
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here are a schema definition of ``Geo`` table and sample data to show usage.

::

 table_create Geo TABLE_HASH_KEY ShortText
 column_create Geo distance COLUMN_SCALAR Int32
 load --table Geo
 [
   {
     "_key": "the record for geo_distance() result"
   }
 ]


.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_setup_distance.log
.. table_create Geo TABLE_HASH_KEY ShortText
.. column_create Geo distance COLUMN_SCALAR Int32
.. load --table Geo
.. [
..   {
..     "_key": "the record for geo_distance() result"
..   }
.. ]

This execution example creates a table named ``Geo`` which has one column named ``distance``.
``distance`` column stores the value of distance.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle.log
.. select Geo --output_columns distance --scorer 'distance = geo_distance("146566000x-266422000", "185428000x-461000", "rectangle")'

This sample shows that ``geo_distance`` use the coordinate of London
and the coordinate of New York to calculate distance.

Parameters
----------

Required parameters
^^^^^^^^^^^^^^^^^^^

There are two required parameter, ``point1`` and ``point2``.

``point1``
""""""""""

Specifies the start point that
you want to calculate the value of distance between two points.

You can specify the value of GeoPoint type. [#]_

See :doc:`/reference/types` about GeoPoint.

``point2``
""""""""""

Specifies the end point that
you want to calculate the value of distance between two points.

You can specify the value of GeoPoint type or
the string indicating the coordinate.

See :doc:`/reference/types` about GeoPoint and the coordinate.

Optional parameter
^^^^^^^^^^^^^^^^^^

There is a optional parameter, ``approximate_type``.

``approximate_type``
""""""""""""""""""""

Specifies how to approximate the geographical features for calculating
the value of distance.

You can specify the value of ``approximate_type`` by one of the followings.

  * ``rectangle``
  * ``sphere``
  * ``ellipsoid``

.. note::

    There is a limitation about ``geo_distance``. ``geo_distance`` can not
    calculate the value of distance between two points across meridian,
    equator or the date line if you use ``sphere`` or ``ellipsoid`` as
    approximate type. There is not such a limitation for ``rectangle``.
    This is temporary limitation according to the implementation of Groonga,
    but it will be fixed in the future release.

``rectangle``
...............

This parameter require to approximate the geographical features
by square approximation for calculating the distance.

Since the value of distance is calculated by simple formula,
you can calculate the value of distance fast.
But, the error of distance increases as it approaches the pole.

You can also specify ``rect`` as abbrev expression.

Here is a sample about calculating the value of distance with column value.

.. include:: ../../example/reference/functions/geo_distance_location_rectangle.log

Here is a sample about calculating the value of distance with explicitly specified point.

.. include:: ../../example/reference/functions/geo_distance_distance_rectangle.log

Here are samples about calculating the value of distance with explicitly specified point across meridian, equator, the date line.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_meridian.log
.. select Geo --output_columns distance --scorer 'distance = geo_distance("175904000x8464000", "145508000x-13291000", "rectangle")'

This sample shows the value of distance across meridian.
The return value of ``geo_distance("175904000x8464000", "145508000x-13291000", "rectangle")`` is the value of distance from Paris, Flance to Madrid, Spain.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_equator.log
.. select Geo --output_columns distance --scorer 'distance = geo_distance("146566000x-266422000", "-56880000x-172310000", "rectangle")'

This sample shows the value of distance across equator.
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.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_distance_rectangle_across_the_date_line.log
.. select Geo --output_columns distance --scorer 'distance = geo_distance("143660000x419009000", "135960000x-440760000", "rectangle")'

This sample shows the value of distance across the date line.
The return value of ``geo_distance("143660000x419009000", "135960000x-440760000", "rectangle")`` is the value of distance from Beijin, China to San Francisco, The United States.

.. note::

    ``geo_distance`` uses square approximation as default. If you omit ``approximate_type``, ``geo_distance`` behaves like ``rectangle`` was specified.

.. note::

    ``geo_distance`` accepts the string indicating the coordinate as
    the value of ``point1`` when the value of ``approximate_type`` is
    ``"rectangle"``.
    If you specified the string indicating the coordinate as the value
    of ``point1`` with ``sphere`` or ``ellipsoid``, ``geo_distance``
    returns 0 as the value of distance.


``sphere``
...............

This parameter require to approximate the geographical features
by spherical approximation for calculating the distance.

It is slower than ``rectangle``, but the error of distance becomes
smaller than ``rectangle``.

You can also specify ``sphr`` as abbrev expression.

Here is a sample about calculating the value of distance with column value.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_location_sphere.log
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "sphere")'

``ellipsoid``
.............

This parameter require to approximate the geographical features
by ellipsoid approximation for calculating the distance.

It uses the calculation of distance by the formula of Hubeny.
It is slower than ``sphere``, but the error of distance becomes
smaller than ``sphere``.

You can also specify ``ellip`` as abbrev expression.

Here is a sample about calculating the value of distance with column value.

.. groonga-command
.. include:: ../../example/reference/functions/geo_distance_location_ellipsoid.log
.. select Cities --output_columns _score --filter 1 --scorer '_score = geo_distance(location, "185428000x-461000", "ellipsoid")'

Return value
------------

``geo_distance`` returns the value of distance in float type.
The unit of return value is meter.

.. rubric:: Footnote

.. [#] You can specify whether TokyoGeoPoint or WGS84GeoPoint.