I have a set of coordinates supposed to be at the centroïds of a grid of 5km x 5km cells and an attached value (subsample provided at the end of this post as a “df” data.frame). I would like to create a 5km raster based on these coordinates.
I tried to do that both with stars and terra packages but I encounter the same problems : the points are not at the centroïd of the raster cells and some empty cells are created where they should be some value.
I’m uncertain about the crs : could be 4326 or 4258 but using one or the other does not change my problems.
library(terra)
library(stars)
library(sf)
library(mapview)
library(leaflet)
points <- vect(df, crs = "EPSG:4258")
points <- vect(df, crs = "EPSG:4326") # not certain which one is right one
# project in a local metric crs
points <- project(points, "EPSG:31370")
# Create a raster template and rasterize the points
r_template <- rast(ext(points), resolution = 5000, crs = crs(points))
r <- rasterize(points, r_template, field = "value")
# Four of the raster cells are empty while they should not be
# The points are not at the centroid of the cells
# x11(width = 8/2.54, height = 14/2.54)
my_lims <- as.vector(ext(r)) + c(-10000, 10000, -10000, 10000)
plot(r, xlim = my_lims[1:2], ylim = my_lims[3:4])
plot(points, col ="red", add = T, cex = 0.6)
I remember that there was a problem when plotting rasters and points with mapview, however both these code chunks provide similar results
mapview::mapview(r) + mapview::mapview(points)
leaflet() |>
addProviderTiles(providers$CartoDB.Positron) |>
addRasterImage(project(r, "EPSG:3857"), project = FALSE) |>
addCircles(data = project(points, "EPSG:4326"))
Maybe the problem comes from the fact that the points are not at the centroïd of the cells but at one of the corners ? I tried to correct that as if it was the bottom left corner but it does not work either
# Shift the coordinates by 2500 meters
tmp <- cbind(df, x = geom(points)[,"x"] - 2500,
y = geom(points)[,"y"] - 2500)
tmp <- tmp[,c("x", "y", "value")]
tmp <- vect(tmp, c("x", "y"), crs = "EPSG:31370")
# Rasterize the variable values
r_template <- rast(ext(tmp), resolution = 5000, crs = crs(tmp))
r <- rasterize(tmp, r_template, field = "value")
# x11(width = 8/2.54, height = 14/2.54)
my_lims <- as.vector(ext(r)) + c(-10000, 10000, -10000, 10000)
plot(r, xlim = my_lims[1:2], ylim = my_lims[3:4])
plot(points, col ="red", add = T, cex = 0.6)
Note that the real distance between points (after projection) is not exactly 5km but rather between 5000 and 5015 meters. Could that be the source of the problem ?
my_dist <- distance(points, points)
my_dist <- my_dist[upper.tri(my_dist)]
my_dist[my_dist<6000] |> round(1) |> table()
#>
#> 5000.5 5000.6 5000.7 5000.8 5000.9 5001 5001.1 5001.2 5001.3 5001.4 5001.5
#> 4 16 29 23 22 32 37 31 34 32 36
#> 5001.6 5001.7 5001.8 5001.9 5002 5002.1 5002.2 5002.3 5002.4 5002.5 5014.7
#> 29 35 19 31 22 29 18 9 3 1 10
#> 5014.8 5014.9 5015 5015.1 5015.2 5015.3 5015.4 5015.5
#> 54 63 80 74 73 57 49 6
# Create point vector objects and project it in a metric crs (31370 is typical for this area)
points <- st_as_sf(df, coords = c("lon", "lat"), crs = 4258)
points <- st_transform(points, 31370)
# Create a raster template and rasterize the points
r_template <- st_as_stars(st_bbox(points), values = NA,
dx = 5000, dy = 5000,
crs = st_crs(points))
r <- st_rasterize(points, template = r_template)
# for some reason, I'm not able to add the points on top of the
# raster despite their crs being the same
# But at least the two empty cells arevisible
# x11(width = 8/2.54, height = 14/2.54)
plot(r)
plot(st_geometry(points), col ="red", add = T, reset = F)
st_crs(r) == st_crs(points)
#> [1] TRUE
mapview::mapview(r) + mapview::mapview(points)
Based on Robert Hijmans's answer and after finding the right projection to get the original 5km grid
# The original projection used to create the grid seems to be :
my_proj <- "+proj=lcc +lat_2=50.569898649999999 +lat_1=50.569898649999999 +lon_0=4.553615160000000 +units=m +no_defs +a=6371229.0 +es=0.0"
points <- vect(df, crs = "EPSG:4326")
points <- project(points, my_proj)
# The distances are then exactly 5000m
#
# Small differences were due to the fact that I rounded the long lat values
# to 7 decimals to save space.
# I have now updated the df dump to keep all the decimals
my_dist <- distance(points, points)
my_dist <- my_dist[upper.tri(my_dist)]
my_dist[my_dist<6000] |> round(1) |> table()
#> [1] 5000
#> [2] 958
# Using type = xyz as we have now a perfectly regular grid
# the problem seems to be that we don't have a square grid so we need to
# impute NA values which seems impossible
tmp <- data.frame(geom(points)[,c("x","y")], value = df[,"value"])
rast(tmp, type = "xyz", crs = my_proj, digits = 0)
#> [1] Error in v[cells, ] <- xyz[, -c(1:2)] :
#> [2] NAs interdits dans les affectations indicées
df <-
structure(list(lon = c(5.4191010793597, 5.48833289530293, 5.55756308279838,
5.62679152130563, 5.41990964531345, 5.48920612673468, 5.55850097514038,
5.62779406965214, 5.69708528940996, 5.76637451357343, 5.83566162132305,
5.28199275995389, 5.35135693922935, 5.42071972338989, 5.49008099118482,
5.55944062137935, 5.62879849275588, 5.69815448411525, 5.76750847427803,
5.83686034208576, 5.90620996640225, 5.28267452673745, 5.3521036218039,
5.4215313178346, 5.49095749323822, 5.5603820264394, 5.62980479588006,
5.69922568002064, 5.76864455734138, 5.83806130634364, 5.9074758055511,
5.14436558491735, 5.21386215746602, 5.28335757095296, 5.35285170341665,
5.42234443290906, 5.49183563749693, 5.56132519526307, 5.63081298430763,
5.70029888274937, 5.76978276872696, 5.83926452040024, 5.90874401595155,
5.07535680221645, 5.14491958098964, 5.21448131975452, 5.28404189619397,
5.35360118800314, 5.42315907289072, 5.49271542858026, 5.56227013281143,
5.63182306334134, 5.70137409794578, 5.77092311442057, 5.84046999058279,
5.91001460427209, 5.00621755632929, 5.07584654683406, 5.14547461707207,
5.2151016443705, 5.28472750606753, 5.35435207951369, 5.42397524207311,
5.49359687112486, 5.56321684406418, 5.63283503830384, 5.7024513312754,
5.77206560043051, 5.84167772324221, 5.00664280448114, 5.07633721172005,
5.14603069609593, 5.21572313458991, 5.28541440419422, 5.35510438191345,
5.42479294476589, 5.49447996978479, 5.56416533401972, 5.63384891453779,
5.70353058842503, 5.77321023278765, 5.0070688524688, 5.07682879947071,
5.14658782100352, 5.21634579370107, 5.28610259420827, 5.35585809918252,
5.42561218529491, 5.49536472923165, 5.56511560769525, 5.63486469740593,
5.70461187510286, 5.77435701754552, 5.00749570255096, 5.07732131269208,
5.14714599474825, 5.21696962500464, 5.28679207975758, 5.35661323531593,
5.42643296800233, 5.49625115415457, 5.56606767012687, 5.63588239229119,
5.70569519703858, 5.77550596078043, 5.00792335699478, 5.07781475400005,
5.14770522029463, 5.21759463181373, 5.28748286450379, 5.3573697943238,
5.42725529724665, 5.4971392492604, 5.56702152636964, 5.6369020045968,
5.70678055998344, 5.77665706859161, 5.84653140650517, 5.00835181807602,
5.07830912602036, 5.14826550061836, 5.21822081745395, 5.28817495212235,
5.35812778023138, 5.42807917740282, 5.49802901927371, 5.5679771814977,
5.63792353974632, 5.70786796971039, 5.77781034710127, 5.84775054765223,
5.008781088079, 5.07880443138867, 5.14882683870639, 5.21884818526348,
5.2888683463026, 5.35888719707909, 5.42890461286231, 5.49892046893693,
5.56893464060431, 5.63894700318383, 5.70895743201418, 5.77896580245472,
5.84897198988682, 5.0092111692967, 5.0793006727506, 5.14938923755693,
5.21947673859308, 5.28956305074781, 5.35964804892264, 5.42973160803316,
5.49981360301037, 5.56989390880202, 5.63997240037395, 5.71004895271143,
5.7801234408205, 5.85019573972927, 5.92026572448933, 5.00964206403074,
5.07979785276178, 5.14995270017957, 5.22010648080621, 5.29025906917526,
5.36041033983307, 5.43056016734012, 5.50070842627239, 5.57085499122269,
5.64099973680199, 5.71114253764079, 5.78128326839046, 5.85142180372457,
5.92155801834023, 5.99169178695944, 5.0100737745915, 5.08029597408789,
5.15051722959525, 5.22073741527907, 5.29095640531632, 5.36117407389684,
5.43139029522467, 5.50160494351942, 5.5718178930176, 5.64202901797399,
5.71223819266298, 5.78244529137992, 5.85265018844247, 5.92285275819194,
5.99305287499466, 5.01050630329807, 5.08079503940472, 5.15108282883641,
5.22136954540064, 5.29165506291648, 5.36193925521588, 5.43222199614512,
5.50250315956605, 5.57278261935757, 5.6430602494169, 5.71333592366095,
5.78360951602772, 5.8538809004776, 5.92414995099479, 5.99441654158857,
5.01093965247837, 5.0812950513982, 5.15164950094694, 5.22200287457277,
5.29235504573544, 5.36270588790771, 5.43305527457669, 5.50340307924517,
5.57374917543303, 5.64409343667858, 5.71443573653993, 5.78477594859635,
5.85511394644963, 5.92544960372546, 5.99578279407474, 5.01137382446915,
5.08179601276448, 5.15221724898234, 5.2226374062102, 5.29305635754718,
5.36347397610546, 5.4338901350116, 5.50430470740797, 5.57471756645409,
5.64512858532799, 5.7155376372276, 5.78594459537211, 5.85634933300333,
5.9267517233871, 5.99715163981458, 5.01180882161605, 5.08229792620993,
5.15278607600968, 5.22327314374068, 5.29375900214004, 5.36424352395796,
5.43472658195914, 5.50520804892412, 5.57568779765069, 5.64616570095523,
5.71664163167414, 5.78711546266514, 5.85758706680872, 5.92805631700947,
5.99852308619747, 5.01224464627361, 5.08280079445125, 5.15335598510773,
5.223910090605, 5.29446298331674, 5.36501453562986, 5.43556461994575,
5.50611310868179, 5.57665987427264, 5.64720478917168, 5.71774772585236,
5.78828855680961, 5.85882715456118, 5.92936339164907, 5.99989714064088,
5.01268130080536, 5.08330462021545, 5.15392697936697, 5.22454825025701,
5.29516830489451, 5.36578701530162, 5.43640425351511, 5.50701989158776,
5.57763380158975, 5.64824585561006, 5.71885592575784, 5.78946388416379,
5.86006960298159, 5.93067295438925, 5.01311878758384, 5.08380940623996,
5.15449906188965, 5.22518762616379, 5.29587497070511, 5.36656096716967,
5.4372454872282, 5.50792840256751, 5.57860958489191, 5.64928890592458,
5.71996623740896, 5.79064145111018, 5.86131441881643, 5.93198501234035,
5.01355710899062, 5.08431515527264, 5.15507223578988, 5.22582822180558,
5.29658298459492, 5.36733639544643, 5.43808832566342, 5.50883864656533,
5.57958722948919, 5.65033394579099, 5.72107866684709, 5.79182126405562,
5.86256160883791, 5.93329957263982, 5.01399626741639, 5.08482187007185,
5.15564650419364, 5.22647004067596, 5.29729235042501, 5.36811330436042,
5.43893277341664, 5.50975062854436, 5.58056674071192, 5.65138098090672,
5.72219322013664, 5.79300332943141, 5.86381117984408, 5.93461664245237,
5.01443626526097, 5.0853295534065, 5.15622187023888, 5.22711308628184,
5.29800307207122, 5.3688916981563, 5.4397788351013, 5.51066435348672,
5.58154812391081, 5.65243001699099, 5.72330990336521, 5.79418765369343,
5.86506313865899, 5.93593622897005, 5.01487710493335, 5.08583820805609,
5.15679833707553, 5.22775736214356, 5.2987151534242, 5.369671581095,
5.44062651534849, 5.51157982639359, 5.58253138445706, 5.65348105978488,
5.72442872264369, 5.79537424332224, 5.86631749213277, 5.93725833941246,
5.01531878885177, 5.08634783681076, 5.15737590786561, 5.22840287179493,
5.29942859838951, 5.37045295745373, 5.44147581880703, 5.51249705228532,
5.58351652774241, 5.65453411505144, 5.72554968410634, 5.79656310482321,
5.86757424714178, 5.93858298102681, 5.01576131944373, 5.08685844247134,
5.15795458578325, 5.22904961878333, 5.30014341088769, 5.37123583152613,
5.44232675014357, 5.51341603620146, 5.58450355917926, 5.65558918857585,
5.72667279391097, 5.79775424472667, 5.86883341058873, 5.93991016108809,
5.01620469914603, 5.08737002784944, 5.15853437401476, 5.22969760666971,
5.3008595948543, 5.3720202076223, 5.44317931404265, 5.51433678320094,
5.58549248420081, 5.65664628616544, 5.72779805823897, 5.01664893040486,
5.08788259576743, 5.15911527575869, 5.23034683902874, 5.30157715424005,
5.3728060900689, 5.44403351520682, 5.51525929836208, 5.58648330826108,
5.65770541364986, 5.01709401567579, 5.08839614905856, 5.15969729422588,
5.2309973194488, 5.30229609301079, 5.37359348320919, 5.44488935835669,
5.51618358678274, 5.58747603683509, 5.65876657688116, 5.73005507530954,
5.01753995742386, 5.08891069056698, 5.16028043263955, 5.23164905153211,
5.30301641514768, 5.3743823914032, 5.44574684823103, 5.51710965358041,
5.58847067541891, 5.65982978173391, 5.73118684053406, 5.01798675812358,
5.08942622314776, 5.1608646942353, 5.23230203889475, 5.30373812464718,
5.3751728190277, 5.44660598958688, 5.51803750389225, 5.58946722952977,
5.66089503410531, 5.73232078524608, 5.01843442025904, 5.08994274966704,
5.16145008226124, 5.23295628516672, 5.30446122552118, 5.37596477047637,
5.44746678719961, 5.51896714287526, 5.59046570470619, 5.66196233991527,
5.73345691574684, 5.80494929946821, 5.01888294632387, 5.09046027300197,
5.16203659997799, 5.23361179399208, 5.30518572179704, 5.37675825015982,
5.44832924586302, 5.51989857570632, 5.59146610650803, 5.66303170510656,
5.73459523836189, 5.80615657315705, 5.01933233882137, 5.09097879604084,
5.16262425065878, 5.23426856902891, 5.30591161751768, 5.37755326250573,
5.44919337038942, 5.52083180758231, 5.59246844051665, 5.66410313564491,
5.73573575944119, 5.80736617840281, 5.87899425905174, 5.01978260026452,
5.0914983216831, 5.16321303758949, 5.23492661394948, 5.30663891674166,
5.37834981195886, 5.45005916560973, 5.5217668437202, 5.59347271233499,
5.66517663751908, 5.73687848535927, 5.80857812196563, 5.88027541347302,
5.02023373317599, 5.09201885283943, 5.16380296406873, 5.23558593244026,
5.30736762354323, 5.3791479029812, 5.45092663637358, 5.52270368935717,
5.59447892758764, 5.66625221674103, 5.02068574008828, 5.09254039243179,
5.16439403340788, 5.23624652820199, 5.30809774201245, 5.37994754005201,
5.45179578754937, 5.52364234975066, 5.02113862354367, 5.09306294339346,
5.16498624893117, 5.23690840494978, 5.45266662402438, 5.52458283017851,
5.02159238609431, 5.09358650866911, 5.16557961397572, 5.0220470303023,
5.09411109121486, 5.02250255873967, 5.09463669399833, 5.02295897398849,
5.09516331999868), lat = c(49.5100149170223, 49.5094694150977,
49.5088819585021, 49.5082525487942, 49.5549690001112, 49.5544229809136,
49.5538349672697, 49.553204960742, 49.5525329630038, 49.5518189758405,
49.5510630011485, 49.6008906614407, 49.6004281971217, 49.5999236957368,
49.599377158628, 49.598788587249, 49.5981579831653, 49.5974853480543,
49.5967706837049, 49.5960139920178, 49.5952152750054, 49.6458468599545,
49.6453839568406, 49.6448789767815, 49.6443319211221, 49.6437427913193,
49.6431115889421, 49.6424383156713, 49.6417229732999, 49.6409655637327,
49.6401660889864, 49.6916039473286, 49.6912248422192, 49.6908036178773,
49.690340275426, 49.6898348161011, 49.6892872412502, 49.6886975523334,
49.6880657509232, 49.6873918387041, 49.6866758174729, 49.6859176891384,
49.685117455722, 49.7368993021074, 49.7365619973096, 49.736182532283,
49.7357609080408, 49.7352971257086, 49.7347911865247, 49.7342430918399,
49.7336528431175, 49.7330204419332, 49.7323458899753, 49.7316291890444,
49.7308703410537, 49.7300693480287, 49.7821536023637, 49.7818581784039,
49.7815205532778, 49.781140727888, 49.78071870325, 49.780254480492,
49.7797480608549, 49.7791994456923, 49.7786086364709, 49.7779756347697,
49.7773004422807, 49.7765830608084, 49.7758234922703, 49.8271132384643,
49.8268175338653, 49.8264795880135, 49.8260994018137, 49.8256769762836,
49.8252123125535, 49.8247054118674, 49.8241562755817, 49.8235649051663,
49.8229313022036, 49.8222554683894, 49.8215374055322, 49.8720733268332,
49.8717773412465, 49.871439074271, 49.8710585268133, 49.8706356998932,
49.8701705946438, 49.8696632123115, 49.8691135542559, 49.8685216219499,
49.8678874169795, 49.8672109410442, 49.8664921959563, 49.9170338401996,
49.916737573276, 49.9163989847777, 49.916018075613, 49.9155948468041,
49.9151292994867, 49.9146214349097, 49.9140712544357, 49.913478759541,
49.9128439518149, 49.9121668329606, 49.9114474047945, 49.961994751266,
49.9616982026555, 49.9613592922342, 49.9609780209126, 49.9605543897151,
49.9600883997796, 49.9595800523579, 49.9590293488158, 49.9584362906325,
49.9578008794011, 49.9571231168282, 49.9564030047343, 49.9556405450535,
50.0069560327083, 50.0066592020601, 50.0063199693149, 50.0059383353853,
50.0055143012979, 50.0050478681931, 50.0045390373256, 50.0039878100639,
50.0033941878906, 50.0027581724023, 50.0020797653092, 50.001358968436,
50.000595783721, 50.0519176571759, 50.0516205441383, 50.0512809886674,
50.0508989916777, 50.0504745541981, 50.0500076773715, 50.0494983624554,
50.0489466108212, 50.0483524239549, 50.0477158034564, 50.0470367510399,
50.0463152685339, 50.0455513578809, 50.0968795972918, 50.0965822015123,
50.0962423229128, 50.0958599624098, 50.0954351210345, 50.0949677999323,
50.0944580003635, 50.0939057237024, 50.0933109714383, 50.0926737451747,
50.0919940466294, 50.0912718776351, 50.0905072401384, 50.0897001362007,
50.1418418256521, 50.1415441467774, 50.1412039446455, 50.140821220175,
50.1403959743994, 50.1399282084665, 50.1394179236394, 50.1388651212955,
50.1382698029272, 50.1376319701417, 50.1369516246606, 50.1362287683204,
50.1354634030723, 50.1346555309819, 50.1338051542296, 50.1868043148266,
50.1865063525025, 50.1861658264335, 50.1857827375403, 50.1853570868584,
50.1848888755385, 50.1843781048461, 50.1838247761619, 50.1832288909815,
50.1825904509156, 50.1819094576896, 50.1811859131441, 50.1804198192344,
50.1796111780308, 50.1787599917186, 50.2317670373586, 50.2314687912301,
50.2311279408183, 50.230744487046, 50.2303184309508, 50.229849773686,
50.2293385165201, 50.2287846608365, 50.2281882081344, 50.2275491600277,
50.2268675182459, 50.2261432846335, 50.2253764611502, 50.2245670498708,
50.2237150529854, 50.2767299657645, 50.2764314354759, 50.2760902603148,
50.2757064412058, 50.2752799791892, 50.2748108754206, 50.2742991311712,
50.2737447478277, 50.2731477268924, 50.272508069983, 50.2718257788325,
50.2711008552897, 50.2703333013185, 50.2695231189983, 50.268670310524,
50.3216930725347, 50.3213942577293, 50.3210527574112, 50.3206685725072,
50.3202417040597, 50.3197721532269, 50.3192599212828, 50.3187050096173,
50.3181074197359, 50.3174671532597, 50.3167842119259, 50.3160585975871,
50.3152903122115, 50.3144793578834, 50.3136257368022, 50.3666563301329,
50.3663572304531, 50.3660154045695, 50.3656308534109, 50.3652035780218,
50.3647335795632, 50.3642208593118, 50.3636654186605, 50.3630672591183,
50.3624263823098, 50.3617427899759, 50.3610164839734, 50.360247466275,
50.3594357389693, 50.3585813042608, 50.411619710996, 50.4113203260836,
50.4109781742251, 50.410593256351, 50.4101655735086, 50.4096951268612,
50.4091819176884, 50.4086259473861, 50.4080272174666, 50.4073857295583,
50.4067014854058, 50.40597448687, 50.405204735928, 50.404392234673,
50.4035369853143, 50.4565831875349, 50.4562835170308, 50.4559410387867,
50.4555557537356, 50.4551276629268, 50.4546567675262, 50.4541430688164,
50.4535865681963, 50.4529872671815, 50.452345167404, 50.4516602706124,
50.4509325786716, 50.450162093563, 50.4493488173846, 50.5015467321338,
50.5012467756778, 50.5009039706369, 50.5005183179458, 50.5000898186563,
50.499618473937, 50.4991042850731, 50.4985472534668, 50.4979473806369,
50.4973046682192, 50.4966191179659, 50.4958907317463, 50.4951195115461,
50.4943054594679, 50.5465103171504, 50.5462100743817, 50.5458669421314,
50.5454809213364, 50.5450520130508, 50.5445802184457, 50.5440655388093,
50.5435079755467, 50.5429075301804, 50.5422642043494, 50.5415779998102,
50.5408489184359, 50.5400769622168, 50.5392621332601, 50.5914739149161,
50.5911733854729, 50.5908299255998, 50.5904435362359, 50.5900142184375,
50.5895419733783, 50.5890268023493, 50.588468706759, 50.5878676881329,
50.5872237481139, 50.5865368884623, 50.5858071110554, 50.5850344178879,
50.5842188110717, 50.6364374977355, 50.6361366812553, 50.6357928933451,
50.6354061349461, 50.6349764071169, 50.6345037110339, 50.633988047991,
50.6334294193997, 50.6328278267889, 50.6321832718054, 50.631495756213,
50.6307652818935, 50.629991850846, 50.6291754651869, 50.6814010378873,
50.6810999340066, 50.6807558176439, 50.6803686897425, 50.6799385513634,
50.6794654036855, 50.6789492480058, 50.6783900857387, 50.6777879184167,
50.67714274769, 50.6764545753266, 50.6757234032124, 50.6749492333509,
50.6741320678631, 50.7263645076233, 50.7260631159777, 50.7257186707462,
50.725331172874, 50.7249006234246, 50.7244270235796, 50.7239103746385,
50.7233506780193, 50.7227479352576, 50.7221021480075, 50.7214133180409,
50.7206814472477, 50.719906537636, 50.7190885913317, 50.7713278791692,
50.7710261993936, 50.7706814248758, 50.7702935565634, 50.769862595522,
50.7693885429359, 50.7688714001077, 50.7683111684583, 50.767707849527,
50.7670614449712, 50.7663719565671, 50.7656393862086, 50.7648637359083,
50.764045007797, 50.8162911247241, 50.8159891564523, 50.81564405223,
50.8152558130067, 50.8148244398505, 50.8143499339482, 50.8138322966055,
50.8132715292463, 50.8126676334133, 50.8120206107679, 50.8113304630899,
50.8612542164607, 50.8609519593259, 50.8606065249797, 50.8602179143737,
50.8597861285785, 50.8593111687836, 50.8587930362974, 50.8582317325472,
50.857627259079, 50.856979617558, 50.9062171265256, 50.9059145801599,
50.9055688152694, 50.9051798328078, 50.9047476338482, 50.9042722195828,
50.9037535913227, 50.9031917504985, 50.9025866986598, 50.9019384374754,
50.9012469687329, 50.9511798270386, 50.9508769910733, 50.9505308952171,
50.950141540426, 50.9497089277754, 50.9492330584601, 50.9487139337942,
50.9481515552116, 50.9475459242652, 50.9468970426275, 50.9462049120904,
50.9961422900934, 50.9958391641589, 50.9954927369147, 50.9951030093189,
50.9946699824493, 50.9941936575033, 50.9936740357983, 50.993111118771,
50.992504907978, 50.9918554050956, 50.9911626119195, 51.0411044877573,
51.0408010714831, 51.0404543124274, 51.0400642115506, 51.0396307699328,
51.0391539887742, 51.038633869395, 51.0380704132353, 51.037463621855,
51.0368134969342, 51.0361200402728, 51.0353832537905, 51.0860663920713,
51.0857626850859, 51.0854155937944, 51.0850251191591, 51.0845912622626,
51.0841140243079, 51.083593406618, 51.0830294106363, 51.0824220379263,
51.0817712901717, 51.0810771691766, 51.0803396768649, 51.1310279750498,
51.1307239769811, 51.1303765530282, 51.1299857041558, 51.1295514314489,
51.1290737361131, 51.1285526194745, 51.1279880829797, 51.1273801281957,
51.12672875681, 51.1260339706307, 51.125295771586, 51.1245141617248,
51.1759892086812, 51.1756849191559, 51.1753371621152, 51.1749459385259,
51.1745112494756, 51.1740330961724, 51.1735114799455, 51.1729464022447,
51.1723378646406, 51.1716858688245, 51.1709904166084, 51.1702515099252,
51.1694691508281, 51.2209500649274, 51.2206454835714, 51.2202973930154,
51.2199057942283, 51.2194706883001, 51.2189920764418, 51.2184699599855,
51.2179043403842, 51.2172952192121, 51.2166425981643, 51.2659105157239,
51.2656056421622, 51.2652572176623, 51.2648652431954, 51.2644297198537,
51.2639506488511, 51.2634280315226, 51.2628618693247, 51.3108705329801,
51.3105653668368, 51.3102166079634, 51.3098242573333, 51.3083856664589,
51.3078189609664, 51.355830088579, 51.3555246294773, 51.3551755357998,
51.4007891543773, 51.4004834019394, 51.4457477022054, 51.4454416560526,
51.4907057038674, 51.4903993636202), value = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 74L,
75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 88L, 89L,
90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 125L,
126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L,
137L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L,
159L, 160L, 161L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L,
182L, 183L, 184L, 185L, 186L, 198L, 199L, 200L, 201L, 202L, 203L,
204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 225L, 226L, 227L,
228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L,
239L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L, 263L,
264L, 265L, 266L, 267L, 268L, 284L, 285L, 286L, 287L, 288L, 289L,
290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 315L, 316L,
317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L,
328L, 329L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 360L,
361L, 362L, 363L, 364L, 365L, 366L, 392L, 393L, 394L, 395L, 396L,
397L, 398L, 399L, 400L, 401L, 402L, 403L, 404L, 405L, 406L, 432L,
433L, 434L, 435L, 436L, 437L, 438L, 439L, 440L, 441L, 442L, 443L,
444L, 445L, 446L, 472L, 473L, 474L, 475L, 476L, 477L, 478L, 479L,
480L, 481L, 482L, 483L, 484L, 485L, 516L, 517L, 518L, 519L, 520L,
521L, 522L, 523L, 524L, 525L, 526L, 527L, 528L, 529L, 561L, 562L,
563L, 564L, 565L, 566L, 567L, 568L, 569L, 570L, 571L, 572L, 573L,
574L, 604L, 605L, 606L, 607L, 608L, 609L, 610L, 611L, 612L, 613L,
614L, 615L, 616L, 617L, 647L, 648L, 649L, 650L, 651L, 652L, 653L,
654L, 655L, 656L, 657L, 658L, 659L, 660L, 692L, 693L, 694L, 695L,
696L, 697L, 698L, 699L, 700L, 701L, 702L, 703L, 704L, 705L, 738L,
739L, 740L, 741L, 742L, 743L, 744L, 745L, 746L, 747L, 748L, 749L,
750L, 751L, 787L, 788L, 789L, 790L, 791L, 792L, 793L, 794L, 795L,
796L, 797L, 798L, 799L, 800L, 836L, 837L, 838L, 839L, 840L, 841L,
842L, 843L, 844L, 845L, 846L, 881L, 882L, 883L, 884L, 885L, 886L,
887L, 888L, 889L, 890L, 925L, 926L, 927L, 928L, 929L, 930L, 931L,
932L, 933L, 934L, 935L, 970L, 971L, 972L, 973L, 974L, 975L, 976L,
977L, 978L, 979L, 980L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L,
1021L, 1022L, 1023L, 1024L, 1025L, 1060L, 1061L, 1062L, 1063L,
1064L, 1065L, 1066L, 1067L, 1068L, 1069L, 1070L, 1071L, 1107L,
1108L, 1109L, 1110L, 1111L, 1112L, 1113L, 1114L, 1115L, 1116L,
1117L, 1118L, 1153L, 1154L, 1155L, 1156L, 1157L, 1158L, 1159L,
1160L, 1161L, 1162L, 1163L, 1164L, 1165L, 1199L, 1200L, 1201L,
1202L, 1203L, 1204L, 1205L, 1206L, 1207L, 1208L, 1209L, 1210L,
1211L, 1243L, 1244L, 1245L, 1246L, 1247L, 1248L, 1249L, 1250L,
1251L, 1252L, 1280L, 1281L, 1282L, 1283L, 1284L, 1285L, 1286L,
1287L, 1309L, 1310L, 1311L, 1312L, 1313L, 1314L, 1330L, 1331L,
1332L, 1342L, 1343L, 1353L, 1354L, 1359L, 1360L)),
row.names = c(NA, -512L), class = "data.frame")
The extent of the raster is not the same of that of the points. You can add 2500 (half a cell size at each side) and then things look pretty good:
library(terra)
# now using the updated coordinate reference system
prj <- "+proj=lcc +lat_2=50.569898649999999 +lat_1=50.569898649999999 +lon_0=4.553615160000000 +units=m +no_defs +a=6371229.0 +es=0.0"
points <- vect(df, crs = "EPSG:4326") |> project(prj)
r_template <- rast(ext(points)+2500, resolution=5000, crs=points)
r <- rasterize(points, r_template, field = "value")
plot(r)
lines(r, col="gray")
points(points, cex=.5, col="red")
Your comment
# project in a local metric crs
suggests that you do not know in which coordinate reference system these points are equally spaced and that may cause the variation in interpoint distances. The variant of the lon/lat crs is probably not very important.
With your updated example I can now do the below to get the same result
library(terra)
prj <- "+proj=lcc +lat_2=50.569898649999999 +lat_1=50.569898649999999 +lon_0=4.553615160000000 +units=m +no_defs +a=6371229.0 +es=0.0"
m <- as.matrix(df)
m[,1:2] <- project(m[,1:2], "EPSG:4326", prj) |> round(6)
rr <- rast(m, type="xyz", crs=prj)
rr
#class : SpatRaster
#dimensions : 45, 15, 1 (nrow, ncol, nlyr)
#resolution : 5000, 5000 (x, y)
#extent : 30000, 105000, 6215514, 6440514 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=lcc +lat_0=0 +lon_0=4.55361516 +lat_1=50.56989865 +lat_2=50.56989865 +x_0=0 +y_0=0 +R=6371229 +units=m +no_defs
#source(s) : memory
#name : value
#min value : 1
#max value : 1360