|
| 1 | +.. _pymongo-geo-queries: |
| 2 | + |
| 3 | +================== |
| 4 | +Geospatial Queries |
| 5 | +================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: code example, coordinates, location, geographic |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to work with **geospatial data**, data formats, |
| 24 | +indexes, and queries. |
| 25 | + |
| 26 | +Geospatial data represents a geographic location on the surface of the Earth. |
| 27 | + |
| 28 | +Examples of geospatial data include: |
| 29 | + |
| 30 | +- Locations of movie theaters |
| 31 | +- Borders of countries |
| 32 | +- Routes of bicycle rides |
| 33 | +- Dog exercise areas in New York City |
| 34 | +- Points on a graph |
| 35 | + |
| 36 | +Geospatial Data Formats |
| 37 | +----------------------- |
| 38 | + |
| 39 | +All geospatial data in MongoDB is stored in one of the following formats: |
| 40 | + |
| 41 | +- GeoJSON, a format that represents geospatial data on an earth-like |
| 42 | + sphere |
| 43 | + |
| 44 | +- Legacy coordinate pairs, a format that represents geospatial data |
| 45 | + on a Euclidean plane |
| 46 | + |
| 47 | +GeoJSON |
| 48 | +~~~~~~~ |
| 49 | + |
| 50 | +Use GeoJSON to store data that represents geospatial information on |
| 51 | +an earth-like sphere. GeoJSON is composed of one or more **positions** |
| 52 | +and a **type**. |
| 53 | + |
| 54 | +Positions |
| 55 | +````````` |
| 56 | + |
| 57 | +A position represents a single location and exists in code as an array |
| 58 | +containing the following values: |
| 59 | + |
| 60 | +- Longitude in the first position (required) |
| 61 | +- Latitude in the second position (required) |
| 62 | +- Elevation in the third position (optional) |
| 63 | + |
| 64 | +The following is the position of the MongoDB Headquarters in New York City, NY. |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | + |
| 68 | + [-73.986805, 40.7620853] |
| 69 | + |
| 70 | +.. important:: Longitude then Latitude |
| 71 | + |
| 72 | + GeoJSON orders coordinates with longitude first and latitude second. |
| 73 | + Make sure to check what format any other tools you are working with use, since many popular |
| 74 | + tools such as OpenStreetMap and Google Maps list coordinates with latitude first and |
| 75 | + longitude second. |
| 76 | + |
| 77 | +Types |
| 78 | +````` |
| 79 | + |
| 80 | +The type of your GeoJSON object determines the geometric shape it represents. Geometric |
| 81 | +shapes are made up of positions. |
| 82 | + |
| 83 | +Here are some common GeoJSON types and how you can specify them with positions: |
| 84 | + |
| 85 | +- ``Point``: a single position. The following ``Point`` represents the location of |
| 86 | + the MongoDB Headquarters: |
| 87 | + |
| 88 | + .. code-block:: python |
| 89 | + |
| 90 | + { |
| 91 | + "type": "Point", |
| 92 | + "coordinates": [-73.856077, 40.848447] |
| 93 | + } |
| 94 | + |
| 95 | +- ``LineString``: an array of two or more positions that forms a series of line |
| 96 | + segments. A ``LineString`` can represent a path, route, border, or any other linear |
| 97 | + geospatial data. The following ``LineString`` represents a segment of |
| 98 | + the Great Wall of China: |
| 99 | + |
| 100 | + .. code-block:: python |
| 101 | + |
| 102 | + { |
| 103 | + "type": "LineString", |
| 104 | + "coordinates": |
| 105 | + [[116.572, 40.430], |
| 106 | + [116.570, 40.434], |
| 107 | + [116.567, 40.436], |
| 108 | + [116.566, 40.441]] |
| 109 | + } |
| 110 | + |
| 111 | +- ``Polygon``: an array of positions in which the first and last |
| 112 | + position are the same and enclose some space. The following |
| 113 | + ``Polygon`` roughly represents the land within the Vatican City: |
| 114 | + |
| 115 | + .. code-block:: python |
| 116 | + |
| 117 | + { |
| 118 | + "type": "Polygon", |
| 119 | + "coordinates": |
| 120 | + [[[12.446086, 41.901977], |
| 121 | + [12.457952, 41.901559], |
| 122 | + [12.455375, 41.907351], |
| 123 | + [12.449863, 41.905186], |
| 124 | + [12.446086, 41.901977]]] |
| 125 | + } |
| 126 | + |
| 127 | +To learn more about the GeoJSON types you can use in MongoDB, see the |
| 128 | +:manual:`GeoJSON manual entry </reference/geojson/>`. |
| 129 | + |
| 130 | +For more information on the GeoJSON format, see the |
| 131 | +`official IETF specification <https://datatracker.ietf.org/doc/html/rfc7946>`__. |
| 132 | + |
| 133 | +Legacy Coordinate Pairs |
| 134 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +Use legacy coordinate pairs to store data that represents geospatial information |
| 137 | +on a two-dimensional plane. |
| 138 | + |
| 139 | +Legacy coordinate pairs are represented by an array of two values, in which the first value |
| 140 | +represents the ``x`` axis value and the second represents the ``y`` axis value. |
| 141 | + |
| 142 | +For more information on legacy coordinate pairs, see the |
| 143 | +:manual:`MongoDB server manual page on legacy coordinate pairs </geospatial-queries/#legacy-coordinate-pairs>`. |
| 144 | + |
| 145 | +Geospatial Indexes |
| 146 | +------------------ |
| 147 | + |
| 148 | +To enable querying on geospatial data, you must create an index that |
| 149 | +corresponds to the data format. The following index types enable geospatial |
| 150 | +queries: |
| 151 | + |
| 152 | +- ``2dsphere``, used for GeoJSON data |
| 153 | +- ``2d``, used for legacy coordinate pairs |
| 154 | + |
| 155 | +To learn more about how to create geospatial indexes, see the :ref:`pymongo-geospatial-index` |
| 156 | +section of the Indexes guide. |
| 157 | + |
| 158 | +Query Operators |
| 159 | +~~~~~~~~~~~~~~~ |
| 160 | + |
| 161 | +To query geospatial data using the ``find`` operator, use one of the following query operators: |
| 162 | + |
| 163 | +- ``$near`` |
| 164 | +- ``$geoWithin`` |
| 165 | +- ``$nearSphere`` |
| 166 | +- ``$geoIntersects`` (*requires a 2dsphere index*) |
| 167 | + |
| 168 | +When using the ``$near`` operator, you can specify the following distance operators: |
| 169 | + |
| 170 | +- ``$minDistance`` |
| 171 | +- ``$maxDistance`` |
| 172 | + |
| 173 | +When using the ``$geoWithin`` operator, you can specify the following shape operators: |
| 174 | + |
| 175 | +- ``$box`` |
| 176 | +- ``$polygon`` |
| 177 | +- ``$center`` |
| 178 | +- ``$centerSphere`` |
| 179 | + |
| 180 | +To query geospatial data using the ``aggregate`` operator, you must use the ``$geoNear`` pipeline stage. |
| 181 | + |
| 182 | +For more information on geospatial query operators, see |
| 183 | +:manual:`Geospatial Query Operators </geospatial-queries/#geospatial-query-operators>` in |
| 184 | +the server manual. |
| 185 | + |
| 186 | +Examples |
| 187 | +-------- |
| 188 | + |
| 189 | +The following examples uses the MongoDB Atlas sample dataset. To obtain this sample |
| 190 | +dataset, see the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 191 | +free MongoDB Atlas cluster and load the sample datasets, see |
| 192 | +:ref:`<pymongo-get-started>`. |
| 193 | + |
| 194 | +The examples use the ``theaters`` collection in the ``sample_mflix`` database |
| 195 | +from the sample dataset. The ``theaters`` collection contains a ``2dsphere`` index |
| 196 | +on the ``location.geo`` field. |
| 197 | + |
| 198 | +Query by Proximity |
| 199 | +~~~~~~~~~~~~~~~~~~ |
| 200 | + |
| 201 | +The following example queries for documents with a ``location.geo`` field value |
| 202 | +within 1000 meters of the MongoDB Headquarters in New York City, NY. It returns documents |
| 203 | +from nearest to farthest. |
| 204 | + |
| 205 | +.. io-code-block:: |
| 206 | + :copyable: true |
| 207 | + |
| 208 | + .. input:: |
| 209 | + :language: python |
| 210 | + |
| 211 | + # set query with point at MongoDB headquarters and a maxDistance of 1000 meters |
| 212 | + query = { |
| 213 | + "location.geo": { |
| 214 | + "$near": { |
| 215 | + "$geometry": { |
| 216 | + # Search around this location |
| 217 | + "type": "Point", |
| 218 | + "coordinates": [-73.986805, 40.7620853] |
| 219 | + }, |
| 220 | + "$maxDistance": 1000 # Distance in meters (1 km) |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + # fetches the _id and theaterId fields |
| 226 | + projection = { "theaterId": 1 } |
| 227 | + |
| 228 | + nearby_places = location.find(query, projection) |
| 229 | + |
| 230 | + for i in nearby_places: |
| 231 | + print(i) |
| 232 | + |
| 233 | + .. output:: |
| 234 | + :language: json |
| 235 | + :visible: false |
| 236 | + |
| 237 | + { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 } |
| 238 | + { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 } |
| 239 | + |
| 240 | +Query by Polygon |
| 241 | +~~~~~~~~~~~~~~~~ |
| 242 | + |
| 243 | +The following example queries for documents with a ``location.geo`` field value that exists |
| 244 | +within the boundaries of Manhattan. |
| 245 | + |
| 246 | +.. io-code-block:: |
| 247 | + :copyable: true |
| 248 | + |
| 249 | + .. input:: |
| 250 | + :language: python |
| 251 | + |
| 252 | + # Polygon representation of Manhattan |
| 253 | + query = { |
| 254 | + "location.geo": { |
| 255 | + "$geoWithin": { |
| 256 | + "$geometry": { |
| 257 | + # Search around this location |
| 258 | + "type": "Polygon", |
| 259 | + "coordinates": |
| 260 | + [[[-73.925492, 40.877410], |
| 261 | + [-73.910372, 40.872366], |
| 262 | + [-73.935127, 40.834020], |
| 263 | + [-73.929049, 40.798569], |
| 264 | + [-73.976485, 40.711432], |
| 265 | + [-74.015747, 40.701229], |
| 266 | + [-74.018859, 40.708367], |
| 267 | + [-74.008007, 40.754307], |
| 268 | + [-73.925492, 40.877410]]] |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + # fetches the _id and theaterId fields |
| 275 | + projection = { "theaterId": 1 } |
| 276 | + |
| 277 | + nearby_places = location.find(query, projection) |
| 278 | + |
| 279 | + for i in nearby_places: |
| 280 | + print(i) |
| 281 | + |
| 282 | + .. output:: |
| 283 | + :language: json |
| 284 | + :visible: false |
| 285 | + |
| 286 | + { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 } |
| 287 | + { "_id" : ObjectId("59a47287cfa9a3a73e51eccb"), "theaterId" : 835 } |
| 288 | + { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 } |
| 289 | + { "_id" : ObjectId("59a47286cfa9a3a73e51e744"), "theaterId" : 1028 } |
| 290 | + { "_id" : ObjectId("59a47287cfa9a3a73e51ebe1"), "theaterId" : 609 } |
| 291 | + { "_id" : ObjectId("59a47287cfa9a3a73e51e8ed"), "theaterId" : 1906 } |
| 292 | + { "_id" : ObjectId("59a47287cfa9a3a73e51e87d"), "theaterId" : 1531 } |
| 293 | + { "_id" : ObjectId("59a47287cfa9a3a73e51eb63"), "theaterId" : 482 } |
| 294 | + |
| 295 | +Additional Resources |
| 296 | +-------------------- |
| 297 | + |
| 298 | +- For more information about working with geospatial data, see the |
| 299 | + :ref:`manual entry for geospatial data <geo-overview-location-data>`. |
| 300 | + |
| 301 | +- For more information about supported GeoJSON types, see the the |
| 302 | + :manual:`GeoJSON manual entry </reference/geojson/>`. |
| 303 | + |
| 304 | +- For more information about geospatial query operators, see the |
| 305 | + :manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`. |
0 commit comments