pub struct Geometry { /* private fields */ }Expand description
Representation of a GEOS geometry.
§Example
use geos::{Geom, Geometry};
let point_geom = Geometry::new_from_wkt("POINT (2.5 3.5)")
.expect("Invalid geometry");
assert_eq!(point_geom.get_x(), Ok(2.5));
assert_eq!(point_geom.get_y(), Ok(3.5));Implementations§
Source§impl Geometry
impl Geometry
Sourcepub fn new_from_wkt(wkt: &str) -> GResult<Geometry>
pub fn new_from_wkt(wkt: &str) -> GResult<Geometry>
Creates a Geometry from the WKT format.
§Example
use geos::Geometry;
let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");Examples found in repository?
4fn fun() -> Result<(), Error> {
5 println!(
6 "geos_c version: {}",
7 version().expect("failed to get version")
8 );
9 let g1 = Geometry::new_from_wkt("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))")?;
10 println!("Geometry 1 created");
11 println!("Area : {}", g1.area()?);
12 println!("Is Geom1 simple : {:?}", g1.is_simple()?);
13 let g2 = Geometry::new_from_wkt("POLYGON ((1 1, 1 3, 5 5, 5 0, 1 1))")?;
14 println!("Geometry 2 created");
15 println!("Geom1 intersects geom2 : {:?}\n", g1.intersects(&g2)?);
16 let g3 = g1.buffer(100.0, 8)?;
17 println!("Previous area = {} \nNew area = {}", g2.area()?, g3.area()?);
18 let result = g1.within(&g2)?;
19 println!("Geom1 within geom2 : {:?}\n", result);
20 println!("Geom1 to wkt : {:?}", g1.to_wkt());
21 let g5 = Geometry::new_from_wkt("LINESTRING(0.0 0.0, 7.0 7.0, 45.0 50.5, 100.0 100.0)")?;
22 println!("Geom5 (linestring) : {:?}", g5.geometry_type());
23 let g6 = g5.buffer(20.0, 10)?;
24 println!("Geom6 (polygon) : {:?}", g6.geometry_type());
25 let g4 = g1.get_centroid()?;
26 println!("Centroid of g1 : {:?}", g4.to_wkt());
27 println!(
28 "Centroid of g1 with round precision of 1: {:?}",
29 g4.to_wkt_precision(1)
30 );
31 println!("Geom4 contains centroid of geom1 : {:?}", g3.contains(&g4)?);
32 println!("Geom4 is valid ? : {}", g3.is_valid());
33 Ok(())
34}Sourcepub fn new_from_hex(hex: &[u8]) -> GResult<Geometry>
pub fn new_from_hex(hex: &[u8]) -> GResult<Geometry>
Create a new Geometry from the HEX format.
§Example
use geos::{Geom, Geometry};
let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
let hex_buf = point_geom.to_hex().expect("conversion to HEX failed");
// The interesting part is here:
let new_geom = Geometry::new_from_hex(hex_buf.as_ref())
.expect("conversion from HEX failed");
assert_eq!(point_geom.equals(&new_geom), Ok(true));Sourcepub fn new_from_wkb(wkb: &[u8]) -> GResult<Geometry>
pub fn new_from_wkb(wkb: &[u8]) -> GResult<Geometry>
Create a new Geometry from the WKB format.
§Example
use geos::{Geom, Geometry};
let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
let wkb_buf = point_geom.to_wkb().expect("conversion to WKB failed");
// The interesting part is here:
let new_geom = Geometry::new_from_wkb(wkb_buf.as_ref())
.expect("conversion from WKB failed");
assert_eq!(point_geom.equals(&new_geom), Ok(true));Sourcepub fn build_area(&self) -> GResult<Geometry>
pub fn build_area(&self) -> GResult<Geometry>
Creates an areal geometry formed by the constituent linework of given geometry.
You can find new illustrations on postgis documentation.
Available using the v3_8_0 feature.
§Example
use geos::{Geom, Geometry};
let geom = Geometry::new_from_wkt("GEOMETRYCOLLECTION( \
POLYGON((0 0, 4 0, 4 4, 0 4, 0 0)), \
POLYGON((1 1, 1 3, 3 3, 3 1, 1 1)))")
.expect("Invalid geometry");
let build_area_geom = geom.build_area().expect("build_area failed");
// Square polygon with square hole.
assert_eq!(build_area_geom.to_wkt_precision(0).unwrap(),
"POLYGON ((0 0, 0 4, 4 4, 4 0, 0 0), (1 1, 3 1, 3 3, 1 3, 1 1))");Sourcepub fn polygonize<T: Borrow<Geometry>>(geometries: &[T]) -> GResult<Geometry>
pub fn polygonize<T: Borrow<Geometry>>(geometries: &[T]) -> GResult<Geometry>
Description from postgis:
Creates a GeometryCollection containing possible polygons formed from the constituent linework of a set of geometries.
§Example:
use geos::{Geom, Geometry};
let geom1 = Geometry::new_from_wkt("POLYGON((-71.040878 42.285678,\
-71.040943 42.2856,\
-71.04096 42.285752,\
-71.040878 42.285678))")
.expect("Failed to create geometry");
let geom2 = Geometry::new_from_wkt("POLYGON((-71.17166 42.353675,\
-71.172026 42.354044,\
-71.17239 42.354358,\
-71.171794 42.354971,\
-71.170511 42.354855,\
-71.17112 42.354238,\
-71.17166 42.353675))")
.expect("Failed to create geometry");
let polygonized = Geometry::polygonize(&[geom1, geom2]).expect("polygonize failed");
assert_eq!(polygonized.to_wkt().unwrap(),
"GEOMETRYCOLLECTION (POLYGON ((-71.0408780000000064 42.2856779999999972, \
-71.0409429999999986 42.2856000000000023, \
-71.0409599999999983 42.2857520000000022, \
-71.0408780000000064 42.2856779999999972)), \
POLYGON ((-71.1716600000000028 42.3536750000000026, \
-71.1720260000000025 42.3540440000000018, \
-71.1723899999999929 42.3543579999999977, \
-71.1717940000000056 42.3549709999999990, \
-71.1705110000000047 42.3548550000000006, \
-71.1711200000000019 42.3542380000000023, \
-71.1716600000000028 42.3536750000000026)))");pub fn polygonizer_get_cut_edges<T: Borrow<Geometry>>( &self, geometries: &[T], ) -> GResult<Geometry>
Sourcepub fn line_merge(&self) -> GResult<Geometry>
pub fn line_merge(&self) -> GResult<Geometry>
Merges Multi Line String geometry into a (set of) Line String.
§Warning
If you use this function on something else than a Multi Line String or a
Line String, it’ll return an empty Geometry collection.
§Example
use geos::{Geom, Geometry};
let lines = Geometry::new_from_wkt("MULTILINESTRING((-29 -27,-30 -29.7,-36 -31,-45 -33),\
(-45 -33,-46 -32))")
.expect("Invalid geometry");
let lines_merged = lines.line_merge().expect("line merge failed");
assert_eq!(
lines_merged.to_wkt_precision(1).unwrap(),
"LINESTRING (-29.0 -27.0, -30.0 -29.7, -36.0 -31.0, -45.0 -33.0, -46.0 -32.0)",
);Sourcepub fn reverse(&self) -> GResult<Geometry>
pub fn reverse(&self) -> GResult<Geometry>
Reverses the order of the vertexes.
Available using the v3_7_0 feature.
§Example
use geos::{Geom, Geometry};
let line = Geometry::new_from_wkt("LINESTRING(1 10,1 2)")
.expect("invalid geometry");
let reversed_line = line.reverse().expect("reverse failed");
assert_eq!(
reversed_line.to_wkt_precision(1).unwrap(),
"LINESTRING (1.0 2.0, 1.0 10.0)",
);Sourcepub fn simplify(&self, tolerance: f64) -> GResult<Geometry>
pub fn simplify(&self, tolerance: f64) -> GResult<Geometry>
Returns a simplified version of the given geometry.
Sourcepub fn topology_preserve_simplify(&self, tolerance: f64) -> GResult<Geometry>
pub fn topology_preserve_simplify(&self, tolerance: f64) -> GResult<Geometry>
Returns a simplified version of the given geometry. It will avoid creating invalid derived geometries.
Sourcepub fn set_srid(&mut self, srid: usize)
pub fn set_srid(&mut self, srid: usize)
Set SRID of self.
§Example
use geos::{Geom, Geometry};
let mut point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 4.0)")
.expect("Invalid geometry");
point_geom.set_srid(4326);
assert_eq!(point_geom.get_srid(), Ok(4326));Sourcepub fn normalize(&mut self) -> GResult<()>
pub fn normalize(&mut self) -> GResult<()>
Normalizes self in its normalized/canonical form. May reorder vertices in polygon rings,
rings in a polygon, elements in a multi-geometry complex.
§Example
use geos::{Geom, Geometry};
let mut geom = Geometry::new_from_wkt(
"GEOMETRYCOLLECTION(POINT(2 3), MULTILINESTRING((0 0, 1 1),(2 2, 3 3)))",
).expect("Invalid geometry");
geom.normalize().expect("normalize failed");
assert_eq!(geom.to_wkt_precision(1).unwrap(),
"GEOMETRYCOLLECTION (MULTILINESTRING ((2.0 2.0, 3.0 3.0), (0.0 0.0, 1.0 1.0)), \
POINT (2.0 3.0))");Sourcepub fn create_empty_polygon() -> GResult<Geometry>
pub fn create_empty_polygon() -> GResult<Geometry>
Creates an empty polygon geometry.
§Example
use geos::{Geom, Geometry};
let geom = Geometry::create_empty_polygon().expect("Failed to build empty polygon");
assert_eq!(geom.to_wkt().unwrap(), "POLYGON EMPTY");Sourcepub fn create_empty_point() -> GResult<Geometry>
pub fn create_empty_point() -> GResult<Geometry>
Creates an empty point geometry.
§Example
use geos::{Geom, Geometry};
let geom = Geometry::create_empty_point().expect("Failed to build empty point");
assert_eq!(geom.to_wkt().unwrap(), "POINT EMPTY");Sourcepub fn create_empty_line_string() -> GResult<Geometry>
pub fn create_empty_line_string() -> GResult<Geometry>
Creates an empty line string geometry.
§Example
use geos::{Geom, Geometry};
let geom = Geometry::create_empty_line_string().expect("Failed to build empty line string");
assert_eq!(geom.to_wkt().unwrap(), "LINESTRING EMPTY");Sourcepub fn create_empty_collection(type_: GeometryTypes) -> GResult<Geometry>
pub fn create_empty_collection(type_: GeometryTypes) -> GResult<Geometry>
Creates an empty collection.
The type_ must be one of:
GeometryTypes::GeometryCollectionGeometryTypes::MultiPointGeometryTypes::MultiLineStringGeometryTypes::MultiPolygon
§Example
use geos::{Geom, Geometry, GeometryTypes};
let geom = Geometry::create_empty_collection(GeometryTypes::MultiPolygon)
.expect("Failed to build empty collection");
assert_eq!(geom.to_wkt().unwrap(), "MULTIPOLYGON EMPTY");Sourcepub fn create_polygon(
exterior: Geometry,
interiors: Vec<Geometry>,
) -> GResult<Geometry>
pub fn create_polygon( exterior: Geometry, interiors: Vec<Geometry>, ) -> GResult<Geometry>
Creates a polygon formed by the given shell and array of holes.
§Note
exterior must be a LinearRing.
§Example
use geos::{Geom, Geometry};
let geom = Geometry::new_from_wkt("LINEARRING(75.15 29.53,77 29,77.6 29.5, 75.15 29.53)")
.expect("Invalid geometry");
let polygon_geom = Geometry::create_polygon(geom, vec![])
.expect("create_polygon failed");
assert_eq!(
polygon_geom.to_wkt_precision(1).unwrap(),
"POLYGON ((75.2 29.5, 77.0 29.0, 77.6 29.5, 75.2 29.5))",
);Sourcepub fn create_geometry_collection(geoms: Vec<Geometry>) -> GResult<Geometry>
pub fn create_geometry_collection(geoms: Vec<Geometry>) -> GResult<Geometry>
Create a geometry collection.
§Example
use geos::{Geom, Geometry};
let geom1 = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0))")
.expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (3.0 4.0)").expect("Invalid geometry");
let geom = Geometry::create_geometry_collection(vec![geom1, geom2])
.expect("Failed to build multipolygon");
assert_eq!(geom.to_wkt_precision(1).unwrap(),
"GEOMETRYCOLLECTION (POLYGON ((0.0 0.0, 10.0 0.0, 10.0 6.0, 0.0 6.0, 0.0 0.0)), \
POINT (3.0 4.0))");Sourcepub fn create_multipolygon(polygons: Vec<Geometry>) -> GResult<Geometry>
pub fn create_multipolygon(polygons: Vec<Geometry>) -> GResult<Geometry>
Create a multi polygon geometry.
§Example
use geos::{Geom, Geometry};
let geom1 = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0))")
.expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POLYGON((3 3, 10 3, 10 6, 3 6, 3 3))")
.expect("Invalid geometry");
let geom = Geometry::create_multipolygon(vec![geom1, geom2])
.expect("Failed to build multipolygon");
assert_eq!(geom.to_wkt_precision(1).unwrap(),
"MULTIPOLYGON (((0.0 0.0, 10.0 0.0, 10.0 6.0, 0.0 6.0, 0.0 0.0)), \
((3.0 3.0, 10.0 3.0, 10.0 6.0, 3.0 6.0, 3.0 3.0)))");Sourcepub fn create_multiline_string(linestrings: Vec<Geometry>) -> GResult<Geometry>
pub fn create_multiline_string(linestrings: Vec<Geometry>) -> GResult<Geometry>
Create a multiline string geometry.
§Example
use geos::{Geom, Geometry};
let geom1 = Geometry::new_from_wkt("LINESTRING (1.0 2.0, 3.0 4.0)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING (5.0 6.0, 7.0 8.0)").expect("invalid geometry");
let geom = Geometry::create_multiline_string(vec![geom1, geom2])
.expect("Failed to build multiline string");
assert_eq!(geom.to_wkt_precision(1).unwrap(),
"MULTILINESTRING ((1.0 2.0, 3.0 4.0), (5.0 6.0, 7.0 8.0))");Sourcepub fn create_multipoint(points: Vec<Geometry>) -> GResult<Geometry>
pub fn create_multipoint(points: Vec<Geometry>) -> GResult<Geometry>
Creates a multi point geometry.
§Example
use geos::{Geom, Geometry};
let geom1 = Geometry::new_from_wkt("POINT (1.0 2.0)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (3.0 4.0)").expect("Invalid geometry");
let geom = Geometry::create_multipoint(vec![geom1, geom2])
.expect("Failed to build multipoint");
assert_eq!(geom.to_wkt_precision(1).unwrap(), "MULTIPOINT (1.0 2.0, 3.0 4.0)");Sourcepub fn create_point(s: CoordSeq) -> GResult<Geometry>
pub fn create_point(s: CoordSeq) -> GResult<Geometry>
Creates a point geometry.
§Example
use geos::{CoordDimensions, CoordSeq, Geom, Geometry};
let coords = CoordSeq::new_from_vec(&[&[1., 2.]])
.expect("failed to create CoordSeq");
let geom = Geometry::create_point(coords).expect("Failed to create a point");
assert_eq!(geom.to_wkt_precision(1).unwrap(), "POINT (1.0 2.0)");Sourcepub fn create_line_string(s: CoordSeq) -> GResult<Geometry>
pub fn create_line_string(s: CoordSeq) -> GResult<Geometry>
Creates a line string geometry.
§Example
use geos::{CoordDimensions, CoordSeq, Geom, Geometry};
let coords = CoordSeq::new_from_vec(&[&[1., 2.], &[3., 4.]])
.expect("failed to create CoordSeq");
let geom = Geometry::create_line_string(coords).expect("Failed to create a line string");
assert_eq!(geom.to_wkt_precision(1).unwrap(), "LINESTRING (1.0 2.0, 3.0 4.0)");Sourcepub fn create_linear_ring(s: CoordSeq) -> GResult<Geometry>
pub fn create_linear_ring(s: CoordSeq) -> GResult<Geometry>
Creates a linear ring geometry.
§Example
use geos::{CoordDimensions, CoordSeq, Geom, Geometry};
let coords = CoordSeq::new_from_vec(&[&[75.15, 29.53],
&[77., 29.],
&[77.6, 29.5],
&[75.15, 29.53]])
.expect("failed to create CoordSeq");
let geom = Geometry::create_linear_ring(coords)
.expect("Failed to create a linea ring");
assert_eq!(geom.to_wkt_precision(1).unwrap(),
"LINEARRING (75.2 29.5, 77.0 29.0, 77.6 29.5, 75.2 29.5)");Trait Implementations§
Source§impl Geom for Geometry
impl Geom for Geometry
fn geometry_type(&self) -> GeometryTypes
Source§fn is_valid_reason(&self) -> GResult<String>
fn is_valid_reason(&self) -> GResult<String>
Source§fn get_coord_seq(&self) -> GResult<CoordSeq>
fn get_coord_seq(&self) -> GResult<CoordSeq>
Source§fn area(&self) -> GResult<f64>
fn area(&self) -> GResult<f64>
Source§fn to_wkt_precision(&self, precision: u32) -> GResult<String>
fn to_wkt_precision(&self, precision: u32) -> GResult<String>
precision. It is a wrapper
around WKTWriter::set_rounding_precision. Read moreSource§fn touches<G: Geom>(&self, other: &G) -> GResult<bool>
fn touches<G: Geom>(&self, other: &G) -> GResult<bool>
true if the only points in common between self and other lie in the union of
the boundaries of self and other. Read moreSource§fn buffer(&self, width: f64, quadsegs: i32) -> GResult<Geometry>
fn buffer(&self, width: f64, quadsegs: i32) -> GResult<Geometry>
self is less than or
equal to distance. Read moreSource§fn buffer_with_params(
&self,
width: f64,
buffer_params: &BufferParams,
) -> GResult<Geometry>
fn buffer_with_params( &self, width: f64, buffer_params: &BufferParams, ) -> GResult<Geometry>
self is less than or
equal to distance. Read moreSource§fn buffer_with_style(
&self,
width: f64,
quadsegs: i32,
end_cap_style: CapStyle,
join_style: JoinStyle,
mitre_limit: f64,
) -> GResult<Geometry>
fn buffer_with_style( &self, width: f64, quadsegs: i32, end_cap_style: CapStyle, join_style: JoinStyle, mitre_limit: f64, ) -> GResult<Geometry>
self is less than or
equal to distance. Read moreSource§fn is_simple(&self) -> GResult<bool>
fn is_simple(&self) -> GResult<bool>
Source§fn envelope(&self) -> GResult<Geometry>
fn envelope(&self) -> GResult<Geometry>
Source§fn union<G: Geom>(&self, other: &G) -> GResult<Geometry>
fn union<G: Geom>(&self, other: &G) -> GResult<Geometry>
Source§fn get_centroid(&self) -> GResult<Geometry>
fn get_centroid(&self) -> GResult<Geometry>
Source§fn voronoi<G: Geom>(
&self,
envelope: Option<&G>,
tolerance: f64,
only_edges: bool,
) -> GResult<Geometry>
fn voronoi<G: Geom>( &self, envelope: Option<&G>, tolerance: f64, only_edges: bool, ) -> GResult<Geometry>
Source§fn boundary(&self) -> GResult<Geometry>
fn boundary(&self) -> GResult<Geometry>
self. Read moreSource§fn is_closed(&self) -> GResult<bool>
fn is_closed(&self) -> GResult<bool>
true if start and end point are coincident. Read moreSource§fn length(&self) -> GResult<f64>
fn length(&self) -> GResult<f64>
self. The unit depends of the SRID. Read moreSource§fn hausdorff_distance_densify<G: Geom>(
&self,
other: &G,
distance_frac: f64,
) -> GResult<f64>
fn hausdorff_distance_densify<G: Geom>( &self, other: &G, distance_frac: f64, ) -> GResult<f64>
Source§fn frechet_distance_densify<G: Geom>(
&self,
other: &G,
distance_frac: f64,
) -> GResult<f64>
fn frechet_distance_densify<G: Geom>( &self, other: &G, distance_frac: f64, ) -> GResult<f64>
Source§fn extract_unique_points(&self) -> GResult<Geometry>
fn extract_unique_points(&self) -> GResult<Geometry>
self.fn nearest_points<G: Geom>(&self, other: &G) -> GResult<CoordSeq>
Source§fn get_point_n(&self, n: usize) -> GResult<Geometry>
fn get_point_n(&self, n: usize) -> GResult<Geometry>
Source§fn get_num_interior_rings(&self) -> GResult<usize>
fn get_num_interior_rings(&self) -> GResult<usize>
Source§fn get_num_coordinates(&self) -> GResult<usize>
fn get_num_coordinates(&self) -> GResult<usize>
self. Read moreSource§fn get_num_dimensions(&self) -> GResult<usize>
fn get_num_dimensions(&self) -> GResult<usize>
self. Read moreSource§fn get_coordinate_dimension(&self) -> GResult<Dimensions>
fn get_coordinate_dimension(&self) -> GResult<Dimensions>
Source§fn make_valid(&self) -> GResult<Geometry>
fn make_valid(&self) -> GResult<Geometry>
self. Read moreSource§fn set_precision(&self, grid_size: f64, flags: Precision) -> GResult<Geometry>
fn set_precision(&self, grid_size: f64, flags: Precision) -> GResult<Geometry>
self. Read moreSource§fn minimum_clearance(&self) -> GResult<f64>
fn minimum_clearance(&self) -> GResult<f64>
self could be moved to produce an
invalid geometry. Read moreSource§fn minimum_clearance_line(&self) -> GResult<Geometry>
fn minimum_clearance_line(&self) -> GResult<Geometry>
self’s minimum clearance. Read moreSource§fn minimum_rotated_rectangle(&self) -> GResult<Geometry>
fn minimum_rotated_rectangle(&self) -> GResult<Geometry>
self. Read moreSource§fn minimum_width(&self) -> GResult<Geometry>
fn minimum_width(&self) -> GResult<Geometry>
self. Read more