Kriging Interpolation

45 Views Asked by At

Hi i want to plot a map of Argentina with certain reports of hail using the interpolation of Kriging but i cant in R or python. Can anybody help me?

Regarding the reports, these come in the following format:

enter image description here

Thank you!

I tried this using chatgpt to help me:

library(gstat)
library(sp)

# Cargar el archivo CSV con los datos de reportes de granizo

datos <- read.csv("C:/Users/Usuario/Desktop/Agus/FACULTAD/Tesis/SI - NO ROMINA.csv", nrows = 7089)  # Reemplaza con la ubicación y nombre de tu archivo CSV

# Convertir las columnas de Latitud y Longitud a formato numérico

datos$Latitud <- as.numeric(as.character(datos$Latitud))
datos$Longitud <- as.numeric(as.character(datos$Longitud))

Crear un objeto SpatialPoints con los datos de latitud y longitud

datos_sp <- SpatialPointsDataFrame(
coords = datos[, c("Longitud", "Latitud")],
data = datos,
proj4string = CRS("+proj=longlat +datum=WGS84")
)

# Crear una cuadrícula regular para la predicción

grd <- expand.grid(
Longitud = seq(min(datos$Longitud, na.rm = TRUE), max(datos$Longitud, na.rm = TRUE), length.out = 100),
Latitud = seq(min(datos$Latitud, na.rm = TRUE), max(datos$Latitud, na.rm = TRUE), length.out = 100)
)
grd_sp <- SpatialPoints(coords = grd, proj4string = CRS("+proj=longlat +datum=WGS84"))

# Realizar la interpolación mediante Kriging

kriged <- krige(rep(1, nrow(datos_sp)) ~ 1, datos_sp, grd_sp)

# Obtener los valores predichos para cada punto de la grilla

predicted_values <- vector("numeric", length = nrow(grd))
for (i in 1:nrow(grd)) {
predicted_values[i] <- krige(rep(1, nrow(datos_sp)) ~ 1, datos_sp, grd_sp[i, ])$predictions
}

# Crear una matriz con los valores predichos

kriged_values <- matrix(predicted_values, nrow = 100)

# Graficar el mapa de interpolación de Kriging

par(mar = c(1, 1, 1, 1)) # Ajustar los márgenes si es necesario
image(
x = seq(min(datos$Longitud, na.rm = TRUE), max(datos$Longitud, na.rm = TRUE), length.out = 100),
y = seq(min(datos$Latitud, na.rm = TRUE), max(datos$Latitud, na.rm = TRUE), length.out = 100),
z = kriged_values,
col = terrain.colors(50),
main = "Mapa de Interpolación de Kriging"
)
0

There are 0 best solutions below