-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9605531
commit f38097f
Showing
10 changed files
with
166 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
@try: | ||
cargo run -- --input ./examples/shapefile/small-example.shp \ | ||
--uri postgresql://pio:password@localhost:25432/popgis \ | ||
--schema test \ | ||
--table waters | ||
|
||
@build-and-run: | ||
cargo build --release | ||
cd ./target/release/ && ./popgis --input ~/Downloads/water-polygons-split-4326/water_polygons.shp \ | ||
--uri postgresql://pio:password@localhost:25432/popgis \ | ||
--schema osm \ | ||
--table waters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::Result; | ||
|
||
use postgres::types::Type; | ||
use std::path::Path; | ||
|
||
use crate::pg::binary_copy::Wkb; | ||
|
||
// Struct to hold column name and data type | ||
pub struct NewTableTypes { | ||
pub column_name: String, | ||
pub data_type: Type, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Row { | ||
pub columns: Vec<AcceptedTypes>, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Rows { | ||
pub row: Vec<Row>, | ||
} | ||
|
||
impl Row { | ||
pub fn new() -> Self { | ||
Row { columns: Vec::new() } | ||
} | ||
pub fn add(&mut self, column: AcceptedTypes) { | ||
self.columns.push(column); | ||
} | ||
} | ||
|
||
impl Rows { | ||
pub fn new() -> Self { | ||
Rows { row: Vec::new() } | ||
} | ||
pub fn add(&mut self, row: Row) { | ||
self.row.push(row); | ||
} | ||
} | ||
|
||
// Enum to hold accepted data types | ||
#[derive(Debug)] | ||
pub enum AcceptedTypes { | ||
Int(Option<i32>), | ||
Float(Option<f64>), | ||
Double(Option<f32>), | ||
Text(Option<String>), | ||
Bool(Option<bool>), | ||
Geometry(Option<Wkb>), | ||
} | ||
|
||
// Create enum of supported file types | ||
pub enum FileType { | ||
Shapefile, | ||
GeoJson, | ||
} | ||
|
||
pub fn determine_file_type(input_file: &str) -> Result<FileType> { | ||
let file_extension = Path::new(input_file) | ||
.extension() | ||
.expect("No file extension found"); | ||
let file_extension_str = file_extension | ||
.to_str() | ||
.expect("Could not convert file extension to string"); | ||
match file_extension_str { | ||
"shp" => Ok(FileType::Shapefile), | ||
"json" => Ok(FileType::GeoJson), | ||
_ => Err("Unsupported file type".into()), | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::Result; | ||
use geo::Coord; | ||
use shapefile::Shape; | ||
|
||
pub fn to_geo(shape: &Shape) -> Result<geo::Geometry<f64>> { | ||
match shape { | ||
Shape::Point(p) => Ok(geo::Point::new(p.x, p.y).into()), | ||
Shape::Polyline(p) => { | ||
let mut coords: Vec<Coord> = Vec::new(); | ||
for part in p.parts().iter() { | ||
for point in part.iter() { | ||
coords.push(Coord::from((point.x, point.y))); | ||
} | ||
} | ||
Ok(geo::LineString::new(coords).into()) | ||
} | ||
Shape::Polygon(p) => { | ||
let mut outer_placeholder: Vec<(f64, f64)> = Vec::new(); | ||
let mut inner_rings: Vec<geo::LineString> = Vec::new(); | ||
|
||
for ring_type in p.rings() { | ||
match ring_type { | ||
//Gather all outer rings | ||
shapefile::PolygonRing::Outer(out) => { | ||
out.iter().for_each(|p| outer_placeholder.push((p.x, p.y))) | ||
} | ||
//Gather all inner rings | ||
shapefile::PolygonRing::Inner(inn) => { | ||
let mut inner_ring: Vec<(f64, f64)> = Vec::new(); | ||
inn.iter().for_each(|p| inner_ring.push((p.x, p.y))); | ||
let ls = geo::LineString::from(inner_ring); | ||
inner_rings.push(ls); | ||
} | ||
} | ||
} | ||
|
||
let outer_ring = geo::LineString::from(outer_placeholder); | ||
if inner_rings.is_empty() { | ||
let poly = geo::Polygon::new(outer_ring, vec![]); | ||
Ok(geo::Geometry::from(poly)) | ||
} else { | ||
let poly = geo::Polygon::new(outer_ring, inner_rings); | ||
Ok(geo::Geometry::from(poly)) | ||
} | ||
} | ||
_ => Err("Unsupported shape type".into()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod common; | ||
mod geo; | ||
pub mod shapefile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ pub type Error = Box<dyn std::error::Error>; | |
|
||
mod utils; | ||
mod pg; | ||
mod file_types; | ||
|
||
use utils::cli::run; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.