Skip to content

Commit

Permalink
Improves to the code style and adds CONTRIBUTING.md
Browse files Browse the repository at this point in the history
  • Loading branch information
flor14 committed Dec 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 44ffc2a commit fec9b37
Showing 33 changed files with 351 additions and 226 deletions.
47 changes: 47 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Contributing to quadkeyr

This outlines how to propose a change to quadkeyr.
For more detailed info about contributing to this, and other tidyverse packages, please see the
[**development contributing guide**](https://rstd.io/tidy-contrib).

## Fixing typos

You can fix typos, spelling mistakes, or grammatical errors in the documentation directly using the GitHub web interface, as long as the changes are made in the _source_ file.
This generally means you'll need to edit [roxygen2 comments](https://roxygen2.r-lib.org/articles/roxygen2.html) in an `.R`, not a `.Rd` file.
You can find the `.R` file that generates the `.Rd` by reading the comment in the first line.

## Bigger changes

If you want to make a bigger change, it's a good idea to first file an issue and make sure someone from the team agrees that it’s needed.
If you’ve found a bug, please file an issue that illustrates the bug with a minimal
[reprex](https://www.tidyverse.org/help/#reprex) (this will also help you write a unit test, if needed).

### Pull request process

* Fork the package and clone onto your computer. If you haven't done this before, we recommend using `usethis::create_from_github("Fernandez-Lab-WSU/quadkeyr", fork = TRUE)`.

* Install all development dependencies with `devtools::install_dev_deps()`, and then make sure the package passes R CMD check by running `devtools::check()`.
If R CMD check doesn't pass cleanly, it's a good idea to ask for help before continuing.
* Create a Git branch for your pull request (PR). We recommend using `usethis::pr_init("brief-description-of-change")`.

* Make your changes, commit to git, and then create a PR by running `usethis::pr_push()`, and following the prompts in your browser.
The title of your PR should briefly describe the change.
The body of your PR should contain `Fixes #issue-number`.

* For user-facing changes, add a bullet to the top of `NEWS.md` (i.e. just below the first header). Follow the style described in <https://style.tidyverse.org/news.html>.

### Code style

* New code should follow the tidyverse [style guide](https://style.tidyverse.org).
You can use the [styler](https://CRAN.R-project.org/package=styler) package to apply these styles, but please don't restyle code that has nothing to do with your PR.

* We use [roxygen2](https://cran.r-project.org/package=roxygen2), with [Markdown syntax](https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html), for documentation.

* We use [testthat](https://cran.r-project.org/package=testthat) for unit tests.
Contributions with test cases included are easier to accept.

## Code of Conduct

Please note that the quadkeyr project is released with a
[Contributor Code of Conduct](CODE_OF_CONDUCT.md). By contributing to this
project you agree to abide by its terms.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ export(clip)
export(complete_grid_for_polygons)
export(create_qk_grid)
export(create_raster)
export(extract_qk_coord)
export(extract_tile_coord)
export(format_data)
export(grid_to_polygon)
6 changes: 4 additions & 2 deletions R/apply_weekly_lag.R
Original file line number Diff line number Diff line change
@@ -28,8 +28,10 @@ for(i in unique(data$quadkey)){

quadkey_lag <- inter |>
dplyr::group_by(.data$quadkey, .data$time) |>
dplyr::mutate(n_crisis_lag_7 = dplyr::lag(as.numeric(.data$n_crisis), n = 7)) |>
dplyr::mutate(percent_change_7 = ((.data$n_crisis_lag_7 - .data$n_crisis)/.data$n_crisis) * 100)
dplyr::mutate(n_crisis_lag_7 = dplyr::lag(as.numeric(.data$n_crisis),
n = 7)) |>
dplyr::mutate(percent_change_7 = ((.data$n_crisis_lag_7 - .data$n_crisis)/
.data$n_crisis) * 100)

out_data <- rbind(out_data, quadkey_lag)

32 changes: 16 additions & 16 deletions R/create_qk_grid.R
Original file line number Diff line number Diff line change
@@ -32,40 +32,40 @@ create_qk_grid <- function(xmin, xmax, ymin, ymax, level){
}

# Values in Microsoft Bing Tile System Documentation
min_latitude = -85.05112878
max_latitude = 85.05112878
min_longitude = -180
max_longitude = 180
min_latitude <- -85.05112878
max_latitude <- 85.05112878
min_longitude <- -180
max_longitude <- 180


# this variables were defined in the function ground_res
if (ymin < min_latitude || ymax > max_latitude ||
xmin < min_longitude || xmax > max_longitude) {
stop(paste("At least one of the provided coordinates are outside the valid range.",
"Latitude must be between -85.05112878 and 85.05112878.",
"Longitude must be between -180 and 180."))
stop(paste("At least one of the provided coordinates are outside",
"the valid range. Latitude must be between -85.05112878",
"and 85.05112878. Longitude must be between -180 and 180."))
}

# x - Convert lat/long coordinates to tile XY coords
pixs = latlong_to_pixelXY(lat = ymin,
pixs <- latlong_to_pixelXY(lat = ymin,
lon = xmin,
level = level)

tilesmn = pixelXY_to_tileXY(pixelX = pixs$pixelX,
tilesmn <- pixelXY_to_tileXY(pixelX = pixs$pixelX,
pixelY = pixs$pixelY)


# y - Convert lat/long coordinates to tile XY coords
pixs = latlong_to_pixelXY(lat = ymax,
pixs <- latlong_to_pixelXY(lat = ymax,
lon = xmax,
level = level)

tilesmx = pixelXY_to_tileXY(pixelX = pixs$pixelX,
tilesmx <- pixelXY_to_tileXY(pixelX = pixs$pixelX,
pixelY = pixs$pixelY)

# How many tile XY coordinates conform the grid?
resy = tilesmx$tileY - tilesmn$tileY
resx = tilesmx$tileX - tilesmn$tileX
resy <- tilesmx$tileY - tilesmn$tileY
resx <- tilesmx$tileX - tilesmn$tileX

if(resx == 0 | resy == 0){
stop(paste(
@@ -80,18 +80,18 @@ create_qk_grid <- function(xmin, xmax, ymin, ymax, level){
num_cols <- abs(resx)

# create the grid with all the possible combination of tile XY coordinates
data = c()
data <- c()
for(c in 0:num_cols){ # I consider 0 as the point provided should be included
for(r in 0:num_rows){

grid = data.frame(tileX = tilesmn$tileX + (c * sign(resx)),
grid <- data.frame(tileX = tilesmn$tileX + (c * sign(resx)),
tileY = tilesmn$tileY + (r * sign(resy))) |>
dplyr::mutate(quadkey = tileXY_to_quadkey(
tileX = tileX,
tileY = tileY,
level = level))

data = rbind(data, grid)
data <- rbind(data, grid)

}}

9 changes: 5 additions & 4 deletions R/create_raster.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#' Create a stars raster
#'
#' @description The use of a template enables the creation of an accurate raster,
#' even in the presence of NAs.
#' @description The use of a template enables the creation of an accurate
#' raster, even in the presence of NAs.
#'
#' @param template A spatial dataset (sf) with the polygon grid used as template
#' @param nx Integer; number of cells in x direction.
#' @param ny Integer; number of cells in y direction.
#' @param data A spatial dataframe (sf) with the variable we want to represent in the raster.
#' @param data A spatial dataframe (sf) with the variable we want to represent
#' in the raster.
#' @param var The column name of the variable to plot.
#'
#' @seealso \code{\link{st_as_stars}}, \code{\link{st_rasterize}}
@@ -50,7 +51,7 @@ raster_tmplt <- stars::st_as_stars(sf::st_bbox(template),
ny = ny,
nx = nx)

r = stars::st_rasterize(data[,as.character(var)],
r <- stars::st_rasterize(data[,as.character(var)],
template = raster_tmplt)

return(r)
40 changes: 20 additions & 20 deletions R/extract_coords.R
Original file line number Diff line number Diff line change
@@ -10,10 +10,10 @@
#' @seealso \code{\link{pixelXY_to_latlong}}
#'
#' @return A spatial dataframe (sf) containing the tiles XY coordinates
#' (tileX, tileY), the QuadKey number (quadkey), and a column for POINT geometry.
#' (tileX, tileY), the QuadKey number (quadkey), and a column for POINT
#' geometry.
#'
#'
#' @export
#'#' @export
#'
#' @examples
#'
@@ -26,14 +26,13 @@
#' grid_coords <- extract_qk_coord(data = grid$data)
#'
#' plot(grid_coords)

extract_qk_coord <- function(data){

if (!"quadkey" %in% colnames(data)) {
stop("Please ensure that the dataset contains a column named 'quadkey'.")
}

for(i in 1:nrow(data)){
for(i in seq_len(nrow(data))){
# check that the data has the correct dimensions for this analysis
level = nchar(data$quadkey[i])

@@ -42,22 +41,22 @@ extract_qk_coord <- function(data){
data$qk_tileX[i] <- qktot$tileX
data$qk_tileY[i] <- qktot$tileY

ttop = tileXY_to_pixelXY(tileX = data$qk_tileX[i],
tileY = data$qk_tileY[i])
ttop <- tileXY_to_pixelXY(tileX = data$qk_tileX[i],
tileY = data$qk_tileY[i])

data$tl_pxx[i] <- ttop$pixelX
data$tl_pxy[i] <- ttop$pixelY

ptoll = pixelXY_to_latlong(pixelX = data$tl_pxx[i],
pixelY = data$tl_pxy[i],
level = level)
ptoll <- pixelXY_to_latlong(pixelX = data$tl_pxx[i],
pixelY = data$tl_pxy[i],
level = level)

data$pxy_lat[i] <- ptoll$lat
data$pxy_lon[i] <- ptoll$lon

}

data = data |>
data <- data |>
dplyr::select("tileX", "tileY", "quadkey",
"pxy_lon", "pxy_lat") |> # tidyselect
sf::st_as_sf(coords = c("pxy_lon", "pxy_lat"), crs = 4326)
@@ -98,28 +97,29 @@ extract_qk_coord <- function(data){
extract_tile_coord <- function(data, level){

if (!any(c('tileX', 'tileY') %in% colnames(data))) {
stop("Please ensure that the dataset contains columns named 'tileX' and 'tileY'")
stop(paste("Please ensure that the dataset contains columns named 'tileX'",
"and 'tileY'"))
}

for(i in 1:nrow(data)){
for(i in seq_len(nrow(data))){

ttop = tileXY_to_pixelXY(tileX = data$tileX[i],
tileY = data$tileY[i])
ttop <- tileXY_to_pixelXY(tileX = data$tileX[i],
tileY = data$tileY[i])

data$tl_pxx[i] <- ttop$pixelX
data$tl_pxy[i] <- ttop$pixelY

ptoll = pixelXY_to_latlong(pixelX = data$tl_pxx[i],
pixelY = data$tl_pxy[i],
level = level)

ptoll <- pixelXY_to_latlong(pixelX = data$tl_pxx[i],
pixelY = data$tl_pxy[i],
level = level)
data$pxy_lat[i] <- ptoll$lat
data$pxy_lon[i] <- ptoll$lon

}

# I have to keep the quadkeys for later use
data = data |>
data <- data |>
dplyr::select("tileX", "tileY", "quadkey",
"pxy_lon", "pxy_lat") |> # tidyselect
sf::st_as_sf(coords = c('pxy_lon', 'pxy_lat'), crs = 4326)
53 changes: 31 additions & 22 deletions R/grid_to_polygon.R
Original file line number Diff line number Diff line change
@@ -51,19 +51,25 @@ complete_grid_for_polygons <- function(data){
tileY = textY),
data.frame(tileX = textX,
tileY = seq(min(data$tileY),
textY -1))) |> # there should be -1 to not duplicate the point in the corner
dplyr::mutate(quadkey = NA) # I am adding this points to fill the grid, the qk value is not important here
textY -1))) |>
# the -1 is to avoid
# duplicating the the point in the corner
dplyr::mutate(quadkey = NA)
# I am adding this points to fill the grid,
# the quadkey value is not important here

}


#' Convert a grid of QuadKeys to square polygons
#'
#' @description The main argument of this function, the grid of QuadKeys points
#' representing lat/long WG84 coordinates specifically indicate the upper-left corner of the QuadKey.
#' representing lat/long WG84 coordinates specifically indicate the upper-left
#' corner of the QuadKey.
#' To transform these coordinates into square polygons, the function
#' supplements the grid by adding a row and column of tiles. This completion of the
#' grid addresses QuadKeys located at the border of the area (complete_grid_for_polygons).
#' supplements the grid by adding a row and column of tiles. These points
#' introduce QuadKeys located at the border of the area
#' (complete_grid_for_polygons).
#' The function constructs the polygons using all the points of the grid.
#' Note that it's possible to associate each QuadKey with its square polygon.
#'
@@ -95,44 +101,46 @@ grid_to_polygon <- function(data){

extragrid <- complete_grid_for_polygons(data)

extragrid <- extract_tile_coord(extragrid, level = unique(nchar(data$quadkey)))
extragrid <- extract_tile_coord(extragrid,
level = unique(nchar(data$quadkey)))

# combines the new data with the extended grid oof points
# combines the new data with the extended grid of points
data = rbind(data, extragrid)

db = c() #https://github.com/r-spatial/sf/issues/354


# The quadkeys of interest are the ones that are not NA
# The original quadkeys of the grid
subdata = subset(data, !is.na(data$quadkey))

for(i in 1:nrow(subdata)){

# podria probar que tengo todos los tiles que necesito y si no los deberia crear
for(i in seq_len(nrow(subdata))){

x = subdata[i, ]$tileX
y = subdata[i, ]$tileY
x <- subdata[i, ]$tileX
y <- subdata[i, ]$tileY

# This point will always exists
a = data |>
# This point will always be a quadkey in the dataframe
a <- data |>
dplyr::filter(.data$tileX == x & .data$tileY == y)

b = data |>
# b, c and d can be part of the extended grid
b <- data |>
dplyr::filter(.data$tileX == x & .data$tileY == (y + 1))

c = data |>
c <- data |>
dplyr::filter(.data$tileX == x + 1 & .data$tileY == y)

d = data |>
d <- data |>
dplyr::filter(.data$tileX == x + 1 & .data$tileY == y + 1)

pixel = rbind(a, b, c, d) |>
pixel <- rbind(a, b, c, d) |>
sf::st_bbox() |>
sf::st_as_sfc()

grid_px = sf::st_sf(quadkey = subdata[i, ]$quadkey,
grid_px <- sf::st_sf(quadkey = subdata[i, ]$quadkey,
geometry = pixel,
sf_column_name = 'geometry')

db = rbind(grid_px, db)
db <- rbind(grid_px, db)
#st_write(pixel, 'pixel.gpkg', append = TRUE)

}
@@ -153,7 +161,8 @@ grid_to_polygon <- function(data){
#
# # elijo los 4 mas cercanos
# pol = data[i,] |>
# st_is_within_distance(data, dist = ratio) #2500 para baires y 750 para amba
# st_is_within_distance(data, dist = ratio) #2500 para baires y
# 750 para amba
#
#
#
Loading

0 comments on commit fec9b37

Please sign in to comment.