Geopandas 数据框到 GeoJSON 到 Elasticsearch 索引?

Geopandas dataframe to GeoJSON to Elasticsearch index?

我有一个与 this question 相关的问题:我对 python 比较陌生,现在已经开始在 Kibana 中进行可视化,我是全新的(如,我以前从未使用过它)。现在我有一个像这样的 pandas datafram geoseries:

    ID      Geometry
0   9417    POLYGON ((229611.185 536552.731, 229611.100 53...
1   3606    POLYGON ((131122.280 460609.117, 131108.312 46...
2   1822    POLYGON ((113160.653 517762.384, 113169.755 51...
3   7325    POLYGON ((196861.725 470370.632, 196869.990 47...
4   9258    POLYGON ((201372.387 579807.340, 201373.195 57...

我想在 kibana 中用这些多边形创建地图,但我真的不知道怎么做。我已经阅读了关于 elasticsearch 和 Whosebug 的不同部分,但我无法将正确的部分组合在一起。问题是,在我们的项目中,我们希望在 python 中导入数据,对其进行一些预处理,然后将其导出到 kibana。所以有一个 Python - GeoJSON - Elasticsearch [7.6] 过程,我发现的所有文献都不包括所有这 3 个资产,所以我不确定如何继续。

我也确实尝试将文件保存为 GeoJSON,然后通过 Kibana 仪表板将其导入,在地图可视化中如 this instruction 所说。当我导入数据时,它不会为我的文件提供索引,因此不会可视化我的任何数据。

我确实读过如何无法为整个多边形编制索引,但我应该将其拆分为坐标。我的问题是我无法在 python 中找到执行此操作的好方法。我还读到 Elasticsearch 中的索引应该具有正确的地理索引映射。但是,我又一次陷入了从 python 创建此地理映射的困境。

有人可以帮助我吗:)?

这应该让你开始:

  1. 导入并初始化
import shapely.geometry
import geopandas
from elasticsearch import Elasticsearch
import json

es = Elasticsearch(['http://localhost:9200'])
geoindex = None
  1. 获取或创建索引(+映射,如果需要)
try:
    geoindex = es.indices.get('geoindex')
except Exception:
    geoindex = es.indices.create('geoindex', {
        "mappings": {
            "properties": {
                "polygon": {
                    "type": "geo_shape",
                    "strategy": "recursive"
                }
            }
        }
    })

  1. 转储为 json 并加载回字典(受 启发;我怀疑一定有更简洁的方法)
shapely_polygon = shapely.geometry.Polygon([(0, 0), (0, 1), (1, 0)])
geojson_str = geopandas.GeoSeries([shapely_polygon]).to_json()
  1. 迭代并同步到 ES
for feature in json.loads(geojson_str)['features']:
    es.index('geoindex', { "polygon": {
        "type": "polygon",
        "coordinates": feature['geometry']['coordinates']
    }}, id=feature['id'])
  1. 验证
count = es.count({}, 'geoindex')
print(count)
  1. 可视化