Skip to content

Commit

Permalink
improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcfrancisco committed Jul 23, 2024
1 parent bb306eb commit e1d7042
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ Importing large datasets into a PostGIS database can take a long time and the ai

## Installation

### Binary
For binary distributions see the table below

| Platform | x64 | ARM-64 |
|----------|-----|--------|
| Linux | | |
| macOS | | |
| Windows | | |

### Cargo
If you prefer using Cargo, you can install Popgis directly by running the Cargo install command
```bash
Expand Down
6 changes: 3 additions & 3 deletions src/file_types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ pub enum FileType {
pub fn determine_file_type(input_file: &str) -> Result<FileType> {
let file_extension = Path::new(input_file)
.extension()
.expect("No file extension found");
.expect("No file extension found");
let file_extension_str = file_extension
.to_str()
.expect("Could not convert file extension to string");
.expect("Could not convert file extension to string");
match file_extension_str {
"shp" => Ok(FileType::Shapefile),
"geojson" => Ok(FileType::GeoJson),
_ => Err("Unsupported file type".into()),
_ => Err("Unsupported file type".into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/file_types/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn to_geo(shape: &Shape) -> Result<geo::Geometry<f64>> {
Ok(geo::Geometry::from(poly))
}
}
_ => Err("Unsupported shape type".into()),
_ => Err("Unsupported shape type".into()),
}
}

Expand Down
30 changes: 18 additions & 12 deletions src/file_types/geojson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
&& table_config[&key] == Type::FLOAT8
{
continue;
} else if table_config.contains_key(&key) && table_config[&key] != Type::INT8 {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
} else {
table_config.insert(key, Type::FLOAT8);
Expand All @@ -49,7 +51,9 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
&& table_config[&key] == Type::TEXT
{
continue;
} else if table_config.contains_key(&key) && table_config[&key] != Type::INT8 {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
} else {
table_config.insert(key, Type::TEXT);
Expand All @@ -60,28 +64,30 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
&& table_config[&key] == Type::BOOL
{
continue;
} else if table_config.contains_key(&key) && table_config[&key] != Type::INT8 {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
} else {
table_config.insert(key, Type::BOOL);
}
}
// If null
serde_json::Value::Null => continue,
_ => println!("Type currently not supported"),
_ => println!("Type currently not supported"),
}
}
}
}
}
_ => println!("Not a feature collection"),
_ => println!("Not a feature collection"),
}

let mut data_types: Vec<NewTableTypes> = Vec::new();
for (column_name, data_type) in table_config.iter() {
for (column_name, data_type) in table_config {
data_types.push(NewTableTypes {
column_name: column_name.clone(),
data_type: data_type.clone(),
column_name,
data_type,
});
}

Expand Down Expand Up @@ -121,21 +127,21 @@ pub fn read_geojson(file_path: &str) -> Result<Rows> {
serde_json::Value::Null => {
row.add(AcceptedTypes::Text(None));
}
_ => println!("Type currently not supported"),
_ => println!("Type currently not supported"),
}
}
let gj_geom = feature.geometry.unwrap();
let geom: geo::Geometry<f64> = gj_geom
.value
.try_into()
.expect("Failed to convert geojson::Geometry to geo::Geometry");
let wkb = geom_to_wkb(&geom).expect("Could not convert geometry to WKB");
.expect("Failed to convert geojson::Geometry to geo::Geometry");
let wkb = geom_to_wkb(&geom).expect("Could not convert geometry to WKB");
// Check length of row
row.add(AcceptedTypes::Geometry(Some(Wkb { geometry: wkb })));
rows.add(row);
}
}
_ => println!("Not a feature collection"),
_ => println!("Not a feature collection"),
}

Ok(rows)
Expand Down
6 changes: 3 additions & 3 deletions src/file_types/shapefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
table_config.insert(column_name, Type::BOOL);
}
}
_ => println!("Type currently not supported"),
_ => println!("Type currently not supported"),
}
}
}
Expand Down Expand Up @@ -136,12 +136,12 @@ pub fn read_shapefile(file_path: &str) -> Result<Rows> {
FieldValue::Logical(value) => {
row.add(AcceptedTypes::Bool(value));
}
_ => println!("Type currently not supported"),
_ => println!("Type currently not supported"),
}
}

let geom = to_geo(&shape)?;
let wkb = geom_to_wkb(&geom).expect("Failed to insert node into database");
let wkb = geom_to_wkb(&geom).expect("Failed to insert node into database");
row.add(AcceptedTypes::Geometry(Some(Wkb { geometry: wkb })));
rows.add(row);
}
Expand Down
2 changes: 1 addition & 1 deletion src/pg/binary_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl ToSql for Wkb {
}

pub fn infer_geom_type(stmt: Statement) -> Result<Type> {
let column = stmt.columns().first().expect("Failed to get columns");
let column = stmt.columns().first().expect("Failed to get columns");
Ok(column.type_().clone())
}

Expand Down

0 comments on commit e1d7042

Please sign in to comment.