diff --git a/src/FACT_bldgs.jl b/src/FACT_bldgs.jl index 0305e23cc4816a3419975b3989b3b94997f07d16..ae8ad285544e9139b9df2d6279e8426ed1da28d4 100644 --- a/src/FACT_bldgs.jl +++ b/src/FACT_bldgs.jl @@ -19,7 +19,7 @@ This function executes two SQL queries to calculate the total footprint area and This example queries the total footprint information and average height for specified regions and returns the results in a DataFrame. """ -function footprint_avg_height(conn::MySQL.Connection, selection::Dict) +function footprint_avg_height(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -31,10 +31,14 @@ function footprint_avg_height(conn::MySQL.Connection, selection::Dict) # given the CRS of the shape, the ST_Area will return squared degrees, hence here the max_footptin and avg_footprint are in millions of squared degrees. query = "SELECT IF(ST_Area(ST_GeomFromText('"*shape*"')) = 0, 0, SUM(ST_Area(SHAPE))/ST_Area(ST_GeomFromText('"*shape*"'))) as perc_footprint, MAX(ST_Area(SHAPE))*1000000 as max_footprint, AVG(ST_Area(SHAPE))*1000000 as avg_footprint FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" query2 = "SELECT CASE WHEN SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) = 0 THEN NULL ELSE SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN JSON_EXTRACT(properties, '\$.height') * ST_Area(SHAPE) ELSE 0 END) / SUM(CASE WHEN JSON_EXTRACT(properties, '\$.height') <> -1 THEN ST_Area(SHAPE) ELSE 0 END) END as avg_height FROM footprints WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" - + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) + # execute result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -73,6 +77,14 @@ This function selects data from a database based on the provided `table` argumen In this example, the function retrieves building footprint and height data for the specified selection criteria and returns it in a DataFrame. """ function get_bldgs_footprint_height(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3315, + "database" => "FACT_bldgs", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "footprint_avg_height" => footprint_avg_height ) @@ -91,7 +103,7 @@ function get_bldgs_footprint_height(table::String, selection::Dict; host::String end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_bldgs", selection; host=host, port=3315) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_geo.jl b/src/FACT_geo.jl index 7085d252018962f3e6ca61c67639ee2869fb7afc..f9c6805a97a32d4b2841ccee003bd4618dbda674 100644 --- a/src/FACT_geo.jl +++ b/src/FACT_geo.jl @@ -17,8 +17,12 @@ This function queries a database for geographical information such as the unique This example queries geographical data for European countries and returns the results in a DataFrame. """ -function eu_countries(conn::MySQL.Connection) +function eu_countries(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT nuts_id as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_countries;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -42,8 +46,12 @@ This function queries a MySQL database for geographical information, including t This example queries geographical data for European macro regions and returns the results in a DataFrame. """ -function eu_macro_regions(conn::MySQL.Connection) +function eu_macro_regions(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT nuts_id as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_macro_regions;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -67,8 +75,12 @@ This function queries a MySQL database for geographical information, including t This example queries geographical data for European regions and returns the results in a DataFrame. """ -function eu_regions(conn::MySQL.Connection) +function eu_regions(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT nuts_id as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_regions;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -84,8 +96,12 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the province, and the region 'EU'. """ -function eu_provinces(conn::MySQL.Connection) +function eu_provinces(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT nuts_id as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_provinces;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -101,8 +117,12 @@ eu_lau(conn::MySQL.Connection) -> DataFrame # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the municipality, and the region 'EU'. """ -function eu_lau(conn::MySQL.Connection) +function eu_lau(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT CONCAT(cntr_code, lau_id) as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_municipalities;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -118,8 +138,12 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the state, and the region 'US'. """ -function us_states(conn::MySQL.Connection) +function us_states(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT statefp as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_states;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end @@ -135,8 +159,12 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the county, and the region 'US'. """ -function us_counties(conn::MySQL.Connection) +function us_counties(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_counties;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end @@ -152,8 +180,12 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the tract, and the region 'US'. """ -function us_tracts(conn::MySQL.Connection) +function us_tracts(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_tracts;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end @@ -169,8 +201,12 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the block group, and the region 'US'. """ -function us_block_groups(conn::MySQL.Connection) +function us_block_groups(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_block_groups;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end @@ -186,30 +222,50 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the geographic ID, WKT representation of the shape, area of the block, and the region 'US'. """ -function us_blocks(conn::MySQL.Connection) +function us_blocks(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid20 as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_blocks;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end # for testing purposes: -function nm_counties(conn::MySQL.Connection) +function nm_counties(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_counties WHERE statefp = 35;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end -function nm_blocks(conn::MySQL.Connection) +function nm_blocks(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT geoid20 as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM us_blocks WHERE statefp20 = 35;") + # close connection + close_connection(conn) dftemp[!, :region] = fill("US", nrow(dftemp)) return dftemp end -function no_counties(conn::MySQL.Connection) +function no_counties(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT nuts_id as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_provinces WHERE cntr_code = 'NO';") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end -function no_lau(conn::MySQL.Connection) +function no_lau(conn_dict::Dict) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) dftemp = query_connection(conn, "SELECT CONCAT(cntr_code, lau_id) as geo_id, ST_AsText(SHAPE) as shape_obj, ST_Area(SHAPE) as area FROM eu_municipalities WHERE cntr_code = 'NO';") + # close connection + close_connection(conn) dftemp[!, :region] = fill("EU", nrow(dftemp)) return dftemp end @@ -234,6 +290,13 @@ This function selects the appropriate data retrieval method from `table_dict` ba In this example, geographical data for European countries is retrieved and returned in a DataFrame. """ function get_geo_objects(scale::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3307, + "database" => "FACT_geo", + "user" => "root", + "password" => "devops" + ) """ table_dict @@ -304,7 +367,7 @@ function get_geo_objects(scale::String, selection::Dict; host::String="127.0.0.1 end # data retrieval - df = get_all_objects_from_table(scale, df_template, table_dict, "FACT_geo"; host=host, port=3307) + df = get_all_objects_from_table(scale, df_template, table_dict, conn_dict) return df end diff --git a/src/FACT_jobs.jl b/src/FACT_jobs.jl index 0fa9b0ae70f642418fbb3a23ebe28b6c817d926a..91be5cf8179d47938ccf76fb391af7a7b715bdc0 100644 --- a/src/FACT_jobs.jl +++ b/src/FACT_jobs.jl @@ -19,7 +19,7 @@ This function queries a MySQL database to retrieve annual job data for US region This example queries annual job data for US regions using the LODES dataset for the year 2023 and specific geographic IDs. """ -function us_lodes(conn::MySQL.Connection, selection::Dict) +function us_lodes(conn_dict::Dict, selection::Dict) year = selection["year"] geo_id = selection["geo_id"] if !isempty(geo_id) @@ -34,7 +34,12 @@ function us_lodes(conn::MySQL.Connection, selection::Dict) elseif first_region_id_length < 15 && first_region_id_length > 0 query = "SELECT sum(CNS01) as jobs_11, sum(CNS02) as jobs_21, sum(CNS03) as jobs_22, sum(CNS04) as jobs_23, sum(CNS05) as jobs_31_33, sum(CNS06) as jobs_42, sum(CNS07) as jobs_44_45, sum(CNS08) as jobs_48_49, sum(CNS09) as jobs_51, sum(CNS10) as jobs_52, sum(CNS11) as jobs_53, sum(CNS12) as jobs_54, sum(CNS13) as jobs_55, sum(CNS14) as jobs_56, sum(CNS15) as jobs_61, sum(CNS16) as jobs_62, sum(CNS17) as jobs_71, sum(CNS18) as jobs_72, sum(CNS19) as jobs_81, sum(CNS20) as jobs_92 FROM LODES8 WHERE Year = "*string(year)*" AND GeoID LIKE '"*id*"%';" end + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) + # execute result = query_connection(conn, query) + # close connection + close_connection(conn) long_df = stack(result, names(result), variable_name=:industry, value_name=:jobs) long_df.industry = replace.(string.(long_df.industry), "jobs_" => "") long_df[!, :geo_id] = fill(id, nrow(long_df)) @@ -66,7 +71,7 @@ This function queries a MySQL database to retrieve annual job data for US region This example queries annual job data for US regions using the QCEW dataset for the year 2023, specific geographic IDs, and aggregation level. """ -function us_qcew(conn::MySQL.Connection, selection::Dict) +function us_qcew(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary year = selection["year"] geo_id = selection["geo_id"] @@ -95,8 +100,12 @@ function us_qcew(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT Naics as industry, (Jan_jobs+Feb_jobs+Mar_jobs+Apr_jobs+May_jobs+Jun_jobs+Jul_jobs+Aug_jobs+Sep_jobs+Oct_jobs+Nov_jobs+Dec_jobs)/12 as jobs FROM QCEW WHERE Year = $year AND GeoID = '$full_id' AND Agglvl_code = '$agglvl_code';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the 'industry' column exists if !("industry" in names(result)) println("No data returned for query with GeoID = $full_id and Year = $year and AND Agglvl_code = $agglvl_code.") @@ -126,7 +135,7 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the region ('EU'), geographic ID, aggregation level, industry identifier, and average number of annual jobs. """ -function eu_lfs(conn::MySQL.Connection, selection::Dict) +function eu_lfs(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary year = selection["year"] geo_id = selection["geo_id"] @@ -136,8 +145,12 @@ function eu_lfs(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT Nace as industry, ((COALESCE(EmpTh_Q1, 0) + COALESCE(EmpTh_Q2, 0) + COALESCE(EmpTh_Q3, 0) + COALESCE(EmpTh_Q4, 0)) / (CASE WHEN EmpTh_Q1 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN EmpTh_Q2 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN EmpTh_Q3 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN EmpTh_Q4 IS NOT NULL THEN 1 ELSE 0 END)) * 1000 as jobs FROM LFS WHERE Year = $year AND GeoID = '$id';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) if !("industry" in names(result)) println("No data returned for query with GeoID = $id and Year = $year.") # Initialize `result` with the expected columns but no rows @@ -166,7 +179,7 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the region ('EU'), geographic ID, aggregation level, industry identifier, and average number of annual jobs. """ -function eu_sbs(conn::MySQL.Connection, selection::Dict) +function eu_sbs(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary year = selection["year"] geo_id = selection["geo_id"] @@ -176,8 +189,12 @@ function eu_sbs(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT Nace as industry, Employment as jobs FROM SBS WHERE Year = "*string(year)*" AND GeoID = '"*id*"';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) if !("industry" in names(result)) println("No data returned for query with GeoID = $id and Year = $year.") # Initialize `result` with the expected columns but no rows @@ -206,7 +223,7 @@ end # Returns - `DataFrame`: A DataFrame containing columns for the region ('EU'), geographic ID, aggregation level, industry identifier, and average number of annual jobs. """ -function eu_rea(conn::MySQL.Connection, selection::Dict) +function eu_rea(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary year = selection["year"] geo_id = selection["geo_id"] @@ -216,8 +233,12 @@ function eu_rea(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT Nace as industry, EmpTh*1000 as jobs FROM REA WHERE Year = "*string(year)*" AND GeoID = '"*id*"';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) if !("industry" in names(result)) println("No data returned for query with GeoID = $id and Year = $year.") # Initialize `result` with the expected columns but no rows @@ -255,6 +276,14 @@ This function allows for the retrieval of annual job data based on the specified This example queries annual job data for US regions using the LODES dataset for the year 2023 and specific geographic IDs. """ function get_annual_jobs(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3308, + "database" => "FACT_jobs", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "us_lodes" => us_lodes, "us_qcew" => us_qcew, @@ -277,7 +306,7 @@ function get_annual_jobs(table::String, selection::Dict; host::String="127.0.0.1 end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_jobs", selection; host=host, port=3308) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_mine.jl b/src/FACT_mine.jl index df0e78605c3f839a111df2492e55cea25d4dd17b..795e447f7f461455ade86817cdfced5cbb589cf7 100644 --- a/src/FACT_mine.jl +++ b/src/FACT_mine.jl @@ -20,10 +20,9 @@ This function retrieves geology data for specified geographic areas based on the This example retrieves geology data for two geographic areas in the "US" region with specified shape data and areas. """ -function geology_full(conn::MySQL.Connection, selection::Dict) +function geology_full(conn_dict::Dict, selection::Dict) # temporarily increase max_allowed_packet size_query = "SET GLOBAL max_allowed_packet=1073741824;" #Sets max_allowed_packet to 1GB - DBInterface.execute(conn, size_query) # getting valuable info from dictionary geo_id = selection["geo_id"] @@ -64,8 +63,13 @@ function geology_full(conn::MySQL.Connection, selection::Dict) SUM(CASE WHEN rxtyp = 'unclassified rocks' THEN ST_Area(ST_Intersection(SHAPE, ST_GeomFromText('$shape'))) ELSE 0 END) / $ar as unclassified \ FROM geology WHERE ST_Intersects(ST_GeomFromText('$shape'), SHAPE); """ + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute + DBInterface.execute(conn, size_query) result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -102,7 +106,7 @@ This function retrieves mineral data for specified geographic areas based on the This example retrieves mineral data for two geographic areas in the "EU" region with specified shape data and areas. """ -function minerals_full(conn::MySQL.Connection, selection::Dict) +function minerals_full(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -112,6 +116,8 @@ function minerals_full(conn::MySQL.Connection, selection::Dict) dftemp = DataFrame() # iterate over geo objects for (id, shape, ar) in zip(geo_id, shape_obj, area) + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) tables = ["carbonatite" , "global_selected" , "laterite" , "nicrpge" , "phosphate" , "podchrome" , "porcu" , "potash" , "ree" , "sedcu" , "sedexmvt" , "sedznpb" , "vms"] result = DataFrame() for table in tables @@ -120,6 +126,8 @@ function minerals_full(conn::MySQL.Connection, selection::Dict) result2 = query_connection(conn, query) result = hcat(result, result2) end + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -159,6 +167,13 @@ This function allows for the retrieval of geology data from a specific table usi This example retrieves geology data using the specified table and selection criteria. """ function get_geology(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3314, + "database" => "FACT_mine", + "user" => "root", + "password" => "devops" + ) table_dict = Dict( "geology_full" => geology_full ) @@ -200,7 +215,7 @@ function get_geology(table::String, selection::Dict; host::String="127.0.0.1") end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_mine", selection; host=host, port=3314) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end @@ -228,6 +243,14 @@ This function allows for the retrieval of mineral data from a specific table usi This example retrieves mineral data using the specified table and selection criteria. """ function get_minerals(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3314, + "database" => "FACT_mine", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "minerals_full" => minerals_full ) @@ -256,7 +279,7 @@ function get_minerals(table::String, selection::Dict; host::String="127.0.0.1") end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_mine", selection; host=host, port=3314) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_population.jl b/src/FACT_population.jl index c719f3569ddf6da02c11847fe825cfc9d39a029c..dc45a05917cc5d8bf3325d08072a9e1354134808 100644 --- a/src/FACT_population.jl +++ b/src/FACT_population.jl @@ -21,7 +21,7 @@ This function retrieves population data for specified LAUs in the European Union This example retrieves population data for two EU LAUs in the year 2011. """ -function eu_lau(conn::MySQL.Connection, selection::Dict) +function eu_lau(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary: acceptable years are 1961, 71, 81, 91, 2001, 2011 year = selection["year"] geo_id = selection["geo_id"] @@ -31,8 +31,12 @@ function eu_lau(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT POP_"*string(year)*"_01_01 as population FROM eu_lau WHERE CNTR_LAU_CODE = '"*id*"';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -69,7 +73,7 @@ This function retrieves population data for specified NUTS regions in the Europe This example retrieves population data for two EU NUTS regions in the year 2018. """ -function eu_nuts(conn::MySQL.Connection, selection::Dict) +function eu_nuts(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary: acceptable years are 2014-2022 year = selection["year"] geo_id = selection["geo_id"] @@ -79,8 +83,12 @@ function eu_nuts(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT Pop_M+Pop_F as population FROM eu_nuts WHERE GeoID = '"*id*"' AND Year = "*string(year)*";" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -117,7 +125,7 @@ This function retrieves population data for specified US Census Blocks for a giv This example retrieves population data for a US Census Block in the year 2020. """ -function us_block(conn::MySQL.Connection, selection::Dict) +function us_block(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary: acceptable years are 1961, 71, 81, 91, 2001, 2011 year = selection["year"] geo_id = selection["geo_id"] @@ -132,8 +140,12 @@ function us_block(conn::MySQL.Connection, selection::Dict) block = parse(Int, id[12:15]) # prepare query query = "SELECT P1_001N as population FROM us_block_2020 WHERE state = "*string(state)*" AND county = "*string(county)*" AND tract = "*string(tract)*" AND block = "*string(block)*";" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -149,8 +161,12 @@ function us_block(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT pop10 as population FROM us_block_2010 WHERE blockid10 = '"*id*"';" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -188,7 +204,7 @@ This function retrieves population data for specified US Counties for a given ye This example retrieves population data for a US County in the year 2022. """ -function us_county(conn::MySQL.Connection, selection::Dict) +function us_county(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary: acceptable years are 2014-2022 year = selection["year"] geo_id = selection["geo_id"] @@ -198,8 +214,12 @@ function us_county(conn::MySQL.Connection, selection::Dict) for id in geo_id # prepare query query = "SELECT Pop_M+Pop_F as population FROM us_county WHERE AgeClass = 0 AND GeoID = '"*string(parse(Int, id))*"' AND Year = "*string(year)*";" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -238,6 +258,14 @@ This function allows for the retrieval of annual population data from a specific This example retrieves annual population data for two EU LAUs in the year 2011 using the "eu_lau" table. """ function get_annual_population(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3309, + "database" => "FACT_population", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "eu_lau" => eu_lau, "eu_nuts" => eu_nuts, @@ -257,7 +285,7 @@ function get_annual_population(table::String, selection::Dict; host::String="127 end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_population", selection; host=host, port=3309) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_ports.jl b/src/FACT_ports.jl index adb78ef3119b9d2703f66c029a0016d865771014..0e30d5c96b7b0dcc651de12e5fb9419735e6fca7 100644 --- a/src/FACT_ports.jl +++ b/src/FACT_ports.jl @@ -19,7 +19,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two EU ports based on their specified port IDs and shapes. """ -function eu_ports(conn::MySQL.Connection, selection::Dict) +function eu_ports(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -30,9 +30,13 @@ function eu_ports(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT COUNT(*) as ports FROM eu_ports_2013_geo WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" query2 = "SELECT SUM(Q1+Q2+Q3+Q4) AS tonnage FROM eu_tonnage et JOIN eu_ports_2013_geo ep ON ep.port_id=et.PORT_ID WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -70,7 +74,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two US ports based on their specified port IDs and shapes. """ -function us_ports(conn::MySQL.Connection, selection::Dict) +function us_ports(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary: acceptable years are 1961, 71, 81, 91, 2001, 2011 geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -80,8 +84,12 @@ function us_ports(conn::MySQL.Connection, selection::Dict) for (id, shape) in zip(geo_id, shape_obj) # prepare query query = "SELECT COUNT(*) as ports, SUM(total) as tonnage FROM us_ports_geo WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) + # close connection + close_connection(conn) # Check if the result is empty if !isempty(result) # add geo_id col @@ -120,6 +128,14 @@ This function allows for the retrieval of port information, including the number This example retrieves port information for two EU ports based on their specified port IDs and shapes using the "eu_ports" table. """ function get_ports_tonnage(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3312, + "database" => "FACT_ports", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "eu_ports" => eu_ports, "us_ports" => us_ports @@ -138,7 +154,7 @@ function get_ports_tonnage(table::String, selection::Dict; host::String="127.0.0 end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_ports", selection; host=host, port=3312) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_power.jl b/src/FACT_power.jl index 0961c795fb926008c874be10a831f9d2cc81423e..f6d169d31cd7f641902c86fb645475e407f51e33 100644 --- a/src/FACT_power.jl +++ b/src/FACT_power.jl @@ -20,7 +20,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two EU power stations and their associated transmission lines based on specified station IDs and shapes. """ -function eu_power(conn::MySQL.Connection, selection::Dict) +function eu_power(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -31,9 +31,13 @@ function eu_power(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT COUNT(*) as stations, SUM(capacity_g) as capacity FROM eu_gen WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" query2 = "SELECT SUM(ST_Length(ST_Intersection(SHAPE, ST_GeomFromText('"*shape*"')))) AS 'lines' FROM eu_trans WHERE ST_Intersects(SHAPE, ST_GeomFromText('"*shape*"'));" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -71,7 +75,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two US power stations and their associated transmission lines based on specified station IDs and shapes. """ -function us_power(conn::MySQL.Connection, selection::Dict) +function us_power(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -82,9 +86,13 @@ function us_power(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT COUNT(*) as stations, SUM(COALESCE(CASE WHEN modified_source_des LIKE '%Battery = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Battery = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Solar = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Solar = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Wind = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Wind = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Natural Gas = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Natural Gas = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Hydroelectric = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Hydroelectric = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Coal = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Coal = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Nuclear = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Nuclear = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Petroleum = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Petroleum = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Biomass = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Biomass = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Pumped Storage = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Pumped Storage = ', -1), ' MW', 1) ELSE 0 END) + COALESCE(CASE WHEN modified_source_des LIKE '%Geothermal = %' THEN SUBSTRING_INDEX(SUBSTRING_INDEX(modified_source_des, 'Geothermal = ', -1), ' MW', 1) ELSE 0 END)) as capacity FROM (SELECT ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE) as intersects, REPLACE(source_des, 'MW', ' MW') as modified_source_des FROM us_gen) as modified_table WHERE intersects;" query2 = "SELECT SUM(ST_Length(ST_Intersection(SHAPE, ST_GeomFromText('"*shape*"')))) AS 'lines' FROM us_trans WHERE ST_Intersects(SHAPE, ST_GeomFromText('"*shape*"'));" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -124,6 +132,14 @@ This function allows for the retrieval of power station and transmission line in This example retrieves power station and transmission line information for two EU power stations based on specified station IDs and shapes using the "eu_power" table. """ function get_power_stations_lines(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3313, + "database" => "FACT_power", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "eu_power" => eu_power, "us_power" => us_power @@ -143,7 +159,7 @@ function get_power_stations_lines(table::String, selection::Dict; host::String=" end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_power", selection; host=host, port=3313) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/FACT_rail.jl b/src/FACT_rail.jl index 06bbbb40bd5f48ee79005d635f6d3108c74c348a..2c615acf587a802f3f61280cc6b7bf0c29959eee 100644 --- a/src/FACT_rail.jl +++ b/src/FACT_rail.jl @@ -19,7 +19,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two EU railway stations and their associated railway lines based on specified station IDs and shapes. """ -function eu_rail(conn::MySQL.Connection, selection::Dict) +function eu_rail(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -30,9 +30,13 @@ function eu_rail(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT COUNT(*) as stations FROM eu_stations WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" query2 = "SELECT SUM(ST_Length(ST_Intersection(SHAPE, ST_GeomFromText('"*shape*"')))) AS 'lines' FROM eu_lines WHERE ST_Intersects(SHAPE, ST_GeomFromText('"*shape*"'));" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -70,7 +74,7 @@ This function communicates with a MySQL database connection to execute SQL queri This example retrieves information about two US railway stations and their associated railway lines based on specified station IDs and shapes. """ -function us_rail(conn::MySQL.Connection, selection::Dict) +function us_rail(conn_dict::Dict, selection::Dict) # getting valuable info from dictionary geo_id = selection["geo_id"] shape_obj = selection["shape_obj"] @@ -81,9 +85,13 @@ function us_rail(conn::MySQL.Connection, selection::Dict) # prepare query query = "SELECT COUNT(*) as stations FROM na_stations WHERE ST_Intersects(ST_GeomFromText('"*shape*"'), SHAPE);" query2 = "SELECT SUM(ST_Length(ST_Intersection(SHAPE, ST_GeomFromText('"*shape*"')))) AS 'lines' FROM na_lines WHERE ST_Intersects(SHAPE, ST_GeomFromText('"*shape*"'));" + # create connection + conn = establish_connection(conn_dict["host"], conn_dict["user"], conn_dict["password"], conn_dict["database"]; port=conn_dict["port"]) # execute result = query_connection(conn, query) result2 = query_connection(conn, query2) + # close connection + close_connection(conn) result = hcat(result, result2) # Check if the result is empty if !isempty(result) @@ -123,6 +131,14 @@ This function allows for the retrieval of railway station and line information, This example retrieves railway station and line information for two EU railway stations based on specified station IDs and shapes using the "eu_rail" table. """ function get_rail_stations_lines(table::String, selection::Dict; host::String="127.0.0.1") + conn_dict = Dict( + "host" => host, + "port" => 3311, + "database" => "FACT_rail", + "user" => "root", + "password" => "devops" + ) + table_dict = Dict( "eu_rail" => eu_rail, "us_rail" => us_rail @@ -141,7 +157,7 @@ function get_rail_stations_lines(table::String, selection::Dict; host::String="1 end # data retrieval - df = get_selected_objects_from_table(table, df_template, table_dict, "FACT_rail", selection; host=host, port=3311) + df = get_selected_objects_from_table(table, df_template, table_dict, selection, conn_dict) return df end diff --git a/src/utils_DB_query.jl b/src/utils_DB_query.jl index 9e4f453f7d49a4d6b6d64e1283aa8bac7b07501e..e3c30fadeaa6f3b7d667e0d489af704602080cc1 100644 --- a/src/utils_DB_query.jl +++ b/src/utils_DB_query.jl @@ -59,7 +59,7 @@ a default function is executed that returns a DataFrame with a message indicatin - The functions in `table_dict` are expected to be of the form `func(conn::MySQL.Connection) -> DataFrame`. - This allows for flexible and dynamic execution of table-specific actions based on the `table` name. """ -function handle_simple_action(table::String, table_dict::Dict, conn::MySQL.Connection) +function handle_simple_action(table::String, table_dict::Dict, conn_dict::Dict) # Default function returns an empty DataFrame with an appropriate message default_func = () -> DataFrame(Message=["No corresponding table: $table"]) @@ -67,7 +67,7 @@ function handle_simple_action(table::String, table_dict::Dict, conn::MySQL.Conne func = get(table_dict, table, default_func) # Call the function with the additional parameters - return func(conn) + return func(conn_dict) end """ @@ -98,15 +98,12 @@ specific tables or actions. For each identified table, the function fetches data - It performs type checking and conversion to ensure that the data types of the columns in the result match those in the original `df`. - Additional data type conversions should be added as necessary for other column types. """ -function get_all_objects_from_table(scale::String, df::DataFrame, table_dict::Dict, database::String; host::String="127.0.0.1", port::Int=3306) +function get_all_objects_from_table(scale::String, df::DataFrame, table_dict::Dict, conn_dict::Dict) # Interpret the scale using an external function interpreted_scale = interpret_scale(scale) - # Establish the connection - conn = establish_connection(host, "root", "devops", database; port=port) - for table in interpreted_scale - result = handle_simple_action(String(table), table_dict, conn) + result = handle_simple_action(String(table), table_dict, conn_dict) result = result[:, names(df)] # Check and convert each column in result to match the type in df for col in names(df) @@ -127,9 +124,6 @@ function get_all_objects_from_table(scale::String, df::DataFrame, table_dict::Di df = vcat(df, result) end - # Close the connection when done - close_connection(conn) - return df end