Skip to content

Commit

Permalink
add dry_run
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Apr 21, 2024
1 parent 7c060ec commit 3c948e2
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 45 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^gebco$
^\.github$
^data-raw$
^CODE_OF_CONDUCT\.md$
126 changes: 126 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at [email protected].
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series of
actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within the
community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
<https://www.contributor-covenant.org/version/2/1/code_of_conduct.html>.

Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][https://github.com/mozilla/inclusion].

For answers to common questions about this code of conduct, see the FAQ at
<https://www.contributor-covenant.org/faq>. Translations are available at <https://www.contributor-covenant.org/translations>.

[homepage]: https://www.contributor-covenant.org
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# filearchy 0.1.0.9000

* Added `dry_run` to `gdal_tiles()`.


# filearchy 0.1.0

* First working package version, inspired by https://github.com/USDAForestService/gdalraster/issues/315.
Expand Down
31 changes: 15 additions & 16 deletions R/gdal_tiles.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## write one tile, we can parallize this call
write_tile <- function(tile, dataset, output_dir = tempdir(), overwrite = FALSE) {
path <- file.path(output_dir, tile$path)
write_tile <- function(tile, dataset, overwrite = FALSE) {
path <- tile$path
if (!overwrite) {
if (fs::file_exists(path)) return(NULL)
}
Expand All @@ -19,6 +19,7 @@ write_tile <- function(tile, dataset, output_dir = tempdir(), overwrite = FALSE)
#' @param update not implemented (please take care)
#' @param output_dir directory to write to, by default a tempdir is used
#' @param overwrite clobber the output directory, `FALSE` is the default
#' @param dry_run if `TRUE` only the scheme is built and returned as a data frame
#'
#' @return the tile scheme, invisibly as a dataframe
#' @export
Expand All @@ -34,21 +35,23 @@ write_tile <- function(tile, dataset, output_dir = tempdir(), overwrite = FALSE)
#' tiles <- gdal_tiles(dsn)
#' if (!interactive()) unlink(tiles$path)
#' #future::plan(sequential)
gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857", update = TRUE, output_dir = tempfile(), overwrite = FALSE) {
gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857", update = TRUE, output_dir = tempfile(), overwrite = FALSE,
dry_run = FALSE) {
#browser()
opt <- gdalraster::get_config_option("GDAL_PAM_ENABLED")
gdalraster::set_config_option("GDAL_PAM_ENABLED", "NO")
on.exit(gdalraster::set_config_option("GDAL_PAM_ENABLED", opt), add = TRUE)


if (!dry_run) {
## put all write-handling in here
if (!overwrite) {
if (file.exists(output_dir)) {
stop("output_dir already exists, delete or set overwrite = TRUE")
}
}
unlink(output_dir, recursive = TRUE)
dir.create(output_dir, showWarnings = FALSE)

}
## we can't get info from gdalraster yet because it will run statistics
##info <- vapour::vapour_raster_info(dsn)
ds <- new(gdalraster::GDALRaster, dsn)
Expand All @@ -66,12 +69,10 @@ gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857",
ex[3] <- max(c(ex[3], -20037508.342789244))
ex[4] <- min(c(ex[4], 20037508.342789244))

filelist <- base$getFileList()
print(filelist)
#[1] "/vsimem/file70fd4d3a0623.vrt"
#[2] "/perm_storage/home/mdsumner/R/x86_64-pc-linux-gnu-library/4.3/filearchy/extdata/gebco_ovr5.vrt"

base$close()
## I'm a bit confused about this still
gdalraster::vsi_rmdir(dirname(tf))
## this would delete our source, so how do we only clean up the vsimem? do we?
#unlink(filelist)

Expand All @@ -85,22 +86,20 @@ gdal_tiles <- function(dsn, nzoom = NULL, blocksize = 256L, t_srs = "EPSG:3857",
## we have to flip the rows (we need tms vs xyz)
ti$tile_col_tms <- ti$tile_col - 1
ti$tile_row_tms <- g$scheme$ntilesY - ti$tile_row
ti$path <- file.path(ti$zoom, ti$tile_col_tms, sprintf("%i.png", ti$tile_row_tms))

ti$path <- file.path(output_dir, ti$zoom, ti$tile_col_tms, sprintf("%i.png", ti$tile_row_tms))
l[[i]] <- ti
}




d <- do.call(rbind, l)
if (dry_run) return(d)

jk <- future_map(split(d, 1:nrow(d)), write_tile,
dataset = dsn,
output_dir = output_dir)
dataset = dsn, overwrite = overwrite)
print(sprintf("tiles in directory: %s", output_dir))

## do we do this earlier?
d$path <- file.path(output_dir, d$path)

invisible(d)

}
39 changes: 37 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ knitr::opts_chunk$set(

The goal of filearchy is to generate pyramid tiled image directories. (Like gdal2tiles.py).

## TODO

-[ ] we have `dry_run` in gdal_tiles() but I think it should be a separate function to render from the scheme
-[ ] implement xyz vs tms mode (I think it's just nrow - row)
-[ ] driver and file extension options
-[ ] make it clear that byte-scaling is not mandatory, perfectly valid to have tiles of data (like (tiles-prod)[https://s3.amazonaws.com/elevation-tiles-prod/geotiff])

## Installation

You can install the development version of filearchy from [GitHub](https://github.com/) with:
Expand Down Expand Up @@ -55,7 +62,35 @@ ds$close()
m <- do.call(cbind, maps::map(plot = F)[1:2])
m[m[,1] > 180, ] <- NA
# library(gdalraster)
# lines(gdalraster::transform_xy(m, srs_to = srs_to_wkt("EPSG:3857"), srs_from = srs_to_wkt("EPSG:4326")))
lines(reproj::reproj_xy(m, "EPSG:3857", source = "EPSG:4326"), col = "firebrick")
## we have to handle missing values
nas <- is.na(m[,1])
xylines <- m
library(gdalraster)
xylines[!nas, ] <- gdalraster::transform_xy(m[!nas, ],
srs_to = srs_to_wkt("EPSG:3857"), srs_from = srs_to_wkt("EPSG:4326"))
lines(xylines)
all(file.exists(tiles$path))
```

Or, just do a dry_run:

```{r scheme-only}
scheme <- gdal_tiles(dsn, dry_run = TRUE, nzoom = 5)
ex <- c(min(scheme$xmin), max(scheme$xmax), min(scheme$ymin), max(scheme$ymax))
plot(ex[1:2], ex[3:4], asp = 1)
md <- dplyr::filter(scheme, zoom == round(mean(unique(zoom)) ))
with(md, rect(xmin, ymin, xmax, ymax, border = "red", lwd = 3))
mx <- dplyr::filter(scheme, zoom == max(zoom) )
with(mx, rect(xmin, ymin, xmax, ymax))
any(file.exists(scheme$path))
```



## Code of Conduct

Please note that the filearchy project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.
Loading

0 comments on commit 3c948e2

Please sign in to comment.