javascriptangularjselasticsearchleafletelasticsearch-geo-shape

Integrating Leaflet Geojson Objects into Elasticsearch Geoshape Query in AngularJs


I have a geoshape query which is running on a local ES instance which is as follows:

httprequest.js with geoshape query

spatialsearch() {
                var _url = 'http://127.0.0.1:9201/_search?';

                var b = {
                    "query": {
                        "bool": {
                            "must": {
                                "match_all": {}
                            },
                            "filter": {
                                "geo_shape": {
                                    "metadata.o2r.spatial.geometry": {
                                        "shape": {
                                            "type": "polygon",
                                            "coordinates": [
                                                [
                                                    [-22.0, 76.0],
                                                    [-27.0, 65.0],
                                                    [-57.0, 65.0],
                                                    [-59.0, 76.0],
                                                    [-22.0, 76.0]
                                                ]
                                            ]
                                        },
                                        "relation": "contains"
                                    }
                                }
                            }
                        }
                    }
                };
                return $http.post(_url, b);
                console.log("hello");

            }

Currently i have hard coded the coordinates into the query but i want to be able to fetch the coordinates from geojson objects that are drawn by user on leaflet map and insert them into the coordinates array in the above function. I am able to display the coordinates from geojson objects in string form in dev-console but i cannot figure out how to save them and retrieve them in above function. Here is how i am making geojson and displaying their coordinates in console.

search.controller.js

leafletData.getMap().then(function(map) {

               leafletData.getLayers().then(function(baselayers) {
                 var drawnItems = baselayers.overlays.draw;

                  map.on('draw:created', function (e) {

                    var layer = e.layer;

                    drawnItems.addLayer(layer);

                    console.log(JSON.stringify(layer.toGeoJSON()));
                  });
               });

           });

Solution

  • i stored the coordinates in a variable and then called it in the http request function coordinates_selected = layer.toGeoJSON();

    Updated Function

    function spatialsearch(coordinates_selected) {
      var coords = coordinates_selected.geometry.coordinates;
      console.log('c', JSON.stringify(coordinates_selected.geometry.coordinates));
      var _url = 'http://localhost:9201/_search?';
    
      var b = {
        "query": {
          "bool": {
            "must": {
              "match_all": {}
            },
            "filter": {
              "geo_shape": {
                "metadata.o2r.spatial.geometry": {
                  "shape": {
                    "type": "polygon",
                    "coordinates": coords
    
                    /* [
                         [-22.0, 76.0],
                         [-27.0, 65.0],
                         [-57.0, 65.0],
                         [-59.0, 76.0],
                         [-22.0, 76.0]
                       ]*/
    
                  },
                  "relation": "within"
                }
              }
            }
          }
        }
      };