Browse Source

basic tests and error checking

delete
boB Rudis 7 years ago
parent
commit
504fc6b9c5
No known key found for this signature in database GPG Key ID: 2A514A4997464560
  1. 1
      .Rbuildignore
  2. 25
      CONDUCT.md
  3. 21
      README.Rmd
  4. 30
      README.md
  5. 6
      src/RcppExports.cpp
  6. 138
      src/main.cpp
  7. 35
      tests/testthat/test-mgrs.R

1
.Rbuildignore

@ -8,3 +8,4 @@
^\.codecov\.yml$
^README_files$
^doc$
^CONDUCT\.md$

25
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/

21
README.Rmd

@ -21,10 +21,10 @@ The following functions are implemented:
- Find all the folks/projects I need to add in the `DESCRIPTION` for auth/ctb & docs
- Vectorize ALL THE THINGS
- Add in support for the other MGRS conversion thingys
- Error checking
- <strike>Add in support for the other MGRS conversion thingys</strike>
- More error checking (basic checking is done)
- Precision validation
- Tests (etc)
- More tests (basic testing is done)
### Installation
@ -64,3 +64,18 @@ ups_to_mgrs("N", 2426773, 1530125)
mgrs_to_ups("ZGC2677330125")
```
### Test Results
```{r test}
library(mgrs)
library(testthat)
date()
test_dir("tests/")
```
### Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

30
README.md

@ -18,10 +18,10 @@ The following functions are implemented:
- Find all the folks/projects I need to add in the `DESCRIPTION` for auth/ctb & docs
- Vectorize ALL THE THINGS
- Add in support for the other MGRS conversion thingys
- Error checking
- <strike>Add in support for the other MGRS conversion thingys</strike>
- More error checking (basic checking is done)
- Precision validation
- Tests (etc)
- More tests (basic testing is done)
### Installation
@ -104,3 +104,27 @@ mgrs_to_ups("ZGC2677330125")
## mgrs hemisphere easting northing
## 1 ZGC2677330125 N 2426773 1530125
### Test Results
``` r
library(mgrs)
library(testthat)
date()
```
## [1] "Sun Apr 9 22:43:31 2017"
``` r
test_dir("tests/")
```
## testthat results ========================================================================================================
## OK: 10 SKIPPED: 0 FAILED: 0
##
## DONE ===================================================================================================================
### Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

6
src/RcppExports.cpp

@ -18,7 +18,7 @@ BEGIN_RCPP
END_RCPP
}
// latlng_to_mgrs
std::string latlng_to_mgrs(double latitude, double longitude, bool degrees, int precision);
String latlng_to_mgrs(double latitude, double longitude, bool degrees, int precision);
RcppExport SEXP mgrs_latlng_to_mgrs(SEXP latitudeSEXP, SEXP longitudeSEXP, SEXP degreesSEXP, SEXP precisionSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
@ -32,7 +32,7 @@ BEGIN_RCPP
END_RCPP
}
// utm_to_mgrs
std::string utm_to_mgrs(long zone, std::string hemisphere, double easting, double northing, long precision);
String utm_to_mgrs(long zone, std::string hemisphere, double easting, double northing, long precision);
RcppExport SEXP mgrs_utm_to_mgrs(SEXP zoneSEXP, SEXP hemisphereSEXP, SEXP eastingSEXP, SEXP northingSEXP, SEXP precisionSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
@ -58,7 +58,7 @@ BEGIN_RCPP
END_RCPP
}
// ups_to_mgrs
std::string ups_to_mgrs(std::string hemisphere, double easting, double northing, long precision);
String ups_to_mgrs(std::string hemisphere, double easting, double northing, long precision);
RcppExport SEXP mgrs_ups_to_mgrs(SEXP hemisphereSEXP, SEXP eastingSEXP, SEXP northingSEXP, SEXP precisionSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;

138
src/main.cpp

@ -22,10 +22,20 @@ NumericVector mgrs_to_latlng(std::string MGRS, bool degrees = true) {
ret = Convert_MGRS_To_Geodetic((char *)MGRS.c_str(), &lat, &lng);
NumericVector coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
NumericVector coords;
if (ret != UTM_NO_ERROR) {
Rcpp::warning("Error converting MGRS to latitude/longitude");
coords = NumericVector::create(
_["lat"] = NA_REAL,
_["lng"] = NA_REAL
);
} else {
coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
}
return(coords);
@ -41,8 +51,8 @@ NumericVector mgrs_to_latlng(std::string MGRS, bool degrees = true) {
//' @examples
//' latlng_to_mgrs(42, -93)
// [[Rcpp::export]]
std::string latlng_to_mgrs(double latitude, double longitude,
bool degrees = true, int precision = 5) {
String latlng_to_mgrs(double latitude, double longitude,
bool degrees = true, int precision = 5) {
if (degrees) {
latitude *= PI / 180.0;
@ -54,7 +64,12 @@ std::string latlng_to_mgrs(double latitude, double longitude,
ret = Convert_Geodetic_To_MGRS(latitude, longitude, precision, buf);
return(std::string(buf));
if (ret != MGRS_NO_ERROR) {
Rcpp::warning("Error conveting latitude/longitudfe to MGRS");
return(NA_STRING);
} else {
return(std::string(buf));
}
}
@ -69,9 +84,9 @@ std::string latlng_to_mgrs(double latitude, double longitude,
//' @examples
//' utm_to_mgrs(48, "N", 377299, 1483035)
// [[Rcpp::export]]
std::string utm_to_mgrs(long zone, std::string hemisphere,
double easting, double northing,
long precision = 5) {
String utm_to_mgrs(long zone, std::string hemisphere,
double easting, double northing,
long precision = 5) {
char buf[80];
long ret;
@ -79,7 +94,12 @@ std::string utm_to_mgrs(long zone, std::string hemisphere,
ret = Convert_UTM_To_MGRS(zone, hemisphere.c_str()[0], easting, northing,
precision, (char *)&buf);
return(std::string(buf));
if (ret != UTM_NO_ERROR) {
Rcpp::warning("Error converting UTM to MGRS");
return(NA_STRING);
} else {
return(std::string(buf));
}
}
@ -101,12 +121,23 @@ DataFrame mgrs_to_utm(std::string mgrs_string) {
ret = Convert_MGRS_To_UTM((char *)mgrs_string.c_str(), &zone, h_buf, &easting, &northing);
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["zone"] = zone,
_["hemisphere"] = std::string(h_buf),
_["easting"] = easting,
_["northing"] = northing,
_["stringsAsFactors"] = false));
if (ret != MGRS_NO_ERROR) {
Rcpp::warning("Error converting MGRS to UGM");
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["zone"] = NA_INTEGER,
_["hemisphere"] = NA_STRING,
_["easting"] = NA_REAL,
_["northing"] = NA_REAL,
_["stringsAsFactors"] = false));
} else {
h_buf[1] = '\0';
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["zone"] = zone,
_["hemisphere"] = std::string(h_buf),
_["easting"] = easting,
_["northing"] = northing,
_["stringsAsFactors"] = false));
}
}
//' Convert UPS to MGRS
@ -119,9 +150,9 @@ DataFrame mgrs_to_utm(std::string mgrs_string) {
//' @examples
//' ups_to_mgrs("N", 2426773, 1530125)
// [[Rcpp::export]]
std::string ups_to_mgrs(std::string hemisphere,
double easting, double northing,
long precision = 5) {
String ups_to_mgrs(std::string hemisphere,
double easting, double northing,
long precision = 5) {
char buf[80];
long ret;
@ -129,7 +160,12 @@ std::string ups_to_mgrs(std::string hemisphere,
ret = Convert_UPS_To_MGRS(hemisphere.c_str()[0], easting, northing,
precision, (char *)&buf);
return(std::string(buf));
if (ret != UPS_NO_ERROR) {
Rcpp::warning("Error converting UPS to MGRS");
return(NA_STRING);
} else {
return(std::string(buf));
}
}
@ -150,11 +186,21 @@ DataFrame mgrs_to_ups(std::string mgrs_string) {
ret = Convert_MGRS_To_UPS((char *)mgrs_string.c_str(), h_buf, &easting, &northing);
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["hemisphere"] = std::string(h_buf),
_["easting"] = easting,
_["northing"] = northing,
_["stringsAsFactors"] = false));
if (ret != MGRS_NO_ERROR) {
Rcpp::warning("Error converting MGRS to UPS");
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["hemisphere"] = NA_STRING,
_["easting"] = NA_REAL,
_["northing"] = NA_REAL,
_["stringsAsFactors"] = false));
} else {
h_buf[1] = '\0';
return(DataFrame::create(_["mgrs"] = mgrs_string,
_["hemisphere"] = std::string(h_buf),
_["easting"] = easting,
_["northing"] = northing,
_["stringsAsFactors"] = false));
}
}
//' Convert UPS to Latitude/Longitude
@ -176,10 +222,20 @@ NumericVector ups_to_latlng(std::string hemisphere,
ret = Convert_UPS_To_Geodetic(hemisphere.c_str()[0], easting, northing, &lat, &lng);
NumericVector coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
NumericVector coords;
if (ret != UPS_NO_ERROR) {
Rcpp::warning("Error converting UPS to latitude/longitude");
coords = NumericVector::create(
_["lat"] = NA_REAL,
_["lng"] = NA_REAL
);
} else {
coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
}
return(coords);
@ -203,12 +259,22 @@ NumericVector utm_to_latlng(long zone, std::string hemisphere,
double lat, lng;
long ret;
ret = Convert_UTM_To_Geodetic(hemisphere.c_str()[0], zone, easting, northing, &lat, &lng);
NumericVector coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
ret = Convert_UTM_To_Geodetic(zone, hemisphere.c_str()[0], easting, northing, &lat, &lng);
NumericVector coords;
if (ret != UTM_NO_ERROR) {
Rcpp::warning("Error converting UTM to latitude/longitude");
coords = NumericVector::create(
_["lat"] = NA_REAL,
_["lng"] = NA_REAL
);
} else {
coords = NumericVector::create(
_["lat"] = degrees ? lat * 180.0/PI : lat,
_["lng"] = degrees ? lng * 180.0/PI : lng
);
}
return(coords);

35
tests/testthat/test-mgrs.R

@ -1,6 +1,39 @@
context("basic functionality")
test_that("we can do something", {
#expect_that(some_function(), is_a("data.frame"))
expect_equal(mgrs_to_latlng("33UXP04"),
structure(c(48.2053484084679, 16.3459269599006),
.Names = c("lat", "lng")))
expect_equal(latlng_to_mgrs(48.20535, 16.34593), "33UXP0000040000")
expect_equal(mgrs_to_latlng("33UXP0500444996"),
structure(c(48.2494747750754, 16.4144942785798),
.Names = c("lat", "lng")))
expect_equal(latlng_to_mgrs(48.24948, 16.41449), "33UXP0500344996")
expect_equal(mgrs_to_latlng("24XWT783908"),
structure(c(83.6273817605432, -32.6687891809823),
.Names = c("lat", "lng")))
expect_equal(latlng_to_mgrs(83.62738, -32.66879), "25XEN0410486507")
expect_equal(utm_to_mgrs(48, "N", 377299, 1483035), "48PUV7729983035")
expect_equal(mgrs_to_utm("48PUV7729883034"),
structure(list(mgrs = "48PUV7729883034", zone = 48, hemisphere = "N",
easting = 377298, northing = 1483034),
.Names = c("mgrs", "zone", "hemisphere", "easting", "northing"),
row.names = c(NA,
-1L), class = "data.frame"))
expect_equal(ups_to_mgrs("N", 2426773, 1530125), "ZGC2677330125")
expect_equal(mgrs_to_ups("ZGC2677330125"),
structure(list(mgrs = "ZGC2677330125", hemisphere = "N", easting = 2426773,
northing = 1530125), .Names = c("mgrs", "hemisphere",
"easting", "northing"),
row.names = c(NA, -1L), class = "data.frame"))
})

Loading…
Cancel
Save