Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix size calculation
Browse files Browse the repository at this point in the history
kylebarron committed Dec 3, 2024
1 parent 5e33589 commit 323c615
Showing 6 changed files with 57 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/reader/geometry_collection.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ pub struct GeometryCollection<'a> {
/// A WKB object for each of the internal geometries
geometries: Vec<Wkb<'a>>,
dim: WKBDimension,
has_srid: bool,
}

impl<'a> GeometryCollection<'a> {
@@ -45,7 +46,11 @@ impl<'a> GeometryCollection<'a> {
geometries.push(geometry);
}

Ok(Self { geometries, dim })
Ok(Self {
geometries,
dim,
has_srid,
})
}

pub fn dimension(&self) -> WKBDimension {
@@ -56,9 +61,11 @@ impl<'a> GeometryCollection<'a> {
// - 1: byteOrder
// - 4: wkbType
// - 4: numGeometries
self.geometries
.iter()
.fold(1 + 4 + 4, |acc, x| acc + x.size())
let mut header = 1 + 4 + 4;
if self.has_srid {
header += 4;
}
self.geometries.iter().fold(header, |acc, x| acc + x.size())
}
}

8 changes: 7 additions & 1 deletion src/reader/linestring.rs
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ pub struct LineString<'a> {
/// LineString contained within a MultiLineString
offset: u64,
dim: WKBDimension,
has_srid: bool,
}

impl<'a> LineString<'a> {
@@ -43,6 +44,7 @@ impl<'a> LineString<'a> {
num_points,
offset,
dim,
has_srid,
}
}

@@ -54,7 +56,11 @@ impl<'a> LineString<'a> {
// - 4: wkbType
// - 4: numPoints
// - 2 * 8 * self.num_points: two f64s for each coordinate
1 + 4 + 4 + (self.dim.size() as u64 * 8 * self.num_points as u64)
let mut header = 1 + 4 + 4;
if self.has_srid {
header += 4;
}
header + (self.dim.size() as u64 * 8 * self.num_points as u64)
}

/// The offset into this buffer of any given coordinate
8 changes: 7 additions & 1 deletion src/reader/multilinestring.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ pub struct MultiLineString<'a> {
/// A LineString object for each of the internal line strings
wkb_line_strings: Vec<LineString<'a>>,
dim: WKBDimension,
has_srid: bool,
}

impl<'a> MultiLineString<'a> {
@@ -49,6 +50,7 @@ impl<'a> MultiLineString<'a> {
Self {
wkb_line_strings,
dim,
has_srid,
}
}

@@ -60,9 +62,13 @@ impl<'a> MultiLineString<'a> {
// - 4: wkbType
// - 4: numPoints
// - Point::size() * self.num_points: the size of each Point for each point
let mut header = 1 + 4 + 4;
if self.has_srid {
header += 4;
}
self.wkb_line_strings
.iter()
.fold(1 + 4 + 4, |acc, ls| acc + ls.size())
.fold(header, |acc, ls| acc + ls.size())
}

pub fn dimension(&self) -> WKBDimension {
13 changes: 11 additions & 2 deletions src/reader/multipolygon.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ pub struct MultiPolygon<'a> {
wkb_polygons: Vec<Polygon<'a>>,

dim: WKBDimension,
has_srid: bool,
}

impl<'a> MultiPolygon<'a> {
@@ -46,7 +47,11 @@ impl<'a> MultiPolygon<'a> {
wkb_polygons.push(polygon);
}

Self { wkb_polygons, dim }
Self {
wkb_polygons,
dim,
has_srid,
}
}

/// The number of bytes in this object, including any header
@@ -56,9 +61,13 @@ impl<'a> MultiPolygon<'a> {
// - 1: byteOrder
// - 4: wkbType
// - 4: numPolygons
let mut header = 1 + 4 + 4;
if self.has_srid {
header += 4;
}
self.wkb_polygons
.iter()
.fold(1 + 4 + 4, |acc, x| acc + x.size())
.fold(header, |acc, x| acc + x.size())
}

pub fn dimension(&self) -> WKBDimension {
16 changes: 13 additions & 3 deletions src/reader/point.rs
Original file line number Diff line number Diff line change
@@ -16,10 +16,16 @@ pub struct Point<'a> {
coord: Coord<'a>,
dim: WKBDimension,
is_empty: bool,
has_srid: bool,
}

impl<'a> Point<'a> {
pub fn new(buf: &'a [u8], byte_order: Endianness, offset: u64, dim: WKBDimension) -> Self {
pub(crate) fn new(
buf: &'a [u8],
byte_order: Endianness,
offset: u64,
dim: WKBDimension,
) -> Self {
let has_srid = has_srid(buf, byte_order, offset);

// The space of the byte order + geometry type
@@ -43,6 +49,7 @@ impl<'a> Point<'a> {
coord,
dim,
is_empty,
has_srid,
}
}

@@ -52,9 +59,12 @@ impl<'a> Point<'a> {
pub fn size(&self) -> u64 {
// - 1: byteOrder
// - 4: wkbType
// - 4: numPoints
// - dim size * 8: two f64s
1 + 4 + (self.dim.size() as u64 * 8)
let mut header = 1 + 4;
if self.has_srid {
header += 4;
}
header + (self.dim.size() as u64 * 8)
}

pub fn dimension(&self) -> WKBDimension {
9 changes: 8 additions & 1 deletion src/reader/polygon.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ const HEADER_BYTES: u64 = 5;
pub struct Polygon<'a> {
wkb_linear_rings: Vec<WKBLinearRing<'a>>,
dim: WKBDimension,
has_srid: bool,
}

impl<'a> Polygon<'a> {
@@ -46,6 +47,7 @@ impl<'a> Polygon<'a> {
Self {
wkb_linear_rings,
dim,
has_srid,
}
}

@@ -57,9 +59,14 @@ impl<'a> Polygon<'a> {
// - 4: wkbType
// - 4: numPoints
// - size of each linear ring
let mut header = 1 + 4 + 4;
if self.has_srid {
header += 4;
}

self.wkb_linear_rings
.iter()
.fold(1 + 4 + 4, |acc, ring| acc + ring.size())
.fold(header, |acc, ring| acc + ring.size())
}

pub fn dimension(&self) -> WKBDimension {

0 comments on commit 323c615

Please sign in to comment.