Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Dec 27, 2024
1 parent 9796b5f commit b14c13e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `split<SpatVector,SpatVector>` did not work properly [#1619](https://github.com/rspatial/terra/issues/1619) by Michael Sumner
- `autocor` improved handling of NA cells for global Moran computation [#1992](https://github.com/rspatial/terra/issues/1592) by Nicholas Berryman
- `shade` is more memory-safe. [#1452](https://github.com/rspatial/terra/issues/1452) by Francis van Oordt and Chris English
- fixed bug in `rasterize` revealed when using `crop(mask=TRUE)` [#1686](https://github.com/rspatial/terra/issues/1686) by edixon1

## enhancements

Expand All @@ -19,6 +20,7 @@
- better handling of the 32 connection limiations set by the HDF4 library [#1481](https://github.com/rspatial/terra/issues/1481) by Dimitri Falk
- When using RStudio a once per session warning is given when using draw, sel or click [#1063](https://github.com/rspatial/terra/issues/1063) by Sergei Kharchenko
- `distance<SpatRaster>` from lon and lat lines/polygons computes distance to the edges instead of the nodes [#1462](https://github.com/rspatial/terra/issues/1462) by Derek Friend
- `distance<SpatVector,SpatVector>` now works for lon/lat data [#1615](https://github.com/rspatial/terra/issues/1615) by Wencheng Lau-Medrano

## new

Expand Down
24 changes: 18 additions & 6 deletions src/distRaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,20 @@ SpatRaster SpatRaster::distance_vector(SpatVector p, bool rasterize, std::string
}
bool lonlat = is_lonlat();

/*
double m=1;
if (!source[0].srs.m_dist(m, lonlat, unit)) {
out.setError("invalid unit");
if ((unit != "m") && (unit != "km")) {
out.setError("invalid unit. Must be 'm' or 'km'");
return(out);
}
*/

if (rasterize) {
//SpatRaster SpatRaster::distance_rasterize(SpatVector p, double target, double exclude, std::string unit, const std::string& method, SpatOptions &opt) {

// double target = NAN;

if ((method != "geo") && (method != "cosine") && (method != "haversine")) {
out.setError("invalid method. Must be 'geo', 'cosine' or 'haversine'");
return(out);
}

double exclude = NAN;

SpatRaster x;
Expand Down Expand Up @@ -410,6 +412,11 @@ SpatRaster SpatRaster::distance_vector(SpatVector p, bool rasterize, std::string
} else {

if ((p.type() == "polygons") || (p.type() == "lines")) {

if ((method != "geo") && (method != "cosine")) {
out.setError("invalid method. Must be 'geo' or 'cosine'");
return(out);
}

if (p.nrow() > 1) {
p = p.aggregate(true);
Expand Down Expand Up @@ -443,6 +450,11 @@ SpatRaster SpatRaster::distance_vector(SpatVector p, bool rasterize, std::string

} else {
//p = p.aggregate(false);
if ((method != "geo") && (method != "cosine") && (method != "haversine")) {
out.setError("invalid method. Must be 'geo', 'cosine' or 'haversine'");
return(out);
}

std::vector<std::vector<double>> pxy = p.coordinates();
SpatOptions ops(opt);
bool setNA = false;
Expand Down
7 changes: 6 additions & 1 deletion src/distVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::vector<double> SpatVector::nearestDistLonLat(std::vector<double> x, std::ve
double m = 1;
if (unit == "km") {
r = 6378.137;
m = 1/1000;
m = 0.001;
}

std::vector<int> inside;
Expand Down Expand Up @@ -170,6 +170,11 @@ std::vector<double> SpatVector::distance(SpatVector x, bool pairwise, std::strin
return(d);
}

if ((method != "geo") && (method != "cosine")) {
setError("invalid method. Must be 'geo' or 'cosine'");
return(d);
}

std::string gtype = type();
std::string xtype = x.type();

Expand Down
6 changes: 2 additions & 4 deletions src/geosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@ double alongTrackDistance_geo(double lon1, double lat1, double lon2, double lat2
double d, b2, b3, azi;
geod_inverse(&geod, lat1, lon1, lat2, lon2, &d, &b2, &azi);
geod_inverse(&geod, lat1, lon1, plat, plon, &d, &b3, &azi);
double toRad = M_PI / 180.;
b2 *= toRad;
b3 *= toRad;
deg2rad(b2);
deg2rad(b3);
double xtr = asin(sin(b3-b2) * sin(d));

double bsign = get_sign(cos(b2-b3));
return fabs(bsign * acos(cos(d) / cos(xtr)) * r);
}
Expand Down

0 comments on commit b14c13e

Please sign in to comment.