So I want to find all places inside Golden Gate Park, so Nopa should not be included.
Now to find them, what I will do because that makes it a bit easier to work with that is I'll go into
the your places tab and create a new map there and here, it will be a bit easier to navigate around and
find coordinates and also show which place the coordinates are for.
So in here, I want to basically well draw a polygon around my park,
so let's start here,
doesn't have to be perfect but somewhere here will be my first place.
So what I will do is I will add a marker here, I'll delete it immediately but here, we get the exact coordinates.
So let's take that
and let's use that in a future query.
So first of all to make this a bit easier,
I'll create four variables,
p1 let's say which essentially will just store these coordinates.
So here I have the longitude of the first place,
the first corner of my polygon and the latitude will be my second element and since the mongo shell uses
javascript,
I can just create constants like this,
so this value is now simply stored in P1 and we can always access it under this name.
So with that, let's remove that point here and let's create a second point in the next corner, here
and of course if you already got your coordinates, you can skip this whole exercise and just go with
them.
So this is my next point,
const p2,
this is my longitude, now let me grab my latitude here,
copy that and store this as a second value in this array
and let's get rid of that point
now. Let's move all the way to the left, to the end of the park on the west side,
add a point here,
all right,
grab the longitude of that point like this
and of course, grab the latitude too.
So let's go over there, correct the latitude and store that in this array too.
Now onto the last point and then we're done,
let me go here,
place a marker in the corner, grab these coordinates,
also the latitude of course and now all the points are stored.
So just to show you which area where we now marked,
well essentially we got three points before,
let's start like this.
We have the Conservatory of Flowers roughly,
So I'll just add a point there, we also took the Academy of Sciences so I'll add a point there
and I have a point at the Golden park tennis courts,
so at this point. We also have one at
Nopa,
so let's move over there and let's add a point there,
so these are four points.
Now the area for which I just got the coordinates is essentially the area around the Golden Gate Park
and I can draw a shape around that with that shape tool here.
So this is essentially the area for which I just got the corner coordinates and what I now want to do
is I want to run a query to find all the places that are inside of the shape and the good thing is we
can easily validate this because we can see which points are in there right,
this one should not be in there.
So now that I got these coordinates,
let's finally get to creating that query.
So on the places, let me quickly clear the console, P1 and so on are still available,
so on the places, I will now find something again and again, I will find something with the help of the
location field
but there in the location field,
I now don't want to find some nearest points, so near is wrong but instead I'll use the geoWithin operator,
$geoWithin. This is an operator provided by mongodb that simply can help us find
all elements within a certain shape, within a certain object,
typically something like polygon. Geowithin takes a document as a value
and here we can add a geometry object which is just a geo json object,
so the value here is a geo json object. The type
now is not a point but a polygon, that's important,
coordinates need to be added of course
and now I need to add the coordinates of a polygon.
Now how do I add a polygon?
Obviously a point was just a pair of coordinates, a polygon actually uses a bit more coordinates, to be
precise we need the four corners, for a polygon we add a nested array,
so an array in an array and in that array, we then again add more arrays where each array now describes
one longitude latitude pair of the corners of our polygon.
So I already got these arrays
so to say in my constants P1 to p4 which I created.
So essentially what we added here is p1, P2, p3, P4 to describe the four corners of our polygon.
There is one important thing though, I need the p1 again at the end because a polygon has to end
with a starting point so that it's treated as complete.
Now with that if I hit enter, I do get back results and I can pretty print them
and we see the California Academy of Sciences in there, we see the Golden Gate tennis park and we see
the Conservatory of Flowers, what we don't see is Nopa which makes a lot of sense because it is not
within these boundaries.
So this is how we can also check whether something is in a certain area, with the help of geoWithin
and the geometry operator and then a polygon instead of a point. A polygon
as you learned is in the end created with coordinates that hold a nested array with the different corner coordinates.
Important here is that
you also add the first corner as a last element to close the polygon.