-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: main
Are you sure you want to change the base?
Conversation
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.
Great stuff! Just a few fixes needed to how the attribution is applied to the map.
pub text: &'static str, | ||
/// - `url`: An optional URL where more information about the attribution can be found. | ||
pub url: Option<&'static str>, |
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.
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
.
fn attribution(&self) -> Option<Attribution> { | ||
None | ||
} |
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.
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> { |
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.
This method should return reference, not the value, since the value is not Copy
.
attribution: Some(Attribution::new( | ||
"© OpenStreetMap contributors", | ||
Some("https://www.openstreetmap.org/copyright"), | ||
)), |
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.
Not all raster tile layers are from OSM, so this attribution should be moved to the RasterTileLayerBuilder::new_osm()
method.
attribution: Some(Attribution { | ||
text: "© MapTiler © OpenStreetMap contributors", | ||
url: Some("https://www.maptiler.com/copyright/"), | ||
}), |
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.
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; |
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.
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"), |
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.
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.
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); | ||
} |
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.
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")
....
}
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() | ||
} | ||
} |
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.
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(...);
}
}
Attribution { | ||
text: "", | ||
url: Some(""), | ||
} |
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.
Don't use empty value to represent what Option::None
was created for.
Description
Related Issue
Fixes #128
Type of Change
How to Test
Verified the change on the examples present in galileo/example