Cleanup and fix parsing of linestrings

This commit is contained in:
Maximilian Ammann 2021-12-02 16:20:26 +01:00
parent 4bca568f6e
commit 1841d9933d
5 changed files with 51 additions and 78 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "vector-tile"
version = "0.1.0"
description = "A library decoding vector tiles"
description = "A library for decoding vector tiles"
readme = "README.md"
categories = ["encoding"]
edition = "2021"
@ -9,8 +9,6 @@ build = "build.rs"
[dependencies]
log = "0.4"
num-traits = "0.2"
pointy = "0.3"
protobuf = "2.25"
tile-grid = "0.3"

View File

@ -72,20 +72,18 @@ impl Decode<GeometryPoint> for Vec<u32> {
let command = self[i] & 0x7;
if command != CMD_MOVE_TO {
// FIXME: ERROR
// FIXME: Not allowed in Points
panic!("error")
}
let count = (self[i] >> 3) as usize;
i += 1;
for parameter in 0..count {
points.push(Point::new(
self[i + parameter].zagzig(),
self[i + parameter + 1].zagzig(),
));
}
for _ in 0..count {
points.push(Point::new(self[i].zagzig(), self[i + 1].zagzig()));
i += count * CMD_MOVE_TO_PARAMETERS;
i += CMD_MOVE_TO_PARAMETERS;
}
}
if points.len() == 1 {
@ -109,34 +107,35 @@ impl Decode<GeometryLineString> for Vec<u32> {
let count = (self[i] >> 3) as usize;
i += 1;
for parameter in 0..count {
match command {
CMD_MOVE_TO => {
match command {
CMD_MOVE_TO => {
for _ in 0..count {
let x_index = i;
commands.push(Command::MoveTo(MoveTo {
x: self[i + parameter].zagzig(),
y: self[i + parameter + 1].zagzig(),
x: self[x_index].zagzig(),
y: self[x_index + 1].zagzig(),
}));
i += CMD_MOVE_TO_PARAMETERS;
}
CMD_LINE_TO => {
}
CMD_LINE_TO => {
for _ in 0..count {
let x_index = i;
commands.push(Command::LineTo(LineTo {
x: self[i + parameter].zagzig(),
y: self[i + parameter + 1].zagzig(),
x: self[x_index].zagzig(),
y: self[x_index + 1].zagzig(),
}));
i += CMD_MOVE_TO_PARAMETERS;
}
CMD_CLOSE_PATH => {
commands.push(Command::Close);
}
_ => {}
}
CMD_CLOSE_PATH => {
// FIXME: Not allowed in LineStrings
panic!("error")
}
_ => {
panic!("error")
}
}
i += count
* match command {
CMD_MOVE_TO => CMD_MOVE_TO_PARAMETERS,
CMD_LINE_TO => CMD_LINE_TO_PARAMETERS,
CMD_CLOSE_PATH => CMD_CLOSE_PATH_PARAMETERS,
_ => 0,
};
}
GeometryLineString { commands }
@ -157,7 +156,7 @@ impl Decode<GeometryPolygon> for Vec<u32> {
match command {
CMD_MOVE_TO => {
for parameter in 0..count {
for _ in 0..count {
let x_index = i;
commands.push(Command::MoveTo(MoveTo {
x: self[x_index].zagzig(),
@ -167,7 +166,7 @@ impl Decode<GeometryPolygon> for Vec<u32> {
}
}
CMD_LINE_TO => {
for parameter in 0..count {
for _ in 0..count {
let x_index = i;
commands.push(Command::LineTo(LineTo {
x: self[x_index].zagzig(),
@ -186,8 +185,6 @@ impl Decode<GeometryPolygon> for Vec<u32> {
panic!("error")
}
}
}
GeometryPolygon { commands }

View File

@ -1,5 +1,3 @@
use num_traits::Num;
type Number = i32;
#[derive(Debug)]

View File

@ -1,6 +1,6 @@
use tile_grid::{extent_wgs84_to_merc, Extent, Grid, GridIterator, Origin, Unit};
fn web_mercator() -> Grid {
pub fn google_mercator() -> Grid {
Grid::new(
256,
256,
@ -12,7 +12,6 @@ fn web_mercator() -> Grid {
},
3857,
Unit::Meters,
// for calculation see fn test_resolutions
vec![
156543.0339280410,
78271.5169640205,
@ -42,45 +41,27 @@ fn web_mercator() -> Grid {
)
}
/// z, x, z
pub fn get_tile_coordinates_bavaria() -> Vec<(u8, u32, u32)> {
let grid = web_mercator();
///
/// Returns coordinates for tiles within bavaria according to the specified grid.
/// The grid is responsible for defining the coordinate system. For example whether
/// [Slippy map tilenames](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) (also known as
/// XYZ) or [TMS](https://wiki.osgeo.org/wiki/Tile_Map_Service_Specification#TileMap_Diagram) is
/// used.
///
/// ## Additional Resources:
///
/// * https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection
/// * https://gist.github.com/maptiler/fddb5ce33ba995d5523de9afdf8ef118
pub fn tile_coordinates_bavaria(grid: &Grid, zoom: u8) -> Vec<(u8, u32, u32)> {
let tile_limits = grid.tile_limits(
extent_wgs84_to_merc(&Extent {
minx: 10.0,
miny: 48.0,
maxx: 12.0,
maxy: 50.0,
minx: 8.9771580802,
miny: 47.2703623267,
maxx: 13.8350427083,
maxy: 50.5644529365,
}),
0,
);
println!("{:?}", grid.tile_extent(0, 0, 0));
println!("{:?}", grid.tile_extent(33, 21, 6));
println!("{:?}", grid.tile_extent_xyz(0, 0, 0));
let z = 6;
let griditer = GridIterator::new(z, z, tile_limits);
griditer.collect()
}
pub fn get_tile_coordinates_tutzing() -> Vec<(u8, u32, u32)> {
let grid = web_mercator();
let tile_limits = grid.tile_limits(
extent_wgs84_to_merc(&Extent {
minx: 11.2772666,
miny: 47.9125117,
maxx: 11.2772666,
maxy: 47.9125117,
}),
1,
);
println!("{:?}", grid.tile_extent(0, 0, 0));
println!("{:?}", grid.tile_extent(33, 21, 6));
println!("{:?}", grid.tile_extent_xyz(0, 0, 0));
let z = 12;
let griditer = GridIterator::new(z, z, tile_limits);
griditer.collect()
GridIterator::new(zoom, zoom, tile_limits).collect()
}

View File

@ -4,17 +4,16 @@ use std::io::BufReader;
use protobuf::Message;
use crate::encoding::Decode;
use crate::grid::get_tile_coordinates_bavaria;
use crate::grid::{GOOGLE_MERCATOR, tile_coordinates_bavaria};
use crate::parse_tile;
use crate::protos::vector_tile::Tile;
#[test]
fn test_parsing_europe_pbf() {
let tile = parse_tile("libs/vector_tile/test_data/europe.pbf");
//println!("{:?}", tile);
parse_tile("libs/vector_tile/test_data/europe.pbf");
}
#[test]
fn test_tile_coordinates_bavaria() {
println!("{:?}", get_tile_coordinates_bavaria());
println!("{:?}", tile_coordinates_bavaria(&GOOGLE_MERCATOR, 6));
}