国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Elasticsearch地理空間之geo_shape

這篇具有很好參考價值的文章主要介紹了Elasticsearch地理空間之geo_shape。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

參考文章:

Elasticsearch地理形狀
Elasticsearch geo_shape地理形狀
ES地理范圍查詢第二講:地理位置信息之geo_shape
ES GEO地理空間查詢java版
Elasticsearch geo_point/geo_shape

一、概述

通常情況,我們使用一個經(jīng)緯度坐標(biāo)表示一個店鋪的位置、一個用戶的位置,經(jīng)緯度在地圖上僅僅表示一個點,有時候需要表示一個區(qū)域,例如:停車場、商場、學(xué)校等等,這些區(qū)域擁有各種各樣的形狀,包括:圓形、多邊形等等。

ES中存儲地理形狀的數(shù)據(jù)類型為: geo_shape

geo_shape支持存儲的常用形狀數(shù)據(jù)如下:

  • 點(point)
  • 圓形(circle)
  • 矩形(envelope)
  • 多邊形 (polygon)

提示: 在geo_shape中,點作為一種特殊的形狀,geo_shape可以存儲一個點。

二、geo_shape數(shù)據(jù)格式

geo_shape支持GeoJsonWKT(Well-Known Text)格式存儲空間形狀數(shù)據(jù)。建議使用wkt。詳細支持的格式描述如下面圖片上描述。
Elasticsearch地理空間之geo_shape

格式說明

  • GeoJson格式數(shù)據(jù)
    GeoJson格式參考官方網(wǎng)站:https://geojson.org/
    在es中則只需要存儲其geometry的屬性值即為geo_shape的值。
{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}
  • wkt Well-Known Text (WKT)
POINT (-77.03653 38.897676) 
LINESTRING (-77.03653 38.897676,-77.009051 38.889939) 
POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0)) 
MULTIPOINT (102.0 2.0, 103.0 2.0) 
MULTILINESTRING ((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0),(100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8)) 
MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2))) 
GEOMETRYCOLLECTION (POINT (100.0 0.0), LINESTRING (101.0 0.0, 102.0 1.0)) 
BBOX (100.0, 102.0, 2.0, 0.0)

存儲示例

  1. 定義geo_shape類型映射
PUT /example
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape" // 定義location字段類型為geo_shape
            }
        }
    }
}
  1. 存儲一個點
POST /example/_doc
{
    "location" : {
        "type" : "point", // 存儲的圖形類型為:point,表示存儲一個坐標(biāo)點
        "coordinates" : [-77.03653, 38.897676] // 坐標(biāo)點格式: [經(jīng)度, 緯度]
    }
}

POST /example/_doc
{
    "location" : "POINT (-77.03653 38.897676)"
}

  1. 存儲一個多邊形
POST /example/_doc
{
  "location": {
    "type": "polygon", // 存儲的圖形類型為: polygon,表示一個多邊形
    "coordinates": [ // 支持多個多邊形
      [ // 第一個多邊形,多邊形由下面的坐標(biāo)數(shù)組組成。
        [100, 0], // 第一個坐標(biāo)點,坐標(biāo)格式: [經(jīng)度, 緯度]
        [101, 0],
        [101, 1],
        [100, 1],
        [100, 0] // 最后一個坐標(biāo)點,要跟第一個坐標(biāo)點相同,這樣多邊形才能形成閉合
      ]
    ]
  }
}

POST /example/_doc
{
    "location" : "POLYGON ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0))"
}

Elasticsearch地理空間之geo_shape

三、geo_shape地理形狀搜索

當(dāng)索引的字段類型定義為geo_shape之后,我們就可以通過geo_shape實現(xiàn)圖形搜索。

1. 圖形搜索類型

下面是geo_shape支持的圖形搜索類型:

intersects - 查詢的形狀與索引的形狀有重疊(默認(rèn)), 即圖形有交集則匹配。
disjoint - 查詢的形狀與索引的形狀完全不重疊。
within - 查詢的形狀包含索引的形狀。

2.圖形搜索例子

GET /example/_search
{
    "query":{
        "bool": { // 布爾組合查詢語句
            "must": {
                "match_all": {} // 這里設(shè)置其他查詢條件,直接匹配全部文檔
            },
            "filter": { // 地理信息搜索,通常不參與相關(guān)性計算,所以使用filter包裹起來
                "geo_shape": { // geo_shape搜索語句
                    "location": { // 圖形數(shù)據(jù)存儲在location字段
                    "relation": "within" // 設(shè)置圖形搜索類型,這里設(shè)置為包含關(guān)系
                    "shape": {   //這個可以理解為其實就是geojson的geometry的值
                    	"type": "polygon",  // 設(shè)置圖形類型,各種圖形格式參考geo_shape的數(shù)據(jù)格式支持的圖形類型
                    	"coordinates": [
		                        [
		                            [
		                                104.0396387972344,
		                                30.59613123035072
		                            ],
		                            [
		                                104.0393476378968,
		                                30.59549712177650
		                            ],
		                            [
		                                104.0396387858758,
		                                30.59638313574942
		                            ],
		                            [
		                                104.0396387972344,
		                                30.59613123035072
		                            ]
		                        ]
		                    ]
		                }
                    }
                }
            }
        }
    }
}

四、Java查詢代碼示例

GeoShape Polygon 查詢

  1. 依賴jar包
<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.18.1</version>
</dependency>
<dependency>
    <groupId>org.locationtech.spatial4j</groupId>
    <artifactId>spatial4j</artifactId>
    <version>0.8</version>
</dependency>
  1. 核心代碼示例
MultiPolygonBuilder multiPolygonBuilder = new MultiPolygonBuilder();
String shapeField = "";
for (MultiCondition.GeoPolygon geoPolygon : geoPolygons){
    List<Coordinate> coordinateList = geoPolygon.geoPoints.stream().map(point -> new Coordinate(point.getLon(),point.getLat())).collect(Collectors.toList());
    CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
    PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
    multiPolygonBuilder.polygon(polygonBuilder);
    shapeField = geoPolygon.getShapeField();
}
try {
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(shapeField, multiPolygonBuilder.buildGeometry());
    queryBuilder.filter(geoShapeQueryBuilder);
} catch (IOException e) {
    e.printStackTrace();
}
  1. 代碼示例
package cn.com.example.cdgisdatahandle.config;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.geo.builders.CoordinatesBuilder;
import org.elasticsearch.common.geo.builders.MultiPolygonBuilder;
import org.elasticsearch.common.geo.builders.PolygonBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.GeoShapeQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.WKTReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ElasticsearchDaoImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private DefConfig defConfig;

    public SearchResponse geoShapeQuery(String wkt) {

        String indexName = defConfig.getEs_realtimepeople_index_name();
        String geoShapeField = defConfig.getEs_realtimepeople_geoshape_field();
        String peopleNumField = defConfig.getEs_realtimepeople_peoplenum_field();

        SearchResponse response = null;
        SearchRequest request = new SearchRequest();
        request.indices(indexName);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.trackTotalHits(true);
        //查詢條件
        BoolQueryBuilder boolQueryBuilder = buildWktBoolQueryBuilder(geoShapeField, wkt);
        boolQueryBuilder.must(QueryBuilders.matchAllQuery());
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "小明"));
        //聚合求和
        AggregationBuilder agg = AggregationBuilders.sum("aggSum").field(peopleNumField);
        try {
            searchSourceBuilder.query(boolQueryBuilder).aggregation(agg);
            request.source(searchSourceBuilder);
            response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return response;
    }

    /**
     * 構(gòu)建Wkt條件
     * 參考文章:https://blog.csdn.net/z69183787/article/details/105563778
     *
     * @date 2019/9/26
     */
    private BoolQueryBuilder buildWktBoolQueryBuilder(String queryField, String wkt) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        WKTReader wktReader = new WKTReader();
        try {
            Geometry geom = wktReader.read(wkt);
            GeoShapeQueryBuilder geoShapeQueryBuilder = null;
            if (wkt.startsWith("POLYGON")) {
                Polygon polygon = (Polygon) geom;
                List<Coordinate> coordinateList = Arrays.stream(polygon.getCoordinates()).collect(Collectors.toList());
                CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
                PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
                geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(queryField, polygonBuilder.buildGeometry())
                        .relation(ShapeRelation.WITHIN);
                boolQueryBuilder.filter(geoShapeQueryBuilder);
            } else if (wkt.startsWith("MULTIPOLYGON")) {
                MultiPolygon multiPolygon = (MultiPolygon) geom;
                int num = multiPolygon.getNumGeometries();
                MultiPolygonBuilder multiPolygonBuilder = new MultiPolygonBuilder();
                for (int i = 0; i < num; i++) {
                    Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
                    List<Coordinate> coordinateList = Arrays.stream(polygon.getCoordinates()).collect(Collectors.toList());
                    CoordinatesBuilder coordinates = new CoordinatesBuilder().coordinates(coordinateList);
                    PolygonBuilder polygonBuilder = new PolygonBuilder(coordinates);
                    multiPolygonBuilder.polygon(polygonBuilder);
                }
                geoShapeQueryBuilder = QueryBuilders.geoShapeQuery(queryField, multiPolygonBuilder.buildGeometry())
                        .relation(ShapeRelation.WITHIN);
                boolQueryBuilder.filter(geoShapeQueryBuilder);
            }
        } catch (Exception e) {
            System.out.println("構(gòu)建Wkt條件錯誤" + e.getMessage());
        }
        return boolQueryBuilder;
    }

    public static void main(String[] args) {
        ElasticsearchDaoImpl elasticsearchDao = new ElasticsearchDaoImpl();
        String wkt = "POLYGON((110.20111083984375 25.431803168948832,110.05897521972658 25.32075284544021,110.07476806640624 25.187233664941914,110.643310546875 25.1070518051296,110.62477111816405 25.230721136478365,110.55198669433594 25.446064798199416,110.35491943359375 25.353644304321108,110.21072387695312 25.299647958643703,110.17501831054689 25.312683764022793,110.20111083984375 25.431803168948832))";
        string wkt2 = "MULTIPOLYGON (((102.0 2.0, 103.0 2.0, 103.0 3.0, 102.0 3.0, 102.0 2.0)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))";
        long startCurrentTimeMillis = System.currentTimeMillis();
        elasticsearchDao.geoShapeQuery(wkt);
		elasticsearchDao.geoShapeQuery(wkt2);
        long endCurrentTimeMillis = System.currentTimeMillis();
        System.out.println((endCurrentTimeMillis - startCurrentTimeMillis)/1000 + " s");
    }
}

MultiPolygonBuilder 構(gòu)建兩個多邊形,外多邊形,內(nèi)多邊形,求環(huán)形部分相交數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-405441.html

Coordinate 結(jié)構(gòu)
用一個數(shù)組表示 經(jīng)緯度 坐標(biāo)點:
[lon,lat]
一組坐標(biāo)點放到一個數(shù)組來表示一個多邊形:
[[lon,lat],[lon,lat], ... ]
一個多邊形( polygon )形狀可以包含多個多邊形;第一個表示多邊形的外輪廓,后續(xù)的多邊形表示第一個多邊形內(nèi)部的空洞:
[
  [[lon,lat],[lon,lat], ... ],  # main polygon
  [[lon,lat],[lon,lat], ... ],  # hole in main polygon
  ...
]

到了這里,關(guān)于Elasticsearch地理空間之geo_shape的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Elasticsearch 地理空間搜索 - 遠超 OpenSearch

    Elasticsearch 地理空間搜索 - 遠超 OpenSearch

    作者:來自 Elastic?Nathan_Reese 2021 年,OpenSearch 和 OpenSearch Dashboards 開始作為 Elasticsearch 和 Kibana 的分支。 盡管 OpenSearch 和 OpenSearch Dashboards 具有相似的血統(tǒng),但它們不提供相同的功能。 在分叉時,只能克隆開源許可的功能。 這給 OpenSearch 留下了一部分功能。 自分叉以來,這種

    2024年02月01日
    瀏覽(26)
  • 【Redis】特殊數(shù)據(jù)類型 Geo (地理位置)

    【Redis】特殊數(shù)據(jù)類型 Geo (地理位置)

    除了五中基本的數(shù)據(jù)類型外,Redis還支持兩種特殊的數(shù)據(jù)類型,第一種 Geo (地理位置):用于存儲地理位置相關(guān)的數(shù)據(jù),例如經(jīng)緯度、距離等。第二種 Stream (流):是一個高級的列表類型,支持對列表的批量操作,如添加多個元素、獲取多個元素等。 Redis GEO(Geo Redis)是一個用

    2024年02月15日
    瀏覽(16)
  • Redis GEO地理位置信息的應(yīng)用

    Redis GEO地理位置信息的應(yīng)用

    Redis的GEO操作是一種基于地理位置信息進行操作的功能。它使用經(jīng)度和緯度坐標(biāo)來表示地理位置,支持存儲地理位置信息用來實現(xiàn)諸如附近位置、搖一搖這類依賴于地理位置信息的功能。 1.GEOADD添加位置信息 將一個或多個指定的地理位置(經(jīng)度、緯度、名稱)添加到指定的鍵

    2024年02月08日
    瀏覽(20)
  • Redis GEO 類型與 API 結(jié)合,地理位置優(yōu)化的絕佳實踐

    Redis GEO 類型與 API 結(jié)合,地理位置優(yōu)化的絕佳實踐

    ?? 嗨,您好 ?? 我是 vnjohn,在互聯(lián)網(wǎng)企業(yè)擔(dān)任 Java 開發(fā),CSDN 優(yōu)質(zhì)創(chuàng)作者 ?? 推薦專欄:Spring、MySQL、Nacos、Java,后續(xù)其他專欄會持續(xù)優(yōu)化更新迭代 ??文章所在專欄:MySQL、Redis、業(yè)務(wù)設(shè)計 ?? 我當(dāng)前正在學(xué)習(xí)微服務(wù)領(lǐng)域、云原生領(lǐng)域、消息中間件等架構(gòu)、原理知識 ?? 向我

    2024年02月08日
    瀏覽(19)
  • R語言【paleobioDB】——pbdb_collections_geo():從PBDB獲取根據(jù)地理位置信息篩選的采集號的基本信息

    Package? paleobioDB ?version 0.7.0 paleobioDB 包在2020年已經(jīng)停止更新,該包依賴PBDB v1 API。 可以選擇在Index of /src/contrib/Archive/paleobioDB (r-project.org)下載安裝包后,執(zhí)行本地安裝。 參數(shù)【...】 :可通過 API 調(diào)用的參數(shù)。所有可用參數(shù)詳見?PBDB Data Service: Single fossil collection。 根據(jù)參數(shù)發(fā)

    2024年01月22日
    瀏覽(20)
  • 285個地級市空間權(quán)重矩陣(空間鄰接、地理距離、經(jīng)濟距離、經(jīng)濟地理嵌套矩陣)

    285個地級市空間權(quán)重矩陣(空間鄰接、地理距離、經(jīng)濟距離、經(jīng)濟地理嵌套矩陣)

    285個地級市空間權(quán)重矩陣(空間鄰接、地理距離、經(jīng)濟距離、經(jīng)濟地理嵌套矩陣) 1、范圍:285個地級市 2、數(shù)據(jù)包括:包括空間鄰接矩陣、空間地理距離矩陣、空間經(jīng)濟距離矩陣、空間經(jīng)濟地理嵌套矩陣 其中空間經(jīng)濟距離矩陣根據(jù)2003-2019年人均GDP得到 3、指標(biāo)說明: 空間權(quán)

    2024年02月16日
    瀏覽(18)
  • 地理空間分析12——地理位置數(shù)據(jù)隱私與安全

    在數(shù)字化時代,地理位置數(shù)據(jù)成為了眾多應(yīng)用程序和服務(wù)不可或缺的一部分。從導(dǎo)航應(yīng)用到社交媒體,從廣告定位到城市規(guī)劃,地理位置數(shù)據(jù)的應(yīng)用范圍廣泛。然而,這些數(shù)據(jù)的收集和使用也引發(fā)了廣泛的隱私和安全擔(dān)憂。本文將探討地理位置數(shù)據(jù)隱私的挑戰(zhàn)和重要性,并介

    2024年03月19日
    瀏覽(28)
  • matlab做經(jīng)濟地理、地理距離、經(jīng)濟距離空間權(quán)重矩陣

    matlab做經(jīng)濟地理、地理距離、經(jīng)濟距離空間權(quán)重矩陣

    首先講下地理加權(quán)空間權(quán)重矩陣: 該矩陣的經(jīng)濟含義是通過不同點的坐標(biāo)系之間的距離遠近來衡量兩地之間的關(guān)系重要程度,當(dāng)兩點之間距離較遠,所占的權(quán)重越低,而距離越近,權(quán)重越高。故操作如下: 首先需要導(dǎo)入坐標(biāo)數(shù)據(jù): A=csvread(\\\'JWD.csv\\\',1,0); % JWD.csv是文件名,csvrea

    2023年04月12日
    瀏覽(24)
  • Elasticsearch ES實現(xiàn)GEO位置搜索

    ES實現(xiàn)GEO位置搜索 Elasticsearch-7.15.2 附近查詢,也叫做距離查詢(geo_distance):查詢到指定中心點小于某個距離值的所有文檔。 創(chuàng)建索引 (my_geo),直接設(shè)置mapping GEO字段的創(chuàng)建:添加一個字段location,類型為 geo_point。 GEO類型的字段是不能使用動態(tài)映射自動生成的,我們需要在創(chuàng)

    2024年01月16日
    瀏覽(26)
  • kaggle學(xué)習(xí)筆記-情感和地理空間分析

    kaggle學(xué)習(xí)筆記-情感和地理空間分析

    秘魯食品評論中的情緒和地理空間分析 自然語言處理 (NLP) 是人工智能的一個分支,致力于讓計算機能夠像人類一樣理解文本和口語單詞。 另一方面,地理空間分析是對圖像、GPS、衛(wèi)星攝影和歷史數(shù)據(jù)的收集、顯示和操作,這些數(shù)據(jù)以地理坐標(biāo)明確描述,或以街道地址、郵政

    2024年02月16日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包