How i show geojson FeatureCollection in a leaflet Map? - leaflet

I've exported shapefile data to geoJson with Qgis, before i try published in leaflet map, but it's not working.
The code i've used is below, where geojson file is "gj2.geojson".
Where is wrong?
GEOJSON is like:
{
"type": "FeatureCollection",
"name": "gj2",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "DN": 976 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -36.647916182333311, -8.742916206999967 ], [ -36.647082848999979, -8.742916206999967 ], [ -36.647082848999979, -8.743749540333299 ], [ -36.647916182333311, -8.743749540333299 ], [ -36.647916182333311, -8.742916206999967 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 971 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -36.647082848999979, -8.742916206999967 ], [ -36.646249515666646, -8.742916206999967 ], [ -36.646249515666646, -8.745416206999966 ], [ -36.647082848999979, -8.745416206999966 ], [ -36.647082848999979, -8.742916206999967 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 966 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -36.646249515666646, -8.742916206999967 ], [ -36.645416182333307, -8.742916206999967 ], [ -36.645416182333307, -8.743749540333299 ], [ -36.646249515666646, -8.743749540333299 ], [ -36.646249515666646, -8.742916206999967 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 975 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -36.647916182333311, -8.743749540333299 ], [ -36.647082848999979, -8.743749540333299 ], [ -36.647082848999979, -8.744582873666634 ], [ -36.647916182333311, -8.744582873666634 ], [ -36.647916182333311, -8.743749540333299 ] ] ] } },
{ "type": "Feature", "properties": { "DN": 965 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -36.646249515666646, -8.743749540333299 ], [ -36.645416182333307, -8.743749540333299 ], [ -36.645416182333307, -8.744582873666634 ], [ -36.646249515666646, -8.744582873666634 ], [ -36.646249515666646, -8.743749540333299 ] ] ] } },
{ "type": "Feature"....
THE CODE i've try:
<!DOCTYPE html>
<html>
<head>
<title>GeoJSON tutorial - Leaflet</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet#1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4.js"></script>
<style>
html, body {
height: 100%;
margin: 0;
}
#map {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id='map'></div>
<script src="geojson.js" type="text/javascript"></script>
<script>
var map = L.map('map').setView([-9,-36], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: 'mapbox.light'
}).addTo(map);
var geojson = L.geoJSON('gj2.geojson').addTo(map);
</script>
</body>
</html>

The problem is that you are trying to specify a filename as a parameter to L.geoJSON(..) but it needs a Javascript object containing the geoJson definition. Add the following to the beginning of your geoJson file (geojson.js):
var myGeoJson =
So that the file starts like this:
var myGeoJson = {
"type": "FeatureCollection",
"name": "gj2",
...
...
And then add a semicolon at the end of the file. This way you have a variable 'myGeoJson' representing the geoJson definition.
Then change this line:
var geojson = L.geoJSON('gj2.geojson').addTo(map);
to this:
var geojson = L.geoJSON(myGeoJson).addTo(map);
The Leaflet GeoJSON example page has a similar example towards the top of the page here: Using GeoJSON with Leaflet

Related

How to dynamically show/hide LineString in Leaflet

I'm loading a GeoJSON file of aircraft tracks into Leaflet. I'd like to plot current locations as icons and previous 'tracks' as lines. Showing all the tracks gets busy as aircraft increase, so I'd like to be able to toggle them on only when the aircraft is clicked on.
Can/how should I show/hide the LineString separately from the marker dynamically?
I've found suggestions to set the style transparent but .setStyle applies to the Feature, not the geometry.
Summarised GeoJSON:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"reg": "G-CGWP",
"type": "fixed",
"model": "website.profile"
},
"id": 12,
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"coordinates": [
[
-0.319281196282617,
52.08664390758181
],
[
-1.076445537370006,
52.79518475653341
],
[
-0.098191354875297,
51.94810149137197
],
[
-0.940941846648286,
53.508162348603435
]
]
},
{
"type": "Point",
"coordinates": [
-0.940941846648286,
53.508162348603435
]
}
]
}
},
...
]
}
I do have control of the GeoJSON so can change that.
Since LineString collection is represented as GeometryCollection you could consider to flatten GeometryCollection into LineString geometries, (for example via Turf.js flatten function):
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"coordinates": [
[-105.00341892242432, 39.75383843460583],
[-105.0008225440979, 39.751891803969535]
]
},
{
"type": "LineString",
"coordinates": [
[-105.0008225440979, 39.751891803969535],
[-104.99820470809937, 39.74979664004068]
]
}
]
}
}
]
}
into
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-105.00341892242432, 39.75383843460583],
[-105.0008225440979, 39.751891803969535]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-105.0008225440979, 39.751891803969535],
[-104.99820470809937, 39.74979664004068]
]
}
}
]
}
and then apply style per layer
layer.on({
click: function(e) {
toggleLayerVisibility(e.target);
}
});
where
var selectedLayerId = null;
function toggleLayerVisibility(layer) {
if (selectedLayerId) {
geojson.resetStyle(layer);
selectedLayerId = null;
} else {
//hide a layer
layer.setStyle({
opacity: 0,
fillOpacity: 0.0
});
selectedLayerId = layer._leaflet_id;
}
}
Here is an example
var data = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "GeometryCollection",
geometries: [
{
type: "LineString",
coordinates: [
[-105.00341892242432, 39.75383843460583],
[-105.0008225440979, 39.751891803969535]
]
},
{
type: "LineString",
coordinates: [
[-105.0008225440979, 39.751891803969535],
[-104.99820470809937, 39.74979664004068]
]
},
{
type: "LineString",
coordinates: [
[-104.99820470809937, 39.74979664004068],
[-104.98689651489258, 39.741052354709055]
]
}
]
}
},
{
type: "Feature",
geometry: {
type: "MultiPolygon",
coordinates: [
[
[
[-105.00432014465332, 39.74732195489861],
[-105.00715255737305, 39.7462000683517],
[-105.00921249389647, 39.74468219277038],
[-105.01067161560059, 39.74362625960105],
[-105.01195907592773, 39.74290029616054],
[-105.00989913940431, 39.74078835902781],
[-105.00758171081543, 39.74059036160317],
[-105.00346183776855, 39.74059036160317],
[-105.00097274780272, 39.74059036160317],
[-105.00062942504881, 39.74072235994946],
[-105.00020027160645, 39.74191033368865],
[-105.00071525573731, 39.74276830198601],
[-105.00097274780272, 39.74369225589818],
[-105.00097274780272, 39.74461619742136],
[-105.00123023986816, 39.74534214278395],
[-105.00183105468751, 39.74613407445653],
[-105.00432014465332, 39.74732195489861]
],
[
[-105.00361204147337, 39.74354376414072],
[-105.00301122665405, 39.74278480127163],
[-105.00221729278564, 39.74316428375108],
[-105.00283956527711, 39.74390674342741],
[-105.00361204147337, 39.74354376414072]
]
],
[
[
[-105.00942707061768, 39.73989736613708],
[-105.00942707061768, 39.73910536278566],
[-105.00685214996338, 39.73923736397631],
[-105.00384807586671, 39.73910536278566],
[-105.00174522399902, 39.73903936209552],
[-105.00041484832764, 39.73910536278566],
[-105.00041484832764, 39.73979836621592],
[-105.00535011291504, 39.73986436617916],
[-105.00942707061768, 39.73989736613708]
]
]
]
}
}
]
};
var map = L.map("map").setView([39.74739, -105], 14);
L.tileLayer(
"https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw",
{
maxZoom: 18,
attribution:
'Map data © OpenStreetMap contributors, ' +
'CC-BY-SA, ' +
'Imagery © Mapbox',
id: "mapbox.light"
}
).addTo(map);
var data_flatten = turf.flatten(data);
var geojson = L.geoJSON(data_flatten, {
style: {
fillColor: '#1c9099',
weight: 8
},
onEachFeature: onEachFeature
}).addTo(map);
var selectedLayerId = null;
function toggleLayerVisibility(layer) {
if (selectedLayerId) {
geojson.resetStyle(layer);
selectedLayerId = null;
} else {
//hide a layer
layer.setStyle({
opacity: 0,
fillOpacity: 0.0
});
selectedLayerId = layer._leaflet_id;
}
}
function onEachFeature(feature, layer) {
layer.on({
click: function(e) {
toggleLayerVisibility(e.target);
}
});
}
#map {
width: 600px;
height: 400px;
}
<link
rel="stylesheet"
href="https://unpkg.com/leaflet#1.5.1/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet#1.5.1/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""
></script>
<script
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.js"
></script>
<div id="map"></div>

How to color individual polygons drawn with mapbox-gl draw?

I'm reading a GeoJSON file and importing the polygons (and other stuff) into mapbox-gl draw using draw.set(geoJSON). How do I color individual polygons by an attribute in the properties of a feature. Example:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
//"id": "the most unique id in the world",
"properties": {
"class_id": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.30961608886719,
61.57192958204744
],
[
79.34309005737303,
61.57192958204744
],
[
79.34309005737303,
61.57871162332267
],
[
79.30961608886719,
61.57871162332267
],
[
79.30961608886719,
61.57192958204744
]
]
]
}
},
{
"type": "Feature",
"properties": {
"class_id": 2
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.35201644897461,
61.58271478278019
],
[
79.35115814208984,
61.573972521656835
],
[
79.38188552856444,
61.57192958204744
],
[
79.35201644897461,
61.58271478278019
]
]
]
}
},
}
The idea is that we color the features with class_id = 1 as red, class_id = 2 as blue, and class_id = 3 as green. How do we do that?
You need set userProperties to true for the properties of a feature will be available for styling. And use prefix user.
And use case expression:
var Draw = new MapboxDraw({
userProperties: true,
styles: [{
'id': 'gl-draw-polygon-fill-inactive',
'type': 'fill',
'filter': ['all', ['==', 'active', 'false'],
['==', '$type', 'Polygon'],
['!=', 'mode', 'static']
],
'paint': {
'fill-color': [
"case",
['==', ['get', "user_class_id"], 1], "#00ff00",
['==', ['get', "user_class_id"], 2], "#0000ff",
'#ff0000'
],
'fill-outline-color': '#3bb2d0',
'fill-opacity': 0.5
}
}...
[ http://jsfiddle.net/5Lotf4ka/ ]

Leaflet - Filtering GeoJSONs data using Selects values

I have a bus network with 2 GeoJSONs : one for stations (points) and the other for lines between them.
I have 3 selects :first one to select the bus line and filter the stations on the other two that are for origin and destination stations.
What I want to do is to dynamically filter the bus lines on the first select and the bus stations on the last two selects.
Something like this :
User selects L1 on first select > Map only shows Line 1 (both geojsons, points and lines) and hide the other Lines that were showing.
User selects Station x on select 2 and Station y on select 3 > Map only shows these stations and the ones in between (both geojsons, points and lines).
Stations GeoJSON (not complete) :
var station ={
"type": "FeatureCollection",
"name": "test",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 0, "nom": "JAMAA EL FNA", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -7.991506070410076, 31.624380871588261 ] } },
{ "type": "Feature", "properties": { "id": 1, "nom": "KOUTOUBIA", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -7.993921192850516, 31.62551706188404 ] } },
{ "type": "Feature", "properties": { "id": 2, "nom": "HOTE DE VILLE", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -7.997800602958748, 31.627492814514493 ] } },
{ "type": "Feature", "properties": { "id": 3, "nom": "R.P BERDII", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -8.003733858117105, 31.630010280990067 ] } },
{ "type": "Feature", "properties": { "id": 4, "nom": "GRAND POSTE", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -8.009040991276375, 31.633004916540266 ] } },
{ "type": "Feature", "properties": { "id": 5, "nom": "CAREE EDEN", "ligne": "L1", "ville": "MARRAKECH", "direction": "A" }, "geometry": { "type": "Point", "coordinates": [ -8.011327830139466, 31.634311225216251 ] } },....
Lines GeoJSON (not complete) :
var lignemarrakech = {
"type": "FeatureCollection",
"name": "ligne",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 0, "ligne_bus": "L1", "direction": "A", "from_st": null, "to_st": null, "ville": "MARRAKECH" }, "geometry": { "type": "LineString", "coordinates": [ [ -7.991506070410076, 31.624380871588261 ], [ -7.99177772113954, 31.62455510452893 ], [ -7.992132146977649, 31.624680377597475 ], [ -7.992359514496435, 31.624737319845583 ], [ -7.992653754814866, 31.624862592668791 ], [ -7.993295733691442, 31.625147303003317 ], [ -7.993583286729908, 31.625306740410121 ], [ -7.993921192850516, 31.62551706188404 ] ] } },
{ "type": "Feature", "properties": { "id": 1, "ligne_bus": "L1", "direction": "A", "from_st": null, "to_st": null, "ville": "MARRAKECH" }, "geometry": { "type": "LineString", "coordinates": [ [ -7.993929771507931, 31.625520494421043 ], [ -7.993921192850516, 31.62551706188404 ], [ -7.994251765813826, 31.625734485234361 ], [ -7.994613027718003, 31.625968537122866 ], [ -7.994699416434219, 31.626022034614714 ], [ -7.995311990967388, 31.626396516196305 ], [ -7.995618278233972, 31.626550320694935 ], [ -7.995971686618494, 31.626730873477591 ], [ -7.99655284707304, 31.627018419778416 ], [ -7.997188982165176, 31.627279216817595 ], [ -7.997597365187289, 31.627433019856959 ], [ -7.997800602958748, 31.627492814514493 ] ] } },
{ "type": "Feature", "properties": { "id": 2, "ligne_bus": "L1", "direction": "A", "from_st": null, "to_st": null, "ville": "MARRAKECH" }, "geometry": { "type": "LineString", "coordinates": [ [ -7.997800602958748, 31.627492814514493 ], [ -7.998131404523898, 31.627586822642048 ], [ -7.998704711458787, 31.62774731223368 ], [ -7.999246604315053, 31.627847618087841 ], [ -7.999898446446502, 31.627981359058502 ], [ -8.000487460420704, 31.628188657183067 ], [ -8.001194277189745, 31.628529695028082 ], [ -8.001987482675004, 31.628984410209988 ], [ -8.00258435016886, 31.629345505977 ], [ -8.002796395199573, 31.629499305600273 ], [ -8.002835662797853, 31.629606296492579 ], [ -8.002859223356822, 31.629659791892589 ], [ -8.002906344474757, 31.629686539581062 ], [ -8.002984879671317, 31.629726661099347 ], [ -8.00307912190719, 31.629733348017368 ], [ -8.003181217662718, 31.629726661099347 ], [ -8.003401116213087, 31.629820277908031 ], [ -8.003733858117105, 31.630010280990067 ] ] } },...
This is what I could do for the first select :
$('#selectLine').on('change', function() {
console.log("chosen_line= " + this.value);
chosen_line = this.value;
filterLines();
if (chosen_line= 'L1'){
L.geoJSON(lignemarrakech, {
filter: function(feature, layer) {
return feature.properties.ligne='L1';
}
}).addTo(mymap);
}
else{
}
});
Beside the fact that it's not generalised, it doesn't work as intended (show only L1 when L1 is selected), it just display all the lines again on top.
if (chosen_line= 'L1')
should be
if (chosen_line == 'L1')

Importing GeoJSON into MongoDB

I'm just trying out some stuff with GeoJSON's by figuring out how to import them into MongoDB. Here is a snippet of the JSON data that I'm trying to import:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"source": "© GeoBasis-DE / BKG 2013 (Daten verändert)",
"features": [
{
"type": "Feature",
"properties": {
"RS": "051580004004",
"DES": "Stadt",
"GEN": "Erkrath",
"EWZ_M": 20929,
"EWZ_W": 22883,
"SHAPE_AREA": 26441754.911268
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
6.88238674728364,
51.24166234476658
],
[
6.881975279589705,
51.23757802188462
],
[
6.891756626701045,
51.23929422878399
],
[
6.903180857084263,
51.23707789012778
],
[
6.908259412237354,
51.24206723420193
],
[
6.918977412277895,
51.24041847413137
],
[
6.926662904165135,
51.24253596694298
],
[
6.937590326994132,
51.23093391057761
],
[
6.94295724765998,
51.2299892278866
],
[
6.939417113978845,
51.227341409094315
],
[
6.951333593915336,
51.22740615125517
],
[
6.950253526637431,
51.224656373607054
],
[
6.955765647972802,
51.22167757046095
],
[
6.964606469495985,
51.22457614378809
],
[
6.975480633820623,
51.223046741435965
],
[
6.973404761769492,
51.22089502422256
],
[
6.980214287180364,
51.21998473062592
],
[
6.982741542191386,
51.214167748907066
],
[
6.994804384322499,
51.215655729126155
],
[
6.994747626413055,
51.21224925095131
],
[
6.985928005605313,
51.20743852466108
],
[
6.98828820437737,
51.20347146797952
],
[
6.944606700602787,
51.198908942197114
],
[
6.943253716046289,
51.19637267292547
],
[
6.937329237188316,
51.19846226521679
],
[
6.932905519738434,
51.194111787062006
],
[
6.907388585403191,
51.19594285093562
],
[
6.903803051706552,
51.20313804926765
],
[
6.910551758112292,
51.21099091708282
],
[
6.878932812936605,
51.21491884904021
],
[
6.872564112818471,
51.219486802570856
],
[
6.873244797535404,
51.226155851357795
],
[
6.878496898776498,
51.22869664243089
],
[
6.872981880136851,
51.232966792978665
],
[
6.871618163754948,
51.24209470504303
],
[
6.88238674728364,
51.24166234476658
]
]
]
}
},
{
"type": "Feature",
"properties": {
"RS": "051160000000",
"DES": "Stadt",
"GEN": "Mönchengladbach",
"EWZ_M": 123662,
"EWZ_W": 131172,
"SHAPE_AREA": 170540962.920759
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
.....
.....
I want now to import this data into my MongoDB instance. When I tried the following, I ran into some errors:
mongoimport --db /Users/joe/mongodb-data -c points --file "gemeinden_simplify200.geojson" --jsonArray
connected to: 127.0.0.1
assertion: 13106 nextSafe(): { $err: "Invalid ns [/Users/joe/mongodb-data.points]", code: 16256 }
I think you need to try-
mongoimport --db test -c points --file "gemeinden_simplify200.geojson" --jsonArray
Here test is database name.

MongoDB: "Malformed geo query" with $geoIntersect on a polygon

I have events in a collection, each containing a desired location, set a GeoJSON Polygon.
I also have service providers in another collection, also with a GeoJSON Polygon, indicating the area where they can deliver.
For a given service provider, I'm trying to list all the events that are in a compatible area.
However, I get this error:
Malformed geo query: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [ [ [ -31.59327575763251, 115.8574693000001 ], [ -31.59676306691357, 115.9162469300458 ], [ -31.60715789289806, 115.9738935747774 ], ...
My Polygons are closed (first and last coords are identical), so that's not coming from there.
Here are the events:
Events:
[
{
"_id": "5237d4bd9899e67c0e000004",
"area": "area",
"authtoken": "e3bf38ff9a1f132a7cc86ea045cf3951",
"budget": 1800,
"creation_date": {
"sec": 1379390653,
"usec": 0
},
"description": "Banana banana banana banana banana banana banana banana bananaaaaaaaaaaaaaaaaa!!!",
"ends": {
"sec": 1383343200,
"usec": 0
},
"geo": {
"type": "Polygon",
"coordinates": [
[
[
-33.740154878816,
150.9241267
],
[
-33.741901642057,
150.95422987078
],
[
-33.747108144055,
150.98375074498
],
[
-33.755673663135,
151.01211808166
],
[
-33.767432456928,
151.03878255704
],
[
-33.782156913661,
151.0632272399
],
[
-33.799561885809,
151.08497749366
],
[
-33.819310129033,
151.10361012448
],
[
-33.841018749004,
151.11876160464
],
[
-33.819310129033,
150.74464327552
],
[
-33.799561885809,
150.76327590634
],
[
-33.782156913661,
150.7850261601
],
[
-33.767432456928,
150.80947084296
],
[
-33.755673663135,
150.83613531834
],
[
-33.747108144055,
150.86450265502
],
[
-33.741901642057,
150.89402352922
],
[
-33.740154878816,
150.9241267
]
]
]
},
"guests": 180,
"loc": {
"city": "Liverpool",
"state": "NSW",
"country": "AU",
"lat": -33.9200192,
"lng": 150.9241267,
"timezone": "Australia\/Sydney"
},
"name": "Bananaaaaaaaa!",
"services": [
{
"creation_date": {
"sec": 1379390661,
"usec": 0
},
"data": {
"name": "Bananaaaaaaaaaaa!!!!!",
"request": "Banana!",
"description": " banana banana banana banana banana banana banana banana banana",
"budget": "1400"
},
"event": "aaca6751-75b2-4cd9-a76a-99d4ee56fb6a",
"service": "a9fe4bd0-d4f2-4f2c-8b81-4899ac19c44f",
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "15ceedd5-608b-48e4-8a05-b60ee4c4c5bb",
"type": "Food & Drinks"
}
],
"starts": {
"sec": 1383300000,
"usec": 0
},
"type": {
"_id": "521666701e6accf7afc1a0e3",
"icon": "uploads\/event-types\/icon-3891a6f4-760f-43f2-ab3a-a51c61fd8d0d.png?rnd=0.6323562911552134",
"name": "Meeting",
"uuid": "3891a6f4-760f-43f2-ab3a-a51c61fd8d0d"
},
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "aaca6751-75b2-4cd9-a76a-99d4ee56fb6a",
"within": {
"km": 20,
"addr": "liverpool, nsw"
}
},
{
"_id": "5237d50a9899e67c0e000005",
"area": "area",
"authtoken": "e3bf38ff9a1f132a7cc86ea045cf3951",
"budget": 1800,
"creation_date": {
"sec": 1379390730,
"usec": 0
},
"description": "Boo!!!",
"ends": {
"sec": 1383285600,
"usec": 0
},
"geo": {
"type": "Polygon",
"coordinates": [
[
[
-32.086853059112,
116.0081156
],
[
-32.088163946126,
116.03027505189
],
[
-32.092071203806,
116.05200507521
],
[
-32.098499107954,
116.07288445521
],
[
-32.1073230624,
116.09250825588
],
[
-32.118371984525,
116.11049558927
],
[
-32.131431584032,
116.12649694645
],
[
-32.146248475035,
116.14020095399
],
[
-32.330851613336,
116.10195418015
],
[
-32.289125251893,
115.86992020233
],
[
-32.2721989398,
115.86018847275
],
[
-32.254291845681,
115.85334073219
],
[
-32.235752983166,
115.84950870874
],
[
-32.098499107954,
115.94334674479
],
[
-32.092071203806,
115.96422612479
],
[
-32.088163946126,
115.98595614811
],
[
-32.086853059112,
116.0081156
]
]
]
},
"guests": 700,
"loc": {
"city": "Byford",
"state": "WA",
"country": "AU",
"zipcode": "6122",
"lat": -32.2217513,
"lng": 116.0081156,
"timezone": "Australia\/Perth"
},
"name": "Halloween",
"services": [
{
"creation_date": {
"sec": 1379390749,
"usec": 0
},
"data": {
"name": "Badass lighting",
"description": "Boom! I want that!",
"type": [
"Furniture",
"Lighting\/lasers",
"LED screens",
"Dancefloor"
],
"budget": "800"
},
"event": "a6c28e98-b647-4224-8bff-f0b7e5c453c3",
"service": "4d4f370e-5780-4794-b988-df9d7a9ed644",
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "217c4896-a986-4c0a-a6c7-46c9aa74cf27",
"type": "Equipment Hire"
}
],
"starts": {
"sec": 1383256800,
"usec": 0
},
"type": {
"_id": "521666701e6accf7afc1a0e3",
"icon": "uploads\/event-types\/icon-3891a6f4-760f-43f2-ab3a-a51c61fd8d0d.png?rnd=0.6323562911552134",
"name": "Meeting",
"uuid": "3891a6f4-760f-43f2-ab3a-a51c61fd8d0d"
},
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "a6c28e98-b647-4224-8bff-f0b7e5c453c3",
"within": {
"km": 15,
"addr": "byford"
}
}
]
And here is my query object:
db.events.find({
"geo": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
-31.593275757633,
115.8574693
],
[
-31.596763066914,
115.91624693005
],
[
-32.087153956437,
116.25114123685
],
[
-32.13233852915,
116.22535569049
],
[
-32.174036124673,
116.19236795072
],
[
-32.304841443191,
115.76898154226
],
[
-32.290955790481,
115.7119268346
],
[
-31.841206471153,
115.45474133646
],
[
-31.794742552646,
115.4770638401
],
[
-31.751364552714,
115.50675244396
],
[
-31.711911850434,
115.5432228167
],
[
-31.677146863424,
115.58576206658
],
[
-31.607157892898,
115.74104502522
],
[
-31.596763066914,
115.79869166995
],
[
-31.593275757633,
115.8574693
]
]
]
}
}
}
})
What am I missing?
My query seems to be identical to the official mongodb doc for geo queries.
Thanks in advance!
Your coordinates are specified as [latitude, longitude]. MongoDB requires that you specify coordinates in the opposite order for 2dsphere geometry: [longitude, latitude].
Your query worked for me once I swapped the coordinate order.