Skip to content

Commit

Permalink
Reverse y for scaled MVT tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Jul 27, 2023
1 parent 1271e35 commit f44e832
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
33 changes: 20 additions & 13 deletions geozero/src/mvt/mvt_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::mvt_error::MvtError;
/// Generator for MVT geometry type.
pub struct MvtWriter {
pub(crate) feature: tile::Feature,
tile_size: f64,
tile_size: Option<f64>,
// Tile extent
left: f64,
bottom: f64,
Expand All @@ -35,7 +35,7 @@ enum LineState {
impl MvtWriter {
pub fn new(tile_size: u32, left: f64, bottom: f64, right: f64, top: f64) -> MvtWriter {
MvtWriter {
tile_size: tile_size as f64,
tile_size: Some(tile_size as f64),
left,
bottom,
right,
Expand All @@ -62,11 +62,11 @@ impl Default for MvtWriter {
fn default() -> Self {
Self {
feature: tile::Feature::default(),
tile_size: 512.0,
tile_size: None,
left: 0.0,
bottom: 0.0,
right: 512.0,
top: 512.0,
right: 0.0,
top: 0.0,
last_x: 0,
last_y: 0,
line_state: LineState::None,
Expand All @@ -76,7 +76,7 @@ impl Default for MvtWriter {
}

impl GeomProcessor for MvtWriter {
fn xy(&mut self, x: f64, y: f64, idx: usize) -> Result<()> {
fn xy(&mut self, x_coord: f64, y_coord: f64, idx: usize) -> Result<()> {
// Omit last coord of ring (emit ClosePath instead)
let last_ring_coord = if let LineState::Ring(size) = self.line_state {
idx == size - 1
Expand All @@ -85,8 +85,17 @@ impl GeomProcessor for MvtWriter {
};

if !last_ring_coord {
let x = ((x - self.left) * self.tile_size / (self.right - self.left)) as i32;
let y = ((y - self.bottom) * self.tile_size / (self.top - self.bottom)) as i32;
let x: i32;
let mut y: i32;
if let Some(tile_size) = self.tile_size {
x = ((x_coord - self.left) * tile_size / (self.right - self.left)) as i32;
y = ((y_coord - self.bottom) * tile_size / (self.top - self.bottom)) as i32;
y = (tile_size as i32).saturating_sub(y); // reverse_y only?
} else {
// unscaled
x = x_coord as i32;
y = y_coord as i32;
}
self.feature
.geometry
.push(ParameterInteger::from(x.saturating_sub(self.last_x)));
Expand Down Expand Up @@ -396,7 +405,6 @@ mod test_mvt {
#[cfg(feature = "with-geojson")]
mod test {
use super::*;
use crate::api::GeozeroGeometry;
use crate::geojson::conversion::ToJson;
use crate::geojson::GeoJson;
use crate::ToMvt;
Expand Down Expand Up @@ -487,15 +495,14 @@ mod test {
fn geo_to_mvt() -> Result<()> {
let geo: geo_types::Geometry<f64> = geo_types::Point::new(960000.0, 6002729.0).into();
let mvt = geo.to_mvt(256, 958826.08, 5987771.04, 978393.96, 6007338.92)?;
assert_eq!(mvt.geometry, [9, 30, 390]);
assert_eq!(mvt.geometry, [9, 30, 122]);
let geojson = mvt.to_json()?;

assert_eq!(
serde_json::from_str::<serde_json::Value>(&geojson).unwrap(),
json!({
"type": "Point",
"coordinates": [15,195]
}) // ??!! reverse_y: y = tile_size.saturating_sub(y) -> [15,61]
"coordinates": [15,61]
}) // without reverse_y: [15,195]
);
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions geozero/tests/mvt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use geozero::mvt::{Message, MvtWriter, Tile};
use geozero::mvt::{Message, Tile};
use geozero::{
ColumnValue, CoordDimensions, FeatureProcessor, GeomProcessor, GeozeroDatasource,
GeozeroGeometry, PropertyProcessor, ToJson, ToMvt,
PropertyProcessor, ToJson, ToMvt,
};
use serde_json::json;
use std::env;
Expand All @@ -21,15 +21,15 @@ fn geo_to_mvt() {
let mvt = geo
.to_mvt(256, 958826.08, 5987771.04, 978393.96, 6007338.92)
.unwrap();
assert_eq!(mvt.geometry, [9, 30, 390]);
assert_eq!(mvt.geometry, [9, 30, 122]);
let geojson = mvt.to_json().unwrap();

assert_eq!(
serde_json::from_str::<serde_json::Value>(&geojson).unwrap(),
json!({
"type": "Point",
"coordinates": [15,195]
}) // ??!! reverse_y: y = tile_size.saturating_sub(y) -> [15,61]
"coordinates": [15,61]
})
);
}

Expand Down

0 comments on commit f44e832

Please sign in to comment.