I'm trying to query a document using its location with two polygons that are quite similar. Both includes the location of the document but one finds it and the other not. I thought that this was related to "big polygons" but I managed to reduce the polygons enough to rule out this possibility. See previous post. Any idea explaining such a difference?
Playground: https://mongoplayground.net/p/sTEtYD3HU8m:
Document:
{
"_id": {
"$oid": "63ef9379e671073bfb963145"
},
"geometry": {
"type": "Point",
"coordinates": [
2.834,
47.264
]
},
"_class": "org.example.springdatamongodbgeowithinissue.model.SamplingGeometry"
}
Is not in search results for this polygon:
{
"geometry": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
-16.1,
49.12
],
[
-16.1,
46.156
],
[
16.1,
46.156
],
[
16.1,
49.12
],
[
-16.1,
49.12
]
]
]
}
}
}
}
Still the same issue with Earth's curvature. Your area is long enough to take it into account:
zooming in and's clearly outside:
The map: https://jsfiddle.net/blex18/w9g4bzyk/1/
var map = new google.maps.Map(document.body, {zoom:5,center:{lat:47.264,lng:2.834}});
new google.maps.Polygon({geodesic:true,map:map,geodesic:true,
path:[
{lat:49.12,lng:-16.1},
{lat:46.156,lng:-16.1},
{lat:46.156,lng:16.1},
{lat:49.12,lng:16.1},
{lat:49.12,lng:-16.1}]});
new google.maps.Marker({
position: {lat:47.264,lng:2.834},
map,
title: "Here",
});
Related
I've already ask the same question in this post. My example was subject to earth curvature issue so lets have a look to another one.
This time I have two nested polygons. A large one and a smaller one. Performing a $geoWithin query with the larger one return no document while the document is found using the smaller polygon. Any thoughts?
https://mongoplayground.net/p/V_3-s-itngA
The document I'm trying to query which is located on an island on France west coast.
{
"_id": {
"$oid": "625fec0f6476793a4581d172"
},
"featureOfInterest": {
"samplingFeature": {
"geometry": {
"type": "Point",
"coordinates": [
-2.3433781679840138,
46.713764788942484
]
},
"name": [
{
"lang": "en",
"text": "France"
}
],
"type": "Feature"
}
}
}
And the query
db.collection.aggregate([
{
$match: {
"$or": [
//Largest polygon - return no document
{
"samplingFeature.geometry": {
"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[[-55.722656,34.161818],[58.007813,34.161818],[ 58.007813,53.540307],[ -55.722656, 53.540307], [-55.722656,34.161818]]]
}
}
}
},
//Smallest polygon - return the document
//{
// "samplingFeature.geometry": {
// "$geoWithin": {
// "$geometry": {
// "type": "Polygon",
// "coordinates": [[[-5.800781, 42.682435], [9.316406, 42.682435], [9.316406, 50.625073], [-5.800781, 50.625073], [-5.800781, 42.682435]]]
// }
// }
//}
//}
]
}
}
])
You've bent the planet other way round. The coordinates in your post make these boxes:
the marker is clearly outside the the larger box.
Please use the link to jsfddle in my answer to your previous question instead of globes, or wherever you get your image from. The shortest path between 2 points far away does not necessarily follow longitudes and latitudes. Please read about the math of Geodesic calculations used in mongo geospatial queries.
I have a database with about 6 million documents where each looks like the following:
{
"_id": ObjectId("5d2327409ac8bc0085f3e733"),
"type": "Feature",
"properties": {
"osm_id": "2956281",
"code": 1500,
"fclass": "building",
"name": null,
"type": "tower"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-0.0154288,
51.4539007
],
[
-0.0153606,
51.4540789
],
[
-0.0150739,
51.4540339
],
[
-0.0151478,
51.4538517
],
[
-0.0154288,
51.4539007
]
]
]
}
}
The values in the coordinates array are pairs of lat/longs, but they're in the wrong order for my requirements so I need to switch them. I have written a script in JavaScript which iterates through the array and switches them but it's very slow as it operates on one document at a time.
Is there any way to do this with a more traditional bulk update call, which presumably would be much faster?
I am GeoJSON unfortunately still a bloody beginner. Now I have to solve a problem and understand, unfortunately, no piece of systematics.
Can you please help me and explain how I can use MongoDB to evaluate GEO data for the following case.
We have three areas (for example, countries), these have a value (say base area) now I have a catchment area that covers all three countries in part. How do I get the percentage of each country?
Here is a graphic to visualize the problem:
It is needed here e.g. the areas of A (n), B (n) and C (n).
In the database (mongodb#3.2) I have corresponding document with GEO data. Example:
{
"type": "FeatureCollecton",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
13.6868862,
51.063946
],
[
13.6873401,
51.063999
],
[
13.6883277,
51.0640888
],
...
]
]
},
"properties": {
"name": "Land A",
"qkm": 65.866862
}
},
]
}, ...
How do you add a point to a polygon as a single feature? According to the GeoJson specs, this is known as a "GeometryCollection".
Example of a 'GeometryCollection':
{ "type": "GeometryCollection",
"geometries": [
{ "type": "Point",
"coordinates": [100.0, 0.0]
},
{ "type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
I tried adding a point to a polygon feature, but I couldn't get it to show on my mapbox map because I guess it is invalid GeoJson.
Anyone know what the proper way of doing this is? There are not many examples to follow on the web.
My take: [jsfilddle]
var myRegions = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometries": [
{
"type": "Point",
"coordinates": [
61.34765625,
48.63290858589535
]
},
{
"type": "Polygon",
"coordinates": [
[
[
59.94140624999999,
50.65294336725709
],
[
54.931640625,
50.90303283111257
],
[
51.943359375,
51.04139389812637
],
[
50.9765625,
48.19538740833338
],
[
52.55859375,
46.46813299215554
],
[
52.998046875,
43.8028187190472
],
[
54.4921875,
42.391008609205045
],
[
57.041015625,
43.29320031385282
],
[
59.8974609375,
45.398449976304086
],
[
62.5341796875,
44.08758502824516
],
[
65.6982421875,
45.73685954736049
],
[
68.37890625,
48.3416461723746
],
[
65.8740234375,
49.18170338770663
],
[
63.720703125,
49.97948776108648
],
[
63.80859374999999,
52.348763181988076
],
[
61.4794921875,
52.32191088594773
],
[
59.9853515625,
51.86292391360244
],
[
61.9189453125,
51.09662294502995
],
[
60.5126953125,
50.51342652633956
],
[
59.94140624999999,
50.65294336725709
]
]
]
}
]
}
]
};
As said in GeoJSON spec, a Feature object has exactly one geometry member, which is a Geometry object (or null).
A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value.
Among the possible geometry's you can indeed use a GeometryCollection, which must have a member geometries. The latter is an array of other geometries, i.e. your point, polygon, etc., or even another GeometryCollection.
A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.
So in your case you could simply do something like:
var myRegions = {
"type": "FeatureCollection",
"features": [{
"type": "Feature", // single feature
"properties": {},
"geometry": { // unique geometry member
"type": "GeometryCollection", // the geometry can be a GeometryCollection
"geometries": [ // unique geometries member
{ // each array item is a geometry object
"type": "Point",
"coordinates": [
61.34765625,
48.63290858589535
]
},
{
"type": "Polygon",
"coordinates": [
[
[
59.94140624999999,
50.65294336725709
],
// more points…
[
59.94140624999999,
50.65294336725709
]
]
]
}
]
}
}]
};
Updated jsfiddle: http://jsfiddle.net/rh8ok5t8/18/
I'm unsure to what you're actually trying to accomplish because you say you want to create a geometrycollection but in your example you're creating a featurecollection which is not the same by far.
A featurecollection is a collection of features:
A GeoJSON object with the type "FeatureCollection" is a feature collection object. An object of type "FeatureCollection" must have a member with the name "features". The value corresponding to "features" is an array.
http://geojson.org/geojson-spec.html#feature-collection-objects
Here's an example of a featurecollection:
{
type: "FeatureCollection",
features: [{
"type": "Feature",
"properties": {
"value": "foo"
},
"geometry": {
"type": "Point",
"coordinates": [0,0]
}
}, {
"type": "Feature",
"properties": {
"value": "bar"
},
"geometry": {
"type": "Polygon",
"coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]]
}
}]
}
A geometrycollection is a single feature (which you could contain in a featurecollection):
A GeoJSON object with the type "Feature" is a feature object. A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value. A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value). If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id".
http://geojson.org/geojson-spec.html#feature-objects
with multiple geometries:
A GeoJSON object with type "GeometryCollection" is a geometry object which represents a collection of geometry objects. A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.
http://geojson.org/geojson-spec.html#geometry-collection
And here's an example of a geometrycollection feature:
{
"type": "GeometryCollection",
"properties": {
"value": "foo"
},
"geometries": [{
"type": "Point",
"coordinates": [0, 0]
}, {
"type": "Polygon",
"coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]]
}]
}
The documents in my mongodb collection look like this:
{
"_id" : ObjectId("5562d6831683523f449e7d85")
"geometry" : {
"type" : "Polygon",
"coordinates" :
[[
[-122.4,37.81],
[-132.9, 39.9],
[-122.28, 37.80],
[-124.18, 39.81]
]]
}}
I have a point (lat long pair), which I'll call x and y. I need to see if the coordinates of the document create a polygon such that these coordinates are inside that polygon.
So, of course I need to compute the line describing each edge of the polygon, etc., and then see if the coordinates of the point are within that. However, for now, let's just assume we want to query documents that have x and y within the maximum and minimum lat long values of the entire array.
Here is how I tried to query this:
db.<collection-name>.find(
{'geometry.coordinates':
{$elemMatch:
{
[{$gt:-122.1}, {$lt:-122.0 }], [{$gt: 37.89 },{$lt: 37.91}]
}
}
}
)
however, the result is: Unexpected token [. I tried to follow the example here.
How do I query an array like this, where there are conditions on each element of the array in the document? thanks.
What you seem to be trying to do by matching array elements is actually "Finding a Polygon that contains a Point", which is why I changed your question title.
To accomplish this you are better off using the geoSpatail features of MongoDB rather than trying to work out the bounds yourself. With valid GeoJSON data the query is quite simple using the $geoIntersects operator.
To demonstrate, I'll first set up a collection, with some Polygon data:
db.areas.insert([
{
"name": "San Jose",
"geometry": {
"type": "Polygon",
"coordinates": [[
[ -122.20916748046876, 37.13404537126446 ],
[ -122.20916748046876, 37.496652341233364 ],
[ -121.65710449218749, 37.496652341233364 ],
[ -121.65710449218749, 37.13404537126446 ],
[ -122.20916748046876, 37.13404537126446 ]
]]
}
},
{
"name": "San Franciso",
"geometry": {
"type": "Polygon",
"coordinates": [[
[ -122.73651123046874, 37.58811876638322 ],
[ -122.73651123046874, 37.89219554724437 ],
[ -122.28332519531249, 37.89219554724437 ],
[ -122.28332519531249, 37.58811876638322 ],
[ -122.73651123046874, 37.58811876638322 ]
]]
}
}
])
Then ( though not required for $geoIntersects specifically ) when working with geoSpatial data it is best to have an "index" defined. The one that makes sense for real GeoJSON locations is "2dsphere". The index is created on the field that contains the "root" of the GeoJSON data, which is in this case called "geometry":
db.areas.createIndex({ "geometry": "2dsphere" })
Then all you need to do is supply a .find() query. I'm using the coordinates for "San Francisco city" here:
db.areas.find({
"geometry": {
"$geoIntersects": {
"$geometry": {
"type": "Point",
"coordinates": [
-122.45361328124999,
37.76420119453823
]
}
}
}
})
Which of course returns the "Polyon" defining the "San Franciso" area since that "Point" lies within that object.
{
"name": "San Franciso",
"geometry": {
"type": "Polygon",
"coordinates": [[
[ -122.73651123046874, 37.58811876638322 ],
[ -122.73651123046874, 37.89219554724437 ],
[ -122.28332519531249, 37.89219554724437 ],
[ -122.28332519531249, 37.58811876638322 ],
[ -122.73651123046874, 37.58811876638322 ]
]]
}
}
That is all there is to finding whether your "Point" lies within a "Polygon" you have stored in your collection.
Also look at tools such as geojsonlint.com and geojson.io (examples, not endorsements) in order to validate and visualise your data, which from your example does not provide a well formed Polygon.