gpt4 book ai didi

elasticsearch - 如何在 elasticsearch 中索引 geojson 文件?

转载 作者:行者123 更新时间:2023-11-29 02:56:52 26 4
gpt4 key购买 nike

我正在尝试使用 PYTHON 将 geojson、csv 文件和形状文件形式的空间数据存储到 elasticsearch 中。我是 elasticsearch 的新手,即使按照文档进行操作后,我也无法成功索引它。任何帮助将不胜感激。

示例 geojson 文件:

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"ID_0": 105,
"ISO": "IND",
"NAME_0": "India",
"ID_1": 1288,
"NAME_1": "Telangana",
"ID_2": 15715,
"NAME_2": "Telangana",
"VARNAME_2": null,
"NL_NAME_2": null,
"HASC_2": "IN.TS.AD",
"CC_2": null,
"TYPE_2": "State",
"ENGTYPE_2": "State",
"VALIDFR_2": "Unknown",
"VALIDTO_2": "Present",
"REMARKS_2": null,
"Shape_Leng": 8.103535,
"Shape_Area": 127258717496
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.14429367552918,
19.500257885106404
],
[
79.14582245808431,
19.498859172536427
],
[
79.14600496956801,
19.498823981691853
],
[
79.14966523737327,
19.495821705263914
]
]
]
}
}
]
}

最佳答案

代码

import geojson
from datetime import datetime
from elasticsearch import Elasticsearch, helpers


def geojson_to_es(gj):

for feature in gj['features']:

date = datetime.strptime("-".join(feature["properties"]["event_date"].split('-')[0:2]) + "-" + feature["properties"]["year"], "%d-%b-%Y")
feature["properties"]["timestamp"] = int(date.timestamp())
feature["properties"]["event_date"] = date.strftime('%Y-%m-%d')
yield feature


with open("GeoObs.json") as f:
gj = geojson.load(f)

es = Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])

k = ({
"_index": "YOUR_INDEX",
"_source": feature,
} for feature in geojson_to_es(gj))

helpers.bulk(es, k)

解释

with open("GeoObs.json") as f:
gj = geojson.load(f)

es = Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])

这部分代码加载外部 geojson 文件,然后连接到 Elasticsearch。

    k = ({
"_index": "conflict-data",
"_source": feature,
} for feature in geojson_to_es(gj))

helpers.bulk(es, k)

此处的 () 创建了一个生成器,我们将其提供给 helpers.bulk(es, k)。请记住 _source 是 Elasticsearch 中的原始数据 - IE:我们的原始 JSON。 _index 只是我们要放置数据的索引。您将在此处看到带有 _doc 的其他示例。这是映射类型的一部分,在 Elasticsearch 7.X+ 中不再存在。

def geojson_to_es(gj):

for feature in gj['features']:

date = datetime.strptime("-".join(feature["properties"]["event_date"].split('-')[0:2]) + "-" + feature["properties"]["year"], "%d-%b-%Y")
feature["properties"]["timestamp"] = int(date.timestamp())
feature["properties"]["event_date"] = date.strftime('%Y-%m-%d')
yield feature

geojson 函数使用生成器生成事件。生成器函数将在每次调用后返回并完成 resume at the keywordyield`。在这种情况下,我们正在生成 GeoJSON 特征。在我的代码中,您还可以看到:

date = datetime.strptime("-".join(feature["properties"]["event_date"].split('-')[0:2]) + "-" + feature["properties"]["year"], "%d-%b-%Y")
feature["properties"]["timestamp"] = int(date.timestamp())
feature["properties"]["event_date"] = date.strftime('%Y-%m-%d')

这只是在将数据发送到 Elasticsearch 之前处理 JSON 中的数据的示例。

关键在你的映射文件中你必须有一些标记为geo_pointgeo_shape的东西。这些数据类型是 Elasticsearch 识别地理数据的方式。我的映射文件中的示例:

...
{
"properties": {
"geometry": {
"properties": {
"coordinates": {
"type": "geo_point"
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
...

也就是说,在使用 Python 上传 GeoJSON 数据之前,您需要创建索引,然后使用包含 geo_shapegeo_point 的映射文件像这样的东西:

curl -X PUT "localhost:9200/YOUR_INDEX?pretty"curl -X PUT localhost:9200/YOUR_INDEX/_mapping?pretty -H "Content-Type: application/json"-d @mapping.json

关于elasticsearch - 如何在 elasticsearch 中索引 geojson 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com