1+ .. _kotlin-fundamentals-geospatial-search:
2+
13===================
24Search Geospatially
35===================
46
7+ .. facet::
8+ :name: genre
9+ :values: reference
10+
11+ .. meta::
12+ :keywords: code example, coordinates, plane, earth
13+
514.. contents:: On this page
615 :local:
716 :backlinks: none
@@ -11,8 +20,9 @@ Search Geospatially
1120Overview
1221--------
1322
14- In this guide, you can learn how to search **geospatial data** with the
15- MongoDB Kotlin Driver, and the different geospatial data formats supported by MongoDB.
23+ In this guide, you can learn how to query **geospatial data** by using
24+ the {+driver-short+}. You can also learn about different geospatial data
25+ formats supported by MongoDB.
1626
1727Geospatial data is data that represents a geographical location on
1828the surface of the Earth. Examples of geospatial data include:
@@ -32,7 +42,7 @@ Here is the location of MongoDB headquarters in GeoJSON:
3242
3343.. code-block:: json
3444
35- "MongoDB Headquarters " : {
45+ "location " : {
3646 "type": "point",
3747 "coordinates": [-73.986805, 40.7620853]
3848 }
@@ -43,7 +53,7 @@ For definitive information on GeoJSON, see the
4353GeoJSON Positions
4454~~~~~~~~~~~~~~~~~
4555
46- A position represents a single place on Earth, and exists in code as an array
56+ A position represents a single place on Earth and is given as an array
4757containing two or three number values:
4858
4959- Longitude in the first position (required)
@@ -52,11 +62,11 @@ containing two or three number values:
5262
5363.. important:: Longitude then Latitude
5464
55- GeoJSON orders coordinates as longitude first and latitude second. This may
56- be surprising as geographic coordinate system conventions generally list
57- latitude first and longitude second. Make sure to check what format any other
58- tools you are working with use. Popular tools such as OpenStreetMap and Google
59- Maps list coordinates as latitude first and longitude second.
65+ GeoJSON orders coordinates as longitude first and latitude second. This may
66+ be surprising as geographic coordinate system conventions generally list
67+ latitude first and longitude second. Make sure to check what format any other
68+ tools you are working with use. Popular tools such as OpenStreetMap and Google
69+ Maps list coordinates as latitude first and longitude second.
6070
6171GeoJSON Types
6272~~~~~~~~~~~~~
@@ -66,70 +76,73 @@ made up of positions.
6676
6777Here are some common GeoJSON types and how you can specify them with positions:
6878
69- - ``Point``: a single position. This could represent the location of a
70- `sculpture <https://en.wikipedia.org/wiki/Chicago_Picasso>`__.
71- - ``LineString``: an array of two or more positions, thus forming a series of line
79+ - ``Point``: Single position. This could represent the location of a
80+ `sculpture <https://en.wikipedia.org/wiki/Chicago_Picasso>`__.
81+ - ``LineString``: Array of two or more positions, thus forming a series of line
7282 segments. This could represent
7383 `the route of the Great Wall of China <https://commons.wikimedia.org/wiki/File:GreatWallChina4.png>`__.
74- - ``Polygon``: an array of positions in which the first and last
75- position are the same, thus enclosing some space. This could represent
84+ - ``Polygon``: Array of positions in which the first and last
85+ position are the same, enclosing some space. This could represent
7686 `the land within Vatican City <https://commons.wikimedia.org/wiki/File:Vatican_City_map_EN.png>`__.
7787
78-
79- To learn more about the shapes you can use in MongoDB, see the
80- :manual:`GeoJSON manual entry </reference/geojson/>`.
88+ To learn more about the shapes you can use in MongoDB, see
89+ :manual:`GeoJSON </reference/geojson/>` in the Server manual.
8190
8291Index
8392~~~~~
8493
8594To query data stored in the GeoJSON format, add the field containing
8695GeoJSON data to a ``2dsphere`` index. The following snippet creates a
87- ``2dsphere`` index on the ``location.geo`` field using the ``Indexes`` builder:
96+ ``2dsphere`` index on the ``location.geo`` field by using the
97+ ``Indexes`` builder:
8898
8999.. literalinclude:: /examples/generated/GeoTest.snippet.geo2dsphere-index.kt
90100 :language: kotlin
91101
92- For more information on the ``Indexes`` builder, see our
93- :doc:`guide on the Indexes builder </fundamentals/builders/ indexes>` .
102+ To learn more about the ``Indexes`` builder, see the
103+ :ref:` indexes-builders` guide .
94104
95105Coordinates on a 2D Plane
96106-------------------------
97107
98108You can store geospatial data using ``x`` and ``y`` coordinates on
99109a two-dimensional Euclidean plane. We refer to coordinates on a two-dimensional
100- plane as " legacy coordinate pairs" .
110+ plane as * legacy coordinate pairs* .
101111
102112Legacy coordinate pairs have the following structure:
103113
104114.. code-block:: json
105115
106- "<field name>" : [ x, y ]
116+ { "location" : [ x, y ] }
107117
108- Your field should contain an array of two values in which the first represents
118+ The field value contains an array of two values in which the first represents
109119the ``x`` axis value and the second represents the ``y`` axis value.
110120
111121Index
112122~~~~~
113123
114124To query data stored as legacy coordinate pairs, you must add the field containing
115125legacy coordinate pairs to a ``2d`` index. The following snippet creates a
116- ``2d`` index on the ``coordinates`` field using the ``Indexes`` builder:
126+ ``2d`` index on the ``coordinates`` field by using the ``Indexes`` builder:
117127
118128.. literalinclude:: /examples/generated/GeoTest.snippet.geo2d-index.kt
119129 :language: kotlin
120130
121- For more information on the ``Indexes`` builder, see our
122- :doc:`guide on the Indexes builder </fundamentals/builders/ indexes>`.
131+ To learn more about the ``Indexes`` builder, see the
132+ :ref:` indexes-builders` guide.
123133
124134For more information on legacy coordinate pairs, see the
125- :manual:`MongoDB server manual page on legacy coordinate pairs </geospatial-queries/#legacy-coordinate-pairs>`.
135+ :manual:`Legacy Coordinate Pairs
136+ </geospatial-queries/#legacy-coordinate-pairs>` section of the
137+ Geospatial Queries guide in the Server manual.
126138
127139.. tip:: Supported Operators
128140
129141 Spherical (``2dsphere``) and flat (``2d``) indexes support some, but
130- not all, of the same query operators. For a full list of operators
131- and their index compatibility, see the
132- :manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
142+ not all, of the same query operators. To view a full list of operators
143+ and their index compatibility, see the :manual:`Geospatial Query
144+ Operators </geospatial-queries/#geospatial-query-operators>` section
145+ of the Geospatial Queries guide in the Server manual.
133146
134147Geospatial Queries
135148------------------
@@ -151,58 +164,56 @@ You can specify these query operators in the MongoDB Kotlin driver with the
151164``near()``, ``geoWithin()``, ``nearSphere()``, and ``geoIntersects()`` utility
152165methods of the ``Filters`` builder class.
153166
154- For more information on geospatial query operators, see the
155- :manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
167+ To learn more about geospatial query operators, see the :manual:`Geospatial Query
168+ Operators </geospatial-queries/#geospatial-query-operators>` section
169+ of the Geospatial Queries guide in the Server manual.
156170
157- For more information on ``Filters``, see our
158- :doc:`guide on the Filters builder </fundamentals/builders/ filters>` .
171+ To learn more about the ``Filters`` builder , see the
172+ :ref:` filters-builders` guide .
159173
160174Query Parameters
161175~~~~~~~~~~~~~~~~
162176
163- To specify a shape to use in a geospatial query, use the
164- ``Position``, ``Point``, ``LineString``, and ``Polygon`` classes of the MongoDB
165- Kotlin driver.
177+ To specify a shape to use in a geospatial query, use the ``Position``,
178+ ``Point``, ``LineString``, and ``Polygon`` classes from the {+driver-short+}.
166179
167- For a full list of the GeoJSON shapes available in the MongoDB Kotlin driver, see the
168- `GeoJSON package
169- <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/geojson/package-summary.html>`__
180+ To learn more about the GeoJSON shape classes, see the
181+ `GeoJSON package <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/geojson/package-summary.html>`__
170182API Documentation.
171183
172- Examples
173- --------
184+ Geospatial Query Examples
185+ -------------------------
174186
175187The following examples use the MongoDB Atlas sample dataset. You can learn how
176188to set up your own free-tier Atlas cluster and how to load the sample dataset
177- in our :doc:`quick start guide </quick-start>` .
189+ in the :ref:`kotlin-quickstart` guide .
178190
179191The examples use the ``theaters`` collection in the ``sample_mflix`` database
180192from the sample dataset.
181193
182- The examples require the following imports:
194+ The examples in this section require the following imports:
183195
184- .. code-block::
185- :source: kotlin
196+ .. code-block:: kotlin
186197
187- import com.mongodb.client.model.geojson.Point
188- import com.mongodb.client.model.geojson.Polygon
189- import com.mongodb.client.model.geojson.Position
190- import com.mongodb.client.model.Filters.near
191- import com.mongodb.client.model.Filters.geoWithin
192- import com.mongodb.client.model.Projections.fields
193- import com.mongodb.client.model.Projections.include
194- import com.mongodb.client.model.Projections.excludeId
198+ import com.mongodb.client.model.geojson.Point
199+ import com.mongodb.client.model.geojson.Polygon
200+ import com.mongodb.client.model.geojson.Position
201+ import com.mongodb.client.model.Filters.near
202+ import com.mongodb.client.model.Filters.geoWithin
203+ import com.mongodb.client.model.Projections.fields
204+ import com.mongodb.client.model.Projections.include
205+ import com.mongodb.client.model.Projections.excludeId
195206
196- The data is modeled using the following Kotlin data class:
207+ The sample documents are modeled by the following {+language+} data class:
197208
198209.. literalinclude:: /examples/generated/GeoTest.snippet.theater-data-class.kt
199- :language: kotlin
210+ :language: kotlin
200211
201- The results are modeled using the following Kotlin data class:
212+ The result documents are modeled by the following {+language+} data class:
202213
203214.. literalinclude:: /examples/generated/GeoTest.snippet.results-data-class.kt
204- :language: kotlin
205-
215+ :language: kotlin
216+
206217The ``theaters`` collection already contains a ``2dsphere`` index on the
207218``"${Theater::location.name}.${Theater.Location::geo.name}"`` field.
208219
@@ -213,8 +224,8 @@ To search for and return documents from nearest to farthest from a point, use
213224the ``near()`` static utility method of the ``Filters`` builder class. The
214225``near()`` method constructs a query with the ``$near`` query operator.
215226
216- The following example queries for theaters between ``10,000 `` and ``5,000``
217- meters from the Great Lawn of Central Park:
227+ The following example queries for movie theaters between ``10000 `` and
228+ ``5000`` meters from the Great Lawn of Central Park:
218229
219230.. io-code-block::
220231
@@ -237,17 +248,14 @@ meters from the Great Lawn of Central Park:
237248 TheaterResults(location=Location(address=Address(city=Flushing)))
238249 TheaterResults(location=Location(address=Address(city=Elmhurst)))
239250
240- .. tip:: Fun Fact
251+ .. tip::
241252
242- MongoDB uses the
243- :manual:`same reference system </reference/glossary/#std-term-WGS84>`
253+ MongoDB uses the :manual:`same reference system </reference/glossary/#std-term-WGS84>`
244254 as GPS satellites to calculate geometries over the Earth.
245255
246- For more information on the ``$near`` operator, see the
247- :manual:`reference documentation for $near </reference/operator/query/near/#mongodb-query-op.-near>`.
248-
249- For more information on ``Filters``, see
250- :doc:`our guide on the Filters builder </fundamentals/builders/filters>`.
256+ To learn more about the ``$near`` operator, see the
257+ :manual:`$near </reference/operator/query/near/>` reference in the
258+ Server manual.
251259
252260Query Within a Range
253261~~~~~~~~~~~~~~~~~~~~
@@ -276,13 +284,11 @@ The following example searches for movie theaters in a section of Long Island.
276284
277285The following figure shows the polygon defined by the
278286``longIslandTriangle`` variable and dots representing the locations of
279- the movie theaters returned by our query.
287+ the movie theaters returned by our query.
280288
281289.. figure:: /includes/figures/geo_geometry.png
282- :alt: Area of Long Island we are searching for movie theaters
283-
284- For more information on the ``$geoWithin`` operator, see the
285- :manual:`reference documentation for $geoWithin </reference/operator/query/geoWithin/>`
290+ :alt: Area of Long Island in which to search for movie theaters
286291
287- For more information on the operators you can use in your query, see the
288- :manual:`MongoDB server manual page on geospatial query operators </geospatial-queries/index.html>`
292+ To learn more about the ``$geoWithin`` operator, see the
293+ :manual:`$geoWithin </reference/operator/query/geoWithin/>` reference in
294+ the Server manual.
0 commit comments