Geospark空间查询
title: ‘模板’
date: 2021-01-01 00:00:00
tags: [scala]
published: true
hideInList: false
#feature: /post-images/hello-gridea.png
isTop: false
Geospark空間查詢
Geospark空間查詢
GeoSpark空間索引
構建索引
// 構建索引 boolean buildOnSpatialPartitionedRDD = false; // 如果只需要在做空間分析的時候構建索引,則設置為true rdd.buildIndex(IndexType.QUADTREE, buildOnSpatialPartitionedRDD);使用索引查詢
// 查詢 GeometryFactory geometryFactory = new GeometryFactory(); Coordinate[] coordinates = new Coordinate[5]; coordinates[0] = new Coordinate(-123.1,49.2); coordinates[1] = new Coordinate(-123.1,49.3); coordinates[2] = new Coordinate(-123.2,49.3); coordinates[3] = new Coordinate(-123.2,29.2); coordinates[4] = coordinates[0]; // The last coordinate is the same as the first coordinate in order to compose a closed ring Polygon polygonObject = geometryFactory.createPolygon(coordinates); boolean usingIndex = true; // 使用索引 JavaRDD<Geometry> queryResult = RangeQuery.SpatialRangeQuery(rdd, polygonObject, false, usingIndex); System.out.println(String.format("查詢結果總數為: %d",queryResult.count())); 查詢結果總數為: 623. 輸出查詢結果
// 遍歷查詢結果 queryResult.foreach(new VoidFunction<Geometry>() {@Overridepublic void call(Geometry geometry) throws Exception {System.out.println(geometry);} }); POLYGON ((-123.15566057081632 49.26206733490204, -123.15564728017853 49.26241791476514, -123.15548939905344 49.262415429329856, -123.15550257747702 49.26206484963618, -123.15566057081632 49.26206733490204)) 1 -9999 Kitsilano N POLYGON ((-123.15760176703519 49.261936547646954, -123.15718706338478 49.2619299178749, -123.15719832396375 49.26162160945501, -123.15761313807661 49.26162814910161, -123.15760218456263 49.26192530535148, -123.15760176703519 49.261936547646954)) 2 208 Rosemary Brown Park Kitsilano W 11th Avenue Vine Street N N N ................................. POLYGON ((-123.12325003271694 49.290529597005786, -123.12325184999034 POLYGON ((-123.11921795166444 49.288179012132034, -123.11889234917355 49.28806261407178, -123.11905901714364 49.28781953241384, -123.11954592548769 49.28796238352621, -123.11921795166444 49.288179012132034)) 80 27 Portal Park Downtown W Hastings Street Thurlow Street N N NKNN臨近查詢
KNN臨近查詢用于查詢距指定點最近的K個Geometry,在本案例中我們指定中心點為(-123.1,49.2),我們來查詢距離這個點最近的5個公園的分布。
注意:QTREE索引不支持KNN查詢,所以在使用KNN的時候,可以選擇R-Tree索引。
// 臨近查詢(KNN) rdd.buildIndex(IndexType.RTREE, buildOnSpatialPartitionedRDD); // QTREE不支持KNN查詢 Point pointObject = geometryFactory.createPoint(new Coordinate(-123.1,49.2)); int K = 5; // K Nearest Neighbors List<Geometry> result = KNNQuery.SpatialKnnQuery(rdd, pointObject, K, usingIndex); 距離點(-123.1,49.2)最近的五個公園是: 1: MULTIPOLYGON... 3 141 Tea Swamp Park Mount Pleasant E 15th Avenue Sophia Street N N N 2: POLYGON ... 23 140 Robson Park Mount Pleasant Kingsway St. George Street N Y N 4: POLYGON... 18 138 Major Matthews Park Mount Pleasant W 11th Avenue Manitoba Street N N N 5: POLYGON ... 24 136 Guelph Park Mount Pleasant E 7th Avenue Brunswick Street N N N空間關聯查詢(Spatial Join Query)
空間關聯查詢:創建一個表聯接(類似于SQL語句中的join關聯),其中根據兩層中特性的相對位置,將一個圖層屬性表中的字段追加到另一個圖層層屬性表中。
現在我們有兩個圖層,一個是公園(polygon),另外一個是公園里一些點(point),現在我們要把這些點所在公園的屬性賦給這些點,用到的就是空間關聯查詢。
// 空間關聯查詢 shapeInputLocation = Learn02.class.getResource("/point").toString(); SpatialRDD pointRdd = ShapefileReader.readToGeometryRDD(sc, shapeInputLocation); // analyze方法主要用來計算邊界 pointRdd.analyze(); parkRdd.analyze(); // spark中的分區操作,空間關聯查詢前必須進行 parkRdd.spatialPartitioning(GridType.KDBTREE); pointRdd.spatialPartitioning(parkRdd.getPartitioner());boolean considerBoundaryIntersection = true; // 因為是以點為基礎,因此必須考慮邊界 usingIndex = false; JavaPairRDD<Geometry, HashSet<Geometry>> joinResult = JoinQuery.SpatialJoinQuery(parkRdd, pointRdd, usingIndex, considerBoundaryIntersection); System.out.println("空間關聯查詢結果為:"); joinResult.foreach((kv)->{System.out.println(String.format("{%s}--{%s}", kv._1.getUserData().toString(), kv._2.toString())); });SpatialJoinQuery返回一個類似于Map集合的RDD,其中Key是SpatialJoinQuery中的第一個RDD,也就是我們的pointRDD,Value是一個Set集合,集合里面每個元素都是來自于SpatialJoinQuery中的第二個RDD,也就是parkRDD,在我們這個案例下,我們的key、value都是一一對應的,但是當我們反過來執行JoinQuery.SpatialJoinQuery(pointRdd, parkRdd, usingIndex, considerBoundaryIntersection),那此時key和value就是一對多關系了,因此一個公園里會有很多個點,但是一個點只會屬于一個公園(邊界上的點除外)。
{5}--{[POLYGON... 62 201 English Bay Beach Park West End Beach Avenue Bidwell Street N Y Y]} {10}--{[POLYGON ... 48 110 Hadden Park Kitsilano Ogden Avenue Cypress Street N Y Y]} {13}--{[POLYGON ... 76 24 Marina Square Downtown Bayshore Drive Denman Street N N Y]} {14}--{[POLYGON... 77 11 Cardero Park Downtown Bayshore Drive Cardero Street N N Y]} {3}--{[POLYGON ... 74 206 Stanley Park West End W Georgia Street Chilco Street N Y Y]} {2}--{[POLYGON... 61 -9999 Sunset Beach Park West End Y ]} {0}--{[POLYGON ... 73 -9999 Nelson Park West End Y ]} {1}--{[POLYGON ... 74 206 Stanley Park West End W Georgia Street Chilco Street N Y Y]} {15}--{[POLYGON ... 75 18 Devonian Harbour Park Downtown W Georgia Street Denman Street N N Y]} {19}--{[POLYGON... 35 28 CRAB Park at Portside Downtown E Waterfront Road Main Street Y Y N]} {8}--{[POLYGON ... 73 -9999 Nelson Park West End Y ]} {6}--{[POLYGON ... 62 201 English Bay Beach Park West End Beach Avenue Bidwell Street N Y Y]} {12}--{[POLYGON ... 79 -9999 Harbour Green Park Downtown N ]} {11}--{[POLYGON ... 78 13 Coal Harbour Park Downtown W Hastings Street Broughton Street N N N]}參考鏈接:https://www.jianshu.com/p/458f05244479
總結
以上是生活随笔為你收集整理的Geospark空间查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Geospark从Shapefile中加
- 下一篇: GeosparkViz 可视化