Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
aaaa
  • Loading branch information
lbusett committed Jul 16, 2017
1 parent 543880f commit 2e8477f
Show file tree
Hide file tree
Showing 15 changed files with 817 additions and 107 deletions.
5 changes: 5 additions & 0 deletions .Rbuildignore
Expand Up @@ -10,3 +10,8 @@ temporary\*
benchmarking\*
^\.travis\.yml$
^D:/Documents/Source/git/sprawl/docs$
^codecov\.yml$
^cran-comments\.md$
^CONDUCT\.md$
^pkgdown$

25 changes: 25 additions & 0 deletions CONDUCT.md
@@ -0,0 +1,25 @@
# Contributor Code of Conduct

As contributors and maintainers of this project, we pledge to respect all people who
contribute through reporting issues, posting feature requests, updating documentation,
submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for
everyone, regardless of level of experience, gender, gender identity and expression,
sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.

Examples of unacceptable behavior by participants include the use of sexual language or
imagery, derogatory comments or personal attacks, trolling, public or private harassment,
insults, or other unprofessional conduct.

Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed
from the project team.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by
opening an issue or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the Contributor Covenant
(http:contributor-covenant.org), version 1.0.0, available at
http://contributor-covenant.org/version/1/0/0/
218 changes: 115 additions & 103 deletions R/create_fishnet.R
Expand Up @@ -27,7 +27,7 @@
#' library(raster)
#' library(sprawl)
#'
#' file_in <- raster::stack(system.file("extdata", "sprawl_EVItest.tif", package = "sprawl"))[[1]]
#' file_in <- system.file("extdata", "sprawl_EVItest.tif", package = "sprawl.data")
#' inrast <- raster::raster(file_in)
#' cellsize <- raster::res(inrast)[1]
#'
Expand Down Expand Up @@ -77,112 +77,124 @@ create_fishnet <- function(in_obj,
}
}

if (!file.exists(pypath)) {
stop("create_fishnet --> gdal_polygonize.py was not found ! please check that it is available on your system
and specify its position through the `py_path` argument ! Aborting !")
}

# checks on in_obj
if (check_spatype(in_obj) == "spobject") {
in_obj <- st_as_sf(in_obj)
}
if (check_spatype(in_obj) == "rastobject") {
if (is.null(cellsize)) {
warning("cellsize not specified. It will be derived from the `in_obj` raster")
cellsize <- res(in_obj)[1]
}
proj <- sp::proj4string(in_obj)
in_obj <- sf::st_as_sf(as(extent(in_obj), 'SpatialPolygons')) %>%
sf::st_set_crs(proj)
}

if (inherits(in_obj, "sf") == FALSE) {
stop("`in_obj` must be an `sp`, `sf` or `*raster` object ! Aborting !")
}

# checks on crop_layer
if (check_spatype(crop_layer) == "spobject") {
crop_layer <- sf::st_as_sf(crop_layer)
}
if (check_spatype(crop_layer) == "rastobject") {
proj <- sp::proj4string(in_obj)
in_obj <- sf::st_as_sf(as(extent(crop_layer), 'SpatialPolygons')) %>%
sf::st_set_crs(proj)
}

if (!is.null(crop_layer) & inherits(crop_layer, "sfc")) {
stop("`crop_layer` must be an `sp`, `sf` or `*raster` object ! Aborting !")
}

# checks on cellsize
if (is.null(cellsize)) {
stop("cellsize not specified ! Aborting !")
}

# ____________________________________________________________________________
# retrieve information from the in_obj and crop_layer inputs ####

in_bbox <- sf::st_bbox(in_obj)
in_proj <- sf::st_crs(in_bbox)$proj4string

if (!is.null(crop_layer)) {
crop_ext <- sf::st_bbox(crop_layer)
crop_proj <- sf::st_crs(crop_ext)$proj4string
# if projection of crop_layer not equal to in_object, reproject the extent####
if (crop_proj != in_proj) {crop_layer <- sf::st_transform(crop_layer, in_proj)}
}

# ____________________________________________________________________________
# Build a spatialgridDataFrame based on the input Extent and resoulution ####
inrast <- build_testraster(100,100,1)
bbox <- raster::extent(inrast)
ext_poly <- sf::st_as_sfc(c(paste0("POLYGON((",
bbox[1], " ", bbox[3], ", ",
bbox[1], " ", bbox[4], ", ",
bbox[2], " ", bbox[4], ", ",
bbox[2], " ", bbox[3], ", ",
bbox[1], " ", bbox[3], "",
"))")),
crs = proj4string(inrast))
fish <- sf::st_make_grid(ext_poly, res(inrast), what = "polygons")

# if (!file.exists(pypath)) {
# stop("create_fishnet --> gdal_polygonize.py was not found ! please check that it is available on your system
# and specify its position through the `py_path` argument ! Aborting !")
# }
#
# # checks on in_obj
# if (check_spatype(in_obj) == "spobject") {
# in_obj <- st_as_sf(in_obj)
# }
# if (check_spatype(in_obj) == "rastobject") {
# if (is.null(cellsize)) {
# warning("cellsize not specified. It will be derived from the `in_obj` raster")
# cellsize <- res(in_obj)[1]
# }
# proj <- sp::proj4string(in_obj)
# in_obj <- sf::st_as_sf(as(extent(in_obj), 'SpatialPolygons')) %>%
# sf::st_set_crs(proj)
# }
#
# if (inherits(in_obj, "sf") == FALSE) {
# stop("`in_obj` must be an `sp`, `sf` or `*raster` object ! Aborting !")
# }
#
# # checks on crop_layer
# if (check_spatype(crop_layer) == "spobject") {
# crop_layer <- sf::st_as_sf(crop_layer)
# }
# if (check_spatype(crop_layer) == "rastobject") {
# proj <- sp::proj4string(in_obj)
# in_obj <- sf::st_as_sf(as(extent(crop_layer), 'SpatialPolygons')) %>%
# sf::st_set_crs(proj)
# }
#
# if (!is.null(crop_layer) & inherits(crop_layer, "sfc")) {
# stop("`crop_layer` must be an `sp`, `sf` or `*raster` object ! Aborting !")
# }
#
cs <- c(cellsize,cellsize) # cell size.
# Identidfy the corner of the grid. Since extent of a spatial object in R is defined
# by centroids, we need to move by half pixel
cc <- c(in_bbox[1], in_bbox[2]) + (cs/2)
# Compute number of cells per direction
cd <- ceiling(c(((in_bbox[3] - in_bbox[1])/cs[1]),
((in_bbox[4] - in_bbox[2])/cs[2]) - 1))
# Build grid topology
grd <- sp::GridTopology(cellcentre.offset = cc, cellsize = cs, cells.dim = cd) # Define grd characteristics
# Create a SpatialGridDataFrame. ids are numbers between 1 and ns*nl
sp_grd <- sp::SpatialGridDataFrame(grd,
data = data.frame(id = seq(1,(prod(cd)),1)),
proj4string = in_proj)
# # checks on cellsize
# if (is.null(cellsize)) {
# stop("cellsize not specified ! Aborting !")
# }
#
# # ____________________________________________________________________________
# # retrieve information from the in_obj and crop_layer inputs ####
#
# in_bbox <- sf::st_bbox(in_obj)
# in_proj <- sf::st_crs(in_bbox)$proj4string
#
# if (!is.null(crop_layer)) {
# crop_ext <- sf::st_bbox(crop_layer)
# crop_proj <- sf::st_crs(crop_ext)$proj4string
# # if projection of crop_layer not equal to in_object, reproject the extent####
# if (crop_proj != in_proj) {crop_layer <- sf::st_transform(crop_layer, in_proj)}
# }
#
# # ____________________________________________________________________________
# # Build a spatialgridDataFrame based on the input Extent and resoulution ####
# #
# cs <- c(cellsize,cellsize) # cell size.
# # Identidfy the corner of the grid. Since extent of a spatial object in R is defined
# # by centroids, we need to move by half pixel
# cc <- c(in_bbox[1], in_bbox[2]) + (cs/2)
# # Compute number of cells per direction
# cd <- ceiling(c(((in_bbox[3] - in_bbox[1])/cs[1]),
# ((in_bbox[4] - in_bbox[2])/cs[2]) - 1))
# # Build grid topology
# grd <- sp::GridTopology(cellcentre.offset = cc, cellsize = cs, cells.dim = cd) # Define grd characteristics
# # Create a SpatialGridDataFrame. ids are numbers between 1 and ns*nl
# sp_grd <- sp::SpatialGridDataFrame(grd,
# data = data.frame(id = seq(1,(prod(cd)),1)),
# proj4string = in_proj)

# ____________________________________________________________________________
# create a temporary raster from the sp_grd ####

out_rst <- raster::raster(sp_grd)
out_rst[] <- seq(1, dim(out_rst)[1]*dim(out_rst)[2],1)

# if crop layer available, crop and mask the temporary raster on it
if (!is.null(crop_layer)) {
message("create_fishnet --> Cropping and masking the fishnet on `crop_layer`")
# out_rst <- raster::crop(out_rst, raster::extent(crop_ext[c(1,3,2,4)]))
# cropper <- as(sf::st_buffer(sf::st_combine(crop_layer), cellsize), "Spatial")
# gc()

out_rst <- mask_rast(out_rst, crop_layer, buffer = res(out_rst)[1])
}
# assign the ids
out_rastfile <- tempfile("tempshp_", tempdir(), ".tif")
raster::writeRaster(out_rst, out_rastfile, overwrite = TRUE)


# ____________________________________________________________________________
# if raster out selected, copy the raster fishnet to the path given ####

if (!is.null(out_raster)) {
message("create_fishnet --> Writing Polygon Grid to: ", out_rastfile, " - Please Wait !")
save_file <- try({
dir.create(dirname(out_rastfile), recursive = T, showWarnings = FALSE)
file.copy(out_rastfile, out_raster)
})

if (class(save_file) == "try-error") {
stop("writing to ", save_file, " failed. Aborting !")
}
}
#
# out_rst <- raster::raster(sp_grd)
# out_rst[] <- seq(1, dim(out_rst)[1]*dim(out_rst)[2],1)
#
# # if crop layer available, crop and mask the temporary raster on it
# if (!is.null(crop_layer)) {
# message("create_fishnet --> Cropping and masking the fishnet on `crop_layer`")
# # out_rst <- raster::crop(out_rst, raster::extent(crop_ext[c(1,3,2,4)]))
# # cropper <- as(sf::st_buffer(sf::st_combine(crop_layer), cellsize), "Spatial")
# # gc()
#
# out_rst <- mask_rast(out_rst, crop_layer, buffer = res(out_rst)[1])
# }
# # assign the ids
# out_rastfile <- tempfile("tempshp_", tempdir(), ".tif")
# raster::writeRaster(out_rst, out_rastfile, overwrite = TRUE)
#
#
# # ____________________________________________________________________________
# # if raster out selected, copy the raster fishnet to the path given ####
#
# if (!is.null(out_raster)) {
# message("create_fishnet --> Writing Polygon Grid to: ", out_rastfile, " - Please Wait !")
# save_file <- try({
# dir.create(dirname(out_rastfile), recursive = T, showWarnings = FALSE)
# file.copy(out_rastfile, out_raster)
# })
#
# if (class(save_file) == "try-error") {
# stop("writing to ", save_file, " failed. Aborting !")
# }
# }

# ____________________________________________________________________________
# create shapefile ouput (if required) ####
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Expand Up @@ -19,6 +19,7 @@ knitr::opts_chunk$set(
[![codecov](https://codecov.io/gh/IREA-CNR-MI/sprawl/branch/master/graph/badge.svg?token=0yWdr6gWG7)](https://codecov.io/gh/IREA-CNR-MI/sprawl)
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)


# sprawl - Spatial Processing (in) R: Amorphous Wrapper Library

## Intro and rationale
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@
- [Citation](#citation)

<!-- README.md is generated from README.Rmd. Please edit that file -->
[![Travis-CI Build Status](https://travis-ci.org/IREA-CNR-MI/sprawl.svg?branch=master)](https://travis-ci.org/IREA-CNR-MI/sprawl) [![codecov](https://codecov.io/gh/IREA-CNR-MI/sprawl/branch/master/graph/badge.svg?token=0yWdr6gWG7)](https://codecov.io/gh/IREA-CNR-MI/sprawl) [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)
[![Travis-CI Build Status](https://travis-ci.org/IREA-CNR-MI/sprawl.svg?branch=master)](https://travis-ci.org/IREA-CNR-MI/sprawl) [![codecov](https://codecov.io/gh/IREA-CNR-MI/sprawl/branch/master/graph/badge.svg?token=0yWdr6gWG7)](https://codecov.io/gh/IREA-CNR-MI/sprawl)

sprawl - Spatial Processing (in) R: Amorphous Wrapper Library
=============================================================
Expand Down

0 comments on commit 2e8477f

Please sign in to comment.