The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by dfjooste, 2018-05-13 11:19:35

Steps

Steps

 Loading spatial data
o Compass
1. Open the MongoDB Compass Community window and connect to the preferred connection.

2. Once connected select the database that you wish to use or create a new one

3. Once the database has been selected and the collection identified that you want the data to be
imported to it should look like this.

4. Now select the Collection Menu at the top of the screen and click on the import data button.

5. The import window will appear. The input data file we will use is the .json extension, once
selected click on the brows button, navigate to the folder created on your desktop (where the
data is) and select the Hospital.json file.

6. Press the import button and wait for the data to be imported.

Your new output should look like this for the hospital points dataset.
Now repeat steppes 3 to 6 this time using the Munic_Boundary collection and the study are.json file
provided in the working documents.

o MongoDB Shell

The same operation can be performed in the MongoDB shell extension.
Launch command prompt in windows and set the working directory to the “Working”file created on
your desktop. E.g. cd c:\users\users_name\Desktop\working
Once the director is set enter the following into command prompt:
“--db Spatial --collection neighborhood --file neigborhood.json –jsonArray”
If the process completed the following should be your output in the window

• Indexing
To create an index in MongoDB Compass, complete the following steps:

1. Navigate to the collection for which you wish to create the index:
a. In the left-hand MongoDB Compass navigation pane, click the database named “Test”.

b. From the database view, click the Neighborhood Collection.
Click the Indexes tab:

2. Click the Create Index button:

The following dialog will appear:

neighb

3. (Optional) Enter the index name.
Leaving this field blank causes MongoDB Compass to create a default name for the index.
For this Example we will give it the name “NeighborIndex” as indicated by the figure above.

4. Add fields to the index.
Use the Configure the index definition section of the dialog to define the fields for your index
and their respective types. To create an index on multiple fields, click Add another field.

5. Click Create to create the index.
6. Repeat this process for the restaurant collection.

Basic selection
Lets start with some basic selection of feature in our dataset. Even though this is a spatial dataset we would still in some
cases want to query the data directly without a spatial element to it.
The method of selecting features in the dataset are as follows:
db.collection.find() will return all the data in the collection
db.collection.findOne() will return the first instance of data in the set
Lets test this:

1. First we will launch the mongoDB shell and navigate to the database we created:
“use test"

2. Then we will review the available collections:
show collections

These are the data collections that we created earlier from the .json files.
3. If we for instance want to view all the restaurants in our dataset, we will use “db.restaurant.find()”

To view this data in a more structure manner we can add the .forEach(printjson) command and the output will
be as follows:

4. Now let’s use this output to look for some more specific items in the data. Let us look for all the “Subway”
restaurant in the database. In the shell type in the following command “db.restaurant.find({name : “Subway”})
The output should look like this:

To view the amount of subways add the .count() command to the statement.

The data can be visualized on a map by using Mongodb Compass by clicking on the schema tab and running the
subway query as displayed below:

5. We can expand on this selection by finding more than one restaurant. Let us now count all the Subway
restaurants as well as the Think Coffee stores.

The or operator is included and has the following format
$or [{option1},{option 2}]
In our example the command line would look like this:
The output now gives us the count of all the Subway as well as Think Coffee restaurants in the dataset.

• Finding features in a given bounding box
In order to find features in a bounding box Mongodb has the Function $geoWithin and the call for $box that defines the
coorinates, this is a geospatial operator that we can use to find all features in a select are such as a selected bounding
box.

1. We can start in MongoDB shell by entering the following command

2. We can visualize the data in this boundingbox in mongoDB compass with the following command and the output
looks as follows: {location: { $geoWithin: {$box:[[80, -80],[-80,80]]}}}

This represents all points within the coordinates [80,-80] top left and [-80,80] bottom right of a box.
the same operation can be done with a circular selection from a mid-point using the same $geoWithin operator
with a $centerSphere call to make the selection.
the $centerSphere command has the following format for bounding data:
$centreSphere: [[<x>,<y>], <sphere readius in Radians]

3. In the MongoDB shell enter the following command as an example:

The same command can be contructed in MongoDB shell and visualized as follows:
{location: {$geoWithin: { $centerSphere: [ [ -73.97571569603512, 40.7492562481838 ],
0.0026402780226786023 ]}}}

• Intersections
In MongoDB we can also check the relation between spatial features, as an example we can check the amount of
restaurants in a specific neighborhood.

1. Let’s say for instance you want to know how many restaurants are in a suburb in New York. Let us use a location
in East village:

We will use the centre point of Thomson square park [-73.981915, 40.726428]
2. The first step will be to intersect your location with the neighborhood layer so we can check the extent of this

polygon that will be used to calculate the number of restaurants in this vicinity. The $geoIntersect command can
be used to accomplish this. Lets start by executing the following function:
db.neighborhood.findOne({geometry: {$geoIntersects:{$geometry: {type: “Point”, coordinates: [-73.981915,
40.726428]}}}})

The resulting output will be the East Village feature.
3. Now that we have this polygon we want to check the restaurants that fall inside it with the $geoWithin Function

as follows:
First we assign the above command to a variable so that we can shorthand it into the coming formula

Followed by the command to find the restaurants

Now we know how many restaurants are in this polygon.
4. If we want to check how many restaurants are within walking distance of 500m from the park we can use the

following command to check a circular radius
5. The result would look like the following:

We can also look at the nearest restaurants to the point and limit the closest to the park
6. Enter the following command into the Shell to receive a list of the nearest 10 restaurants to the point ordered
closest to furthest:

We can also edit the original dataset directly in the database. Let’s add a rating field for restaurants, Enter the following
command in mongoDB shell:

This has added a field called Rating and set the default value to 0.

Now let us test some other features in MongoDB
1. Lets create some ratings for all the features at random: Enter the following command


Click to View FlipBook Version