Geospark从Shapefile中加载RDD


title: ‘(二)Geospark从Shapefile中加载RDD’
date: 2021-04-29 19:09:48
tags: []
published: true
hideInList: false
#feature: /post-images/(二)Geospark从Shapefile中加载RDD.png
isTop: false


从Shapefile中加载RDD

我们从Shapefile中创建一个Spark的RDD,本次要导入的Shapefile是广州全市的公交站点.

Geospark从Shapefile中加载RDD

从Shapefile中加载RDD

  1. 我们从Shapefile中创建一个Spark的RDD,本次要导入的Shapefile是广州全市的公交站点.

img

image.png

img

image.png

  1. 然后我们初始化一个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);

查询

1. 使用Envelop查询
//Envelop
Envelope rangeQueryWindow = new Envelope(-123.1, -123.2, 49.2, 49.3);
boolean considerBoundaryIntersection = false;// Only return gemeotries fully covered by the window
boolean usingIndex = false;
JavaRDD<Geometry> queryResult = RangeQuery.SpatialRangeQuery(rdd, rangeQueryWindow, considerBoundaryIntersection, usingIndex);
System.out.println(String.format("查询结果总数为: %d",queryResult.count()));
查询结果总数为: 62
2. 使用Geomtery查询

GeoSpark提供了GeomtryFactory来构造Polygon、Line、Point等Geometry。

// Geometry
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);
queryResult = RangeQuery.SpatialRangeQuery(rdd, polygonObject, considerBoundaryIntersection, usingIndex);
System.out.println(String.format("查询结果总数为: %d",queryResult.count()));
查询结果总数为: 62
3. 输出查询结果
// 遍历查询结果
queryResult.foreach(new VoidFunction<Geometry>() {
    @Override
    public 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

参考链接:https://www.jianshu.com/p/4b9e875227aa

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学亮编程手记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值