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

Pixel size for sampling tolerance in WGS84 #285

Open
ChristianBeilschmidt opened this issue Apr 7, 2022 · 1 comment
Open

Pixel size for sampling tolerance in WGS84 #285

ChristianBeilschmidt opened this issue Apr 7, 2022 · 1 comment

Comments

@ChristianBeilschmidt
Copy link

I'm using T-Rex to generate WGS84 vector tiles for Natural Earth Data. For this, I import the geopackage into PostGIS and then use this as a datasource for T-Rex.

One layer is specified like this:

[[tileset.layer]]
name = "boundary_lines_land"
geometry_field = "geom"
geometry_type = "MULTILINESTRING"
fid_field = "fid"
srid = 4326
buffer_size = 0
simplify = true
make_valid = true

[[tileset.layer.query]]
minzoom = 0
maxzoom = 2
sql = """SELECT geom, fid, featurecla, name, min_zoom FROM ne_110m_admin_0_boundary_lines_land WHERE min_zoom::integer <= !zoom! AND geom && !bbox!"""

[[tileset.layer.query]]
minzoom = 3
maxzoom = 4
sql = """SELECT geom, fid, featurecla, name, min_zoom FROM ne_50m_admin_0_boundary_lines_land WHERE min_zoom::integer <= !zoom! AND geom && !bbox!"""

[[tileset.layer.query]]
minzoom = 5
maxzoom = 10
sql = """SELECT geom, fid, featurecla, name, min_zoom FROM ne_10m_admin_0_boundary_lines_land WHERE min_zoom::integer <= !zoom! AND geom && !bbox!"""

It seems to work in general 🎉 but unfortunately every line gets simplified to having only ~3 nodes.

grafik

Then I found the tolerance parameter, which is per default !pixel_width! / 2.
However, there was no difference when I changed the 2 to 4, 16 or 100 – which appeared very strange.

I look into the codebase about the database queries and the string replacement seems to be working just fine, so this left me to question what pixel_width is.
In more detail, I found the following code:

t-rex/tile-grid/src/grid.rs

Lines 185 to 193 in f2433f6

/// Pixel width for 256x256 tile
pub fn pixel_width(&self, zoom: u8) -> f64 {
const METERS_PER_DEGREE: f64 = 6378137.0 * 2.0 * consts::PI / 360.0;
match self.units {
Unit::Meters => self.resolutions[zoom as usize],
Unit::Degrees => self.resolutions[zoom as usize] * METERS_PER_DEGREE,
Unit::Feet => self.resolutions[zoom as usize] * 0.3048,
}
}

So pixel width has something to do with 256-pixel tile width in meters for Web Mercator.

Then, I tried to divide the pixel width with the fixed value that was multiplied to our degree values.
This leads to the following setting:

tolerance = "!pixel_width! / 3507975.35 / 2"

And this seems to solve the simplification problem to reasonable country borders.

grafik

So I guess either there should either be different defaults for different projections for tolerance or the pixel size for degrees is debatable.

@pka
Copy link
Member

pka commented Apr 22, 2022

Thanks for your investigations. Seems clear that the current tolerance default doesn't make sense for geographic coordinates. Thanks for documenting a tolerance configuration,which works better!

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

No branches or pull requests

2 participants