use super::{FORMAT_VERSION, tag};
use crate::topology::de9im::IntersectionMatrix;
use crate::topology::label::{Dimension, Location};
use crate::types::{
Coord2d, CoordSeq, Envelope, Geometry, GeometryCollection, LineString, LinearRing,
MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
};
#[cfg(feature = "3d")]
use crate::types3d::{
Coord3d, CoordSeq3d, LineString3d, LinearRing3d, MultiLineString3d, MultiPoint3d,
MultiPolygon3d, Point3d, Polygon3d,
};
pub trait Encode {
const TAG: u8;
fn encode_body(&self, out: &mut Vec<u8>);
}
#[must_use]
pub fn encode<T: Encode>(value: &T) -> Vec<u8> {
let mut out = Vec::new();
encode_into(value, &mut out);
out
}
pub fn encode_into<T: Encode>(value: &T, out: &mut Vec<u8>) {
out.push(FORMAT_VERSION);
out.push(T::TAG);
value.encode_body(out);
}
pub fn put_count(n: usize, out: &mut Vec<u8>) {
let n = u32::try_from(n).unwrap_or(u32::MAX);
out.extend_from_slice(&n.to_le_bytes());
}
pub fn put_f64(v: f64, out: &mut Vec<u8>) {
out.extend_from_slice(&v.to_le_bytes());
}
impl Encode for Coord2d {
const TAG: u8 = tag::COORD2D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_f64(self.x, out);
put_f64(self.y, out);
}
}
impl Encode for CoordSeq {
const TAG: u8 = tag::COORD_SEQ;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.len(), out);
for c in self.as_slice() {
c.encode_body(out);
}
}
}
impl Encode for Point {
const TAG: u8 = tag::POINT;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coord().encode_body(out);
}
}
impl Encode for LineString {
const TAG: u8 = tag::LINE_STRING;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coords().encode_body(out);
}
}
impl Encode for LinearRing {
const TAG: u8 = tag::LINEAR_RING;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coords().encode_body(out);
}
}
impl Encode for Polygon {
const TAG: u8 = tag::POLYGON;
fn encode_body(&self, out: &mut Vec<u8>) {
self.exterior().encode_body(out);
put_count(self.holes().len(), out);
for h in self.holes() {
h.encode_body(out);
}
}
}
impl Encode for MultiPoint {
const TAG: u8 = tag::MULTI_POINT;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.points().len(), out);
for p in self.points() {
p.encode_body(out);
}
}
}
impl Encode for MultiLineString {
const TAG: u8 = tag::MULTI_LINE_STRING;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.line_strings().len(), out);
for l in self.line_strings() {
l.encode_body(out);
}
}
}
impl Encode for MultiPolygon {
const TAG: u8 = tag::MULTI_POLYGON;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.polygons().len(), out);
for p in self.polygons() {
p.encode_body(out);
}
}
}
impl Encode for Geometry {
const TAG: u8 = tag::GEOMETRY;
fn encode_body(&self, out: &mut Vec<u8>) {
match self {
Geometry::Point(p) => {
out.push(Point::TAG);
p.encode_body(out);
}
Geometry::LineString(l) => {
out.push(LineString::TAG);
l.encode_body(out);
}
Geometry::Polygon(p) => {
out.push(Polygon::TAG);
p.encode_body(out);
}
Geometry::MultiPoint(m) => {
out.push(MultiPoint::TAG);
m.encode_body(out);
}
Geometry::MultiLineString(m) => {
out.push(MultiLineString::TAG);
m.encode_body(out);
}
Geometry::MultiPolygon(m) => {
out.push(MultiPolygon::TAG);
m.encode_body(out);
}
Geometry::GeometryCollection(c) => {
out.push(GeometryCollection::TAG);
c.encode_body(out);
}
}
}
}
impl Encode for GeometryCollection {
const TAG: u8 = tag::GEOMETRY_COLLECTION;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.len(), out);
let mut stack = vec![self.geometries().iter()];
while let Some(top) = stack.last_mut() {
match top.next() {
None => {
stack.pop();
}
Some(Geometry::GeometryCollection(c)) => {
out.push(Self::TAG);
put_count(c.len(), out);
stack.push(c.geometries().iter());
}
Some(g) => g.encode_body(out),
}
}
}
}
impl Encode for Envelope {
const TAG: u8 = tag::ENVELOPE;
fn encode_body(&self, out: &mut Vec<u8>) {
let (min, max) = (self.min(), self.max());
put_f64(min.x, out);
put_f64(min.y, out);
put_f64(max.x, out);
put_f64(max.y, out);
}
}
impl Encode for Location {
const TAG: u8 = tag::LOCATION;
fn encode_body(&self, out: &mut Vec<u8>) {
out.push(match self {
Location::Interior => 0,
Location::Boundary => 1,
Location::Exterior => 2,
});
}
}
impl Encode for Dimension {
const TAG: u8 = tag::DIMENSION;
fn encode_body(&self, out: &mut Vec<u8>) {
let v: i8 = match self {
Dimension::Empty => -1,
Dimension::Point => 0,
Dimension::Line => 1,
Dimension::Area => 2,
};
out.push(v.cast_unsigned());
}
}
impl Encode for IntersectionMatrix {
const TAG: u8 = tag::INTERSECTION_MATRIX;
fn encode_body(&self, out: &mut Vec<u8>) {
for d in [
self.interior_interior,
self.interior_boundary,
self.interior_exterior,
self.boundary_interior,
self.boundary_boundary,
self.boundary_exterior,
self.exterior_interior,
self.exterior_boundary,
self.exterior_exterior,
] {
d.encode_body(out);
}
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for Coord3d {
const TAG: u8 = tag::COORD3D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_f64(self.x, out);
put_f64(self.y, out);
put_f64(self.z, out);
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for CoordSeq3d {
const TAG: u8 = tag::COORD_SEQ3D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.len(), out);
for c in self.as_slice() {
c.encode_body(out);
}
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for Point3d {
const TAG: u8 = tag::POINT3D;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coord().encode_body(out);
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for LineString3d {
const TAG: u8 = tag::LINE_STRING3D;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coords().encode_body(out);
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for LinearRing3d {
const TAG: u8 = tag::LINEAR_RING3D;
fn encode_body(&self, out: &mut Vec<u8>) {
self.coords().encode_body(out);
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for Polygon3d {
const TAG: u8 = tag::POLYGON3D;
fn encode_body(&self, out: &mut Vec<u8>) {
self.exterior().encode_body(out);
put_count(self.holes().len(), out);
for h in self.holes() {
h.encode_body(out);
}
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for MultiPoint3d {
const TAG: u8 = tag::MULTI_POINT3D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.points().len(), out);
for p in self.points() {
p.encode_body(out);
}
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for MultiLineString3d {
const TAG: u8 = tag::MULTI_LINE_STRING3D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.line_strings().len(), out);
for l in self.line_strings() {
l.encode_body(out);
}
}
}
#[cfg(feature = "3d")]
#[cfg_attr(docsrs, doc(cfg(feature = "3d")))]
impl Encode for MultiPolygon3d {
const TAG: u8 = tag::MULTI_POLYGON3D;
fn encode_body(&self, out: &mut Vec<u8>) {
put_count(self.polygons().len(), out);
for p in self.polygons() {
p.encode_body(out);
}
}
}