Skip to content

Commit 2a83b9e

Browse files
author
Harry Zhu
committed
add st_join and st_example
1 parent 32fd2ec commit 2a83b9e

File tree

14 files changed

+446
-230
lines changed

14 files changed

+446
-230
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Depends:
1313
R (>= 3.1.2)
1414
Imports:
1515
sparklyr (>= 1.0.0)
16+
RoxygenNote: 6.0.1.9000

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
# Generated by roxygen2: do not edit by hand
2+
13
export(register_gis)
4+
export(st_join)
5+
export(st_pit_example)
6+
export(st_plg_example)
27
import(sparklyr)

R/st_example.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#' Spark polygons example.
2+
#'
3+
#' @examples
4+
#' \dontrun{
5+
#' library(sparklyr)
6+
#' sc <- spark_connect(master = "local")
7+
#' register_gis(sc)
8+
#' st_plg_example(sc)
9+
#' }
10+
#' @export
11+
st_plg_example <- function(sc) {
12+
polygons <- read.table(system.file(package="geospark","examples/polygons.txt"), sep="|", col.names=c("area","geom"))
13+
polygons_wkt <- copy_to(dest = sc, df = polygons, name = "geospark_example_polygons", overwrite = T) %>%
14+
mutate(geom = st_geomfromwkt(geom))
15+
return(polygons_wkt)
16+
}
17+
18+
#' Spark points example.
19+
#'
20+
#' @examples
21+
#' \dontrun{
22+
#' library(sparklyr)
23+
#' sc <- spark_connect(master = "local")
24+
#' register_gis(sc)
25+
#' st_pit_example(sc)
26+
#' }
27+
#' @export
28+
st_pit_example <- function(sc) {
29+
points <- read.table(system.file(package="geospark","examples/points.txt"), sep="|", col.names=c("city","state","geom"))
30+
points_wkt <- copy_to(dest = sc, df = points, name = "geospark_example_points", overwrite = T) %>%
31+
mutate(geom = st_geomfromwkt(geom))
32+
return(points_wkt)
33+
}

R/st_join.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' @title spatial join
2+
#' @name st_join
3+
#' @import sparklyr
4+
#' @param x a spark spatial data frame
5+
#' @param y a spark spatial data frame
6+
#' @description
7+
#' st_join: spatial join
8+
#'
9+
#' @details alternative values for argument join are: ST_Contains, ST_Intersects, ST_Within, ST_Equals, ST_Crosses, ST_Touches, ST_Overlaps, ST_Distance
10+
#'
11+
#' @return a spark spatial data frame, joined based on geometry
12+
#'
13+
#' @examples
14+
#' \dontrun{
15+
#'
16+
#' library(dplyr)
17+
#' polygons <- read.table(system.file(package="geospark","examples/polygons.txt"), sep="|", col.names=c("area","geom"))
18+
#' points <- read.table(system.file(package="geospark","examples/points.txt"), sep="|", col.names=c("city","state","geom"))
19+
#' polygons_wkt <- copy_to(sc, polygons)
20+
#' points_wkt <- copy_to(sc, points)
21+
22+
#' polygons_wkt <- mutate(polygons_wkt, y = st_geomfromwkt(geom))
23+
#' points_wkt <- mutate(points_wkt, x = st_geomfromwkt(geom))
24+
25+
#' sc_res = st_join(polygons_wkt, points_wkt, join = sql("st_contains(y,x)"))
26+
#' group_by(area, state) %>%
27+
#' summarise(cnt = n())
28+
#' }
29+
#' @export
30+
st_join <- function(x, y, join = NULL) {
31+
full_join(x %>% mutate(dummy_s4pu629cnd=TRUE),
32+
y %>% mutate(dummy_s4pu629cnd=TRUE),
33+
by = "dummy_s4pu629cnd") %>%
34+
filter(join) %>%
35+
select(-dummy_s4pu629cnd)
36+
}

README.Rmd

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ Then, you can join as follows:
118118

119119
``` r
120120
library(dplyr)
121-
polygons_wkt <- mutate(polygons_wkt, y = st_geomfromwkt(geom, "4326"))
122-
points_wkt <- mutate(points_wkt, x = st_geomfromwkt(geom, "4326"))
121+
polygons_wkt <- mutate(polygons_wkt, y = st_geomfromwkt(geom))
122+
points_wkt <- mutate(points_wkt, x = st_geomfromwkt(geom))
123123

124-
sc_res = inner_join(polygons_wkt, points_wkt, by = sql("st_contains(y, x)")) %>%
124+
sc_res = full_join(polygons_wkt %>% mutate(dummy=TRUE),
125+
points_wkt %>% mutate(dummy=TRUE),
126+
by = "dummy") %>%
127+
filter(sql("st_contains(y,x)")) %>%
125128
group_by(area, state) %>%
126-
summarise(cnt = n())
129+
summarise(cnt = n())
127130

128131
sc_res %>%
129132
head()

0 commit comments

Comments
 (0)