-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend the existing possibilities of writing ogr datasets. #31
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
505996c
Add basic support for writing ogr data
e00c7a3
Allow to clone geometries + Add example of reading-reprojecting-writi…
2614188
Allow to get/set more field types + improve read/write example
0304f14
minor clean-up + add example layer
aec9987
Add shortest methods to create/set fields/values
9353476
Try yo improve examples
7cba326
Update tests to reflect changes
ca9abcd
Add OGRFieldType enum + better error handling + methods renaming
8963266
fix method name in test in comment
63c97a8
Avoid using unwrap in library methods + simplify 'create_feature_fiel…
a4361e1
Return a Result on Feature 'field' method + fix test in comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
extern crate gdal; | ||
|
||
use std::fs; | ||
use std::path::Path; | ||
use gdal::vector::*; | ||
use gdal::spatial_ref::{SpatialRef, CoordTransform}; | ||
|
||
fn main() { | ||
let mut dataset_a = Dataset::open(Path::new("fixtures/roads.geojson")).unwrap(); | ||
let layer_a = dataset_a.layer(0).unwrap(); | ||
let fields_defn = layer_a.defn().fields() | ||
.map(|field| (field.name(), field.field_type(), field.width())) | ||
.collect::<Vec<_>>(); | ||
|
||
// Create a new dataset : | ||
fs::remove_file("/tmp/abcde.shp"); | ||
let drv = Driver::get("ESRI Shapefile").unwrap(); | ||
let mut ds = drv.create(Path::new("/tmp/abcde.shp")).unwrap(); | ||
let lyr = ds.create_layer().unwrap(); | ||
|
||
// Copy the origin layer shema to the destination layer : | ||
for fd in &fields_defn { | ||
let field_defn = FieldDefn::new(&fd.0, fd.1).unwrap(); | ||
field_defn.set_width(fd.2); | ||
field_defn.add_to_layer(&lyr); | ||
} | ||
|
||
// Prepare the origin and destination spatial references objects : | ||
let spatial_ref_src = SpatialRef::from_epsg(4326).unwrap(); | ||
let spatial_ref_dst = SpatialRef::from_epsg(3025).unwrap(); | ||
|
||
// And the feature used to actually transform the geometries : | ||
let htransform = CoordTransform::new(&spatial_ref_src, &spatial_ref_dst).unwrap(); | ||
|
||
// Get the definition to use on each feature : | ||
let defn = Defn::from_layer(&lyr); | ||
|
||
for feature_a in layer_a.features() { | ||
// Get the original geometry : | ||
let geom = feature_a.geometry(); | ||
// Get a new transformed geometry : | ||
let new_geom = geom.transform(&htransform).unwrap(); | ||
// Create the new feature, set its geometry : | ||
let mut ft = Feature::new(&defn).unwrap(); | ||
ft.set_geometry(new_geom); | ||
// copy each field value of the feature : | ||
for fd in &fields_defn { | ||
ft.set_field(&fd.0, &feature_a.field(&fd.0).unwrap()).unwrap(); | ||
} | ||
// Add the feature to the layer : | ||
ft.create(&lyr); | ||
} | ||
} |
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,99 @@ | ||
extern crate gdal; | ||
|
||
use std::path::Path; | ||
use std::fs; | ||
use gdal::vector::{Defn, Driver, Feature, FieldDefn, Geometry, OGRFieldType, FieldValue}; | ||
|
||
fn main(){ | ||
/// Example 1, the detailed way : | ||
{ | ||
fs::remove_file("/tmp/output1.geojson"); | ||
let drv = Driver::get("GeoJSON").unwrap(); | ||
let mut ds = drv.create(Path::new("/tmp/output1.geojson")).unwrap(); | ||
|
||
let lyr = ds.create_layer().unwrap(); | ||
|
||
let field_defn = FieldDefn::new("Name", OGRFieldType::OFTString).unwrap(); | ||
field_defn.set_width(80); | ||
field_defn.add_to_layer(&lyr); | ||
|
||
let field_defn = FieldDefn::new("Value", OGRFieldType::OFTReal).unwrap(); | ||
field_defn.add_to_layer(&lyr); | ||
|
||
let defn = Defn::from_layer(&lyr); | ||
|
||
// 1st feature : | ||
let mut ft = Feature::new(&defn).unwrap(); | ||
ft.set_geometry(Geometry::from_wkt("POINT (45.21 21.76)").unwrap()); | ||
ft.set_field_string("Name", "Feature 1"); | ||
ft.set_field_double("Value", 45.78); | ||
ft.create(&lyr); | ||
|
||
// 2nd feature : | ||
let mut ft = Feature::new(&defn).unwrap(); | ||
ft.set_geometry(Geometry::from_wkt("POINT (46.50 22.50)").unwrap()); | ||
ft.set_field_string("Name", "Feature 2"); | ||
ft.set_field_double("Value", 0.789); | ||
ft.create(&lyr); | ||
|
||
// Feature triggering an error due to a wrong field name : | ||
let mut ft = Feature::new(&defn).unwrap(); | ||
ft.set_geometry(Geometry::from_wkt("POINT (46.50 22.50)").unwrap()); | ||
ft.set_field_string("Name", "Feature 2"); | ||
match ft.set_field_double("Values", 0.789) { | ||
Ok(v) => v, Err(err) => println!("{:?}", err.to_string()), | ||
}; | ||
ft.create(&lyr); | ||
} | ||
|
||
/// Example 2, same output, shortened way : | ||
{ | ||
fs::remove_file("/tmp/output2.geojson"); | ||
let driver = Driver::get("GeoJSON").unwrap(); | ||
let mut ds = driver.create(Path::new("/tmp/output2.geojson")).unwrap(); | ||
let mut layer = ds.create_layer().unwrap(); | ||
|
||
layer.create_defn_fields(&[("Name", OGRFieldType::OFTString), ("Value", OGRFieldType::OFTReal)]); | ||
// Shortcut for : | ||
// let field_defn = FieldDefn::new("Name", OFT_STRING); | ||
// field_defn.add_to_layer(&layer); | ||
// let field_defn = FieldDefn::new("Value", OFT_REAL); | ||
// field_defn.add_to_layer(&layer); | ||
|
||
layer.create_feature_fields( | ||
Geometry::from_wkt("POINT (45.21 21.76)").unwrap(), | ||
&["Name", "Value"], | ||
&[FieldValue::StringValue("Feature 1".to_string()), FieldValue::RealValue(45.78)] | ||
).unwrap(); | ||
|
||
layer.create_feature_fields( | ||
Geometry::from_wkt("POINT (46.50 22.50)").unwrap(), | ||
&["Name", "Value"], | ||
&[FieldValue::StringValue("Feature 2".to_string()), FieldValue::RealValue(0.789)] | ||
).unwrap(); | ||
|
||
// Feature creation triggering an error due to a wrong field name : | ||
match layer.create_feature_fields( | ||
Geometry::from_wkt("POINT (46.50 22.50)").unwrap(), | ||
&["Abcd", "Value"], | ||
&[FieldValue::StringValue("Feature 2".to_string()), FieldValue::RealValue(0.789)]) { | ||
Ok(v) => v, | ||
Err(err) => println!("{:?}", err.to_string()), | ||
}; | ||
// Shortcuts for : | ||
// let defn = Defn::new_from_layer(&layer); | ||
// | ||
// let mut ft = Feature::new(&defn); | ||
// ft.set_geometry(Geometry::from_wkt("POINT (45.21 21.76)").unwrap()); | ||
// ft.set_field("Name", OFT_STRING, "Feature 1"); | ||
// ft.set_field("Value", OFT_REAL, 45.78); | ||
// ft.create(&lyr); | ||
// | ||
// let mut ft = Feature::new(&defn); | ||
// ft.set_geometry(Geometry::from_wkt("POINT (46.50 22.50)").unwrap()); | ||
// ft.set_field("Name", OFT_STRING, "Feature 2"); | ||
// ft.set_field("Value", OFT_REAL, 0.789); | ||
// ft.create(&lyr); | ||
} | ||
|
||
} |
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a more descriptive name for this error (maybe
InvalidFieldType
)?Also i added the method name to the errors related to the GDAL FFI calls to enable users to look up methods in the GDAL API. I'm not sure if it is needed here.