Skip to content

Commit

Permalink
typo
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Oct 5, 2024
1 parent 0586da7 commit a1cd98c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/local/postgresql/data
.DS_Store
110 changes: 55 additions & 55 deletions src/duckdb_load/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,61 +118,6 @@ fn load_data_postgis(conn: &Connection, table_name: &str) -> Result<(), Box<dyn
Ok(())
}

// DuckDB file loader
fn process_file(file_path: &str, file_type: &FileType) -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute("INSTALL spatial;", [])?;
conn.execute("LOAD spatial;", [])?;
conn.execute("INSTALL postgres;", [])?;
conn.execute("LOAD postgres;", [])?;

let create_table_query = match file_type {
FileType::Geopackage | FileType::Shapefile | FileType::Geojson => {
format!(
"CREATE TABLE data AS
SELECT *
FROM ST_Read('{}');",
file_path
)
}
FileType::Excel => {
format!(
"CREATE TABLE data AS SELECT * FROM st_read('{}');",
file_path
)
}
FileType::Csv => {
format!(
"CREATE TABLE data AS SELECT * FROM read_csv('{}');",
file_path
)
}
FileType::Parquet => {
format!(
"CREATE TABLE data AS SELECT * FROM parquet_scan('{}');",
file_path
)
}
};

// Create the table in DuckDB
conn.execute(&create_table_query, [])?;

// Call to query and print data schema
query_and_print_schema(&conn)?;

// Transform
duckdb_transform(&conn, file_path)?;

// Call to load data into postgres and handle the result
match load_data_postgis(&conn, "lllllll") {
Ok(_) => println!("Data successfully loaded into PostgreSQL"),
Err(e) => eprintln!("Error loading data into PostgreSQL: {}", e),
}

Ok(())
}

fn get_crs_number(conn: &Connection, file_path: &str) -> Result<String, duckdb::Error> {
let query = format!(
"SELECT layers[1].geometry_fields[1].crs.auth_code AS crs_number FROM st_read_meta('{}');",
Expand Down Expand Up @@ -242,6 +187,61 @@ fn duckdb_transform(conn: &Connection, file_path: &str) -> Result<String, duckdb
transform_crs(conn, file_path, "4326")
}

// DuckDB file loader
fn process_file(file_path: &str, file_type: &FileType) -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute("INSTALL spatial;", [])?;
conn.execute("LOAD spatial;", [])?;
conn.execute("INSTALL postgres;", [])?;
conn.execute("LOAD postgres;", [])?;

let create_table_query = match file_type {
FileType::Geopackage | FileType::Shapefile | FileType::Geojson => {
format!(
"CREATE TABLE data AS
SELECT *
FROM ST_Read('{}');",
file_path
)
}
FileType::Excel => {
format!(
"CREATE TABLE data AS SELECT * FROM st_read('{}');",
file_path
)
}
FileType::Csv => {
format!(
"CREATE TABLE data AS SELECT * FROM read_csv('{}');",
file_path
)
}
FileType::Parquet => {
format!(
"CREATE TABLE data AS SELECT * FROM parquet_scan('{}');",
file_path
)
}
};

// Create the table in DuckDB
conn.execute(&create_table_query, [])?;

// Call to query and print data schema
query_and_print_schema(&conn)?;

// Transform
duckdb_transform(&conn, file_path)?;

// Call to load data into postgres and handle the result
match load_data_postgis(&conn, "lllllll") {
Ok(_) => println!("Data successfully loaded into PostgreSQL"),
Err(e) => eprintln!("Error loading data into PostgreSQL: {}", e),
}

Ok(())
}

// Process file
pub fn launch_process_file(file_path: &str) -> io::Result<()> {
let mut file = File::open(file_path)?;
Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
mod duckdb_load;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let file_path = "test_files/GLA_High_Street_boundaries.gpkg";
let file_path = "test_files/hotosm_twn_populated_places_points_geojson.geojson";
println!("Processing file: {}", file_path);

match duckdb_load::launch_process_file(file_path) {
Ok(_) => {
println!("File processed successfully.");
Ok(())
}
Ok(_) => Ok(()),
Err(e) => {
eprintln!("Error processing file: {}", e);
Err(Box::new(e))
Expand Down

0 comments on commit a1cd98c

Please sign in to comment.