Geographic Information Systems Asked on June 11, 2021
I have a set of sf lines, which on an irregular grid, in other words, they have random coordinates.
Is there any way to interpolate LINESTRING coordinates to a regualr grid for example as defined below:
> class(lines)
[1] "sf" "data.frame"
> lines
Geometry set for 9 features
geometry type: LINESTRING
dimension: XY
bbox: xmin: 160.6388 ymin: -17.22663 xmax: 277.5178 ymax: 12.53342
CRS: +proj=longlat +datum=WGS84 +no_defs
First 5 geometries:
LINESTRING (160.6388 -8.756245, 160.7918 -8.826...
LINESTRING (160.6388 -8.756245, 160.7918 -8.826...
LINESTRING (187.4896 2.427888, 187.5281 2.43538...
LINESTRING (160.6388 -8.756245, 160.7918 -8.826...
LINESTRING (160.6388 -8.756245, 160.7918 -8.826...
#Pre-defined regular grid
desired_lon <- seq(0,360,0.5)
desired_lat <- seq(90,-90,-0.5)
I have tried interp()
from interp package in R didn’t work well. since the input should be points, Lines IDs and Lines vertices sort ID will be lost.
A suggestion in R or python is appreciated.
I came up with this solution and it has been tested to interpolate to a finer grid. Maybe someone will find it useful :)
#A function to interpolate lines to another coordination
lines_interpolation<-function(2Darray,raw_line){
require(sf)
DIMNAMES<-dimnames(2Darray)
lon<-as.numeric(DIMNAMES[[1]])
lat<-as.numeric(DIMNAMES[[2]])
interpolated_LINE<-raw_line
raw_line_crs<-st_coordinates(raw_line)
interpolated_crs<-raw_line_crs
for (k in 1:nrow(raw_line_crs)) {
lon_div<-abs(lon-raw_line_crs[k,1])
near_lon_index<-which(lon_div ==min(lon_div))
interpolated_crs[k,1]<-lon[near_lon_index]
lat_div<-abs(lat-raw_line_crs[k,2])
near_lat_index<-which(lat_div ==min(lat_div))
interpolated_crs[k,2]<-lat[near_lat_index]
}
interpolated_line_crs<-unique(interpolated_crs)
interpolated_line<-st_linestring(x = as.matrix(interpolated_line_crs),
dim= "XYM")
interpolated_line<-st_as_sf(interpolated_line)
#Convert data frame to sf object and assign CRS
st_crs(interpolated_line)<-"+proj=longlat +datum=WGS84"
return(interpolated_line)
}
Correct answer by ahmathelte on June 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP