DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Cover image for Spatial Search with Amazon OpenSearch Service and Visualized It in QGIS
Yasunori Kirimoto for AWS Heroes

Posted on

Spatial Search with Amazon OpenSearch Service and Visualized It in QGIS

img

I tried a spatial search with Amazon OpenSearch Service and visualized it with QGIS ๐ŸŽ‰

The following is a detailed explanation.

  • Advance Preparation
  • Create a domain
  • Connect to OpenSearch Dashboards
  • Register location data
  • Spatial Search

Advance Preparation

Prepare GIS data for use with Amazon OpenSearch Service. This time, I prepared a sample of OpenStreetMap data in QGIS in advance.

I prepared GIS data of points in GeoJSON and then pre-modified GeoJSON to JSON for import.
img

I have registered this sample data on GitHub, so please feel free to use it.

https://github.com/dayjournal/data/tree/main/try-092

Now you're all set with your preliminary GIS data!

Creating a domain

This is how to create a domain in Amazon OpenSearch Service.

Click AWS Management Console โ†’ Amazon OpenSearch Service.
img

Click on "Create domain."
img

Set the domain name, deployment type, and autotuning.
img

Set up a data node and dedicated master node.
img

Configure network, fine-grained access control, and access policy.
Enter user name and password to create user.
The network was set up with public access for this demonstration.
img

Set encryption โ†’ Click "Create".
img

Wait a few moments and the environment construction will be completed.
img

You have now completed the creation of your domain!

Connect to OpenSearch Dashboards

This is how to connect to OpenSearch Dashboards with Amazon OpenSearch Service.

Click on the OpenSearch Dashboards URL.
img

Enter the username and password you set when creating the domain โ†’ Click "Log In".
img

You will be connected to OpenSearch Dashboards.
img

You are now connected to OpenSearch Dashboards!

Location data registration

This is how to register location data with Amazon OpenSearch Service.

Connect to OpenSearch Dashboards โ†’ Click on "Dev Tools".
img

Set the index for location data. In this case, we used "geo_shape".

PUT point_geo_sample
{
  "mappings": {
        "properties": {
            "geometry": {
                "type": "geo_shape"
            }
        }
  }
}
Enter fullscreen mode Exit fullscreen mode

img

Import location data. The original GeoJSON data is pre-modified to JSON for import.

POST point_geo_sample/_bulk
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n260423690", "name": "ๆ–‡ๆ•™ๅ ‚ๆ›ธๅบ—", "shop": "books" }, "geometry": { "type": "Point", "coordinates": [ 139.7577071, 35.6678306 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n260424181", "name": "LABI", "shop": "electric" }, "geometry": { "type": "Point", "coordinates": [ 139.757034800000014, 35.6671262 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n260424208", "name": "ใ‚ญใƒ ใƒฉใƒค", "shop": "electric" }, "geometry": { "type": "Point", "coordinates": [ 139.7577643, 35.665613 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n261024380", "name": "ใ‚ฝใƒ•ใƒˆใƒใƒณใ‚ฏใ‚ทใƒงใƒƒใƒ—", "shop": "mobile_phone" }, "geometry": { "type": "Point", "coordinates": [ 139.7773429, 35.6772184 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n410500414", "name": "ใƒŸใƒ‹ใ‚นใƒˆใƒƒใƒ—", "shop": "convenience" }, "geometry": { "type": "Point", "coordinates": [ 139.745115, 35.7081941 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n417434047", "name": "Inplexe", "shop": "accessories" }, "geometry": { "type": "Point", "coordinates": [ 139.7414085, 35.7005685 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n439115834", "name": "ใƒ•ใ‚ฉใƒซใ‚ฏใ‚นใƒฏใƒผใ‚ฒใƒณๅ…ญๆœฌๆœจ", "shop": "car" }, "geometry": { "type": "Point", "coordinates": [ 139.7359042, 35.665522 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n474605269", "name": "Family Mart", "shop": "convenience" }, "geometry": { "type": "Point", "coordinates": [ 139.7499649, 35.6692143 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n474605283", "name": "ใ‚ปใƒ–ใƒณ-ใ‚คใƒฌใƒ–ใƒณ", "shop": "convenience" }, "geometry": { "type": "Point", "coordinates": [ 139.7540003, 35.668082 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n474605289", "name": "Lawson", "shop": "convenience" }, "geometry": { "type": "Point", "coordinates": [ 139.754741599999988, 35.6676069 ] } }
{"index": {}}
{ "type": "Feature", "properties": { "full_id": "n559359769", "name": "LAWSON ้Š€ๅบง๏ผ–ไธ็›ฎๅบ—", "shop": "convenience" }, "geometry": { "type": "Point", "coordinates": [ 139.7657173, 35.6687584 ] } }
{"index": {}}
....
Enter fullscreen mode Exit fullscreen mode

img

This completes the registration of location data!

Spatial Search

Finally, we will show you how to perform a spatial search with Amazon OpenSearch Service.

For this spatial search, we will search for point data in the bbox as shown below. In advance, visualize the data in QGIS and prepare the latitude and longitude of the upper left and lower right points you want to search.
img

Specify the conditions for spatial search and enter the latitude and longitude of the upper left and lower right.

POST point_geo_sample/_search
{
  "query":{
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_shape": {
          "geometry": {
            "shape": {
              "type": "envelope",
              "coordinates" : [[139.7802, 35.6781], [139.7878, 35.6724]]
            },
              "relation": "within"
          }
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The search results matched the QGIS visualization!

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "point_geo_sample",
        "_type" : "_doc",
        "_id" : "mdQ_54EBMGUV534vZahT",
        "_score" : 1.0,
        "_source" : {
          "type" : "Feature",
          "properties" : {
            "full_id" : "n1892534950",
            "name" : "ใ‚ปใƒ–ใƒณ-ใ‚คใƒฌใƒ–ใƒณ",
            "shop" : "convenience"
          },
          "geometry" : {
            "type" : "Point",
            "coordinates" : [
              139.7819038,
              35.675297
            ]
          }
        }
      },
      {
        "_index" : "point_geo_sample",
        "_type" : "_doc",
        "_id" : "yNQ_54EBMGUV534vZa1Z",
        "_score" : 1.0,
        "_source" : {
          "type" : "Feature",
          "properties" : {
            "full_id" : "n3758524951",
            "name" : "ใ‚ปใƒ–ใƒณ-ใ‚คใƒฌใƒ–ใƒณ",
            "shop" : "convenience"
          },
          "geometry" : {
            "type" : "Point",
            "coordinates" : [
              139.7859394,
              35.6769875
            ]
          }
        }
      },
      {
        "_index" : "point_geo_sample",
        "_type" : "_doc",
        "_id" : "KtQ_54EBMGUV534vZadN",
        "_score" : 1.0,
        "_source" : {
          "type" : "Feature",
          "properties" : {
            "full_id" : "n1215847202",
            "name" : "NATURALใ€€LAWSON",
            "shop" : "convenience"
          },
          "geometry" : {
            "type" : "Point",
            "coordinates" : [
              139.781879,
              35.6744447
            ]
          }
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

img

By using the Amazon OpenSearch Service, you can search the imported data spatially!

Related Articles

References
Amazon OpenSearch Service
QGIS

Top comments (0)

๐ŸŒš Browsing with dark mode makes you a better developer.

It's a scientific fact.