diff --git a/src/common.rs b/src/common.rs index 1ef2fbd..d640994 100644 --- a/src/common.rs +++ b/src/common.rs @@ -36,7 +36,6 @@ impl TryFrom 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: {}", @@ -86,7 +85,12 @@ impl WKBType { let geometry_type = match byte_order { 0 => reader.read_u32::().unwrap(), 1 => reader.read_u32::().unwrap(), - other => panic!("Unexpected byte order: {}", other), + other => { + return Err(WKBError::General(format!( + "Unexpected byte order: {}", + other + ))) + } }; Self::try_from_u32(geometry_type) } diff --git a/src/writer/polygon.rs b/src/writer/polygon.rs index 7bfd28b..3cd4ca4 100644 --- a/src/writer/polygon.rs +++ b/src/writer/polygon.rs @@ -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); @@ -46,18 +46,22 @@ fn write_polygon_content( writer.write_u32::(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::(num_rings.try_into().unwrap())?; - let ext_ring = geom.exterior().unwrap(); - writer.write_u32::(ext_ring.num_coords().try_into().unwrap())?; + if let Some(ext_ring) = geom.exterior() { + writer.write_u32::(ext_ring.num_coords().try_into().unwrap())?; - for coord in ext_ring.coords() { - writer.write_f64::(coord.x())?; - writer.write_f64::(coord.y())?; - if geom.dim().size() == 3 { - writer.write_f64::(coord.nth_unchecked(2))?; + for coord in ext_ring.coords() { + writer.write_f64::(coord.x())?; + writer.write_f64::(coord.y())?; + if geom.dim().size() == 3 { + writer.write_f64::(coord.nth_unchecked(2))?; + } } }