-
Notifications
You must be signed in to change notification settings - Fork 94
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
Take the field index, not name, in the feature getters and setters #581
base: master
Are you sure you want to change the base?
Conversation
r? @metasim, @ChristianBeilschmidt, @jdroenner since this is a relatively big change Hopefully I'll be able to get rid of this thing soon: (it goes on and on) |
I think this is nice, some questions... can we actually change the fields in any way that could broke the field id? From where is the image? I can see some transformations from |
You can delete fields from the
From Or we could go the other way around and introduce a |
Gentle nudge @jdroenner 😅. |
I thinking the next thing. Actually you said there is no function exposed that would be able to change the columns (remove/add), at some point I think would be good to have some tests that checks the actual idx do not change in any of this operations, just to be sure this is going to work fine in the future too. No idea if rn is the best moment. |
@latot if we don't expose
Keep in mind that the intended usage is something like: let id_idx = layer.defn().field_index("id")?;
let name_idx = layer.defn().field_index("name")?;
// you could call `layer.defn().delete_field(name_idx)?` here if it existed, but why?
for feature in layer.features() {
let id = feature.field_as_integer(id_idx)?.expect("id is set");
let name = feature.field_as_string(id_idx)?.expect("name is set");
println!("Feature {id}, name: {name}");
// you could never call `layer.defn().delete_field(name_idx)?` here because Rust
} |
Yes, the would be the usage, is more like a note to when something that could affect the id is exposed to the user, also, the only side I'm seeing, some ppl, could try to call that function directly even if is not exposed, would be the last concern from me. |
We can't prevent every bug. You can always pass in the wrong field index or name, or even delete the whole layer. But if you do, you'll notice pretty quickly once you run your program. What's harder to notice is that your program is running twice as slow because you've used the wrong field access methods. |
CHANGES.md
if knowledge of this change could be valuable to users.Field access by name tends to be very slow (I've seen it take 30% of the time in some programs), but beginners are unlikely to realize that they're doing a number of expensive lookups per feature.
Summary of changes:
field_count
returnusize
for consistencyDefn::field_index
and makeFeature::field_index
publicLayerAccess::create_feature_fields
(it could be updated, but it's a bit weird)