-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GeoPackage用のSinkを実装(現在の部品をもとにして) (#136)
close #121 #89 , #129 での、Gpkg変換の初期実装をもとに、全体の流れに組み込む(sinkを作る)。 とりあえず書いてみたコード例のようなものなので、コメントいただきたいです。 ## 悩み SQLx(async)の取り扱い。とりあえず、Sinkの中でブロックしてやってみているが、こう言う方法でいいのか? --------- Co-authored-by: Taku Fukada <naninunenor@gmail.com>
- Loading branch information
Showing
7 changed files
with
142 additions
and
16 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod geometry; | ||
pub mod geometry; | ||
mod handler; | ||
|
||
pub use handler::GpkgHandler; | ||
pub use handler::*; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//! GeoPackage sink | ||
|
||
use rayon::prelude::*; | ||
|
||
use crate::configuration::Config; | ||
use crate::pipeline::{Feedback, Receiver}; | ||
use crate::sink::{DataSink, DataSinkProvider, SinkInfo}; | ||
|
||
use nusamai_gpkg::geometry::multipolygon_to_bytes; | ||
use nusamai_gpkg::GpkgHandler; | ||
|
||
pub struct GpkgSinkProvider {} | ||
|
||
impl DataSinkProvider for GpkgSinkProvider { | ||
fn create(&self, _config: &Config) -> Box<dyn DataSink> { | ||
Box::<GpkgSink>::default() | ||
} | ||
|
||
fn info(&self) -> SinkInfo { | ||
SinkInfo { | ||
name: "GeoPackage".to_string(), | ||
} | ||
} | ||
|
||
fn config(&self) -> Config { | ||
Config::default() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct GpkgSink {} | ||
|
||
impl GpkgSink { | ||
pub async fn run_async(&mut self, upstream: Receiver, feedback: &mut Feedback) { | ||
let mut handler = GpkgHandler::init("output.gpkg").await.unwrap(); | ||
|
||
let (sender, mut receiver) = tokio::sync::mpsc::channel(100); | ||
|
||
let producers = { | ||
let feedback = feedback.clone(); | ||
tokio::task::spawn_blocking(move || { | ||
let _ = upstream.into_iter().par_bridge().try_for_each_with( | ||
sender, | ||
|sender, parcel| { | ||
if feedback.is_cancelled() { | ||
return Err(()); | ||
} | ||
let cityobj = parcel.cityobj; | ||
if !cityobj.geometries.multipolygon.is_empty() { | ||
let bytes = multipolygon_to_bytes( | ||
&cityobj.geometries.vertices, | ||
&cityobj.geometries.multipolygon, | ||
4326, | ||
); | ||
if sender.blocking_send(bytes).is_err() { | ||
return Err(()); | ||
}; | ||
} | ||
Ok(()) | ||
}, | ||
); | ||
}) | ||
}; | ||
|
||
let mut tx = handler.begin().await.unwrap(); | ||
while let Some(gpkg_bin) = receiver.recv().await { | ||
if feedback.is_cancelled() { | ||
return; | ||
} | ||
tx.insert_feature(&gpkg_bin).await; | ||
} | ||
tx.commit().await.unwrap(); | ||
|
||
producers.await.unwrap(); | ||
} | ||
} | ||
|
||
impl DataSink for GpkgSink { | ||
fn run(&mut self, upstream: Receiver, feedback: &mut Feedback) { | ||
let runtime = tokio::runtime::Runtime::new().unwrap(); | ||
runtime.block_on(self.run_async(upstream, feedback)); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test() {} | ||
} |
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,5 @@ | ||
pub mod geojson; | ||
pub mod gpkg; | ||
pub mod noop; | ||
pub mod serde; | ||
|
||
|