Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nathcarr55 authored and cran-robot committed Jun 23, 2023
0 parents commit 57bbc23
Show file tree
Hide file tree
Showing 22 changed files with 2,157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Package: mgpStreamingSDK
Type: Package
Title: Interact with the Maxar MGP Streaming API
Version: 0.1.0
Author: Nathan Carr
Maintainer: Nathan Carr <nathan.carr@maxar.com>
Description: This grants the functionality of the Maxar Geospatial Platform (MGP) Streaming API. It can search for images using the WFS method. It can Download images using WMS WMTS. It can also Download a full resolution image.
License: Apache License 2.0
Encoding: UTF-8
Imports: R6, reticulate
RoxygenNote: 7.2.3
VignetteBuilder: knitr
Suggests: knitr, rmarkdown
NeedsCompilation: no
Packaged: 2023-06-22 22:24:53 UTC; ncarr
Repository: CRAN
Date/Publication: 2023-06-23 08:40:09 UTC
21 changes: 21 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
afef02d4bf3b10316f9305b717773725 *DESCRIPTION
0c801c34f23f355a6d7edce96085b74d *NAMESPACE
c7178c78384ade98048dbcb62efda86c *R/interface.R
42d3fc873d6ac07f7700d5c0447eded0 *build/vignette.rds
8d0042343ea7f2c12773d6785e066041 *inst/doc/config-file.R
fb64af42ca39cd94aa6e657412855ea1 *inst/doc/config-file.Rmd
810ef51eff018d1b3db4983a21ea9f33 *inst/doc/config-file.html
1e8b8acb060d09b694cb4b5cb1d05dd2 *inst/doc/download_image.R
f593afc3a6bb7824f11650d5281da2b1 *inst/doc/download_image.Rmd
a9eb37663ebb9fc1882b48aa7ead6cb0 *inst/doc/download_image.html
60e97a56ea45738c2b30e7e67718014c *inst/doc/get_rull_resolution.R
3ad9de520419d47b155e39a729b71205 *inst/doc/get_rull_resolution.Rmd
a0800e37bc3c97f4a2764189f50a0437 *inst/doc/get_rull_resolution.html
00ae087586df449c1f7d5795c9724306 *inst/doc/search.R
19cdd48093f25238851b68596179bb3f *inst/doc/search.Rmd
a556c4043b3a2e4efb3ea3c0e1e5b188 *inst/doc/search.html
64f4a49130c4a9c905990b0dd6db0114 *man/Interface.Rd
fb64af42ca39cd94aa6e657412855ea1 *vignettes/config-file.Rmd
f593afc3a6bb7824f11650d5281da2b1 *vignettes/download_image.Rmd
3ad9de520419d47b155e39a729b71205 *vignettes/get_rull_resolution.Rmd
19cdd48093f25238851b68596179bb3f *vignettes/search.Rmd
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(Interface)
import(reticulate)
importFrom(R6,R6Class)
181 changes: 181 additions & 0 deletions R/interface.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#' Interface class for interacting with WMS, WFS, and WMTS Streaming classes.
#'
#' This class provides a unified interface to access the WMS, WFS, and WMTS Streaming classes.
#' @title Interface
#' @importFrom R6 R6Class
#' @import reticulate
#' @export Interface
Interface <- R6::R6Class(
"Interface",
public = list(
#' @field mgp_sdk (Optional) An instance of the MGP_SDK Python library. If NULL, a new instance will be created. Default is NULL.
mgp_sdk = NULL,
#' @field py_interface (Optional) An instance of the Interface class from the MGP_SDK Python library. If NULL, a new instance will be created. Default is NULL.
py_interface = NULL,
#' @field env_name = (Optional) The name of the environment where the MGP_SDK Python library is installed. Default is "R-MGP-SDK".
env_name = NULL,
#' @description
#' Initializes the `Interface` object. Sets up the environment for using the MGP_SDK Python library.
#'
#' @param mgp_sdk (Optional) An instance of the MGP_SDK Python library. If NULL, a new instance will be created. Default is NULL.
#' @param py_interface (Optional) An instance of the Interface class from the MGP_SDK Python library. If NULL, a new instance will be created. Default is NULL.
#' @param env_name (Optional) The name of the environment where the MGP_SDK Python library is installed. Default is "R-MGP-SDK".
initialize = function(mgp_sdk=NULL, py_interface = NULL, env_name = "R-MGP-SDK") {
Sys.unsetenv("HOME")
Sys.setenv(HOME = getwd())
if (private$check_virtualenv(env_name)) {
#do nothing the venv is already set up
library(reticulate)
reticulate::use_virtualenv(file.path(Sys.getenv("HOME"), ".virtualenvs", env_name))
}
else{
private$initialize_python_env("MGP-SDK")
library(reticulate)
reticulate::use_virtualenv(file.path(Sys.getenv("HOME"), ".virtualenvs", env_name))
}
self$mgp_sdk <<- reticulate::import("MGP_SDK")
self$py_interface <<- self$mgp_sdk$interface$Interface()
},

#' @description Perform a search for features within the specified bounding box and/or with a specified filter.
#'
#' @param bbox A string indicating the bounding box of the area of interest (miny,minx,maxy,maxx).
#' @param filter A string containing a CQL filter used to refine the data of the search. Default is NULL.
#' @param shapefile A logical indicating whether to return a shapefile. Default is FALSE.
#' @param csv A logical indicating whether to return a CSV file. Default is FALSE.
#' @param ... Additional arguments to pass to the `search` method.
#'
#' @details The `search` function performs a search for features within the specified bounding box and/or with a specified filter.
#'
#' @return If `shapefile` is TRUE, the function returns a shapefile of all features and associated metadata.
#' If `csv` is TRUE, the function returns a CSV file. If neither is specified, the function returns a list of features.
#' @export
search = function(bbox=NULL, filter=NULL, shapefile=FALSE, csv=FALSE, ...) {
kwargs <- list(...)
args <- c(list(bbox = bbox, filter = filter, shapefile = shapefile, csv = csv), kwargs)
result <- do.call(self$py_interface$streaming$search, args)
return(result)
},

#' @description Download an image from a WMS or WMTS service
#'
#' @description This function allows you to download an image from a Web Map Service (WMS) or a
#' @description Web Map Tile Service (WMTS). You can specify the bounding box, image dimensions, image
#' @description format, and other parameters to customize the downloaded image.
#'
#' @param bbox A vector of four numeric values specifying the bounding box of the image.
#' @param srsname A string specifying the spatial reference system (SRS) of the bounding box. Default is "EPSG:4326".
#' @param height The height of the image in pixels.
#' @param width The width of the image in pixels.
#' @param img_format A string specifying the image format. Must be one of "jpeg", "png", or "geotiff".
#' @param identifier A string specifying the identifier of the image.
#' @param gridoffsets A vector of two numeric values specifying the grid offsets of the image.
#' @param zoom_level An integer specifying the zoom level of the WMTS image.
#' @param download A logical value indicating whether to download the image (TRUE) or return the raw image data (FALSE).
#' @param outputpath A string specifying the directory where the downloaded image should be saved.
#' @param display A logical value indicating whether to display the downloaded image (TRUE) or not (FALSE).
#' @param ... Additional parameters to be passed to the WMS or WMTS service.
#'
#' @return If `download` is TRUE, the function returns the filename of the downloaded image. If `download` is FALSE,
#' the function returns the raw image data as a binary vector.
#' @export
download_image = function(bbox = NULL, srsname = "EPSG:4326", height = NULL, width = NULL, img_format = "jpeg", identifier = NULL, zoom_level = NULL, download = TRUE, outputpath = NULL, display = FALSE) {
args <- c(list(bbox = bbox, srsname = srsname, height = as.integer(height), width = as.integer(width), img_format = img_format, identifier = identifier, zoom_level = zoom_level, download = download, outputpath = outputpath, display = display, legacyId = legacyId))
result <- do.call(self$py_interface$streaming$download_image, args)
#result <- self$py_interface$streaming$download_image(bbox = bbox, srsname = srsname, height = as.integer(height), width = as.integer(width), img_format = img_format, identifier = identifier, zoom_level = zoom_level, download = download, outputpath = outputpath, display = display)
return(result)
},

#'
#' @description This function is a wrapper for a Python function that retrieves full resolution images.
#' @description The function downloads an image with the specified feature ID and additional parameters.
#'
#' @param featureid A character string representing the unique ID of the feature for which the image is required.
#' @param thread_number An integer indicating the number of threads to use for the download process. Default is 100.
#' @param bbox A character string representing the bounding box coordinates in the format 'xmin, ymin, xmax, ymax'. If NULL, the bounding box will be determined based on the feature ID. Default is NULL.
#' @param mosaic A logical value indicating whether to mosaic the images or not. If TRUE, images covering the defined area will be combined into a single image. Default is FALSE.
#' @param srsname A character string representing the spatial reference system to be used for the image. Default is 'EPSG:4326'.
#' @param outputdirectory A character string representing the directory where the image should be saved. If NULL, the image will be saved in the current working directory. Default is NULL.
#' @param image_format A character string representing the format of the image file to be downloaded. Default is 'jpeg'.
#' @param filename A character string representing the name of the file to be saved. Default is "Maxar_Download".
#'
#' @return The function returns the result of the Python function call. The nature of this result depends on the Python function implementation.
#' @export

get_full_res_image = function(featureid, thread_number = 100, bbox = NULL, mosaic = FALSE,
srsname = 'EPSG:4326', outputdirectory=getwd(),image_format='jpeg', filename="Maxar_Download") {
arguments <- c(list(featureid=featureid,thread_number=as.integer(thread_number),bbox=bbox,mosaic=mosaic,srsname=srsname,outputdirectory=outputdirectory,image_format=image_format,filename=filename))
result <- do.call(self$py_interface$streaming$get_full_res_image,arguments)
return(result)
}
),
private = list(
initialize_python_env = function(package_name, env_name = "R-MGP-SDK") {
# Get the current Python configuration
base_python = private$get_system_python_path()
# Create virtual environment if it does not exist
if (!private$check_virtualenv(env_name)) {
# create the virtual environment
private$create_virtualenv(package_name = package_name, env_name = env_name)
}
},
get_system_python_path = function() {
# Attempt to find Python in common locations
base_python_path <- NULL
if (Sys.info()["sysname"] == "Windows") {
base_python_path <- system("where python", intern = TRUE)[1]
} else { # For Unix and MacOS
base_python_path <- system("which python", intern = TRUE)
}

# If base_python_path is NULL or empty, Python was not found
if (is.null(base_python_path) || base_python_path == "") {
message("Python not found, installing...")
reticulate::install_python(version = "3.9:latest", list = FALSE, force = FALSE)
# Try finding Python again after installing Miniconda
if (Sys.info()["sysname"] == "Windows") {
base_python_path <- system("where python", intern = TRUE)[1]
} else { # For Unix and MacOS
base_python_path <- system("which python", intern = TRUE)
}
# If Python is still not found, stop the function
if (is.null(base_python_path) || base_python_path == "") {
stop("Python installation failed.")
}
}
return(base_python_path)
},
check_virtualenv = function(env_name) {
env_path <- file.path(Sys.getenv("HOME"), ".virtualenvs", env_name)
dir.exists(env_path)
return(dir.exists(env_path))
},
create_virtualenv = function(package_name, env_name = "R-MGP-SDK") {
# Define the base Python interpreter. You might need to adjust this depending
# on where Python is installed on your system.
base_python <- private$get_system_python_path()
# Define the path to the new virtual environment
venv_path <- file.path(Sys.getenv("HOME"), ".virtualenvs", env_name)
# Command to create the virtual environment
create_venv_cmd <- paste(paste0("\"", base_python, "\""), "-m venv", venv_path)
system(create_venv_cmd)
if (Sys.info()['sysname'] == "Windows") {
library(reticulate)
reticulate::use_virtualenv(file.path(Sys.getenv("HOME"), ".virtualenvs", env_name))
reticulate::py_install("MGP_SDK", envname = file.path(Sys.getenv("HOME"), ".virtualenvs", env_name))
} else {
pip_install_cmd = paste("source", file.path(venv_path, "bin", "activate"), "&&", "python -m pip install", package_name)
system(pip_install_cmd)
}
# Set the RETICULATE_PYTHON environment variable to the Python interpreter of the new environment
if (Sys.info()['sysname'] == "Windows") {
python_interpreter <- file.path(venv_path, "Scripts", "python.exe")
} else {
python_interpreter <- file.path(venv_path, "bin", "python")
}
Sys.setenv(RETICULATE_PYTHON = python_interpreter)
}
)
)


Binary file added build/vignette.rds
Binary file not shown.
14 changes: 14 additions & 0 deletions inst/doc/config-file.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## ---- include = FALSE---------------------------------------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)

## ----setup--------------------------------------------------------------------
#Create a config file named ".MGP-config" in your home directory or working directory.
#Ex file format.
#' [MGP]
#' user_name=MGPUSERNAME
#' user_password=MGPPASSWORD
#' client_id=CLIENTID

24 changes: 24 additions & 0 deletions inst/doc/config-file.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "config-file"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{config-file}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

```{r setup}
#Create a config file named ".MGP-config" in your home directory or working directory.
#Ex file format.
#' [MGP]
#' user_name=MGPUSERNAME
#' user_password=MGPPASSWORD
#' client_id=CLIENTID
```

0 comments on commit 57bbc23

Please sign in to comment.