-
Notifications
You must be signed in to change notification settings - Fork 91
/
read_write_ogr_datetime.rs
63 lines (54 loc) · 2.14 KB
/
read_write_ogr_datetime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use gdal::{vector::LayerAccess, DriverManager};
fn run() -> gdal::errors::Result<()> {
use chrono::Duration;
use gdal::vector::{Defn, Feature, FieldDefn, FieldValue};
use gdal::Dataset;
use std::ops::Add;
use std::path::Path;
println!("gdal crate was build with datetime support");
let dataset_a = Dataset::open(Path::new("fixtures/points_with_datetime.json"))?;
let mut layer_a = dataset_a.layer(0)?;
// Create a new dataset:
let path = std::env::temp_dir().join("later.geojson");
let _ = std::fs::remove_file(&path);
let drv = DriverManager::get_driver_by_name("GeoJSON")?;
let mut ds = drv.create_vector_only(path.to_str().unwrap())?;
let lyr = ds.create_layer(Default::default())?;
// 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())?.unwrap() {
// 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(())
}
fn main() {
run().unwrap();
}