-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR-Julia-Geostats-v-fixing.qmd
155 lines (125 loc) · 9.57 KB
/
R-Julia-Geostats-v-fixing.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "Untitled"
format: md
jupyter: julia-1.8
---
## Bibliothèques Julia
```{julia}
#| output: false
# Import package
using GeoDataFrames, Distributed, StatsAPI, Plots, DelimitedFiles, DataFrames, CSV, GeoStats, Rasters, Shapefile,GeoTables, CairoMakie; const GDF=GeoDataFrames; import WGLMakie as Mke
```
## Importer les données
```{julia}
#| output: false
# Get the current working directory
current_directory = pwd()
#import data
meuse = CSV.read(joinpath(current_directory,"meuse.txt"),DataFrame;delim="," )
meuse_grid = CSV.read(joinpath(current_directory, "meuse.grid.txt"),DataFrame; delim=",")
meuse_geom = georef(CSV.File(joinpath(current_directory,"meuse.txt")),(:x,:y,:cadmium))
#describe data
describe(meuse)
describe(meuse_grid)
```
## Transformation en données spatiale
```{julia}
#| output: false
#add log zinc to the data
logmeuse = log.(meuse.zinc)
meuse.logzinc = logmeuse
#create coord of data meuse
coord = [(x, y) for (x, y) in zip(meuse.x, meuse.y)]
#create spatial object of meuse
meuselog = georef(meuse[:,["x","y","logzinc"]],coord)
# list of properties with coordinates
props = (logzinc=meuse.logzinc,)
𝒟 = georef(props, coord)
```
## Variogramme
Le fit pour tester les différents variogramme va nous donner le SineHoleVariogram avec tous les paramètres optimisé. On décide à la base d'utiliser ce dernier mais il s'avère qu'il ne donne pas de bon résultat pour le Krigeage. On utilisera donc un variogramme de Matérn. Si vous voulez observer ce dernier dans le krigeage il suffira de mettre dans la fonction kriging, le variogramme désiré.
```{julia}
#| output: false
# empirical variogram
@time empvario = EmpiricalVariogram(meuselog, :logzinc, maxlag = 2000.)
## plot of empirical variogram
# Mke.plot(empvario) plot interactive
# CairoMakie.plot(empvario) #plot sans interaction
## plot theorical variogram
# CairoMakie.plot(MaternVariogram(range=691, nugget=0.04))
# CairoMakie.plot(SineHoleVariogram(range=691, nugget=0.25))
#fit theorical and empirical variogramand
@time fitvario = fit(Variogram, empvario)
# CairoMakie.plot(empvario)
# CairoMakie.plot!(fitvario, maxlag = 2000.)
# CairoMakie.current_figure()
```
## Création de la grille
```{julia}
#| output: false
# grid creation
# Convert the x and y coordinates to numeric arrays
x_coords = Float64.(meuse.x)
y_coords = Float64.(meuse.y)# Create the Cartesian grid dimensions, origin, and spacing
dims = (100, 100) # Number of grid points in x and y directions
origin = (minimum(x_coords), minimum(y_coords)) # Lower left corner of the grid
spacing = ((maximum(x_coords) - minimum(x_coords)) / (dims[1] - 1),
(maximum(y_coords) - minimum(y_coords)) / (dims[2] - 1))
# Cell spacing in x and y directions# Create the CartesianGrid
𝒢 = CartesianGrid(dims, origin, spacing)
```
## Krigeage
```{julia}
#| output: false
𝒫 = EstimationProblem(𝒟, 𝒢, :logzinc)
@time 𝒮 = Kriging(:logzinc => (variogram=MaternVariogram(nugget = 0.21,sill = 0.59,range=706.78),))
# perform estimation
@time Ω = solve(𝒫, 𝒮)
# CairoMakie.plot(Ω.geometry, color = Ω.logzinc_variance)
```
## Affichage du Krigeage
```{julia}
#Export Kriging
GeoTables.save(joinpath(current_directory,"data_geostats_julia/Kriging2.shp"), Ω,force=true )
#read kriging
shape = Shapefile.Handle(joinpath(current_directory,"data_geostats_julia/Kriging2.shp")).shapes
# read grid
grid_shp = Shapefile.Handle(joinpath(current_directory,"data_geostats_julia/boundary1.shp")).shapes
# Define a reducer function (e.g., to sum values within each cell)
reducer(cell_values) = sum(cell_values)
rasterized_krig = rasterize(shape, fill=Ω.logzinc, reducer = reducer, size=(100, 100))
Plots.plot(rasterized_krig)
```
```{julia}
#Crop
masked = mask(rasterized_krig,with=grid_shp)
Plots.plot(masked, title = "Krigeage logzinc")
```
# 4 - Comparaison
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Package | R : gstat | R : GeoModels | Julia : GeoStats |
+===========================================================+===========================================+============================================+===================================================================+
| Type de données d'entrée | sp, sf | matrices, vecteurs | dataframes |
| | | | |
| | | | Coordonées de type CartesianGrid |
| | | | |
| | | | Inconvénient : peu de documentation - difficile à prendre en main |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Documentation | CRAN | CRAN | <https://github.com/JuliaEarth/GeoStats.jl> |
| | | | |
| | | https://vmoprojs.github.io/GeoModels-page/ | https://juliapackages.com/p/geostats |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Estimation de paramètres du variogramme | Moindres carrés sur variogramme empirique | Vraisemblance composite sur les données | Moindres carrés sur le variogramme empirique |
| | | | |
| | 1 modèle à la fois au choix | 1 modèle à la fois | Tous les modèles disponibles à la fois |
| | | | |
| | | Paramètres fixes ou variables au choix | |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Vitesse d'exécution - estimation du variogramme empirique | 0.05s | 0.07s | 0.012s |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Vitesse d'exécution - estimation du variogramme théorique | 0.04s | 1.15s | 0.05s (TOUS LES MODELES) |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+
| Vitesse d'exécution - krigeage | 0.42s | 0.87s | 0.08s (Kriging) |
| | | | |
| | | | 10.6s (solve) |
+-----------------------------------------------------------+-------------------------------------------+--------------------------------------------+-------------------------------------------------------------------+