-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from nmandery/datetime
read/write support for date und datetime fields
- Loading branch information
Showing
5 changed files
with
198 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
extern crate gdal; | ||
#[cfg(feature = "datetime")] | ||
extern crate chrono; | ||
|
||
use std::path::Path; | ||
use gdal::errors::Error; | ||
use gdal::vector::*; | ||
use std::fs; | ||
#[cfg(feature = "datetime")] | ||
use chrono::Duration; | ||
use std::ops::Add; | ||
|
||
#[cfg(feature = "datetime")] | ||
fn run() -> Result<(), Error> { | ||
println!("gdal crate was build with datetime support"); | ||
|
||
let mut dataset_a = Dataset::open(Path::new("fixtures/points_with_datetime.json"))?; | ||
let layer_a = dataset_a.layer(0)?; | ||
|
||
// Create a new dataset: | ||
let _ = fs::remove_file("/tmp/later.geojson"); | ||
let drv = Driver::get("GeoJSON")?; | ||
let mut ds = drv.create(Path::new("/tmp/later.geojson"))?; | ||
let lyr = ds.create_layer()?; | ||
|
||
// Copy the origin layer shema to the destination layer: | ||
for field in layer_a.defn().fields() { | ||
let field_defn = FieldDefn::new(&field.name(), field.field_type())?; | ||
field_defn.set_width(field.width()); | ||
field_defn.add_to_layer(lyr)?; | ||
} | ||
|
||
// Get the definition to use on each feature: | ||
let defn = Defn::from_layer(lyr); | ||
|
||
for feature_a in layer_a.features() { | ||
let mut ft = Feature::new(&defn)?; | ||
ft.set_geometry(feature_a.geometry().clone())?; | ||
// copy each field value of the feature: | ||
for field in defn.fields() { | ||
ft.set_field(&field.name(), &match feature_a.field(&field.name())? { | ||
|
||
// add one day to dates | ||
FieldValue::DateValue(value) => { | ||
println!("{} = {}", field.name(), value); | ||
FieldValue::DateValue(value.add(Duration::days(1))) | ||
}, | ||
|
||
// add 6 hours to datetimes | ||
FieldValue::DateTimeValue(value) => { | ||
println!("{} = {}", field.name(), value); | ||
FieldValue::DateTimeValue(value.add(Duration::hours(6))) | ||
}, | ||
v => v | ||
})?; | ||
} | ||
// Add the feature to the layer: | ||
ft.create(lyr)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(not(feature = "datetime"))] | ||
fn run() -> Result<(), Error> { | ||
println!("gdal crate was build without datetime support"); | ||
Ok(()) | ||
} | ||
|
||
|
||
fn main() { | ||
run().unwrap(); | ||
} | ||
|
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,19 @@ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
34.4, | ||
12.3 | ||
] | ||
}, | ||
"properties": { | ||
"dt": "2011-07-14T19:43:37-0500", | ||
"d": "2018-01-04" | ||
} | ||
} | ||
] | ||
} |
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