Geopandas数据框到GeoJSON到Elasticsearch索引? [英] Geopandas dataframe to GeoJSON to Elasticsearch index?

查看:106
本文介绍了Geopandas数据框到GeoJSON到Elasticsearch索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此问题相关的问题:我是python的新手,现在已经开始在Kibana中进行可视化,而我在Kibana中是全新的(例如,我以前从未使用过它).现在,我有了像这样的pandas datafram地理系列:

I've a question that is related to this question: I'm relatively new to python and now have started to visualize in Kibana, which I'm brand new at (as in, I've never used it before). Now I have a pandas datafram geoseries like this:

    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和Stackoverflow的不同部分,但是我无法将正确的部分组合在一起.事实是,在我们的项目中,我们要使用python导入数据,对其进行一些预处理,然后将其导出到kibana.因此,有一个 Python-GeoJSON-Elasticsearch [7.6] 流程,我发现的所有文献都不包括所有这3种资产,因此我不确定如何进行.

And I would like to create a map with these polygons in kibana but I really don't know how. I've read different parts on elasticsearch and stackoverflow but I can't get the right pieces together. The thing is, that in our project we want to import data in python, preprocess it a bit, and export it to kibana. So there is a Python - GeoJSON - Elasticsearch [7.6] process, and all the literature I found, does not include all these 3 assets so I'm not sure how to proceed.

我也曾尝试将文件另存为GeoJSON,然后通过Kibana仪表板将其导入,如

I also did try to save the file as a GeoJSON and then import it via the Kibana dashboard, in the map visualization like this instruction says. When I import the data, it won't give my file an index and it therefore won't visualize any of my data.

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

I did read about how you can't index a whole polygon but I should split it into coordinates. My problem is that I can't fint a good way to do this in python. Also I read that the index in Elasticsearch should have the right mapping for geo indexing. But again, I get stuck in creating this geo mapping from 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并加载回字典中(受的启发;我怀疑必须有一种更简洁的方法)
  1. Dump as json and load back into a dict (inspired by this; there must be a cleaner way, I suspect)

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. 可视化

这篇关于Geopandas数据框到GeoJSON到Elasticsearch索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆