-
-
Notifications
You must be signed in to change notification settings - Fork 220
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
Make tile service GoogleMaps-Compatible when the cog file is GoogleMapsCompatible #1637
Draft
sharkAndshark
wants to merge
2
commits into
maplibre:main
Choose a base branch
from
sharkAndshark:google_schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ use crate::{ | |
MartinResult, Source, TileData, UrlQuery, | ||
}; | ||
|
||
use regex::Regex; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct CogSource { | ||
id: String, | ||
|
@@ -41,6 +43,8 @@ struct Meta { | |
zoom_and_ifd: HashMap<u8, usize>, | ||
zoom_and_tile_across_down: HashMap<u8, (u32, u32)>, | ||
nodata: Option<f64>, | ||
google_compatible_max_zoom: Option<u8>, | ||
google_compatible_min_zoom: Option<u8>, | ||
} | ||
|
||
#[async_trait] | ||
|
@@ -75,9 +79,14 @@ impl Source for CogSource { | |
Decoder::new(tif_file).map_err(|e| CogError::InvalidTiffFile(e, self.path.clone()))?; | ||
decoder = decoder.with_limits(tiff::decoder::Limits::unlimited()); | ||
|
||
let ifd = self.meta.zoom_and_ifd.get(&(xyz.z)).ok_or_else(|| { | ||
let actual_zoom = if let Some(google_zoom) = self.meta.google_compatible_max_zoom { | ||
self.meta.max_zoom - google_zoom + xyz.z | ||
} else { | ||
xyz.z | ||
}; | ||
let ifd = self.meta.zoom_and_ifd.get(&(actual_zoom)).ok_or_else(|| { | ||
CogError::ZoomOutOfRange( | ||
xyz.z, | ||
actual_zoom, | ||
self.path.clone(), | ||
self.meta.min_zoom, | ||
self.meta.max_zoom, | ||
|
@@ -216,8 +225,8 @@ impl SourceConfigExtras for CogConfig { | |
let meta = get_meta(&path)?; | ||
let tilejson = tilejson! { | ||
tiles: vec![], | ||
minzoom: meta.min_zoom, | ||
maxzoom: meta.max_zoom | ||
minzoom: meta.google_compatible_min_zoom.unwrap_or(meta.min_zoom), | ||
maxzoom: meta.google_compatible_max_zoom.unwrap_or(meta.max_zoom), | ||
}; | ||
Ok(Box::new(CogSource { | ||
id, | ||
|
@@ -287,6 +296,9 @@ fn get_meta(path: &PathBuf) -> Result<Meta, FileError> { | |
} else { | ||
None | ||
}; | ||
|
||
let (tiling_schema_name, zoom_level) = get_tilling_schema(&mut decoder).unwrap_or((None, None)); | ||
|
||
let images_ifd = get_images_ifd(&mut decoder); | ||
|
||
let mut zoom_and_ifd: HashMap<u8, usize> = HashMap::new(); | ||
|
@@ -315,9 +327,21 @@ fn get_meta(path: &PathBuf) -> Result<Meta, FileError> { | |
.keys() | ||
.max() | ||
.ok_or_else(|| CogError::NoImagesFound(path.clone()))?; | ||
|
||
let google_compatible_max_zoom = | ||
if tiling_schema_name == Some("GoogleMapsCompatible".to_string()) { | ||
zoom_level | ||
} else { | ||
None | ||
}; | ||
let google_compatible_min_zoom = | ||
google_compatible_max_zoom.map(|google_max_zoom| google_max_zoom - max_zoom + min_zoom); | ||
|
||
Ok(Meta { | ||
min_zoom: *min_zoom, | ||
max_zoom: *max_zoom, | ||
google_compatible_max_zoom, | ||
google_compatible_min_zoom, | ||
zoom_and_ifd, | ||
zoom_and_tile_across_down, | ||
nodata, | ||
|
@@ -375,3 +399,64 @@ fn get_images_ifd(decoder: &mut Decoder<File>) -> Vec<usize> { | |
} | ||
res | ||
} | ||
|
||
fn get_tilling_schema( | ||
decoder: &mut Decoder<File>, | ||
) -> Result<(Option<String>, Option<u8>), CogError> { | ||
let gdal_metadata = decoder | ||
.get_tag_ascii_string(Tag::Unknown(42112)) | ||
.map_err(|e| CogError::TagsNotFound(e, vec![42112], 0, PathBuf::new()))?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just return None. It's normal a tiff file have no tag 42112 |
||
|
||
let mut tiling_schema_name = None; | ||
let mut zoom_level = None; | ||
|
||
let re_name = Regex::new(r#"<Item name="NAME" domain="TILING_SCHEME">([^<]+)</Item>"#).unwrap(); | ||
let re_zoom = | ||
Regex::new(r#"<Item name="ZOOM_LEVEL" domain="TILING_SCHEME">([^<]+)</Item>"#).unwrap(); | ||
|
||
if let Some(caps) = re_name.captures(&gdal_metadata) { | ||
tiling_schema_name = Some(caps[1].to_string()); | ||
} | ||
|
||
if let Some(caps) = re_zoom.captures(&gdal_metadata) { | ||
zoom_level = caps[1].parse().ok(); | ||
} | ||
|
||
Ok((tiling_schema_name, zoom_level)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case("../tests/fixtures/cog/google_compatible.tif", Some("GoogleMapsCompatible".to_string()), Some(14))] | ||
#[case("../tests/fixtures/cog/rgba_u8_nodata.tiff", None, None)] | ||
fn test_get_gdal_metadata( | ||
#[case] path: &str, | ||
#[case] expected_tiling_schema_name: Option<String>, | ||
#[case] expected_zoom_level: Option<u8>, | ||
) { | ||
use std::{fs::File, path::PathBuf}; | ||
|
||
use tiff::decoder::Decoder; | ||
|
||
use crate::cog::get_tilling_schema; | ||
|
||
let path = PathBuf::from(path); | ||
let tif_file = File::open(&path).expect("Failed to open test fixture file"); | ||
let mut decoder = Decoder::new(tif_file).expect("Failed to create decoder"); | ||
|
||
let (tiling_schema_name, zoom_level) = | ||
get_tilling_schema(&mut decoder).expect("Failed to get GDAL metadata"); | ||
|
||
assert_eq!( | ||
tiling_schema_name, expected_tiling_schema_name, | ||
"TILING_SCHEME_NAME value should match the expected value" | ||
); | ||
assert_eq!( | ||
zoom_level, expected_zoom_level, | ||
"ZOOM_LEVEL value should match the expected value" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"maxzoom": 14, | ||
"minzoom": 12, | ||
"tilejson": "3.0.0", | ||
"tiles": [ | ||
"http://localhost:3111/google_compatible/{z}/{x}/{y}" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should calculate the actual_x and actual_y either.