diff --git a/NEWS.md b/NEWS.md index e5e721c64..a50240964 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ - `split` 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 @@ -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` 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` now works for lon/lat data [#1615](https://github.com/rspatial/terra/issues/1615) by Wencheng Lau-Medrano ## new diff --git a/src/distRaster.cpp b/src/distRaster.cpp index 6fa4bc9dd..140155144 100644 --- a/src/distRaster.cpp +++ b/src/distRaster.cpp @@ -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; @@ -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); @@ -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> pxy = p.coordinates(); SpatOptions ops(opt); bool setNA = false; diff --git a/src/distVector.cpp b/src/distVector.cpp index 05ed4fcce..4bc679bbe 100644 --- a/src/distVector.cpp +++ b/src/distVector.cpp @@ -30,7 +30,7 @@ std::vector SpatVector::nearestDistLonLat(std::vector x, std::ve double m = 1; if (unit == "km") { r = 6378.137; - m = 1/1000; + m = 0.001; } std::vector inside; @@ -170,6 +170,11 @@ std::vector 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(); diff --git a/src/geosphere.cpp b/src/geosphere.cpp index 4edfb1ac0..671651af8 100644 --- a/src/geosphere.cpp +++ b/src/geosphere.cpp @@ -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); }