# Index Polygons and MultiPolygons with logstash

**URL:** https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235
**Category:** Elasticsearch
**Created:** [November 15, 2023, 3:41pm UTC](https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235 "2023-11-15T15:41:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Henri\_L](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henri_l/32/102762_2.png) [@Henri\_L](https://discuss.elastic.co/u/Henri_L)
#### Post date: [November 15, 2023, 3:41pm UTC](https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235/1 "2023-11-15T15:41:21Z")

</div>

Hi

I use ES to index geo-shapes like Polygon and MultiPolygon from CSV via logstash but I can't find a way to make ES ingest properly the datas... I use the following code but it failed for Polygon with holes and MultiPolygons... So it's not very good at all 😉

```auto
input {
    stdin {}
}
filter {
	csv {
		columns => [
			"CODE_IRIS","NOM_IRIS","NOM_COM","INSEE_COM","type","coordinates"
		]
		separator => ","
		skip_empty_columns => true
		}
	if [TYPECOM] == "CODE_IRIS" {
        drop { }
    }
	mutate {
        convert => { "coordinates" => "string" }
    }

	if [type] == "MultiPolygon" {
		ruby {
			code => '
				polygons = event.get("coordinates").split("), (").map { |polygon| polygon.tr("()", "") }

				multi_polygon_coordinates = []
				polygons.each do |polygon|
					polygon_coordinates = polygon.split(", ").map(&:to_f).each_slice(2).to_a
					converted_polygon_coordinates = polygon_coordinates.each_slice(2).map { |slice| slice.each_slice(2).to_a }
					multi_polygon_coordinates << converted_polygon_coordinates
				end

				event.set("[location][type]", "MultiPolygon")
				event.set("[location][coordinates]", multi_polygon_coordinates)
			'
		}
    } else if [type] == "Polygon" {
        ruby {
            code => '
				# Récupérer les coordonnées de la chaîne en supprimant les crochets et en divisant à chaque virgule
				coordinates_string = event.get("coordinates").gsub(/[\[\]]/, "").split(",")

				# Convertir chaque paire de coordonnées (longitude, latitude) en nombres flottants
				coordinates = coordinates_string.each_slice(2).map { |pair| pair.map(&:to_f) }

				# Créer la structure du document pour le champ geo_shape
				event.set("[location][type]", "Polygon")
				event.set("[location][coordinates]", [coordinates])
			'
        }
    }
    
	mutate {
		remove_field => ["type", "coordinates", "message", "host", "@version", "path", "event", "log", "[event][original]", "[log][file][path]", "[event]", "[log]"]
	}

}
output {
		elasticsearch {
			hosts => "http://localhost:9200"
			index => "iris_2"
			document_id => "%{CODE_IRIS}"
			timeout => 30
			workers => 1
			doc_as_upsert => true
			action => "update"
		}
#stdout { codec => rubydebug }
}

```

And of course I have this mapping:

```auto
{
    "mappings": {
      "properties": {
        "location": {
          "type": "geo_shape"
        }
      }
    }
  }

```

If someone can help thanks a lot

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [November 15, 2023, 6:39pm UTC](https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235/2 "2023-11-15T18:39:43Z")

</div>

> [@Henri\_L](#):
>
> I use the following code but it failed

Failed how? Do you get an error message? Also, if you add `output { stdout { codec => rubydebug } }` then what does a failing event look like?

---

<div class="post-metadata">

### Author: ![Henri\_L](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henri_l/32/102762_2.png) [@Henri\_L](https://discuss.elastic.co/u/Henri_L)
#### Post date: [November 15, 2023, 6:48pm UTC](https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235/3 "2023-11-15T18:48:49Z")

</div>

Finally simple is better, here is the working code, just a JSON.parse...:

```auto
mutate {
        add_field => {
            "[location][type]" => "%{type}"
        }
    }
	ruby {
		code => "
			require 'json'
			event.set('[location][coordinates]', JSON.parse(event.get('coordinates')))
		"
	}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 13, 2023, 6:49pm UTC](https://discuss.elastic.co/t/index-polygons-and-multipolygons-with-logstash/347235/4 "2023-12-13T18:49:44Z")

</div>

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