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是广州全市的公交站点.

Geospark从Shapefile中加载RDD_ide

image.png

Geospark从Shapefile中加载RDD_ide_02

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