forked from apache/sedona
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoParquetAccessor.java
More file actions
90 lines (75 loc) · 3.51 KB
/
Copy pathGeoParquetAccessor.java
File metadata and controls
90 lines (75 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package spark;
import org.apache.sedona.core.spatialOperator.RangeQuery;
import org.apache.sedona.core.spatialOperator.SpatialPredicate;
import org.apache.sedona.core.spatialRDD.SpatialRDD;
import org.apache.sedona.sql.utils.Adapter;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import java.util.List;
public class GeoParquetAccessor {
private final SparkSession session;
private String parquetPath;
public GeoParquetAccessor() {
this.session = new SedonaSparkSession().getSession();
this.parquetPath = "";
}
//Overload with constructor that has Spark session provided
//Use to avoid error - can't have two SparkContext objects on one JVM
public GeoParquetAccessor(SparkSession session, String parquetPath) {
this.session = session;
this.parquetPath = parquetPath;
}
public List<Geometry> selectFeaturesByPolygon(double xmin, double ymax,
double xmax, double ymin,
String geometryColumn) {
//Read the GeoParquet file into a DataFrame
Dataset<Row> insarDF = session.read().format("geoparquet").load(parquetPath);
//Convert the DataFrame to a SpatialRDD
//The second argument to toSpatialRdd is the name of the geometry column.
SpatialRDD<Geometry> insarRDD = Adapter.toSpatialRdd(insarDF, geometryColumn);
// Define the polygon for the spatial query
GeometryFactory geometryFactory = new GeometryFactory();
Coordinate[] coordinates = new Coordinate[] {
new Coordinate(xmin, ymin),
new Coordinate(xmax, ymin),
new Coordinate(xmax, ymax),
new Coordinate(xmin, ymax),
new Coordinate(xmin, ymin) // A closed polygon has the same start and end coordinate
};
Polygon queryPolygon = geometryFactory.createPolygon(coordinates);
// Perform the spatial range query
// This will return all geometries that intersect with the query polygon.
// Alternatives are SpatialPredicate.CONTAINS or SpatialPredicate.WITHIN
SpatialRDD<Geometry> resultRDD = new SpatialRDD<>();
try {
resultRDD.rawSpatialRDD = RangeQuery.SpatialRangeQuery(insarRDD, queryPolygon, SpatialPredicate.INTERSECTS, false);
} catch (Exception e) {
e.printStackTrace();
}
// Collect the results back to the driver
return resultRDD.getRawSpatialRDD().collect();
}
}