[R-sig-Geo] Again, coordinate interpolation with NAs

Karl Ove Hufthammer karl at huftis.org
Wed Aug 29 19:04:26 CEST 2012


Mario A. Pardo skreiv:

> I need a new set of coordinates interpolated at a given resolution,
> let's say one data point each xx kilometers or degrees, or at a given
> proportion of the total length, etc.
>
> Think of this coordinates as transect points, and I need to keep the
> NAs, since they mark cut offs in my transect.

I would recommend using SpatialLines objects instead, which can 
easily hold polylines and multiple lines. Example:

library(sp)
library(geosphere)

lon=c(-115.86, -115.83, NA, -115.78, -115.78, -115.75, NA, -115.68, -115.66)
lat=c(  29.76,   29.61, NA,   29.55,   29.53,   29.41, NA, 29.13, 29.09)
d=data.frame(lon,lat)        # Convert to data frame
d$group=cumsum(is.na(d$lon)) # Add line ID
d=d[!is.na(d$lon),]          # Remove NA rows

d.lines=split(d,d$group)    # Split by line ID

# Convert into SpatialLines object
d.sp=SpatialLines(lapply(d.lines, function(df) Lines(Line(df[c("lon","lat")]), ID=df$group[1])),
                  proj4string=CRS("+proj=longlat"))

# Add intermediate points
d.sp.int=makeLine(d.sp, interval=1000)


# The actual coordinates are available in this list
# (though I don’t know why there are duplicates –
# a small bug in makeLine, perhaps?):
lapply(d.sp.int at lines, function(x) x at Lines[[1]]@coords)

-- 
Karl Ove Hufthammer
E-mail: karl at huftis.org
Jabber: huftis at jabber.no



More information about the R-sig-Geo mailing list