pub struct Polygon { /* private fields */ }Expand description
A polygon with exterior and optional interior rings.
This wraps geo::Polygon and provides additional functionality for
GeoJSON conversion and spatial operations.
§Examples
use spatio_types::geo::Polygon;
use geo::polygon;
let poly = polygon![
(x: -80.0, y: 35.0),
(x: -70.0, y: 35.0),
(x: -70.0, y: 45.0),
(x: -80.0, y: 45.0),
(x: -80.0, y: 35.0),
];
let wrapped = Polygon::from(poly);Implementations§
Source§impl Polygon
impl Polygon
Sourcepub fn new(exterior: LineString, interiors: Vec<LineString>) -> Polygon
pub fn new(exterior: LineString, interiors: Vec<LineString>) -> Polygon
Create a new polygon from an exterior ring and optional interior rings (holes).
§Arguments
exterior- The outer boundary of the polygoninteriors- Optional holes within the polygon
Sourcepub fn from_coords(
exterior: &[(f64, f64)],
interiors: Vec<Vec<(f64, f64)>>,
) -> Polygon
pub fn from_coords( exterior: &[(f64, f64)], interiors: Vec<Vec<(f64, f64)>>, ) -> Polygon
Create a new polygon from coordinate arrays without requiring geo::LineString.
This is a convenience method that allows creating polygons from raw coordinates
without needing to import types from the geo crate.
§Arguments
exterior- Coordinates for the outer boundary [(lon, lat), …]interiors- Optional holes within the polygon, each as [(lon, lat), …]
§Examples
use spatio_types::geo::Polygon;
// Create a simple rectangle
let polygon = Polygon::from_coords(
&[
(-80.0, 35.0),
(-70.0, 35.0),
(-70.0, 45.0),
(-80.0, 45.0),
(-80.0, 35.0), // Close the ring
],
vec![],
);
// Create a polygon with a hole
let polygon_with_hole = Polygon::from_coords(
&[
(-80.0, 35.0),
(-70.0, 35.0),
(-70.0, 45.0),
(-80.0, 45.0),
(-80.0, 35.0),
],
vec![
vec![
(-75.0, 38.0),
(-74.0, 38.0),
(-74.0, 40.0),
(-75.0, 40.0),
(-75.0, 38.0),
]
],
);Sourcepub fn exterior(&self) -> &LineString
pub fn exterior(&self) -> &LineString
Get a reference to the exterior ring.
Sourcepub fn interiors(&self) -> &[LineString]
pub fn interiors(&self) -> &[LineString]
Get references to the interior rings (holes).
Sourcepub fn into_inner(self) -> Polygon
pub fn into_inner(self) -> Polygon
Convert into the inner geo::Polygon.
Sourcepub fn contains(&self, point: &Point) -> bool
pub fn contains(&self, point: &Point) -> bool
Check if a point is contained within this polygon.
§Examples
use spatio_types::geo::{Point, Polygon};
use geo::polygon;
let poly = polygon![
(x: -80.0, y: 35.0),
(x: -70.0, y: 35.0),
(x: -70.0, y: 45.0),
(x: -80.0, y: 45.0),
(x: -80.0, y: 35.0),
];
let polygon = Polygon::from(poly);
let point = Point::new(-75.0, 40.0);
assert!(polygon.contains(&point));Sourcepub fn to_geojson(&self) -> Result<String, GeoJsonError>
pub fn to_geojson(&self) -> Result<String, GeoJsonError>
Convert to GeoJSON string representation.
§Examples
use spatio_types::geo::Polygon;
use geo::polygon;
let poly = polygon![
(x: -80.0, y: 35.0),
(x: -70.0, y: 35.0),
(x: -70.0, y: 45.0),
(x: -80.0, y: 45.0),
(x: -80.0, y: 35.0),
];
let polygon = Polygon::from(poly);
let json = polygon.to_geojson().unwrap();
assert!(json.contains("Polygon"));Sourcepub fn from_geojson(geojson: &str) -> Result<Polygon, GeoJsonError>
pub fn from_geojson(geojson: &str) -> Result<Polygon, GeoJsonError>
Parse from GeoJSON string.
§Examples
use spatio_types::geo::Polygon;
let json = r#"{"type":"Polygon","coordinates":[[[-80.0,35.0],[-70.0,35.0],[-70.0,45.0],[-80.0,45.0],[-80.0,35.0]]]}"#;
let polygon = Polygon::from_geojson(json).unwrap();
assert_eq!(polygon.exterior().coords().count(), 5);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Polygon
impl<'de> Deserialize<'de> for Polygon
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Polygon, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Polygon, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Serialize for Polygon
impl Serialize for Polygon
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl StructuralPartialEq for Polygon
Auto Trait Implementations§
impl Freeze for Polygon
impl RefUnwindSafe for Polygon
impl Send for Polygon
impl Sync for Polygon
impl Unpin for Polygon
impl UnwindSafe for Polygon
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more