diff --git a/Dashboards/demo_dash.py b/Dashboards/demo_dash.py index 3e810e4..fc1ce9f 100644 --- a/Dashboards/demo_dash.py +++ b/Dashboards/demo_dash.py @@ -1,153 +1,40 @@ from dash import Dash, html, dcc, Output, Input import plotly.express as px -import plotly.graph_objects as go import pandas as pd -from pathlib import Path -import sys -chemin_dossier_parent = Path(__file__).parent.parent -sys.path.append(str(chemin_dossier_parent)) -from my_data.db_connect import get_session -from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology + +# Source : https://discuss.streamlit.io/t/develop-a-dashboard-app-with-streamlit-using-plotly/37148/4 +# Dash version +# run with : python Dashboard/demo_dash.py + +# Load data function +def load_data(): + return pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv") # Initialize Dash app app = Dash(__name__) # Load data -lichen_df = get_lichen_data() -lichen_species_df = get_lichen_species_data() -observation_df = get_observation_data() -table_df = get_table_data() -tree_species_df = get_tree_species() -ecology_df = get_lichen_ecology() - -# Fonction pour calculer la fréquence des valeurs E, N, O, S -def calculate_frequency(column): - return column.apply(lambda x: sum(1 for char in x if char in ['E', 'N', 'O', 'S'])) - -# Calculer la fréquence -table_df['freq'] = ( - table_df['sq1'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq2'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq3'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq4'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq5'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) -) - -# Joindre table avec lichen et observation -merged_df = table_df.merge(lichen_df, left_on='lichen_id', right_on='id', suffixes=('', '_l')) -merged_df = merged_df.merge(lichen_species_df, left_on='species_id', right_on='id', suffixes=('', '_ls')) -merged_df = merged_df.merge(observation_df, left_on='observation_id', right_on='id', suffixes=('', '_o')) - -# Grouper par 'species' et 'observation_id' et additionner les fréquences -grouped_df = merged_df.groupby(['name', 'observation_id'])['freq'].sum().reset_index() - -# Regrouper les deux tables afficher les données écologiques -grouped_df = grouped_df.merge(ecology_df, left_on='name', right_on='cleaned_taxon', suffixes=('', '_e')) - -# ajustement des noms finaux -grouped_df = grouped_df[['observation_id', 'name', 'freq','pH','eutrophication', 'poleotolerance']] -grouped_df = grouped_df.rename( - columns={ - 'observation_id': 'id', - 'name': 'lichen', - 'freq': 'freq', - 'pH': 'ph', - 'eutrophication': 'eutrophication', - 'poleotolerance': 'poleotolerance' - }) - -# Calcul du degrés d'artificialisation -def deg_artif(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - base_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['poleotolerance'] == 'resistant')]['freq'].sum() - - return round((base_freq / global_freq) * 100, 2) - -# Calcul de la pollution acidé -def pollution_acide(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - acid_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['ph'] == 'acidophilous')]['freq'].sum() - - return round((acid_freq / global_freq) * 100, 2) - -def pollution_azote(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - azote_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['eutrophication'] == 'eutrophic')]['freq'].sum() - - return round((azote_freq / global_freq) * 100, 2) +df = load_data() # Define app layout with a hidden input app.layout = html.Div([ - html.H1("Gauge bar Dash", style={'textAlign': 'center'}), - dcc.Dropdown(grouped_df["id"].unique(), 419, id='dropdown-selection', style={'width': '50%', 'margin': 'auto'}), - html.Div([ - dcc.Graph(id='graph-content1', style={'flex': '1', 'margin': '10px'}), - dcc.Graph(id='graph-content2', style={'flex': '1', 'margin': '10px'}), - dcc.Graph(id='graph-content3', style={'flex': '1', 'margin': '10px'}) - ], style={'display': 'flex', 'justify-content': 'space-around', 'width': '100%'}) + html.H1("Air Travel Time Series Plot", style={'textAlign': 'center'}), + html.Div("This chart shows the number of air passengers traveled in each month from 1949 to 1960", style={'textAlign': 'center'}), + dcc.Graph(id='air-travel-graph'), + html.Div(id='dummy-div', style={'display': 'none'}) # Hidden div acting as a placeholder ]) + # Define callback to update graph @app.callback( - [ - Output('graph-content1', 'figure'), - Output('graph-content2', 'figure'), - Output('graph-content3', 'figure') - ], - Input('dropdown-selection', 'value') + Output('air-travel-graph', 'figure'), + Input('dummy-div', 'children') # Use the hidden div as input ) - def update_graph(value): - fig1 = go.Figure(go.Indicator( - domain = {'x': [0, 1], 'y': [0, 1]}, - value = deg_artif(value), - mode = "gauge+number", - title = {'text': "Degré d'artificialisation"}, - gauge = {'axis': {'range': [0, 100], 'dtick': 25}, - 'bar': {'color': "#000000"}, - 'steps' : [ - {'range': [0, 25], 'color': "green"}, - {'range': [25, 50], 'color': "yellow"}, - {'range': [50, 75], 'color': "orange"}, - {'range': [75, 100], 'color': "red"} - ], - 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': deg_artif(value)} - })) - - fig2 = go.Figure(go.Indicator( - domain = {'x': [0, 1], 'y': [0, 1]}, - value = pollution_acide(value), - mode = "gauge+number", - title = {'text': "Degré d'artificialisation"}, - gauge = {'axis': {'range': [0, 100], 'dtick': 25}, - 'bar': {'color': "#000000"}, - 'steps' : [ - {'range': [0, 25], 'color': "green"}, - {'range': [25, 50], 'color': "yellow"}, - {'range': [50, 75], 'color': "orange"}, - {'range': [75, 100], 'color': "red"} - ], - 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_acide(value)} - })) - - fig3 = go.Figure(go.Indicator( - domain = {'x': [0, 1], 'y': [0, 1]}, - value = pollution_azote(value), - mode = "gauge+number", - title = {'text': "Degré d'artificialisation"}, - gauge = {'axis': {'range': [0, 100], 'dtick': 25}, - 'bar': {'color': "#000000"}, - 'steps' : [ - {'range': [0, 25], 'color': "green"}, - {'range': [25, 50], 'color': "yellow"}, - {'range': [50, 75], 'color': "orange"}, - {'range': [75, 100], 'color': "red"} - ], - 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_azote(value)} - })) - return fig1, fig2, fig3 + fig = px.line(df, x="Month", y=df.columns[1:], title="Air Passenger Travel") + return fig # Run the app if __name__ == '__main__': - app.run_server(debug=True) + app.run_server(debug=True) \ No newline at end of file diff --git a/Dashboards/demo_streamlit.py b/Dashboards/demo_streamlit.py index 33e803d..91331ff 100644 --- a/Dashboards/demo_streamlit.py +++ b/Dashboards/demo_streamlit.py @@ -1,134 +1,20 @@ import streamlit as st import pandas as pd -import my_data.datasets as df -import plotly.graph_objects as go -import numpy as np - -from pathlib import Path -import sys -chemin_dossier_parent = Path(__file__).parent.parent -sys.path.append(str(chemin_dossier_parent)) -from my_data.db_connect import get_session -from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology +import plotly.express as px # Source : https://discuss.streamlit.io/t/develop-a-dashboard-app-with-streamlit-using-plotly/37148/4 -# run with : streamlit run Dashboards/demo_streamlit.py - -# Récupération des datasets -environment_df = get_environment_data() -lichen_df = get_lichen_data() -lichen_species_df = get_lichen_species_data() -observation_df = get_observation_data() -table_df = get_table_data() -tree_df = get_tree_data() -tree_species_df = get_tree_species() -ecology_df = get_lichen_ecology() - -# Fonction pour calculer la fréquence des valeurs E, N, O, S -def calculate_frequency(column): - return column.apply(lambda x: sum(1 for char in x if char in ['E', 'N', 'O', 'S'])) - -# Calculer la fréquence -table_df['freq'] = ( - table_df['sq1'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq2'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq3'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq4'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + - table_df['sq5'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) -) - -# Joindre table avec lichen et observation -merged_df = table_df.merge(lichen_df, left_on='lichen_id', right_on='id', suffixes=('', '_l')) -merged_df = merged_df.merge(lichen_species_df, left_on='species_id', right_on='id', suffixes=('', '_ls')) -merged_df = merged_df.merge(observation_df, left_on='observation_id', right_on='id', suffixes=('', '_o')) - -# Grouper par 'species' et 'observation_id' et additionner les fréquences -grouped_df = merged_df.groupby(['name', 'observation_id'])['freq'].sum().reset_index() - -# Regrouper les deux tables afficher les données écologiques -grouped_df = grouped_df.merge(ecology_df, left_on='name', right_on='cleaned_taxon', suffixes=('', '_e')) - -# ajustement des noms finaux -grouped_df = grouped_df[['observation_id', 'name', 'freq','pH','eutrophication', 'poleotolerance']] -grouped_df = grouped_df.rename( - columns={ - 'observation_id': 'id', - 'name': 'lichen', - 'freq': 'freq', - 'pH': 'ph', - 'eutrophication': 'eutrophication', - 'poleotolerance': 'poleotolerance' - }) - -# Calcul du degrés d'artificialisation -def deg_artif(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - base_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['poleotolerance'] == 'resistant')]['freq'].sum() - - return round((base_freq / global_freq) * 100, 2) - -# Calcul de la pollution acidé -def pollution_acide(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - acid_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['ph'] == 'acidophilous')]['freq'].sum() - - return round((acid_freq / global_freq) * 100, 2) - -def pollution_azote(my_input: int): - global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() - azote_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['eutrophication'] == 'eutrophic')]['freq'].sum() - - return round((azote_freq / global_freq) * 100, 2) +# run with : streamlit run Dashboard/demo_streamlit.py -# Sélection du site -id_site = st.selectbox( - "Sur quel site voulez-vous ?", - grouped_df["id"].unique(), - index=None, - placeholder="site n°", -) +def load_data(): + return pd.read_csv("https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv") -# Sélection metrique -metrics = st.selectbox( - "Quelle métrique voulez-vous ?", - ("Degré d'artificialisation", "Pollution acide", "Pollution azote"), - index=None, - placeholder="Je sélectionne la métrique...", -) +# Load data +df = load_data() -# Affichage des éléments -if id_site: - if metrics == "Degré d'artificialisation": - artificialisation_proportions = deg_artif(id_site) - elif metrics == "Pollution acide": - artificialisation_proportions = pollution_acide(id_site) - elif metrics == "Pollution azote": - artificialisation_proportions = pollution_azote(id_site) +# Display some information +st.title("Air Travel Time Series Plot") +st.write("This chart shows the number of air passenger traveled in each month from 1949 to 1960") -# Dataviz charts -if id_site and metrics: - ########################## - # Affichage du graphique # - ########################## - st.write("# Gauge bar") - st.write(f"Site : {id_site}") - st.write(f"Lichen : {metrics}") - fig1 = go.Figure(go.Indicator( - domain = {'x': [0, 1], 'y': [0, 1]}, - value = artificialisation_proportions, - mode = "gauge+number", - title = {'text': "Degré d'artificialisation"}, - gauge = {'axis': {'range': [0, 100], 'dtick': 25}, - 'bar': {'color': "#000000"}, - 'steps' : [ - {'range': [0, 25], 'color': "green"}, - {'range': [25, 50], 'color': "yellow"}, - {'range': [50, 75], 'color': "orange"}, - {'range': [75, 100], 'color': "red"} - ], - 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': artificialisation_proportions} - })) - # Afficher les donnée dans streamlit - st.plotly_chart(fig1) -else: - st.write("Veuillez sélectionner un site et une métrique") \ No newline at end of file +# Plotly figure +fig = px.line(df, x="Month", y=df.columns[1:], title="Air Passenger Travel") +st.plotly_chart(fig) \ No newline at end of file diff --git a/Dashboards/gauge_charts.py b/Dashboards/gauge_charts.py new file mode 100644 index 0000000..e367eb6 --- /dev/null +++ b/Dashboards/gauge_charts.py @@ -0,0 +1,116 @@ +import sys +import pandas as pd +from pathlib import Path +import plotly.express as px +import plotly.graph_objects as go +from dash import Dash, html, dcc, Output, Input +chemin_dossier_parent = Path(__file__).parent.parent +sys.path.append(str(chemin_dossier_parent)) +from my_data.computed_datasets import df_frequency + + +# Initialize Dash app +app = Dash(__name__) + +# Load the dataset +grouped_df = df_frequency() + +# Calcul du degrés d'artificialisation +def deg_artif(my_input: int): + global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() + base_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['poleotolerance'] == 'resistant')]['freq'].sum() + + return round((base_freq / global_freq) * 100, 2) + +# Calcul de la pollution acidé +def pollution_acide(my_input: int): + global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() + acid_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['ph'] == 'acidophilous')]['freq'].sum() + + return round((acid_freq / global_freq) * 100, 2) + +# Calcul de la pollution azoté +def pollution_azote(my_input: int): + global_freq = grouped_df[grouped_df['id']== my_input]['freq'].sum() + azote_freq = grouped_df[(grouped_df['id'] == my_input) & (grouped_df['eutrophication'] == 'eutrophic')]['freq'].sum() + + return round((azote_freq / global_freq) * 100, 2) + +# Define app layout with a hidden input +app.layout = html.Div([ + html.H1("Gauge bar Dash", style={'textAlign': 'center'}), + dcc.Dropdown(grouped_df["id"].unique(), 419, id='dropdown-selection', style={'width': '50%', 'margin': 'auto'}), + html.Div([ + dcc.Graph(id='graph-content1', style={'flex': '1', 'margin': '10px'}), + dcc.Graph(id='graph-content2', style={'flex': '1', 'margin': '10px'}), + dcc.Graph(id='graph-content3', style={'flex': '1', 'margin': '10px'}) + ], style={'display': 'flex', 'justify-content': 'space-around', 'width': '100%'}) +]) + +# Define callback to update graph +@app.callback( + [ + Output('graph-content1', 'figure'), + Output('graph-content2', 'figure'), + Output('graph-content3', 'figure') + ], + Input('dropdown-selection', 'value') +) + +def update_graph(value): + fig1 = go.Figure(go.Indicator( + domain = {'x': [0, 1], 'y': [0, 1]}, + value = deg_artif(value), + number = {'suffix': "%"}, + mode = "gauge+number", + title = {'text': "Degré d'artificialisation"}, + gauge = {'axis': {'range': [0, 100], 'dtick': 25}, + 'bar': {'color': "#000000"}, + 'steps' : [ + {'range': [0, 25], 'color': "green"}, + {'range': [25, 50], 'color': "yellow"}, + {'range': [50, 75], 'color': "orange"}, + {'range': [75, 100], 'color': "red"} + ], + 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': deg_artif(value)} + })) + + fig2 = go.Figure(go.Indicator( + domain = {'x': [0, 1], 'y': [0, 1]}, + value = pollution_acide(value), + number = {'suffix': "%"}, + mode = "gauge+number", + title = {'text': "Pollution acide"}, + gauge = {'axis': {'range': [0, 100], 'dtick': 25}, + 'bar': {'color': "#000000"}, + 'steps' : [ + {'range': [0, 25], 'color': "green"}, + {'range': [25, 50], 'color': "yellow"}, + {'range': [50, 75], 'color': "orange"}, + {'range': [75, 100], 'color': "red"} + ], + 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_acide(value)} + })) + + fig3 = go.Figure(go.Indicator( + domain = {'x': [0, 1], 'y': [0, 1]}, + value = pollution_azote(value), + number = {'suffix': "%"}, + mode = "gauge+number", + title = {'text': "Pollution azoté"}, + gauge = {'axis': {'range': [0, 100], 'dtick': 25}, + 'bar': {'color': "#000000"}, + 'steps' : [ + {'range': [0, 25], 'color': "green"}, + {'range': [25, 50], 'color': "yellow"}, + {'range': [50, 75], 'color': "orange"}, + {'range': [75, 100], 'color': "red"} + ], + 'threshold' : {'line': {'color': "#000000", 'width': 4}, 'thickness': 0.75, 'value': pollution_azote(value)} + })) + return fig1, fig2, fig3 + + +# Run the app +if __name__ == '__main__': + app.run_server(debug=True) diff --git a/Dashboards/my_data/computed_datasets.py b/Dashboards/my_data/computed_datasets.py new file mode 100644 index 0000000..65a18f0 --- /dev/null +++ b/Dashboards/my_data/computed_datasets.py @@ -0,0 +1,54 @@ +import sys +from pathlib import Path +import pandas as pd +chemin_dossier_parent = Path(__file__).parent.parent +sys.path.append(str(chemin_dossier_parent)) +from my_data.db_connect import get_session +from my_data.datasets import get_environment_data, get_lichen_data, get_lichen_species_data, get_observation_data, get_table_data, get_tree_data, get_tree_species, get_lichen_ecology + + +def calculate_frequency(column): + return column.apply(lambda x: sum(1 for char in x if char in ['E', 'N', 'O', 'S'])) + +def df_frequency(): + # Load usefull datasets + lichen_df = get_lichen_data() + lichen_species_df = get_lichen_species_data() + observation_df = get_observation_data() + table_df = get_table_data() + tree_species_df = get_tree_species() + ecology_df = get_lichen_ecology() + + # Calculer la fréquence + table_df['freq'] = ( + table_df['sq1'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + + table_df['sq2'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + + table_df['sq3'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + + table_df['sq4'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + + table_df['sq5'].apply(lambda x: len(x) if pd.notnull(x).any() else 0) + ) + + # Joindre table avec lichen et observation + merged_df = table_df.merge(lichen_df, left_on='lichen_id', right_on='id', suffixes=('', '_l')) + merged_df = merged_df.merge(lichen_species_df, left_on='species_id', right_on='id', suffixes=('', '_ls')) + merged_df = merged_df.merge(observation_df, left_on='observation_id', right_on='id', suffixes=('', '_o')) + + # Grouper par 'species' et 'observation_id' et additionner les fréquences + grouped_df = merged_df.groupby(['name', 'observation_id'])['freq'].sum().reset_index() + + # Regrouper les deux tables afficher les données écologiques + grouped_df = grouped_df.merge(ecology_df, left_on='name', right_on='cleaned_taxon', suffixes=('', '_e')) + + # ajustement des noms finaux + grouped_df = grouped_df[['observation_id', 'name', 'freq','pH','eutrophication', 'poleotolerance']] + grouped_df = grouped_df.rename( + columns={ + 'observation_id': 'id', + 'name': 'lichen', + 'freq': 'freq', + 'pH': 'ph', + 'eutrophication': 'eutrophication', + 'poleotolerance': 'poleotolerance' + }) + + return grouped_df diff --git a/Dashboards/my_data/requests.sql b/Dashboards/my_data/requests.sql deleted file mode 100644 index 7dc857d..0000000 --- a/Dashboards/my_data/requests.sql +++ /dev/null @@ -1,40 +0,0 @@ --- Base requête qui est fausse -SELECT - o.id AS id_site, - ls.name AS lichen, - le."pH" AS pH, - -- Addition des longueurs des tableaux pour chaque ligne - COALESCE(array_length(ta.sq1, 1), 0) + - COALESCE(array_length(ta.sq2, 1), 0) + - COALESCE(array_length(ta.sq3, 1), 0) + - COALESCE(array_length(ta.sq4, 1), 0) + - COALESCE(array_length(ta.sq5, 1), 0) AS freq, - -- Afficher les donnée écologiques - le.eutrophication AS eutrophication, - le.poleotolerance AS poleotolerance -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id - JOIN main_table ta ON ta.tree_id = t.id - JOIN lichen_ecology le ON ls.name = le.cleaned_taxon; - --- transformer la table main_table pour avoir une fréquence entre 0 et 20 -SELECT - ta.id, - ta.tree_id, - COALESCE(array_length(ta.sq1, 1), 0) + - COALESCE(array_length(ta.sq2, 1), 0) + - COALESCE(array_length(ta.sq3, 1), 0) + - COALESCE(array_length(ta.sq4, 1), 0) + - COALESCE(array_length(ta.sq5, 1), 0) AS freq -FROM - main_table ta -ORDER BY - freq DESC; - --- Debugger le count -SELECT * -FROM main_table -WHERE id = 17149; \ No newline at end of file diff --git a/Dashboards/my_data/sql_stuff.sql b/Dashboards/my_data/sql_stuff.sql deleted file mode 100644 index 17df326..0000000 --- a/Dashboards/my_data/sql_stuff.sql +++ /dev/null @@ -1,126 +0,0 @@ --- Listing des modifications faites sur la base de données --- POC requête pour afficher les fréquences 17/07/2024 -SELECT - o.id AS id_site, - ls.name AS main_lichenspecies, - COUNT(l.id) AS frequency -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id - -GROUP BY - o.id, ls.name - -ORDER BY o.id, frequency DESC; - --- Pour la commandline PSQL -SELECT o.id AS id_site, ls.name AS main_lichenspecies, COUNT(l.id) AS frequency FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id GROUP BY o.id, ls.name ORDER BY o.id, frequency DESC; - --- Créer une table view récupérable dans le modèle intégrée dans la base de données le 17/07/2024 -CREATE VIEW lichen_frequency AS -SELECT - o.id AS id_site, - ls.name AS main_lichenspecies, - COUNT(l.id) AS frequency -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id -GROUP BY - o.id, ls.name -ORDER BY - o.id, frequency DESC; - - --- Update SQL command -CREATE VIEW lichen_frequency AS -SELECT - ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id, - o.id AS id_site, - ls.name AS main_lichenspecies, - COUNT(l.id) AS frequency -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id -GROUP BY - o.id, ls.name -ORDER BY - o.id, frequency DESC; - -CREATE VIEW lichen_frequency AS SELECT ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id, o.id AS id_site, ls.name AS main_lichenspecies, COUNT(l.id) AS frequency FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id GROUP BY o.id, ls.name ORDER BY o.id, frequency DESC; - --- Update post ajustement avec Hugo 7/8/24 -SELECT - ta.id AS id_ta, - o.id AS id_site, - ls.name AS lichen, - le."pH" AS pH, - -- Addition des longueurs des tableaux pour chaque ligne - COALESCE(array_length(ta.sq1, 1), 0) + - COALESCE(array_length(ta.sq2, 1), 0) + - COALESCE(array_length(ta.sq3, 1), 0) + - COALESCE(array_length(ta.sq4, 1), 0) + - COALESCE(array_length(ta.sq5, 1), 0) AS freq, - -- Afficher les donnée écologiques - le.eutrophication AS eutrophication, - le.poleotolerance AS poleotolerance -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id - JOIN main_table ta ON ta.tree_id = t.id - JOIN lichen_ecology le ON ls.name = le.cleaned_taxon -ORDER BY - freq DESC; - --- Creation de la vue pour lichenfrequency -CREATE VIEW lichen_frequency AS -SELECT - ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id, - o.id AS id_site, - ls.name AS lichen, - le."pH" AS pH, - -- Addition des longueurs des tableaux pour chaque ligne - COALESCE(array_length(ta.sq1, 1), 0) + - COALESCE(array_length(ta.sq2, 1), 0) + - COALESCE(array_length(ta.sq3, 1), 0) + - COALESCE(array_length(ta.sq4, 1), 0) + - COALESCE(array_length(ta.sq5, 1), 0) AS freq, - -- Afficher les donnée écologiques - le.eutrophication AS eutrophication, - le.poleotolerance AS poleotolerance -FROM - main_observation o - JOIN main_tree t ON o.id = t.observation_id - JOIN main_lichen l ON o.id = l.observation_id - JOIN main_lichenspecies ls ON l.species_id = ls.id - JOIN main_table ta ON ta.tree_id = t.id - JOIN lichen_ecology le ON ls.name = le.cleaned_taxon; - --- PSQL commandline -CREATE VIEW lichen_frequency AS SELECT ROW_NUMBER() OVER (ORDER BY o.id, ls.name) AS id, o.id AS id_site, ls.name AS lichen, le."pH" AS pH, COALESCE(array_length(ta.sq1, 1), 0) + COALESCE(array_length(ta.sq2, 1), 0) + COALESCE(array_length(ta.sq3, 1), 0) + COALESCE(array_length(ta.sq4, 1), 0) + COALESCE(array_length(ta.sq5, 1), 0) AS freq, le.eutrophication AS eutrophication, le.poleotolerance AS poleotolerance FROM main_observation o JOIN main_tree t ON o.id = t.observation_id JOIN main_lichen l ON o.id = l.observation_id JOIN main_lichenspecies ls ON l.species_id = ls.id JOIN main_table ta ON ta.tree_id = t.id JOIN lichen_ecology le ON ls.name = le.cleaned_taxon; - --- Nettoyage de données -SELECT - id, - taxon AS original_taxon, - REPLACE(REPLACE(taxon, ' / ', '/'), ' /', '/') AS cleaned_taxon -FROM - lichen_ecology -WHERE - taxon LIKE '% / %' OR taxon LIKE '% /'; - -SELECT id, taxon AS original_taxon, REPLACE(REPLACE(taxon, ' / ', '/'), ' /', '/') AS cleaned_taxon FROM lichen_ecology; - --- ajout column -ALTER TABLE lichen_ecology ADD COLUMN cleaned_taxon VARCHAR(255); - --- UPDATE -UPDATE lichen_ecology SET cleaned_taxon = REPLACE(REPLACE(taxon, ' / ', '/'), ' /', '/'); -ALTER TABLE lichen_ecology DROP COLUMN cleaned_taxon;