pub struct Geography { /* private fields */ }Expand description
A geography (spatial) value.
This type wraps raw geography bytes and provides type safety when working
with spatial data. With the geography feature enabled, it also supports
WKT/WKB parsing and conversion to/from geo_types::Geometry.
§Binary Format Notes
Important: This type can hold data in two different binary formats:
-
Hyper’s Legacy Format: Data read from Hyper query results is stored in Hyper’s proprietary legacy serialization format. This format is not WKB-compatible.
-
WKB Format: When created via
from_wkt()orfrom_wkb()(with thegeographyfeature), data is stored in WKB (Well-Known Binary) format.
Methods like to_geometry() and to_wkt() (available with the geography
feature) expect WKB format and will fail when called on data in Hyper’s
legacy format.
§Basic Usage (Always Available)
use hyperdb_api_core::types::Geography;
// Create from raw bytes (e.g., from query results)
let geo = Geography::from_bytes(vec![0x01, 0x02, 0x03]);
// Get raw bytes for insertion
let bytes: &[u8] = geo.as_bytes();§WKT/WKB Parsing (Requires geography Feature)
use hyperdb_api_core::types::Geography;
// Create from WKT string (requires "geography" feature)
let geo = Geography::from_wkt("POINT(-122.4194 37.7749)")?;
// Convert to geo-types for processing
let geometry = geo.to_geometry()?;
// Export as WKT
let wkt_string = geo.to_wkt()?;Implementations§
Source§impl Geography
impl Geography
Sourcepub fn from_bytes(data: impl Into<Vec<u8>>) -> Geography
pub fn from_bytes(data: impl Into<Vec<u8>>) -> Geography
Creates a Geography from raw binary data in Hyper’s legacy format.
Use this when reading geography data from Hyper query results. The data will be in Hyper’s internal legacy serialization format.
§Format Compatibility
Data read from Hyper is in Hyper’s proprietary legacy format, not WKB.
Methods like to_geometry() and to_wkt() (available with the geography
feature) expect WKB format and will fail when called on data created
via this method. Use format() to check the format before calling these methods.
Sourcepub fn from_wkb_bytes(data: impl Into<Vec<u8>>) -> Geography
pub fn from_wkb_bytes(data: impl Into<Vec<u8>>) -> Geography
Creates a Geography from WKB (Well-Known Binary) data.
Use this when you have WKB data from an external source.
Methods like to_geometry() and to_wkt() will work on this data.
Sourcepub fn binary_format(&self) -> GeographyBinaryFormat
pub fn binary_format(&self) -> GeographyBinaryFormat
Returns the binary format of this geography value.
Use this to check whether WKB-dependent methods like to_geometry()
and to_wkt() will work on this value.
Note: When the geography feature is enabled, there’s also a format()
method that performs runtime detection and returns the actual data.
Sourcepub fn is_wkb(&self) -> bool
pub fn is_wkb(&self) -> bool
Returns true if this geography is in WKB format.
WKB-dependent methods like to_geometry() and to_wkt() will only
work if this returns true.
Sourcepub fn is_hyper_legacy(&self) -> bool
pub fn is_hyper_legacy(&self) -> bool
Returns true if this geography is in Hyper’s legacy format.
Data in this format cannot be converted to geometry or WKT.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes self and returns the underlying bytes.
Source§impl Geography
impl Geography
Sourcepub const DEFAULT_RING_CLOSURE_EPSILON: f64 = 1e-6
pub const DEFAULT_RING_CLOSURE_EPSILON: f64 = 1e-6
Validates geometry coordinate ranges. Default tolerance for ring closure validation. This is more lenient than typical machine epsilon to accommodate geographic data that may have slightly different precision.
Sourcepub fn from_wkt(
wkt_str: &str,
) -> Result<Geography, Box<dyn Error + Sync + Send>>
pub fn from_wkt( wkt_str: &str, ) -> Result<Geography, Box<dyn Error + Sync + Send>>
Creates a Geography from a WKT (Well-Known Text) string.
This parses the WKT string and converts it to WKB (Well-Known Binary) format for storage. Coordinates are validated to be within valid geographic ranges (longitude: [-180, 180], latitude: [-90, 90]).
§Supported WKT Types
POINT(x y)- A single pointLINESTRING(x1 y1, x2 y2, ...)- A linePOLYGON((x1 y1, x2 y2, ..., x1 y1))- A polygonMULTIPOINT((x1 y1), (x2 y2), ...)- Multiple pointsMULTILINESTRING((...), (...))- Multiple linesMULTIPOLYGON(((...)), ((...)))- Multiple polygonsGEOMETRYCOLLECTION(...)- Collection of geometries
§Example
use hyperdb_api_core::types::Geography;
// Requires "geography" feature
let point = Geography::from_wkt("POINT(-122.4194 37.7749)")?;
let line = Geography::from_wkt("LINESTRING(0 0, 1 1, 2 2)")?;
let polygon = Geography::from_wkt("POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))")?;§Errors
Returns an error if the WKT string is invalid or contains out-of-range coordinates.
Sourcepub fn from_wkb(
wkb_bytes: &[u8],
) -> Result<Geography, Box<dyn Error + Sync + Send>>
pub fn from_wkb( wkb_bytes: &[u8], ) -> Result<Geography, Box<dyn Error + Sync + Send>>
Creates a Geography from WKB (Well-Known Binary) data.
This accepts standard WKB format and validates it eagerly.
§Errors
Returns an error if the WKB data is invalid.
Sourcepub fn validate_wkb_format(&self) -> Result<(), Box<dyn Error + Sync + Send>>
pub fn validate_wkb_format(&self) -> Result<(), Box<dyn Error + Sync + Send>>
Validates that the stored data is valid WKB.
Calling this on geography values sourced from Hyper query results (which are in Hyper’s legacy format, not WKB) will return an error.
§Errors
- Returns an error if the payload is fewer than 9 bytes (the minimum WKB envelope size).
- Returns an error if the leading endianness marker is not
0x00(big-endian) or0x01(little-endian). - Returns an error if the bytes cannot be parsed as a valid
WKB geometry via
geozero.
Sourcepub fn format(&self) -> Result<GeographyFormat, Box<dyn Error + Sync + Send>>
pub fn format(&self) -> Result<GeographyFormat, Box<dyn Error + Sync + Send>>
Detects the underlying binary format (legacy vs WKB).
This helps avoid calling WKB-only methods on legacy data by making the format explicit. Returns an error for empty payloads.
§Errors
Returns an error if the geography data is empty. A non-empty payload
that fails WKB validation is reported as GeographyFormat::Legacy
rather than an error.
Sourcepub fn from_geometry(
geometry: &Geometry,
) -> Result<Geography, Box<dyn Error + Sync + Send>>
pub fn from_geometry( geometry: &Geometry, ) -> Result<Geography, Box<dyn Error + Sync + Send>>
Creates a Geography from a geo_types::Geometry.
Coordinates are validated to be within valid geographic ranges.
§Example
use hyperdb_api_core::types::Geography;
use geo_types::Point;
// Requires "geography" feature
let point = Point::new(-122.4194, 37.7749);
let geo = Geography::from_geometry(&point.into())?;§Errors
- Returns an error if any coordinate’s longitude falls outside
[-180, 180]or latitude outside[-90, 90]. - Returns an error if a polygon ring is not closed within
Self::DEFAULT_RING_CLOSURE_EPSILON. - Returns an error if
geozerofails to serialize the geometry to WKB.
Sourcepub fn to_geometry(&self) -> Result<Geometry, Box<dyn Error + Sync + Send>>
pub fn to_geometry(&self) -> Result<Geometry, Box<dyn Error + Sync + Send>>
Converts this geography to a geo_types::Geometry.
§Format Requirements
This method requires WKB format. It will fail if called on data
created via from_bytes() with data read from Hyper query results.
Use is_wkb() or binary_format() to check the format before calling.
§Errors
- Returns an error if this value is in Hyper’s legacy format
(
GeographyBinaryFormat::HyperLegacy). Construct viaSelf::from_wktorSelf::from_wkbto use this method. - Returns an error if the stored bytes cannot be parsed as WKB
by
geozero.
Sourcepub fn to_wkt(&self) -> Result<String, Box<dyn Error + Sync + Send>>
pub fn to_wkt(&self) -> Result<String, Box<dyn Error + Sync + Send>>
Exports this geography as a WKT (Well-Known Text) string.
§Format Requirements
This method requires WKB format. It will fail if called on data
created via from_bytes() with data read from Hyper query results.
Use is_wkb() or binary_format() to check the format before calling.
§Errors
- Returns an error if this value is in Hyper’s legacy format
(
GeographyBinaryFormat::HyperLegacy). Construct viaSelf::from_wktorSelf::from_wkbto use this method. - Returns an error if the stored bytes cannot be serialized to
WKT by
geozero(malformed WKB).
Sourcepub fn validate_geometry_bounds_with_tolerance(
geometry: &Geometry,
ring_closure_epsilon: f64,
) -> Result<(), Box<dyn Error + Sync + Send>>
pub fn validate_geometry_bounds_with_tolerance( geometry: &Geometry, ring_closure_epsilon: f64, ) -> Result<(), Box<dyn Error + Sync + Send>>
Validates geometry bounds with a configurable ring closure tolerance.
Use this method when you need a different tolerance for ring closure validation (e.g., for data with lower precision).
§Arguments
geometry- The geometry to validatering_closure_epsilon- The tolerance for ring closure validation. Use a larger value for data with lower precision.
§Errors
- Returns an error if any coordinate longitude is outside
[-180, 180]or latitude outside[-90, 90]. - Returns an error if a polygon ring’s first and last vertices
differ by more than
ring_closure_epsilonon either axis.
Trait Implementations§
Source§impl FromHyperBinary for Geography
impl FromHyperBinary for Geography
Source§impl ToHyperBinary for Geography
impl ToHyperBinary for Geography
Source§fn to_hyper_binary(
&self,
buf: &mut BytesMut,
) -> Result<(), Box<dyn Error + Sync + Send>>
fn to_hyper_binary( &self, buf: &mut BytesMut, ) -> Result<(), Box<dyn Error + Sync + Send>>
Source§fn to_hyper_binary_not_null(
&self,
buf: &mut BytesMut,
) -> Result<(), Box<dyn Error + Sync + Send>>
fn to_hyper_binary_not_null( &self, buf: &mut BytesMut, ) -> Result<(), Box<dyn Error + Sync + Send>>
Source§fn hyper_binary_size(&self) -> usize
fn hyper_binary_size(&self) -> usize
Source§fn hyper_binary_size_not_null(&self) -> usize
fn hyper_binary_size_not_null(&self) -> usize
Source§impl TryFrom<LineString> for Geography
impl TryFrom<LineString> for Geography
Source§impl TryFrom<MultiLineString> for Geography
impl TryFrom<MultiLineString> for Geography
Source§impl TryFrom<MultiPoint> for Geography
impl TryFrom<MultiPoint> for Geography
Source§impl TryFrom<MultiPolygon> for Geography
impl TryFrom<MultiPolygon> for Geography
impl Eq for Geography
impl StructuralPartialEq for Geography
Auto Trait Implementations§
impl Freeze for Geography
impl RefUnwindSafe for Geography
impl Send for Geography
impl Sync for Geography
impl Unpin for Geography
impl UnsafeUnpin for Geography
impl UnwindSafe for Geography
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<Ctx, T> MeasureWith<Ctx> for T
impl<Ctx, T> MeasureWith<Ctx> for T
Source§fn measure_with(&self, _ctx: &Ctx) -> usize
fn measure_with(&self, _ctx: &Ctx) -> usize
Self, given the ctx?