Skip to content
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

Add OSM attribution in the examples that use OSM tiles #128 #155

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

siddheshzz
Copy link

Description

  • This PR adds the attribution to the UI
  • OSM & Maptiler will have respective attribution as per their policy

Related Issue

Fixes #128

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please specify):

How to Test

Verified the change on the examples present in galileo/example

Screenshot 2025-02-21 at 10 50 41 AM Screenshot 2025-02-21 at 10 51 25 AM

Copy link
Owner

@Maximkaaa Maximkaaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! Just a few fixes needed to how the attribution is applied to the map.

Comment on lines +9 to +11
pub text: &'static str,
/// - `url`: An optional URL where more information about the attribution can be found.
pub url: Option<&'static str>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both fields should be String instead of &'static str. Attribution can come from any kind of source, e.g. app itself, file, internet etc, and it won't work with static reference.

Also, let's not make the fields public. Keep the internals of the struct opaque, and add methods to get the values as &str.

Comment on lines +391 to +393
fn attribution(&self) -> Option<Attribution> {
None
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have default implementation on the trait, so this duplicate is not needed. Also below.

@@ -44,6 +44,10 @@ pub trait Layer: MaybeSend + MaybeSync {
fn tile_schema(&self) -> Option<TileSchema> {
None
}
/// Returns the attribution of the layer, if available.
fn attribution(&self) -> Option<Attribution> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should return reference, not the value, since the value is not Copy.

Comment on lines +95 to +98
attribution: Some(Attribution::new(
"© OpenStreetMap contributors",
Some("https://www.openstreetmap.org/copyright"),
)),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all raster tile layers are from OSM, so this attribution should be moved to the RasterTileLayerBuilder::new_osm() method.

Comment on lines +141 to +144
attribution: Some(Attribution {
text: "© MapTiler © OpenStreetMap contributors",
url: Some("https://www.maptiler.com/copyright/"),
}),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be moved to the VectorTileLayerBuilder struct. Add a new method there with_attribution, allowing to provide the attribution when creating the layer. The specific MapTiler attribution must be set in the example code itself, when the layer is created.

@@ -27,6 +27,7 @@
//! * [`controls`](control) that actually change state of the map or layers based on the user input.

pub(crate) mod async_runtime;
pub mod attribution;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this module from crate::attribution to crate::layer::attribution. The module itself doesn't need to be public, the Attribution structure can be reexported at the root of the crate.

.show(ui.ctx(), |ui| {
ui.hyperlink_to(
self.map.collect_attributions().text,
self.map.collect_attributions().url.expect("URL error"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clearly wrong. The url field is optional, we don't expect it to be specified. Just don't create a link if it's not specified, make a simple label.

Comment on lines +32 to +49
let attribution = self.map.collect_attributions();
if !attribution.text.is_empty() {
egui::Window::new("Attribution")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::RIGHT_BOTTOM, [-10., -10.])
.fixed_size([350., 150.])
.show(ui.ctx(), |ui| {
ui.hyperlink_to(
self.map.collect_attributions().text,
self.map.collect_attributions().url.expect("URL error"),
);
});
self.map.render(ui);
} else {
self.map.render(ui);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic must be moved to the EguiMapState::render function. Allso, you don't need to put everything into the if/state, just check if there are attribution, and add the attribution window in that case something along the lines:

pub fn render(&mut self, ui: &mut egui::Ui) {
    if let Some(attributions) = self.collect_attributions() {
        self.show_attributions(ui);
    }
    ....
}

fn show_attributions(&mut self, ui: &mut egui::Ui) {
    egui::Window::new("Attribution")
    ....
}

Comment on lines +166 to +181
pub fn collect_attributions(&self) -> Attribution {
let all_layer: Vec<_> = self
.map
.layers()
.iter()
.filter_map(|layer| layer.attribution())
.collect();
if all_layer.is_empty() {
Attribution {
text: "",
url: Some(""),
}
} else {
all_layer[0].clone()
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to have a map with several layers that require attribution. In this case we want to show attribution for every one of them, not only the first one. Just collection Attribution of all layers to a vector, and if the vector is not empty, show the attribution block. In the block, for each entry in the vector add a hyperlink (or a label if URL is not specified), and add them to the block separated by the | symbol. Something like:

let mut is_first = true;
for attribution in &attributions {
    self.add_attribution_entry(attribution, ui);
    if !is_first { ui.add_label(" | ") }
    is_first = false;
}
...
fn add_attribution_entry(&mut self, attributionui: &mut egui::Ui) {
    if let Some(url) = attribution.url() {
      ui.hyperlink(...);
    } else {
      ui.label(...);
    }
}

Comment on lines +174 to +177
Attribution {
text: "",
url: Some(""),
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use empty value to represent what Option::None was created for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add OSM attribution in the examples that use OSM tiles
2 participants