Skip to content

How to find ocean depth at specific locations?

1 message · Paul Hiemstra

#
Hi Dale,

Going from latlong to utm destroys the grid yeah. I used the inverse 
distance weighted interpolation of the gstat package. Along the lines of:

# 1. Create target grid, for example by using a bbox polygon of your
# area of interest using spsample. Getting a bbox-polygon from a 
SpatialObject
# little home brewn function :)

bbox2polygon = function(obj) {
# This function converts a bounding box of a spatial object to a SpatialPolygons-object
# obj :  Spatial object
  bb = bbox(obj)
  pl = Polygon(data.frame(rbind(c(bb["x","min"], bb["y","max"]),
                                c(bb["x","max"], bb["y","max"]),
                                c(bb["x","max"], bb["y","min"]),
                                c(bb["x","min"], bb["y","min"]),
                                c(bb["x","min"], bb["y","max"])
                            )))
  pls = Polygons(list(pl), ID = "bbox")
  spat_poly = SpatialPolygons(list(pls), proj4string = CRS(proj4string(obj)))
  return(spat_poly)
}


data(meuse)
coordinates(meuse) =~x+y
bb = bbox2polygon(meuse)
spplot(meuse, "zinc",sp.layout= list("sp.polygons",bb))
grd = spsample(bb, cellsize = c(50,50), type = "regular") # Cell size in m
spplot(meuse, "zinc",sp.layout= list("sp.points",grd))

# 2. Interpolate to this grid, mind to keep either the number of max
# allowd points small (nmax), or limit the search radius (maxdist). 
Experiment a bit
require(gstat)
new_grd = krige(zinc~1, meuse, grd, maxdist = 50) # In your case you 
ofcourse
    # have much more points and more regularly spaced
gridded(new_grd) = TRUE
spplot(new_grd)

Good luck!

cheers,
Paul

Dale Steele schreef: