Skip to content

Commit

Permalink
Merge pull request #113 from pedro-andrade-inpe/master
Browse files Browse the repository at this point in the history
new functionality and small updates
  • Loading branch information
rafapereirabr committed Apr 12, 2020
2 parents 9c8cbf9 + 8d55ecd commit d07906c
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 13 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ Imports:
Rcpp,
units,
sf,
sp,
rgdal,
sfheaders,
lwgeom,
utils,
raster,
pbapply
RoxygenNote: 7.0.2
VignetteBuilder: knitr
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(append_height)
export(cpp_snap_points)
export(filter_by_agency_id)
export(filter_by_shape_id)
Expand All @@ -19,7 +20,7 @@ importFrom(Rcpp,compileAttributes)
importFrom(data.table,"%between%")
importFrom(data.table,":=")
importFrom(data.table,fifelse)
importFrom(lwgeom,st_make_valid)
importFrom(lwgeom,st_geod_length)
importFrom(magrittr,"%>%")
importFrom(stats,na.omit)
importFrom(utils,head)
Expand Down
27 changes: 27 additions & 0 deletions R/append_height.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' @title Add a column with height to GPS data
#' @description Add a column named height to GPS data using a tif data as reference.
#' @param gps A GPS data created from gtfs2gps().
#' @param heightfile The pathname of a tif file with height data.
#' @return The GPS data with a new column named height.
#' @export
#' @examples
#' library(dplyr)
#'
#' fortaleza <- system.file("extdata/fortaleza.zip", package = "gtfs2gps")
#' srtmfile <- system.file("extdata/fortaleza-srtm.tif", package = "gtfs2gps")
#'
#' gtfs <- read_gtfs(fortaleza) %>%
#' filter_week_days() %>%
#' filter_single_trip()
#'
#' fortaleza_gps <- gtfs2gps(gtfs, progress = FALSE) %>% append_height(srtmfile)
append_height <- function(gps, heightfile){
f_gps <- gps

sp::coordinates(f_gps) <- ~shape_pt_lon + shape_pt_lat

myraster <- raster::raster(heightfile)
gps$height <- raster::extract(myraster, f_gps)

return(gps)
}
12 changes: 7 additions & 5 deletions R/gps_as_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#' @title Convert GPS-like data.table to a Simple Feature object
#' @description Convert a GPS data stored in a data.table into a Simple Feature.
#' @param gps A data.table with timestamp data.
#' @param crs A Coordinate Reference System. The default value is 4326 (latlong WGS84)
#' @return A simple feature (sf) object.
#' @param crs A Coordinate Reference System. The default value is 4326 (latlong WGS84).
#' @return A simple feature (sf) object with point data.
#' @export
#' @examples
#' library(dplyr)
Expand All @@ -16,10 +16,12 @@
#' poa_gps_sf <- gps_as_sf(poa_gps)
gps_as_sf <- function(gps, crs = 4326){
# convert to sf
temp_gps <- sfheaders::sf_multipoint(gps, x = "shape_pt_lon", y = "shape_pt_lat",
multipoint_id = "shape_id", keep = TRUE)
temp_gps <- sfheaders::sf_point(gps, x = "shape_pt_lon", y = "shape_pt_lat", keep = TRUE)

temp_gps<- temp_gps[, -duplicated(names(temp_gps))]
dup <- duplicated(names(temp_gps))

if(any(dup))
temp_gps<- temp_gps[, -dup]

# add projection
sf::st_crs(temp_gps) <- crs
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ utils::globalVariables(c(".", "%>%", ":="))
#' @importFrom utils head tail object.size
#' @importFrom stats na.omit
#' @importFrom Rcpp compileAttributes
#' @importFrom lwgeom st_make_valid
#' @importFrom lwgeom st_geod_length
#' @useDynLib gtfs2gps, .registration = TRUE
NULL

Expand Down
25 changes: 22 additions & 3 deletions demo/fortaleza.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,38 @@ fortaleza <- read_gtfs(system.file("extdata/fortaleza.zip", package="gtfs2gps"))
names(fortaleza)

ids <- fortaleza$shapes$shape_id %>% unique()
ids

fortaleza <- filter_by_shape_id(fortaleza, ids[1:2])
fortaleza <- filter_by_shape_id(fortaleza, ids[1:4])

for_small_sf <- gtfs_shapes_as_sf(fortaleza)

for_gps <- gtfs2gps(fortaleza, parallel = TRUE)
srtmfile <- system.file("extdata/fortaleza-srtm.tif", package = "gtfs2gps")

for_gps <- gtfs2gps(fortaleza, parallel = TRUE) %>% append_height(srtmfile)

for_gps_sf <- gps_as_sf(for_gps)
for_gps_small <- for_gps[1:60, ]

#write_sf(for_gps_sf, "for-gps2.shp")

for_gps_small <- for_gps

for_gps_small_sf <- gps_as_sf(for_gps_small)

plot(sf::st_geometry(for_gps_small_sf), pch = 20)
plot(sf::st_geometry(for_small_sf), col = "blue", add = TRUE)
plot(sf::st_geometry(for_gps_sf), add = TRUE, pch = 20)
box()

# plotting GPS points with height

srtmraster <- raster::raster(srtmfile)

XYData<- data.frame(
X = for_gps$shape_pt_lon,
Y = for_gps$shape_pt_lat)

plot(srtmraster)
points(XYData, type = "p")

plot(for_gps_sf["height"], pch = 20)
Binary file added inst/extdata/fortaleza-srtm.tif
Binary file not shown.
Binary file modified inst/extdata/fortaleza.zip
Binary file not shown.
31 changes: 31 additions & 0 deletions man/append_height.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/gps_as_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions prep_data/fortaleza-srtm/generate-fortaleza-srtm-simplified.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SRTM data downloaded from
# http://www.dsr.inpe.br/topodata/acesso.php

require(raster)
require(gtfs2gps)

inputDir <- "prep_data/fortaleza-srtm/"

myraster <- paste0(inputDir, "03S39_ZN.tif") %>% raster::raster()

fortaleza <- read_gtfs(system.file("extdata/fortaleza.zip", package="gtfs2gps"))

fortaleza_sf <- gtfs_shapes_as_sf(fortaleza) %>% sf::st_buffer(0.001) %>% sf::as_Spatial()

mm.sub <- raster::crop(myraster, fortaleza_sf)

raster::writeRaster(mm.sub, "inst/extdata/fortaleza-srtm.tif", overwrite = TRUE)

2 changes: 1 addition & 1 deletion prep_data/fortaleza.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gtfs_shapes_as_sf(gtfs) %>%

unique(gtfs$shapes$shape_id)

shape_ids <- paste0("shape", 800:900, "-I")
shape_ids <- paste0("shape", c(800:822, 825:854, 856:900), "-I")

new_gtfs <- filter_by_shape_id(gtfs, shape_ids)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test_append_height.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

test_that("append_height", {
fortaleza <- system.file("extdata/fortaleza.zip", package="gtfs2gps")
srtmfile <- system.file("extdata/fortaleza-srtm.tif", package="gtfs2gps")

gtfs <- read_gtfs(fortaleza) %>%
filter_week_days() %>%
filter_single_trip()

fortaleza_gps <- gtfs2gps(gtfs, progress = FALSE) %>% append_height(srtmfile)

expect_equal(sum(fortaleza_gps$height), 72932, 0.05)

fort <- fortaleza_gps[which(is.na(fortaleza_gps$height)),]

#fott <- gps_as_sf(fort)
#sf::write_sf(fott, "fort-na.shp")
})

0 comments on commit d07906c

Please sign in to comment.