MediaComem / comem-geoinf-2015-mongodb-gis

MongoDB Geospatial Queries Exercise

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Geospatial Queries with MongoDB

Based on Spatial Query Language: Part 1.

Exercise

Connect to the database

You will find installation and usage instructions in the MongoDB Manual.

Once connected, select a database:

use mongodb-geospatial-queries

Create indices

MongoDB uses GeoJSON to store geographical data. To perform geospatial queries on a collection containing GeoJSON objects, we must add a 2dsphere index on the property which will contain the objects.

db.test.createIndex({ "geom": "2dsphere" });

Insert sample data

See the GeoJSON specification on how to write GeoJSON objects.

db.test.insert({ "name" : "building1", "geom" : { "type" : "Polygon", "coordinates" : [ [ [ 10, 10 ], [ 20, 40 ], [ 8, 35 ], [ 4, 12 ], [ 10, 10 ] ] ] } })
db.test.insert({ "name" : "building2", "geom" : { "type" : "Polygon", "coordinates" : [ [ [ 40, 10 ], [ 30, 20 ], [ 40, 30 ], [ 35, 40 ], [ 60, 50 ], [ 80, 35 ], [ 60, 20 ], [ 40, 10 ] ] ] } })
db.test.insert({ "name" : "building3", "geom" : { "type" : "Polygon", "coordinates" : [ [ [ 95, 10 ], [ 95, 20 ], [ 135, 20 ], [ 135, 20 ], [ 95, 10 ] ] ] } })

db.test.insert({ "name" : "pedestrian1", "geom" : { "type" : "Point", "coordinates" : [ 70, 10 ] } });
db.test.insert({ "name" : "pedestrian2", "geom" : { "type" : "Point", "coordinates" : [ 30, 30 ] } });
db.test.insert({ "name" : "pedestrian3", "geom" : { "type" : "Point", "coordinates" : [ 70, 35 ] } });
db.test.insert({ "name" : "pedestrian4", "geom" : { "type" : "Point", "coordinates" : [ 60, 35 ] } });

db.test.insert({ "name" : "bordureRoute1", "geom": { "type": "LineString", "coordinates" : [ [ 85, 1 ], [ 85, 50 ] ] } });
db.test.insert({ "name" : "bordureRoute2", "geom": { "type": "LineString", "coordinates" : [ [ 92, 1 ], [ 92, 50 ] ] } });

Get the areas of objects

MongoDB cannot calculate the area of polygons at this time. It is necessary to use an external library, like geojson-area for Node.js.

Node.js example.

Find the closest object

To find the objects closest to a geographical object, we use MongoDB's $near operator. The following query will return the object closest to the coordinates of pedestrian2:

db.test.find({ "name": { "$ne": "pedestrian2" }, "geom": { "$near": { "$geometry": { "type": "point", "coordinates": [ 30, 30 ] } } } }).limit(1)

Node.js example.

Find the objects within

To find the objects within a geographical object, we use MongoDB's $geoWithin operator. The following query will return the objects within the polygon of building2:

db.test.find({ "name": { "$ne": "building2" }, "geom": { "$geoWithin": { "$geometry": { "type": "Polygon", "coordinates": [ [ [ 40, 10 ], [ 30, 20 ], [ 40, 30 ], [ 35, 40 ], [ 60, 50 ], [ 80, 35 ], [ 60, 20 ], [ 40, 10 ] ] ] } } } });

Node.js example

Demo Script

This repository contains a Node.js script that performs the exercise and logs the results on the command line.

It requires Node.js to be installed, and MongoDB to be installed and running on the default port.

To run the exercise on the command line:

cd /path/to/repo
npm install
npm start

About

MongoDB Geospatial Queries Exercise


Languages

Language:JavaScript 100.0%