-
Notifications
You must be signed in to change notification settings - Fork 38
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
Documentation for parsing GeoJSON from a file #198
Comments
The |
Thanks for the quick reply. I found this issue and adapted his code to do what I needed, along with this bit of their docs. I'm thinking that the answer to my question is " Update: Thanks for the note about Update 2: I've achieved another order of magnitude speed-up by using rayon and placing the entire For posterity, here's what I ended up with: use geo::{MultiLineString, MultiPolygon};
use geojson::{Feature, GeoJson, Value};
use std::fs::File;
use std::io::BufReader;
fn load_geojson(path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let geojson = GeoJson::from_reader(reader)?;
match geojson {
GeoJson::FeatureCollection(collection) => {
for feature in collection.features {
let _ = process_feature(&feature)?;
}
}
_ => println!("Unsupported GeoJSON type"),
}
Ok(())
}
fn process_feature(feature: &Feature) -> Result<(), Box<dyn std::error::Error>> {
// String value of the "name" property, or an empty string
let name = feature
.property("name")
.and_then(|v| v.as_str())
.map_or(String::from(""), |s| s.to_string());
let geom = feature.geometry.as_ref().unwrap();
match &geom.value {
Value::MultiPolygon(_) => {
let p: MultiPolygon<f64> = geom.value.clone().try_into().unwrap();
println!("{p:?}");
}
Value::MultiLineString(_) => {
let p: MultiLineString<f64> = geom.value.clone().try_into().unwrap();
println!("{p:?}");
}
_ => panic!("not a recognized feature type"),
};
Ok(())
} |
I'd say the opposite. I'd say the real issue is that there's no "default" library in georust for handling geometries with attributes. You can parse to |
OK, that's interesting to know. If this is the higher-level library, I think it's even more important to have some sample code of loading a GeoJSON file (ideally from disk, but at least from a string) and iterating its features. I never got that to work with As for geometries and attributes, the |
You'd need to impl your own GeozeroDatasource. It's a bit of work, which is why it's not done often; instead converting to existing representations instead of creating your own.
Sure, but that's storing attributes in the GeoJSON model, which is quite restrictive. For example, you can't store a date time in GeoJSON; you can only store a string.
It's not geozero's concern to provide those structs. Geozero focuses only on conversions between representations. I'm building a representation around Arrow, which enables storing properties quite efficiently, but does incur some large dependencies. |
OK, well I filed this bug to document that I wanted to do something I thought was very common, and yet could find no sample code on how to do it. I've solved my problem at this point. If you want people to actually use this library for parsing features and properties out of geojson files, especially given that it's nonobvious how to do that, then having some sample code would really help and I'd suggest this bug should stay open. But if people looking for a simple way to iterate through features/properties in a GeoJSON files should just use Thanks for engaging with me on this, just trying to make things a little easier for the next guy or gal. :-) |
I am trying to read GeoJSON from a file and iterate through the features in the FeatureCollection with their geom and feature properties. I would have thought this would be a very common use case, but I don't see any documentation giving example code for how to do this. All the examples seem to assume you've got the string of a single geometry in memory already, or that you're using FlatGeobuf (
*.fgb
) files.I've found two examples on GitHub, but:
GeoJsonReader
orFeatureProcessor
, which seems like it should be the right way…GeoTableBuilder
thing which I think is part of Arrow, and thus not something someone who isn't using Arrow would use.Have I missed the sample code for this? I'd expect a simple example in the overview on the
GeoJsonReader
page, and probably in the main README as well.The text was updated successfully, but these errors were encountered: