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

Avoid panic when writing empty polygon #29

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl TryFrom<geo_traits::Dimensions> for WKBDimension {
Xyz | Unknown(3) => Self::Xyz,
Xym => Self::Xym,
Xyzm | Unknown(4) => Self::Xyzm,
// TODO: switch to tryfrom
Unknown(n_dim) => {
return Err(WKBError::General(format!(
"Unsupported number of dimensions: {}",
Expand Down Expand Up @@ -86,7 +85,12 @@ impl WKBType {
let geometry_type = match byte_order {
0 => reader.read_u32::<BigEndian>().unwrap(),
1 => reader.read_u32::<LittleEndian>().unwrap(),
other => panic!("Unexpected byte order: {}", other),
other => {
return Err(WKBError::General(format!(
"Unexpected byte order: {}",
other
)))
}
};
Self::try_from_u32(geometry_type)
}
Expand Down
28 changes: 16 additions & 12 deletions src/writer/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub fn polygon_wkb_size(geom: &impl PolygonTrait) -> usize {

let each_coord = geom.dim().size() * 8;

// TODO: support empty polygons where this will panic
let ext_ring = geom.exterior().unwrap();
sum += 4 + (ext_ring.num_coords() * each_coord);
if let Some(ext_ring) = geom.exterior() {
sum += 4 + (ext_ring.num_coords() * each_coord);
}

for int_ring in geom.interiors() {
sum += 4 + (int_ring.num_coords() * each_coord);
Expand Down Expand Up @@ -46,18 +46,22 @@ fn write_polygon_content<W: Write, B: ByteOrder>(
writer.write_u32::<LittleEndian>(wkb_type.into())?;

// numRings
// TODO: support empty polygons where this will panic
let num_rings = 1 + geom.num_interiors();
let num_rings = if geom.exterior().is_some() {
1 + geom.num_interiors()
} else {
0
};
writer.write_u32::<B>(num_rings.try_into().unwrap())?;

let ext_ring = geom.exterior().unwrap();
writer.write_u32::<B>(ext_ring.num_coords().try_into().unwrap())?;
if let Some(ext_ring) = geom.exterior() {
writer.write_u32::<B>(ext_ring.num_coords().try_into().unwrap())?;

for coord in ext_ring.coords() {
writer.write_f64::<B>(coord.x())?;
writer.write_f64::<B>(coord.y())?;
if geom.dim().size() == 3 {
writer.write_f64::<B>(coord.nth_unchecked(2))?;
for coord in ext_ring.coords() {
writer.write_f64::<B>(coord.x())?;
writer.write_f64::<B>(coord.y())?;
if geom.dim().size() == 3 {
writer.write_f64::<B>(coord.nth_unchecked(2))?;
}
}
}

Expand Down
Loading