Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 55 additions & 25 deletions crates/ui-egui/src/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Session-only image/grid choices shared by Annotate and the ordinary Edit tools.
use crate::{Landed, RightPane, RustGisApp};
use rustgis_engine::data::GeometryType;
use rustgis_engine::{Tool, box_edit::Frame, image_grid::Source};
use serde_json::{Value, json};

Expand Down Expand Up @@ -79,6 +80,7 @@ pub(crate) fn sync(app: &mut RustGisApp) {
}
}
crate::annotation_classes::sync(app);
crate::mapview::cancel_stale_annotation(app);
}
/// Keep annotation navigation in the image plane, including when a shape tool is
/// retained while another pane is open. Explicit camera commands remain available.
Expand Down Expand Up @@ -244,14 +246,14 @@ pub(crate) fn drawing_frame(app: &RustGisApp) -> Result<Frame, String> {
.clone()
.ok_or_else(|| app.annotation.error.clone().unwrap_or_else(|| tl!("Wait for the original image grid before drawing.").into()))
}
fn polygon_layer(app: &RustGisApp, id: u32) -> bool {
app.session.map().layer(id).is_some_and(is_polygon_layer)
fn annotation_layer(app: &RustGisApp, id: u32) -> bool {
app.session.map().layer(id).is_some_and(is_annotation_layer)
}
fn is_polygon_layer(l: &rustgis_engine::map::Layer) -> bool {
pub(crate) fn is_annotation_layer(l: &rustgis_engine::map::Layer) -> bool {
!l.is_streamed()
&& !l.graphics
&& l.raster.is_none()
&& matches!(l.geometry_type(), rustgis_engine::data::GeometryType::Polygon | rustgis_engine::data::GeometryType::None)
&& matches!(l.geometry_type(), GeometryType::Point | GeometryType::Polygon | GeometryType::None)
}
pub(crate) fn filename_option(app: &mut RustGisApp, on: bool) {
sync(app);
Expand Down Expand Up @@ -279,6 +281,9 @@ pub(crate) fn export_name(app: &RustGisApp, layer: u32, ext: &str, annotation: b
if annotation && ext != "geojson" {
return Err(tl!("Annotation export uses GeoJSON.").into());
}
if annotation && !annotation_layer(app, layer) {
return Err(tl!("Choose a loaded editable point or polygon layer.").into());
}
if annotation && app.annotation.image_filename {
return image_export_name(app)
.ok_or_else(|| tl!("Select an image with an available filename, or uncheck Use image filename.").into());
Expand All @@ -287,17 +292,28 @@ pub(crate) fn export_name(app: &RustGisApp, layer: u32, ext: &str, annotation: b
}
pub(crate) fn select_layer(app: &mut RustGisApp, p: &Value) -> Result<(), String> {
let id = app.session.layer_id(p)?;
if !polygon_layer(app, id) {
return Err(tl!("Choose a loaded editable polygon layer.").into());
if !annotation_layer(app, id) {
return Err(tl!("Choose a loaded editable point or polygon layer.").into());
}
app.run("layer.select", json!({"layer":id}))?;
let kind = app.session.map().layer(id).map(|l| l.geometry_type());
let compatible = match app.session.tool {
Tool::CreatePoint => kind == Some(GeometryType::Point),
Tool::CreatePolygon => kind == Some(GeometryType::Polygon),
Tool::RotatedBox | Tool::Circle => kind != Some(GeometryType::Point),
_ => true,
};
if !compatible {
app.run("tool.set", json!({"tool":"select"}))?;
}
app.run("layer.select", json!({"layer":id})).map(|_| ())
Ok(())
}
/// Annotation selection is scoped to its target layer. It selects ordinary polygons too,
/// but only constructions already known to this session activate an exact-shape editor.
pub(crate) fn click(app: &mut RustGisApp, c: rustgis_engine::geom::Coord, modifiers: egui::Modifiers) -> bool {
if app.ui.right != Some(RightPane::Annotation)
|| !matches!(app.session.tool, Tool::Explore | Tool::Select)
|| !app.session.active_layer.is_some_and(|id| polygon_layer(app, id))
|| !app.session.active_layer.is_some_and(|id| annotation_layer(app, id))
{
return false;
}
Expand Down Expand Up @@ -385,7 +401,7 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) {
let _ = app.run("ui.annotationGrid", json!({"pixels":pixels}));
}
if app.annotation.pixels {
ui.small(tl!("New shapes use the original GeoTIFF pixels, including rotation and shear. Existing objects keep their own grid."));
ui.small(tl!("New circles and rotated boxes use original GeoTIFF pixels, including rotation and shear. Existing shapes keep their construction grid."));
if let Some(f) = &app.annotation.frame {
ui.small(crate::i18n::fmt(
"{width} × {height} pixels · EPSG:{epsg}",
Expand All @@ -405,7 +421,7 @@ pub(crate) fn grid_controls(app: &mut RustGisApp, ui: &mut egui::Ui) {
}
} else {
ui.small(grid_units(&layer_frame(app)).unwrap_or_default());
ui.small(tl!("Image pixels are the default when an image is selected."));
ui.small(tl!("Selecting an image defaults circles and rotated boxes to its pixel grid."));
}
if image.is_some() && !available {
ui.colored_label(egui::Color32::RED, tl!("This image is unavailable. Choose another image or No image."));
Expand All @@ -417,12 +433,13 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) {
ui.heading(tl!("Image annotation"));
grid_controls(app, ui);
ui.separator();
let active = app.session.active_layer.filter(|id| polygon_layer(app, *id));
let active = app.session.active_layer.filter(|id| annotation_layer(app, *id));
let mut layer = active;
let name =
layer.and_then(|id| app.session.map().layer(id)).map_or_else(|| tl!("Choose a polygon layer").to_string(), |l| l.name.clone());
let name = layer
.and_then(|id| app.session.map().layer(id))
.map_or_else(|| tl!("Choose a point or polygon layer").to_string(), |l| l.name.clone());
egui::ComboBox::from_label(tl!("Target layer")).selected_text(name).show_ui(ui, |ui| {
for l in app.session.map().layers.iter().filter(|l| is_polygon_layer(l)) {
for l in app.session.map().layers.iter().filter(|l| is_annotation_layer(l)) {
ui.selectable_value(&mut layer, Some(l.id), &l.name);
}
});
Expand All @@ -432,7 +449,7 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) {
let _ = app.run("ui.annotationLayer", json!({"layer":id}));
}
if layer.is_none() {
ui.label(tl!("Add or create a polygon feature layer using Data, then choose it here."));
ui.label(tl!("Add or create a point or polygon feature layer using Data, then choose it here."));
}
let mut enabled = app.session.map().editing.settings.enabled;
if ui.checkbox(&mut enabled, tl!("Enable editing")).changed() {
Expand All @@ -454,17 +471,28 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) {
"This is an ordinary polygon. To edit it as an exact shape, right-click it and choose Convert to Shape…; otherwise use Edit Vertices."
));
}
let target = layer.and_then(|id| app.session.map().layer(id));
let geometry = target.map(|l| l.geometry_type());
let editable = target.is_some_and(|l| app.session.map().draws_at(l, app.session.scale())) && enabled;
let constrained = geometry != Some(GeometryType::Point);
let ready = crate::boxedit::ready(app) && drawing_frame(app).is_ok();
ui.add_enabled_ui(ready, |ui| {
ui.horizontal(|ui| {
for (tool, name) in [(Tool::RotatedBox, tl!("Rotated Box")), (Tool::Circle, tl!("Circle"))] {
if ui.selectable_label(app.session.tool == tool, name).clicked() {
let _ = app.run("tool.set", json!({"tool":tool.code()}));
}
ui.horizontal_wrapped(|ui| {
let tools = [
(Tool::CreatePoint, tl!("Point"), geometry == Some(GeometryType::Point), editable),
(Tool::CreatePolygon, tl!("Polygon"), geometry == Some(GeometryType::Polygon), editable),
(Tool::RotatedBox, tl!("Rotated Box"), constrained, ready),
(Tool::Circle, tl!("Circle"), constrained, ready),
];
for (tool, name, show, usable) in tools {
if show && ui.add_enabled(usable, egui::Button::selectable(app.session.tool == tool, name)).clicked() {
let _ = app.run("tool.set", json!({"tool":tool.code()}));
}
});
}
});
if !crate::boxedit::ready(app) {
if app.session.tool == Tool::CreatePolygon {
ui.small(tl!("Click to add vertices. Double-click to finish, or press Escape to cancel."));
}
if constrained && !crate::boxedit::ready(app) {
let hidden = app.session.active_layer.and_then(|id| app.session.map().layer(id)).is_some_and(|l| {
rustgis_engine::box_edit::check_tool(&app.session, l.id).is_ok() && !app.session.map().draws_at(l, app.session.scale())
});
Expand All @@ -476,7 +504,9 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) {
} else if hidden {
ui.small(tl!("The polygon layer doesn't draw at this scale. Zoom in or out until it draws, then draw."));
} else {
ui.small(tl!("Drawing requires editing enabled, a loaded polygon layer and a flat Earth map with zero pitch."));
ui.small(tl!(
"Circle and rotated box drawing requires editing enabled, a loaded polygon layer and a flat Earth map with zero pitch."
));
}
}
ui.small(tl!("Space, middle-drag or right-drag pans; releasing Space returns to the active drawing tool. Map rotation and tilt gestures are disabled while annotating. Undo/Redo use the existing commands."));
Expand Down Expand Up @@ -515,7 +545,7 @@ pub(crate) fn panel(app: &mut RustGisApp, ui: &mut egui::Ui) {
}
});
ui.small(tl!(
"Exports contain ordinary polygons and feature properties. Image/grid choices and exact constructions remain session-only."
"GeoJSON keeps points, polygons and their properties. Circles and rotated boxes reopen as polygons. Right-click a polygon and choose Convert to Shape… to restore constrained editing."
));
}

Expand Down
57 changes: 48 additions & 9 deletions crates/ui-egui/src/annotation_classes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Class defaults are session-only. Values and colours use ordinary attributes and symbology.
use crate::{RightPane, RustGisApp};
use rustgis_engine::data::{FieldType, GeometryType};
use rustgis_engine::data::FieldType;
#[cfg(test)]
use rustgis_engine::data::GeometryType;
use rustgis_engine::map::{Layer, Renderer};
use serde_json::{Value, json};

Expand Down Expand Up @@ -29,9 +31,7 @@ pub(crate) struct State {
refusal: Option<String>,
}
fn target(app: &RustGisApp) -> Option<&Layer> {
app.session.active_layer.and_then(|id| app.session.map().layer(id)).filter(|l| {
!l.is_streamed() && !l.graphics && l.raster.is_none() && matches!(l.geometry_type(), GeometryType::Polygon | GeometryType::None)
})
app.session.active_layer.and_then(|id| app.session.map().layer(id)).filter(|l| crate::annotation::is_annotation_layer(l))
}
fn binding(app: &RustGisApp) -> Option<Binding> {
let l = target(app)?;
Expand Down Expand Up @@ -81,17 +81,21 @@ fn field(app: &RustGisApp) -> Result<&str, String> {
return Err(tl!("The target layer changed. Choose its class field again.").into());
}
let name = state.field.as_deref().ok_or_else(|| tl!("Choose a class field first.").to_string())?;
validate_field(app, name)?;
Ok(name)
}
fn validate_field(app: &RustGisApp, name: &str) -> Result<(), String> {
if target(app).is_some_and(|l| l.dataset.fields.iter().filter(|f| f.name.eq_ignore_ascii_case(name)).count() > 1) {
return Err(tl!("Class field names must be unique ignoring case. Rename the duplicate fields first.").into());
}
if !target(app).is_some_and(|l| l.dataset.fields.iter().any(|f| f.name == name && f.ty == FieldType::Text)) {
return Err(tl!("The class field is missing or is no longer text. Choose another class field.").into());
}
Ok(name)
Ok(())
}
pub(crate) fn select_field(app: &mut RustGisApp, p: &Value) -> Result<(), String> {
sync(app);
let layer = target(app).ok_or_else(|| tl!("Choose a loaded editable polygon layer.").to_string())?;
let layer = target(app).ok_or_else(|| tl!("Choose a loaded editable point or polygon layer.").to_string())?;
let name = match p.get("field") {
Some(Value::Null) => None,
Some(Value::String(name)) => {
Expand Down Expand Up @@ -135,7 +139,7 @@ pub(crate) fn select_class(app: &mut RustGisApp, p: &Value) -> Result<(), String
}
pub(crate) fn create_field(app: &mut RustGisApp) -> Result<(), String> {
sync(app);
let layer = target(app).ok_or_else(|| tl!("Choose a loaded editable polygon layer.").to_string())?;
let layer = target(app).ok_or_else(|| tl!("Choose a loaded editable point or polygon layer.").to_string())?;
if layer.dataset.field_index("class").is_some() {
return Err(tl!("A field named class already exists. Choose a text field, or rename the existing field in Data.").into());
}
Expand Down Expand Up @@ -164,6 +168,41 @@ pub(crate) fn drawing_attributes(app: &RustGisApp) -> Result<Value, String> {
attrs.insert(name.to_string(), json!(value));
Ok(Value::Object(attrs))
}
/// A multi-click sketch keeps only its target identity and the class at its first vertex.
pub(crate) struct SketchClass {
target: Binding,
attributes: Value,
}
impl SketchClass {
pub(crate) fn capture(app: &RustGisApp) -> Result<Self, String> {
if !app.session.map().editing.settings.enabled {
return Err(tl!("Enable editing before drawing an annotation.").into());
}
if !target(app).is_some_and(|l| app.session.map().draws_at(l, app.session.scale())) {
return Err(tl!("Choose a point or polygon layer that is visible at this scale.").into());
}
let target = binding(app).ok_or_else(|| tl!("Choose a loaded editable point or polygon layer.").to_string())?;
Ok(Self { target, attributes: drawing_attributes(app)? })
}
pub(crate) fn check_target(&self, app: &RustGisApp) -> Result<(), String> {
if !app.session.map().editing.settings.enabled {
return Err(tl!("Editing was turned off, so the annotation sketch was cancelled.").into());
}
if Some(self.target) != binding(app) {
return Err(tl!("The annotation target changed. Start drawing again.").into());
}
Ok(())
}
pub(crate) fn attributes(self, app: &RustGisApp) -> Result<Value, String> {
self.check_target(app)?;
if let Some(fields) = self.attributes.as_object() {
for name in fields.keys() {
validate_field(app, name)?;
}
}
Ok(self.attributes)
}
}
pub(crate) fn assign(app: &mut RustGisApp, p: &Value) -> Result<(), String> {
sync(app);
let name = field(app)?.to_string();
Expand All @@ -172,7 +211,7 @@ pub(crate) fn assign(app: &mut RustGisApp, p: &Value) -> Result<(), String> {
Some(Value::String(value)) => class_name(value)?,
_ => return Err(tl!("Choose a non-empty class name.").into()),
};
let l = target(app).ok_or_else(|| tl!("Choose a loaded editable polygon layer.").to_string())?;
let l = target(app).ok_or_else(|| tl!("Choose a loaded editable point or polygon layer.").to_string())?;
let [row] = l.selection.as_slice() else { return Err(tl!("Select exactly one annotation to change its class.").into()) };
let fi = l.dataset.fields.iter().position(|f| f.name == name).ok_or_else(|| tl!("Choose a class field first.").to_string())?;
let feature = l.dataset.features.row(*row).ok_or_else(|| tl!("The selected annotation is no longer available.").to_string())?;
Expand All @@ -184,7 +223,7 @@ pub(crate) fn assign(app: &mut RustGisApp, p: &Value) -> Result<(), String> {
pub(crate) fn colours(app: &mut RustGisApp) -> Result<(), String> {
sync(app);
let name = field(app)?.to_string();
let l = target(app).ok_or_else(|| tl!("Choose a loaded editable polygon layer.").to_string())?;
let l = target(app).ok_or_else(|| tl!("Choose a loaded editable point or polygon layer.").to_string())?;
let styled = matches!(&l.renderer, Renderer::Unique { field, expression: None, .. } if *field == name);
app.ui.symbology_pending = if styled { None } else { Some(crate::PendingUniqueSymbology::new(&app.session, l, name)) };
app.ui.open_right(RightPane::Symbology);
Expand Down
Loading
Loading