Skip to content

seegSDM::nearestLand() does not permit lon/lat coordinates #51

@edwardlavender

Description

@edwardlavender

I am just leaving this comment here for others that are interested in taking advantage of this package - I appreciate it is not necessarily designed for wider use though. I noticed that seegSDM::nearestLand() assumes planar coordinates. It would be very helpful to add this to the documentation. Alternatively, you could consider tweaking the function to enable correct geodesic distance calculations, e.g.,:

nearestLand2 <- 
  function(points, raster, max_distance, lonlat) {
    nearest <- function(lis, raster) {
      neighbours <- matrix(lis[[1]], ncol = 2)
      point <- lis[[2]]
      land <- !is.na(neighbours[, 2])
      if (!any(land)) {
        return(c(NA, NA))
      }
      else {
        coords <- raster::xyFromCell(raster, neighbours[land, 1])
        if (nrow(coords) == 1) {
          return(coords[1, ])
        }
        # Use raster::pointDistance() or a related function to permit lon/lat distance calculations 
        dists <- raster::pointDistance(coords, cbind(point[1], point[2]), lonlat = lonlat)
        return(coords[which.min(dists), ])
      }
    }
    neighbour_list <- raster::extract(raster, points, buffer = max_distance, 
                                      cellnumbers = TRUE)
    neighbour_list <- lapply(1:nrow(points), function(i) {
      list(neighbours = neighbour_list[[i]], point = as.numeric(points[i, 
      ]))
    })
    return(t(sapply(neighbour_list, nearest, raster)))
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions