Spatial Search with Amazon OpenSearch Service and Visualized It in QGIS
Tags: opensearch, aws, qgis, gis
Published: 2022-08-03 10:54:19

![img](https://day-journal.com/memo/images/try-092_01.png)

### I tried a spatial search with Amazon OpenSearch Service and visualized it with QGIS :tada:  

<br>

The following is a detailed explanation.

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

<br>

## Advance Preparation

Prepare GIS data for use with [Amazon OpenSearch Service](https://aws.amazon.com/opensearch-service/). This time, I prepared a sample of [OpenStreetMap](https://www.openstreetmap.org) data in [QGIS](https://qgis.org) in advance.

I prepared GIS data of points in GeoJSON and then pre-modified GeoJSON to JSON for import.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-003_01.png)

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!

<br>

## Creating a domain

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

Click AWS Management Console → Amazon OpenSearch Service.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_01.png)

Click on "Create domain."
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_02.png)

Set the domain name, deployment type, and autotuning.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_03.png)

Set up a data node and dedicated master node.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_04.png)

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](https://day-journal.com/memo/images/amazon-opensearch-service-001_05.png)

Set encryption → Click "Create".
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_06.png)

Wait a few moments and the environment construction will be completed.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-001_07.png)

You have now completed the creation of your domain!

<br>

## Connect to OpenSearch Dashboards

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

Click on the OpenSearch Dashboards URL.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-002_01.png)

Enter the username and password you set when creating the domain → Click "Log In".
![img](https://day-journal.com/memo/images/amazon-opensearch-service-002_02.png)

You will be connected to OpenSearch Dashboards.
![img](https://day-journal.com/memo/images/amazon-opensearch-service-002_03.png)

You are now connected to OpenSearch Dashboards!

<br>

## Location data registration

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

Connect to OpenSearch Dashboards → Click on "Dev Tools".
![img](https://day-journal.com/memo/images/amazon-opensearch-service-003_02.png)

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

```json
PUT point_geo_sample
{
  "mappings": {
        "properties": {
            "geometry": {
                "type": "geo_shape"
            }
        }
  }
}
```

![img](https://day-journal.com/memo/images/amazon-opensearch-service-003_03.png)

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

```json
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": {}}
....
```

![img](https://day-journal.com/memo/images/amazon-opensearch-service-003_04.png)

This completes the registration of location data!

<br>

## 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](https://day-journal.com/memo/images/amazon-opensearch-service-004_01.png)

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

```json
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"
          }
        }
      }
    }
  }
}
```

The search results matched the QGIS visualization!

```json
{
  "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
            ]
          }
        }
      }
    ]
  }
}
```

![img](https://day-journal.com/memo/images/amazon-opensearch-service-004_02.png)

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

<br>
<br>

Related Articles

{% link https://dev.to/aws-builders/spatial-search-of-amazon-s3-data-with-amazon-athena-and-visualized-it-in-qgis-2b9a %}

{% link https://dev.to/aws-builders/spatial-search-with-amazon-redshift-and-dbeaver-3d2d %}

{% link https://dev.to/aws-builders/using-qgis-and-amazon-auroras-postgresql-postgis-to-display-location-data-5ecm %}

<br>

References
[Amazon OpenSearch Service](https://aws.amazon.com/opensearch-service/)
[QGIS](http://www.qgis.org/ja/site/)