Global Positioning System Reference
In-Depth Information
To import data into MongoDB, the import tool (mongoimport) can be
used as shown in Fig. 10. The -d option indicates the database in which
data will be imported, in our example, gisdb. The -c option specifi es the
data collection and the last parameter is the fi le input (pois.json).
Then, the indexes can be created to allow spatial queries, as illustrated
in Fig. 11.
The following algorithms were written using the JavaScript API for
the MongoDB shell. The algorithms assume that the data were imported
into the “gisdb” database, the corresponding items were created, and the
connection to the database was established in MongoDB.
The algorithm that implements query Q1 (retrieve all restaurants located
in New York City) for MongoDB is presented in Fig. 12.
The fi rst line of Fig. 12 retrieves the municipality of New York from
the Municipalities collection. The result is stored in the New York City
variable as a document of the Municipalities entity in the BSON format. In
the fragment between lines four and ten, all of the PoIs inside the polygon
of New York City (lines four to eight) that are of the type “Restaurant”
(ninth line) are retrieved.
The algorithm that performs query Q2 (list all drugstores located within
a radius of one km from hospitals) is presented in Fig. 13.
The query Q2 is performed similarly to Q1. The main difference is that
the “$near” method (second line of Fig. 13) is used which requires a set of
1
2
ogr2ogr -f "GeoJSON" ./pois/pois.json ./pois/pois.shp pois
-lco ENCODING=UTF-8
Fig. 9. Converting a shapefi le into MongoDB.
1
mongoimport -d gisdb -c pois pois.json
Fig. 10. Importing the gisdb database into MongoDB.
1
gisdb.pois.ensureIndex({"geometry.coordinates":"2dsphere"})
Fig. 11. Creating a spatial index in MongoDB.
1
2
3
4
5
6
7
8
9
10
var newYorkCity = gisdb.municipalities.find(
{"name":"New York"});
gisdb.pois.find({ "geometry.coordinates" :
{ "$geoWithin" :
{ "$polygon" :
newYorkCity
} },
"type" : "Restaurant"
});
Fig. 12. Algorithm that performs Q1 on MongoDB.
Search WWH ::




Custom Search