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空间索引
- GeoSpark提供两种空间索引:Quad-Tree和R-Tree
- 和上一节一样,我们初始化一个SparkContext,并调用GeoSpark的ShapefileReader,将我们的Shape文件导入。
SparkConf conf = new SparkConf();
conf.setAppName("GeoSpark02");
conf.setMaster("local[*]");
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
conf.set("spark.kryo.registrator", "org.datasyslab.geospark.serde.GeoSparkKryoRegistrator");
JavaSparkContext sc = new JavaSparkContext(conf);// Get SpatialRDD
String shapeInputLocation = Learn02.class.getResource("/parks").toString();
SpatialRDD rdd = ShapefileReader.readToGeometryRDD(sc, shapeInputLocation);
构建索引
// 构建索引
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()));
查询结果总数为: 62
3. 输出查询结果
// 遍历查询结果
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 N
KNN临近查询
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系列之三【空间查询篇】
系列目录 带你入门GeoSpark系列之一[环境篇] 带你入门GeoSpark系列之二[Spatial RDD篇] 带你入门GeoSpark系列之三[空间查询篇] 1.空间范围查询( Spatial ...
- Oracle表空间查询及扩充表空间
oralce 表空间查询 SQL> select a.tablespace_name,a.bytes / 1024 / 1024 "sum MB",(a.bytes - b ...
- oracle数据类型查询,Oracle 空间查询, 数据类型为 sdo_geometry
数据:通过arcgis直连数据库,导入测试数据,导入时数据类型选择SDO_GEOMETRY. 测试数据包含点线面,点数据MAP_USER_POINT, 线数据MAP_USER_LINE,面数据MAP_ ...
- mongodb空间查询之查询单位
1.建立空间索引 db.collection.ensureIndex({'geom.coordinates':'2d'}) 2.空间查询 这里遇到的问题是查询的时候会涉及到范围,那么这个范围的单位是什 ...
- cesium 3dtiles 加载本地数据_cesium结合geoserver实现地图空间查询(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- 表空间oracle查询,Oracle表和表空间查询
用户查询 查询和用户相关的数据 创建用户 CREATE USER user IDENTIFIED BY password [DEFAULT TABLESPACE tablespace] [TEMPOR ...
- Hibernate Search 4.2最终发布:支持空间查询
JBoss已宣布发布Hibernate Search 4.2 final. 您可以从Sourceforge下载它或使用Maven构件 . 在新版本中,包含了一些有趣的功能: Hibernate Sea ...
- MYSQL的空间查询(转帖)
SELECT x(location),y(location) FROM frddata.points; 本文将向各位介绍如何使用MySql5.x中的空间数据库,并展示一下它高效的性能(前提是正确使用) ...
- Oracle 空间查询, 数据类型为 sdo_geometry
因网上搜索到的相关资料大部分都是关于sdo_geometry的介绍和以及通过sql语句添加要素,查询要素等等.没有找到存储过程相关的例子,所以只好自己动手啦. 准备 环境:windowsxp系统,安装 ...
最新文章
- 菜鸟也来学习ORACLE(1)_linux下安装oracle 11g
- java与c/c++之间的数据交互-----jni点滴
- meta name id class 标签的区别
- 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1123:图像相似度
- ASP+MSSQL注入工具 web版 beta 3 final release
- BigDecimal.setScale 处理java小数点
- java.util.timertask_java.util.TimerTask翻译
- 学fpga(组合逻辑和时序逻辑)
- 理财产品收益可以保证吗?
- python16-前端之课堂笔记
- C#写的制程能力CPK分析程序
- 切图具体需要切什么内容_【切图】UI设计师要懂得切图技巧
- 截止失真放大电路_Multisim基本放大电路仿真实验
- wamp+php+下载,WAMP(Windows+Apache+Mysql+PHP) 下载配置一条龙
- Google学术搜索镜像网站搜集
- 安卓ttf格式的字体包_【新品】比百思不得姐细一点的小粗黑丨正常大小+小字体...
- 电脑重启后 虚拟机不见了?
- Python有趣的小案例-美国队长盾牌
- 简析客户流失的原因及解决办法
- 45岁,拿到华为20级架构师offer要去吗
热门文章
- APS如何入门1-综述
- rust(65)-rust常用指针
- postgresql数据类型
- vb.net2019- 机器学习ml.net情绪分析(1)
- 【深度学习】这千层transformer让我目瞪口呆
- 【学术相关】人工智能顶会审稿现状—理想中的审稿人vs实际审稿人
- 【机器学习】scikit-learn 1.0 版本重要新特性一览
- 关于最近实践 Bert 的一些坑
- 员外带你读论文:SeqGAN论文分享
- An Invitation to 3-D Vision: From Images to Geometric Models 邀请 3d 视觉从图像的几何模型(免费下载)