Is there a way to parse country names to Geospatial Coordinates?

I am attempting to create a map using Kibana. I currently only have the country name as one of my data points but was wondering if there was a way to easily convert it to the Geospatial Coordinates? I am using a Logstash pipeline to ingest the data in Elasticsearch from MongoDB. I looked into GeoIP but it doesn't seem to do what I am looking for.

1 Like

There's a few external APIs out there that you can use, but I don't know a nicely packaged way to do this. Hopefully someone can comment though cause this is definitely something of interest for me! :slight_smile:

Hi @caitlyn.carlisle

Curious when you say Geospatial Coordinates do want a point or the outline / boundary of the country?

Also did you know you can do a join in the map itself if you have the country code in one index and then do a "term join" to the country boundaries in another index.

See here

If you did get a geojson of say the centroids of the countries you could use an enrich processor to add the geo data on ingest.

Just a couple thoughts

Lucene Geo-Gazetteer is an interesting project that appears to do this.

Mordecai may also be of interest to you.

Note, I have not tested either of these projects.

But, since you're only using countries, my earlier suggestions might be out of scope for this.

I like @stephenb's idea of using the enrich processor for this. But countries can be referred to by many different names (e.g. Vietnam, Viet Nam, Socialist Republic of Vietnam, ...). The gazetteer data from Geonames has places names, centroids, and alternative names. Perhaps you can ingest the country names and centroids from geonames into Elasticsearch. I would put the country name and all alternative names into a single keyword field. Then we can use this with an enrich policy to add the locations to your logs at ingest.

I copied and pasted a test from my own Kibana console as an example below.

PUT geonames-countries

PUT geonames-countries/_mapping
{
  "properties": {
    "name": {
      "type": "keyword"
    },
    "location": {
      "type": "geo_point"
    }
  }
}

PUT geonames-countries/_doc/1
{
  "name": ["Vietnam", "Viet Nam", "Socialist Republic of Vietnam"],
  "location": [108.533936,13.370915]
}


GET geonames-countries/_search
{
  "query": {
    "term": {
      "name": {
        "value": "Viet Nam"
      }
    }
  }
}

PUT _enrich/policy/country_centroids
{
  "match": {
    "indices": "geonames-countries",
    "match_field": "name",
    "enrich_fields": ["location"]
  }
}

POST _enrich/policy/country_centroids/_execute

POST _ingest/pipeline/_simulate?verbose=false
{
  "pipeline": {
    "processors": [
      {
        "enrich": {
          "policy_name": "country_centroids",
          "field": "country_name",
          "target_field": "location"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "my-logs-index",
      "_type": "_doc",
      "_id": "foo",
      "_source": {
        "country_name": "Viet Nam"
      }
    }
  ]
}
2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.