Skip to content
GitLab
Explorer
Connexion
S'inscrire
Navigation principale
Rechercher ou aller à…
Projet
E
Eo Toolbox Rtd
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de paquets
Registre de conteneur
Registre de modèles
Opération
Environnements
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté
Contribuer à GitLab
Donner votre avis
Conditions générales et politique de confidentialité
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
ELIE
Eo Toolbox Rtd
Validations
aa95deef
Valider
aa95deef
rédigé
1 year ago
par
Boris Nörgaard
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
first test
parent
9aa699eb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
2
Masquer les modifications d'espaces
En ligne
Côte à côte
Affichage de
2 fichiers modifiés
docs/source/index.md
+2
-0
2 ajouts, 0 suppression
docs/source/index.md
docs/source/other/reclassify_by_interval.ipynb
+233
-0
233 ajouts, 0 suppression
docs/source/other/reclassify_by_interval.ipynb
avec
235 ajouts
et
0 suppression
docs/source/index.md
+
2
−
0
Voir le fichier @
aa95deef
...
...
@@ -68,6 +68,8 @@ classification/extract_random_points
zonal_stats/index
composite/composites
other/reclassify_by_interval
```
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
docs/source/other/reclassify_by_interval.ipynb
0 → 100644
+
233
−
0
Voir le fichier @
aa95deef
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reclassify raster by defined intervals"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All libraries successfully imported!\n"
]
}
],
"source": [
"import rasterio\n",
"import numpy as np\n",
"import os\n",
"\n",
"print('All libraries successfully imported!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set directory"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"computer_path = '/export/miro/ndeffense/LBRAT2104/'\n",
"grp_letter = 'X'\n",
"\n",
"# Directory for all work files\n",
"work_path = f'{computer_path}GROUP_{grp_letter}/WORK/'\n",
"\n",
"input_file = f'{work_path}NDVI/T31UFS_20200417T104021_NDVI.tif'\n",
"\n",
"output_file = f'{input_file[:-4]}_reclassified.tif'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set parameters"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, -0.5, 0, 0.5, 1]\n"
]
}
],
"source": [
"nodata_val = -10000\n",
"\n",
"# User must defined intervals\n",
"interval = [-1,-0.5,0,0.5,1]\n",
"\n",
"dtype_out = 'int16'\n",
"\n",
"print(interval)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reclassify raster by defined intervals\n",
"\n",
"Return the indices of the bins to which each value in input array belongs.\n",
"\n",
"\n",
"| `right` | **order of bins** | **returned index `i` satisfies** |\n",
"| --- | --- | --- |\n",
"|``False``|increasing | ``bins[i-1] <= x < bins[i]`` |\n",
"|``True``| increasing | ``bins[i-1] < x <= bins[i]`` |\n",
"|``False``| decreasing | ``bins[i-1] > x >= bins[i]`` |\n",
"|``True``| decreasing | ``bins[i-1] >= x > bins[i]`` |\n",
"\n",
"\n",
"By default, `right` = False\n",
"\n",
"If values in `x` are beyond the bounds of `bins`, 0 or ``len(bins)`` is returned as appropriate.\n",
"\n",
"https://numpy.org/doc/stable/reference/generated/numpy.digitize.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original continuous values : [ 1.2 10. 12.4 15.5 20. ]\n",
"Interval : [ 0 5 10 15 20]\n",
"Reclassified discrete values : \n",
"[1 2 3 4 4]\n",
"[1 3 3 4 5]\n"
]
}
],
"source": [
"x = np.array([1.2, 10.0, 12.4, 15.5, 20.])\n",
"bins = np.array([0, 5, 10, 15, 20])\n",
"\n",
"inds_right_true = np.digitize(x,bins,right=True)\n",
"inds_right_false = np.digitize(x,bins,right=False)\n",
"\n",
"print(f'Original continuous values : {x}')\n",
"print(f'Interval : {bins}')\n",
"print('Reclassified discrete values : ')\n",
"print(inds_right_true)\n",
"print(inds_right_false)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"data type = int16\n",
"[[[4 4 4 ... 4 4 4]\n",
" [4 4 4 ... 4 4 3]\n",
" [4 4 4 ... 4 3 3]\n",
" ...\n",
" [4 4 4 ... 3 3 3]\n",
" [4 4 4 ... 3 3 3]\n",
" [4 4 4 ... 3 3 3]]]\n"
]
}
],
"source": [
"src = rasterio.open(input_file, 'r')\n",
"im_arr = src.read()\n",
"\n",
"# Update the dtype in raster metadata (= profile)\n",
"profile = src.profile\n",
"profile.update(dtype = dtype_out)\n",
"\n",
"\n",
"# Replace -10000 by np.nan\n",
"im_arr[im_arr==nodata_val] = np.nan\n",
"\n",
"# Create a mask with all no data value\n",
"mask = np.isnan(im_arr)\n",
"\n",
"# Convert interval into array\n",
"bins = np.array(interval)\n",
"\n",
"# Return the indices of the bins to which each value in input array belongs\n",
"im_arr_reclass = np.digitize(im_arr, bins, right=False)\n",
"\n",
"# Apply mask on reclassified raster\n",
"im_arr_reclass = np.where(mask, nodata_val, im_arr_reclass)\n",
"\n",
"# Change dtype of raster to match the dtype of profile\n",
"im_arr_reclass = im_arr_reclass.astype(dtype_out)\n",
"\n",
"print(f'data type = {im_arr_reclass.dtype}')\n",
"\n",
"print(im_arr_reclass)\n",
"\n",
"# Write output file\n",
"dst = rasterio.open(output_file, \"w\", **profile)\n",
"dst.write(im_arr_reclass)\n",
"\n",
"src.close()\n",
"dst.close()"
]
}
],
"metadata": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
},
"kernelspec": {
"display_name": "Python 3.6.12 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# Reclassify raster by defined intervals
%% Cell type:code id: tags:
```
python
import
rasterio
import
numpy
as
np
import
os
print
(
'
All libraries successfully imported!
'
)
```
%% Output
All libraries successfully imported!
%% Cell type:markdown id: tags:
## Set directory
%% Cell type:code id: tags:
```
python
computer_path
=
'
/export/miro/ndeffense/LBRAT2104/
'
grp_letter
=
'
X
'
# Directory for all work files
work_path
=
f
'
{
computer_path
}
GROUP_
{
grp_letter
}
/WORK/
'
input_file
=
f
'
{
work_path
}
NDVI/T31UFS_20200417T104021_NDVI.tif
'
output_file
=
f
'
{
input_file
[
:
-
4
]
}
_reclassified.tif
'
```
%% Cell type:markdown id: tags:
## Set parameters
%% Cell type:code id: tags:
```
python
nodata_val
=
-
10000
# User must defined intervals
interval
=
[
-
1
,
-
0.5
,
0
,
0.5
,
1
]
dtype_out
=
'
int16
'
print
(
interval
)
```
%% Output
[-1, -0.5, 0, 0.5, 1]
%% Cell type:markdown id: tags:
## Reclassify raster by defined intervals
Return the indices of the bins to which each value in input array belongs.
|
`right`
|
**order of bins**
|
**returned index `i` satisfies**
|
| --- | --- | --- |
|
``False``
|increasing |
``bins[i-1] <= x < bins[i]``
|
|
``True``
| increasing |
``bins[i-1] < x <= bins[i]``
|
|
``False``
| decreasing |
``bins[i-1] > x >= bins[i]``
|
|
``True``
| decreasing |
``bins[i-1] >= x > bins[i]``
|
By default,
`right`
= False
If values in
`x`
are beyond the bounds of
`bins`
, 0 or
``len(bins)``
is returned as appropriate.
https://numpy.org/doc/stable/reference/generated/numpy.digitize.html
%% Cell type:markdown id: tags:
### Example
%% Cell type:code id: tags:
```
python
x
=
np
.
array
([
1.2
,
10.0
,
12.4
,
15.5
,
20.
])
bins
=
np
.
array
([
0
,
5
,
10
,
15
,
20
])
inds_right_true
=
np
.
digitize
(
x
,
bins
,
right
=
True
)
inds_right_false
=
np
.
digitize
(
x
,
bins
,
right
=
False
)
print
(
f
'
Original continuous values :
{
x
}
'
)
print
(
f
'
Interval :
{
bins
}
'
)
print
(
'
Reclassified discrete values :
'
)
print
(
inds_right_true
)
print
(
inds_right_false
)
```
%% Output
Original continuous values : [ 1.2 10. 12.4 15.5 20. ]
Interval : [ 0 5 10 15 20]
Reclassified discrete values :
[1 2 3 4 4]
[1 3 3 4 5]
%% Cell type:code id: tags:
```
python
src
=
rasterio
.
open
(
input_file
,
'
r
'
)
im_arr
=
src
.
read
()
# Update the dtype in raster metadata (= profile)
profile
=
src
.
profile
profile
.
update
(
dtype
=
dtype_out
)
# Replace -10000 by np.nan
im_arr
[
im_arr
==
nodata_val
]
=
np
.
nan
# Create a mask with all no data value
mask
=
np
.
isnan
(
im_arr
)
# Convert interval into array
bins
=
np
.
array
(
interval
)
# Return the indices of the bins to which each value in input array belongs
im_arr_reclass
=
np
.
digitize
(
im_arr
,
bins
,
right
=
False
)
# Apply mask on reclassified raster
im_arr_reclass
=
np
.
where
(
mask
,
nodata_val
,
im_arr_reclass
)
# Change dtype of raster to match the dtype of profile
im_arr_reclass
=
im_arr_reclass
.
astype
(
dtype_out
)
print
(
f
'
data type =
{
im_arr_reclass
.
dtype
}
'
)
print
(
im_arr_reclass
)
# Write output file
dst
=
rasterio
.
open
(
output_file
,
"
w
"
,
**
profile
)
dst
.
write
(
im_arr_reclass
)
src
.
close
()
dst
.
close
()
```
%% Output
data type = int16
[[[4 4 4 ... 4 4 4]
[4 4 4 ... 4 4 3]
[4 4 4 ... 4 3 3]
...
[4 4 4 ... 3 3 3]
[4 4 4 ... 3 3 3]
[4 4 4 ... 3 3 3]]]
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter