Trait geos::Geom[][src]

pub trait Geom<'a>: AsRaw<RawType = GEOSGeometry> + ContextHandling<Context = Arc<ContextHandle<'a>>> {
Show methods fn get_type(&self) -> GResult<String>;
fn geometry_type(&self) -> GeometryTypes;
fn is_valid(&self) -> bool;
fn is_valid_reason(&self) -> GResult<String>;
fn get_coord_seq(&self) -> GResult<CoordSeq<'a>>;
fn area(&self) -> GResult<f64>;
fn to_wkt(&self) -> GResult<String>;
fn to_wkt_precision(&self, precision: u32) -> GResult<String>;
fn is_ring(&self) -> GResult<bool>;
fn intersects<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn crosses<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn disjoint<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn touches<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn overlaps<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn within<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn equals<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn equals_exact<'b, G: Geom<'b>>(
        &self,
        other: &G,
        precision: f64
    ) -> GResult<bool>;
fn covers<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn covered_by<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn contains<'b, G: Geom<'b>>(&self, other: &G) -> GResult<bool>;
fn buffer(&self, width: f64, quadsegs: i32) -> GResult<Geometry<'a>>;
fn is_empty(&self) -> GResult<bool>;
fn is_simple(&self) -> GResult<bool>;
fn difference<'b, G: Geom<'b>>(&self, other: &G) -> GResult<Geometry<'a>>;
fn envelope(&self) -> GResult<Geometry<'a>>;
fn sym_difference<'b, G: Geom<'b>>(
        &self,
        other: &G
    ) -> GResult<Geometry<'a>>;
fn union<'b, G: Geom<'b>>(&self, other: &G) -> GResult<Geometry<'a>>;
fn get_centroid(&self) -> GResult<Geometry<'a>>;
fn unary_union(&self) -> GResult<Geometry<'a>>;
fn voronoi<'b, G: Geom<'b>>(
        &self,
        envelope: Option<&G>,
        tolerance: f64,
        only_edges: bool
    ) -> GResult<Geometry<'a>>;
fn intersection<'b, G: Geom<'b>>(&self, other: &G) -> GResult<Geometry<'a>>;
fn convex_hull(&self) -> GResult<Geometry<'a>>;
fn boundary(&self) -> GResult<Geometry<'a>>;
fn has_z(&self) -> GResult<bool>;
fn is_closed(&self) -> GResult<bool>;
fn length(&self) -> GResult<f64>;
fn distance<'b, G: Geom<'b>>(&self, other: &G) -> GResult<f64>;
fn distance_indexed<'b, G: Geom<'b>>(&self, other: &G) -> GResult<f64>;
fn hausdorff_distance<'b, G: Geom<'b>>(&self, other: &G) -> GResult<f64>;
fn hausdorff_distance_densify<'b, G: Geom<'b>>(
        &self,
        other: &G,
        distance_frac: f64
    ) -> GResult<f64>;
fn frechet_distance<'b, G: Geom<'b>>(&self, other: &G) -> GResult<f64>;
fn frechet_distance_densify<'b, G: Geom<'b>>(
        &self,
        other: &G,
        distance_frac: f64
    ) -> GResult<f64>;
fn get_length(&self) -> GResult<f64>;
fn snap<'b, G: Geom<'b>>(
        &self,
        other: &G,
        tolerance: f64
    ) -> GResult<Geometry<'a>>;
fn extract_unique_points(&self) -> GResult<Geometry<'a>>;
fn nearest_points<'b, G: Geom<'b>>(
        &self,
        other: &G
    ) -> GResult<CoordSeq<'a>>;
fn get_x(&self) -> GResult<f64>;
fn get_y(&self) -> GResult<f64>;
fn get_z(&self) -> GResult<f64>;
fn get_point_n(&self, n: usize) -> GResult<Geometry<'a>>;
fn get_start_point(&self) -> GResult<Geometry<'a>>;
fn get_end_point(&self) -> GResult<Geometry<'a>>;
fn get_num_points(&self) -> GResult<usize>;
fn get_num_interior_rings(&self) -> GResult<usize>;
fn get_num_coordinates(&self) -> GResult<usize>;
fn get_num_dimensions(&self) -> GResult<usize>;
fn get_coordinate_dimension(&self) -> GResult<Dimensions>;
fn make_valid(&self) -> GResult<Geometry<'a>>;
fn get_num_geometries(&self) -> GResult<usize>;
fn get_srid(&self) -> GResult<usize>;
fn get_precision(&self) -> GResult<f64>;
fn set_precision(
        &self,
        grid_size: f64,
        flags: Precision
    ) -> GResult<Geometry<'a>>;
fn get_x_max(&self) -> GResult<f64>;
fn get_x_min(&self) -> GResult<f64>;
fn get_y_max(&self) -> GResult<f64>;
fn get_y_min(&self) -> GResult<f64>;
fn minimum_clearance(&self) -> GResult<f64>;
fn minimum_clearance_line(&self) -> GResult<Geometry<'a>>;
fn minimum_rotated_rectangle(&self) -> GResult<Geometry<'a>>;
fn minimum_width(&self) -> GResult<Geometry<'a>>;
fn delaunay_triangulation(
        &self,
        tolerance: f64,
        only_edges: bool
    ) -> GResult<Geometry<'a>>;
fn interpolate(&self, d: f64) -> GResult<Geometry<'a>>;
fn interpolate_normalized(&self, d: f64) -> GResult<Geometry<'a>>;
fn project<'b, G: Geom<'b>>(&self, p: &G) -> GResult<f64>;
fn project_normalized<'b, G: Geom<'b>>(&self, p: &G) -> GResult<f64>;
fn node(&self) -> GResult<Geometry<'a>>;
fn offset_curve(
        &self,
        width: f64,
        quadrant_segments: i32,
        join_style: JoinStyle,
        mitre_limit: f64
    ) -> GResult<Geometry<'a>>;
fn point_on_surface(&self) -> GResult<Geometry<'a>>;
fn polygonize_full(
        &self
    ) -> GResult<(Geometry<'a>, Option<Geometry<'a>>, Option<Geometry<'a>>, Option<Geometry<'a>>)>;
fn shared_paths<'b, G: Geom<'b>>(&self, other: &G) -> GResult<Geometry<'a>>;
fn to_hex(&self) -> GResult<CVec<u8>>;
fn to_wkb(&self) -> GResult<CVec<u8>>;
fn to_prepared_geom<'c>(&'c self) -> GResult<PreparedGeometry<'c>>;
fn clone(&self) -> Geometry<'a>;
fn get_geometry_n<'c>(&'c self, n: usize) -> GResult<ConstGeometry<'a, 'c>>;
fn get_interior_ring_n<'c>(
        &'c self,
        n: u32
    ) -> GResult<ConstGeometry<'a, 'c>>;
fn get_exterior_ring<'c>(&'c self) -> GResult<ConstGeometry<'a, 'c>>;
}

Required methods

Returns the type of the geometry.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))")
                    .expect("Invalid geometry");
assert_eq!(geom.get_type(), Ok("Polygon".to_owned()));

Checks if the geometry is valid.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))")
                    .expect("Invalid geometry");
assert!(geom.is_valid() == false);

Returns an explanation on why the geometry is invalid.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))")
                    .expect("Invalid geometry");
assert_eq!(
    geom.is_valid_reason(),
    Ok("Self-intersection[0 0]".to_owned()),
);

Get the underlying geos CoordSeq object from the geometry

Note: this clones the underlying CoordSeq to avoid double free (because CoordSeq handles the object ptr and the CoordSeq is still owned by the geos geometry) if this method’s performance becomes a bottleneck, feel free to open an issue, we could skip this clone with cleaner code.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT (2 3)")
                    .expect("Invalid geometry");
let coord_seq = geom.get_coord_seq().expect("get_coord_seq failed");

assert_eq!(coord_seq.get_x(0), Ok(2.));
assert_eq!(coord_seq.get_y(0), Ok(3.));

Returns the area of the geometry. Units are specified by the SRID of the given 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");
assert_eq!(geom1.area(), Ok(60.));

Returns a WKT representation of the geometry. It defaults to 2 dimensions output. Use WKTWriter type directly if you want more control.

Examples

use geos::{Geom, Geometry, OutputDimension, WKTWriter};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)")
                          .expect("Invalid geometry");
assert_eq!(
    point_geom.to_wkt().unwrap(),
    "POINT (2.5000000000000000 2.5000000000000000)",
);

// A three dimension point will be output just as a 2 dimension:
let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 3)")
                          .expect("Invalid geometry");
assert_eq!(
    point_geom.to_wkt().unwrap(),
    "POINT (2.5000000000000000 2.5000000000000000)",
);

// To "fix" it, use `WKTWriter` instead:
let mut wkt_writer = WKTWriter::new()
                               .expect("Failed to create WKTWriter");
wkt_writer.set_output_dimension(OutputDimension::ThreeD);
assert_eq!(
    wkt_writer.write(&point_geom).unwrap(),
    "POINT Z (2.5000000000000000 2.5000000000000000 3.0000000000000000)",
);

Returns a WKT representation of the geometry with the given precision. It is a wrapper around WKTWriter::set_rounding_precision.

Example

use geos::{Geom, Geometry, WKTWriter};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
assert_eq!(point_geom.to_wkt_precision(2).unwrap(), "POINT (2.50 2.50)");

// It is a wrapper around:
let mut writer = WKTWriter::new().expect("Failed to create WKTWriter");
writer.set_rounding_precision(2);
assert_eq!(writer.write(&point_geom).unwrap(), "POINT (2.50 2.50)");

Returns true if the geometry is a ring.

Example

use geos::{Geom, Geometry};

let circle = Geometry::new_from_wkt("LINESTRING(0 0, 0 1, 1 1, 0 0)").expect("Invalid geometry");
assert_eq!(circle.is_ring(), Ok(true));

Returns true if self shares any portion of space with other. So if any of this is true:

  • self overlaps other
  • self touches other
  • self is within other

Then intersects will return true as well.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT(0 0)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(2 0, 0 2)").expect("invalid geometry");
let geom3 = Geometry::new_from_wkt("LINESTRING(0 0, 0 2)").expect("invalid geometry");

assert_eq!(geom1.intersects(&geom2), Ok(false));
assert_eq!(geom1.intersects(&geom3), Ok(true));

Returns true if self and other have at least one interior into each other.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING(1 1,2 2)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(2 1,1 2)").expect("invalid geometry");

assert_eq!(geom1.crosses(&geom2), Ok(true));

Returns true if self doesn’t:

  • Overlap other
  • Touch other
  • Is within other

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT(0 0)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(2 0, 0 2)").expect("invalid geometry");
let geom3 = Geometry::new_from_wkt("LINESTRING(0 0, 0 2)").expect("invalid geometry");

assert_eq!(geom1.disjoint(&geom2), Ok(true));
assert_eq!(geom1.disjoint(&geom3), Ok(false));

Returns true if the only points in common between self and other lie in the union of the boundaries of self and other.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING(0 0, 1 1, 0 2)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT(1 1)").expect("invalid geometry");

assert_eq!(geom1.touches(&geom2), Ok(false));

let geom2 = Geometry::new_from_wkt("POINT(0 2)").expect("invalid geometry");

assert_eq!(geom1.touches(&geom2), Ok(true));

Returns true if self spatially overlaps other.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT(1 0.5)").expect("invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(1 0, 1 1, 3 5)").expect("invalid geometry");

assert_eq!(geom1.overlaps(&geom2), Ok(false));

let geom1 = geom1.buffer(3., 8).expect("buffer failed");
let geom2 = geom2.buffer(0.5, 8).expect("buffer failed");

assert_eq!(geom1.overlaps(&geom2), Ok(true));

Returns true if self is completely inside other.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT(50 50)").expect("invalid geometry");
let small_geom = geom.buffer(20., 8).expect("buffer failed");
let big_geom = geom.buffer(40., 8).expect("buffer failed");

assert_eq!(small_geom.within(&small_geom), Ok(true));
assert_eq!(small_geom.within(&big_geom), Ok(true));
assert_eq!(big_geom.within(&small_geom), Ok(false));

Checks if the two Geometry objects are equal.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (3.8 3.8)").expect("Invalid geometry");
let geom3 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");

assert!(geom1.equals(&geom2) == Ok(false));
assert!(geom1.equals(&geom3) == Ok(true));

Note that you can also use method through the PartialEq trait:

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (3.8 3.8)").expect("Invalid geometry");
let geom3 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");

assert!(geom1 != geom2);
assert!(geom1 == geom3);

Checks if the two Geometry objects are exactly equal.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (3.8 3.8)").expect("Invalid geometry");
let geom3 = Geometry::new_from_wkt("POINT (2.5 2.5)").expect("Invalid geometry");

assert_eq!(geom1.equals_exact(&geom2, 0.1), Ok(false));
assert_eq!(geom1.equals_exact(&geom3, 0.1), Ok(true));

Returns true if no point of other is outside of self.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let little_geom = geom.buffer(10., 8).expect("buffer failed");
let big_geom = geom.buffer(20., 8).expect("buffer failed");

assert_eq!(little_geom.covers(&big_geom), Ok(false));
assert_eq!(big_geom.covers(&little_geom), Ok(true));

Returns true if no point of self is outside of other.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let little_geom = geom.buffer(10., 8).expect("buffer failed");
let big_geom = geom.buffer(20., 8).expect("buffer failed");

assert_eq!(little_geom.covered_by(&big_geom), Ok(true));
assert_eq!(big_geom.covered_by(&little_geom), Ok(false));

Returns true if no points of the other geometry is outside the exterior of self.

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 (2.5 2.5)").expect("Invalid geometry");

assert_eq!(geom1.contains(&geom2), Ok(true));

Returns a geometry which represents all points whose distance from self is less than or equal to distance.

You can find nice examples about this in postgis documentation.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT(1 3)").expect("Invalid geometry");
let buffer_geom = geom.buffer(50., 2).expect("buffer failed");

assert_eq!(buffer_geom.to_wkt_precision(1).unwrap(),
           "POLYGON ((51.0 3.0, 36.4 -32.4, 1.0 -47.0, -34.4 -32.4, -49.0 3.0, -34.4 38.4, \
                      1.0 53.0, 36.4 38.4, 51.0 3.0))");

Returns true if the given geometry is empty.

Example

use geos::{Geom, Geometry};

let geom = Geometry::create_empty_polygon().expect("Invalid geometry");
assert_eq!(geom.is_empty(), Ok(true));

let geom = Geometry::new_from_wkt("POLYGON EMPTY").expect("Invalid geometry");
assert_eq!(geom.is_empty(), Ok(true));

let geom = Geometry::new_from_wkt("POINT(1 3)").expect("Invalid geometry");
assert_eq!(geom.is_empty(), Ok(false));

Returns true if the given geometry has no anomalous geometric points, such as self intersection or self tangency.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT (2.5 2.5)")
                    .expect("Invalid geometry");
assert_eq!(geom.is_simple(), Ok(true));

let geom = Geometry::new_from_wkt("LINESTRING(1 1,2 2,2 3.5,1 3,1 2,2 1)")
                    .expect("Invalid geometry");
assert_eq!(geom.is_simple(), Ok(false));

Returns a geometry which represents part of self that doesn’t intersect with other.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING(50 100, 50 200)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(50 50, 50 150)").expect("Invalid geometry");

let difference_geom = geom1.difference(&geom2).expect("envelope failed");

assert_eq!(difference_geom.to_wkt_precision(1).unwrap(),
           "LINESTRING (50.0 150.0, 50.0 200.0)");

Returns the minimum bouding box of the given geometry.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT(1 3)").expect("Invalid geometry");
let envelope_geom = geom.envelope().expect("envelope failed");

assert_eq!(envelope_geom.to_wkt_precision(1).unwrap(), "POINT (1.0 3.0)");

let geom = Geometry::new_from_wkt("LINESTRING(0 0, 1 3)").expect("Invalid geometry");
let envelope_geom = geom.envelope().expect("envelope failed");

assert_eq!(envelope_geom.to_wkt_precision(1).unwrap(),
           "POLYGON ((0.0 0.0, 1.0 0.0, 1.0 3.0, 0.0 3.0, 0.0 0.0))");

Returns a geometry which represents the parts of self and other that don’t intersect.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING(50 100, 50 200)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(50 50, 50 150)").expect("Invalid geometry");

let sym_diff_geom = geom1.sym_difference(&geom2).expect("sym_difference failed");

assert_eq!(sym_diff_geom.to_wkt_precision(1).unwrap(),
           "MULTILINESTRING ((50.0 150.0, 50.0 200.0), (50.0 50.0, 50.0 100.0))");

Aggregates the given geometry with another one.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT(1 2)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT(3 4)").expect("Invalid geometry");

let union_geom = geom1.union(&geom2).expect("union failed");

assert_eq!(union_geom.to_wkt_precision(1).unwrap(), "MULTIPOINT (1.0 2.0, 3.0 4.0)");

Returns the geometric center or (equivalently) the center of mass of the given geometry as a point.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("MULTIPOINT(-1 0, -1 2, -1 3, -1 4, -1 7, 0 1, 0 3, 1 1)")
                    .expect("Invalid geometry");
let centroid = geom.get_centroid().expect("failed to get centroid");

assert_eq!(centroid.to_wkt_precision(1).unwrap(), "POINT (-0.5 2.6)");

Documentation from postgis:

Unlike ST_Union, ST_UnaryUnion does dissolve boundaries between components of a multipolygon (invalid) and does perform union between the components of a geometrycollection. Each components of the input geometry is assumed to be valid, so you won’t get a valid multipolygon out of a bow-tie polygon (invalid).

You may use this function to node a set of linestrings. You may mix ST_UnaryUnion with ST_Collect to fine-tune how many geometries at once you want to dissolve to be nice on both memory size and CPU time, finding the balance between ST_Union and ST_MemUnion.

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((1 1, 2 1, 2 5, 1 5, 1 1))")
                     .expect("Invalid geometry");

let geom = Geometry::create_multipolygon(vec![geom1, geom2])
                    .expect("Failed to build multipolygon");

let union_geom = geom.unary_union().expect("unary_union failed");

assert_eq!(union_geom.to_wkt_precision(1).unwrap(),
           "POLYGON ((0.0 0.0, 0.0 6.0, 10.0 6.0, 10.0 0.0, 0.0 0.0))");

Create a voronoi diagram.

Example

use geos::{Geom, Geometry};

let input = Geometry::new_from_wkt("MULTIPOINT((100 200), (105 202), (110 200), (140 230),
                                               (210 240), (220 190), (170 170), (170 260),
                                               (213 245), (220 190))")
                  .expect("Invalid geometry");
let mut expected = Geometry::new_from_wkt(
    "GEOMETRYCOLLECTION(POLYGON ((-20 50, -20 380, -3.75 380, 105 235, 105 115, 77.14285714285714 50, -20 50)),\
                         POLYGON ((247 50, 77.14285714285714 50, 105 115, 145 195, 178.33333333333334 211.66666666666666, 183.51851851851853 208.7037037037037, 247 50)),\
                         POLYGON ((-3.75 380, 20.000000000000007 380, 176.66666666666666 223.33333333333334, 178.33333333333334 211.66666666666666, 145 195, 105 235, -3.75 380)),\
                         POLYGON ((105 115, 105 235, 145 195, 105 115)),\
                         POLYGON ((20.000000000000007 380, 255 380, 176.66666666666666 223.33333333333334, 20.000000000000007 380)),\
                         POLYGON ((255 380, 340 380, 340 240, 183.51851851851853 208.7037037037037, 178.33333333333334 211.66666666666666, 176.66666666666666 223.33333333333334, 255 380)),\
                         POLYGON ((340 240, 340 50, 247 50, 183.51851851851853 208.7037037037037, 340 240)))")
                            .expect("Invalid geometry");

let mut voronoi = input.voronoi(None::<&Geometry>, 6., false)
                       .expect("voronoi failed");

expected.normalize().expect("normalize failed");
voronoi.normalize().expect("normalize failed");

assert_eq!(expected.equals(&voronoi), Ok(true));

Returns a geometry representing the intersection between self and other.

Example

use geos::{Geom, Geometry};

let mut geom1 = Geometry::new_from_wkt("POINT(0 0)").expect("Invalid geometry");
let mut geom2 = Geometry::new_from_wkt("LINESTRING(2 0, 0 2)").expect("Invalid geometry");

let intersection_geom = geom1.intersection(&geom2).expect("intersection failed");

// No intersection.
assert_eq!(intersection_geom.is_empty(), Ok(true));

// We slighty change the linestring so we have an intersection:
let mut geom2 = Geometry::new_from_wkt("LINESTRING(0 0, 0 2)").expect("Invalid geometry");

let intersection_geom = geom1.intersection(&geom2).expect("intersection failed");

// Intersection!
assert_eq!(intersection_geom.to_wkt_precision(1).unwrap(), "POINT (0.0 0.0)");

Documentation from postgis:

The convex hull of a geometry represents the minimum convex geometry that encloses all geometries within the set.

One can think of the convex hull as the geometry you get by wrapping an elastic band around a set of geometries. This is different from a concave hull which is analogous to shrink-wrapping your geometries.

It is usually used with MULTI and Geometry Collections. Although it is not an aggregate - you can use it in conjunction with ST_Collect to get the convex hull of a set of points. ST_ConvexHull(ST_Collect(somepointfield)).

It is often used to determine an affected area based on a set of point observations.

Example

use geos::{Geom, Geometry};

let mut geom1 = Geometry::new_from_wkt("MULTILINESTRING((100 190,10 8),
                                                        (150 10, 20 30))")
                         .expect("Invalid geometry");
let mut geom2 = Geometry::new_from_wkt("MULTIPOINT(50 5, 150 30, 50 10, 10 10)")
                         .expect("Invalid geometry");

let geom = geom1.union(&geom2).expect("union failed");
let convex_hull_geom = geom.convex_hull().expect("convex_hull failed");

assert_eq!(convex_hull_geom.to_wkt_precision(1).unwrap(),
           "POLYGON ((50.0 5.0, 10.0 8.0, 10.0 10.0, 100.0 190.0, 150.0 30.0, 150.0 10.0, 50.0 5.0))");

Returns the closure of the combinatorial boundary of self.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING(1 1,0 0, -1 1)").expect("Invalid geometry");
let boundary_geom = geom.boundary().expect("boundary failed");

assert_eq!(boundary_geom.to_wkt_precision(1).unwrap(), "MULTIPOINT (1.0 1.0, -1.0 1.0)");

Returns true if self has a Z coordinate.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POINT(1 2 3)").expect("Invalid geometry");
assert_eq!(geom.has_z(), Ok(true));

let geom = Geometry::new_from_wkt("POINT(1 2)").expect("Invalid geometry");
assert_eq!(geom.has_z(), Ok(false));

Returns true if start and end point are coincident.

Only works on LineString and MultiLineString.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING(0 0, 1 1)").expect("Invalid geometry");
assert_eq!(geom.is_closed(), Ok(false));

let geom = Geometry::new_from_wkt("LINESTRING(0 0, 0 1, 1 1, 0 0)").expect("Invalid geometry");
assert_eq!(geom.is_closed(), Ok(true));

let geom = Geometry::new_from_wkt("MULTILINESTRING((0 0, 0 1, 1 1, 0 0),(0 0, 1 1))")
                    .expect("Invalid geometry");
assert_eq!(geom.is_closed(), Ok(false));

Returns the length of self. The unit depends of the SRID.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING(743238 2967416,743238 2967450)")
                    .expect("Invalid geometry");

assert_eq!(
    geom.length().map(|x| format!("{:.2}", x)).unwrap(),
    "34.00",
);

Returns the distance between self and other. The unit depends of the SRID.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (2 2)").expect("Invalid geometry");

assert_eq!(geom1.distance(&geom2).map(|x| format!("{:.2}", x)).unwrap(), "1.00");

Returns the indexed distance between self and other. The unit depends of the SRID.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (2 2)").expect("Invalid geometry");

assert_eq!(geom1.distance_indexed(&geom2).map(|x| format!("{:.2}", x)).unwrap(), "1.00");

Returns the hausdorff distance between self and other. The unit depends of the SRID.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (2 2)").expect("Invalid geometry");

assert_eq!(geom1.hausdorff_distance(&geom2).map(|x| format!("{:.2}", x)).unwrap(), "1.00");

Returns the hausdorff distance between self and other. The unit depends of the SRID.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POINT (1 2)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("POINT (2 2)").expect("Invalid geometry");

assert_eq!(geom1.hausdorff_distance_densify(&geom2, 1.).map(|x| format!("{:.2}", x))
                                                       .unwrap(), "1.00");

Returns the frechet distance between self and other. The unit depends of the SRID.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING (0 0, 100 0)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING (0 0, 50 50, 100 0)").expect("Invalid geometry");

assert_eq!(geom1.frechet_distance(&geom2).map(|x| format!("{:.2}", x)).unwrap(), "70.71");

Returns the frechet distance between self and other. The unit depends of the SRID.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("LINESTRING (0 0, 100 0)").expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING (0 0, 50 50, 100 0)").expect("Invalid geometry");

assert_eq!(geom1.frechet_distance_densify(&geom2, 1.).map(|x| format!("{:.2}", x))
                                                     .unwrap(), "70.71");

Returns the length of the given geometry.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING (1 2, 3 4, 5 6)")
                    .expect("Invalid geometry");

assert_eq!(geom.get_length().map(|x| format!("{:.2}", x)).unwrap(), "5.66");

Documentation from postgis:

Snaps the vertices and segments of a geometry another Geometry’s vertices. A snap distance tolerance is used to control where snapping is performed. The result geometry is the input geometry with the vertices snapped. If no snapping occurs then the input geometry is returned unchanged.

Snapping one geometry to another can improve robustness for overlay operations by eliminating nearly-coincident edges (which cause problems during noding and intersection calculation).

Too much snapping can result in invalid topology being created, so the number and location of snapped vertices is decided using heuristics to determine when it is safe to snap. This can result in some potential snaps being omitted, however.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("MULTIPOLYGON(((26 125, 26 200, 126 200, 126 125, 26 125),
                                                  (51 150, 101 150, 76 175, 51 150)),
                                                 ((151 100, 151 200, 176 175, 151 100)))")
                     .expect("Invalid geometry");
let geom2 = Geometry::new_from_wkt("LINESTRING(5 107, 54 84, 101 100)")
                     .expect("Invalid geometry");

let distance = geom1.distance(&geom2).expect("distance failed");
let snap_geom = geom1.snap(&geom2, distance * 1.25).expect("snap failed");

assert_eq!(snap_geom.to_wkt_precision(1).unwrap(),
           "MULTIPOLYGON (((5.0 107.0, 26.0 200.0, 126.0 200.0, 126.0 125.0, 101.0 100.0, 54.0 84.0, 5.0 107.0), \
                           (51.0 150.0, 101.0 150.0, 76.0 175.0, 51.0 150.0)), \
                          ((151.0 100.0, 151.0 200.0, 176.0 175.0, 151.0 100.0)))");

Returns unique points of self.

Returns the X position. The given Geometry must be a Point, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (1.5 2.5 3.5)").expect("Invalid geometry");
assert!(point_geom.get_x() == Ok(1.5));

Returns the Y position. The given Geometry must be a Point, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (1.5 2.5 3.5)").expect("Invalid geometry");
assert!(point_geom.get_y() == Ok(2.5));

Returns the Z position. The given Geometry must be a Point, otherwise it’ll fail.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 4.0)").expect("Invalid geometry");
assert!(point_geom.get_z() == Ok(4.0));

Returns the nth point of the given geometry.

The given Geometry must be a LineString, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING (1 2, 3 4, 5 6)")
                    .expect("Invalid geometry");
let nth_point = geom.get_point_n(1).expect("get_point_n failed");

assert_eq!(nth_point.to_wkt_precision(1).unwrap(), "POINT (3.0 4.0)");

Returns the start point of self.

The given Geometry must be a LineString, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING (1 2, 3 4)")
                    .expect("Invalid geometry");
let start_point = geom.get_start_point().expect("get_start_point failed");

assert_eq!(start_point.to_wkt_precision(1).unwrap(), "POINT (1.0 2.0)");

Returns the end point of self.

The given Geometry must be a LineString, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING (1 2, 3 4)")
                    .expect("Invalid geometry");
let end_point = geom.get_end_point().expect("get_end_point failed");

assert_eq!(end_point.to_wkt_precision(1).unwrap(), "POINT (3.0 4.0)");

Returns the number of points of self.

The given Geometry must be a LineString, otherwise it’ll fail.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING (1 2, 3 4)")
                    .expect("Invalid geometry");

assert_eq!(geom.get_num_points(), Ok(2));

Returns the number of interior rings.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),\
                                           (1 1, 2 1, 2 5, 1 5, 1 1),\
                                           (8 5, 8 4, 9 4, 9 5, 8 5))")
                    .expect("Invalid geometry");

assert_eq!(geom.get_num_interior_rings(), Ok(2));

Returns the number of coordinates inside self.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0))")
                    .expect("Invalid geometry");

assert_eq!(geom.get_num_coordinates(), Ok(5));

Returns the number of dimensions used in self.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0))")
                    .expect("Invalid geometry");

assert_eq!(geom.get_num_dimensions(), Ok(2));

Return in which coordinate dimension the geometry is.

Example

use geos::{Dimensions, Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 4.0)").expect("Invalid geometry");
assert!(point_geom.get_coordinate_dimension() == Ok(Dimensions::ThreeD));

let point_geom = Geometry::new_from_wkt("POINT (2.5 4.0)").expect("Invalid geometry");
assert!(point_geom.get_coordinate_dimension() == Ok(Dimensions::TwoD));

This functions attempts to return a valid representation of self.

Available using the v3_8_0 feature.

Returns the number of geometries.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING(77.29 29.07,77.42 29.26,77.27 29.31,77.29 29.07)")
                    .expect("Invalid geometry");
assert_eq!(geom.get_num_geometries(), Ok(1));

let geom = Geometry::new_from_wkt("GEOMETRYCOLLECTION(MULTIPOINT(-2 3 , -2 2),\
                                                      LINESTRING(5 5 ,10 10),\
                                                      POLYGON((-7 4.2,-7.1 5,-7.1 4.3,-7 4.2)))")
                    .expect("Invalid geometry");
assert_eq!(geom.get_num_geometries(), Ok(3));

Get 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));

Returns the precision of self.

Available using the v3_6_0 feature.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 4.0)").expect("Invalid geometry");
assert_eq!(point_geom.get_precision().map(|x| format!("{:.2}", x)).unwrap(), "0.00");

Returns the precision of self.

Available using the v3_6_0 feature.

Example

use geos::{Geom, Geometry, Precision};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5 4.0)").expect("Invalid geometry");

point_geom.set_precision(1., Precision::KeepCollapsed);
assert_eq!(point_geom.get_precision().map(|x| format!("{:.2}", x)).unwrap(), "0.00");

Returns the biggest X of the geometry.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let line = Geometry::new_from_wkt("LINESTRING(1 3 4, 5 6 7)").expect("Invalid WKT");
assert_eq!(line.get_x_max(), Ok(5.));

Returns the smallest X of the geometry.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let line = Geometry::new_from_wkt("LINESTRING(1 3 4, 5 6 7)").expect("Invalid WKT");
assert_eq!(line.get_x_min(), Ok(1.));

Returns the biggest Y of the geometry.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let line = Geometry::new_from_wkt("LINESTRING(1 3 4, 5 6 7)").expect("Invalid WKT");
assert_eq!(line.get_y_max(), Ok(6.));

Returns the smallest Y of the geometry.

Available using the v3_7_0 feature.

Example

use geos::{Geom, Geometry};

let line = Geometry::new_from_wkt("LINESTRING(1 3 4, 5 6 7)").expect("Invalid WKT");
assert_eq!(line.get_y_min(), Ok(3.));

Returns the smallest distance by which a vertex of self could be moved to produce an invalid geometry.

Available using the v3_6_0 feature.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("LINESTRING(1 3 4, 5 6 7)").expect("Invalid WKT");
assert_eq!(geom.minimum_clearance().map(|x| format!("{:.8}", x)).unwrap(), "5.00000000");

Returns the two-point LineString spanning of self’s minimum clearance.

Available using the v3_6_0 feature.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON ((0 0, 1 0, 1 1, 0.5 3.2e-4, 0 0))")
                    .expect("Invalid WKT");
let line = geom.minimum_clearance_line().expect("minimum_clearance_line failed");
assert_eq!(line.to_wkt_precision(1).unwrap(), "LINESTRING (0.5 0.0, 0.5 0.0)");

Returns the minimum rotated rectangle inside of self.

Available using the v3_6_0 feature.

Returns the minimum width inside of self.

Available using the v3_6_0 feature.

Returns a delaunay triangulation around the vertices of self.

Example

use geos::{Geom, Geometry};

let geom1 = Geometry::new_from_wkt("POLYGON((175 150, 20 40, 50 60, 125 100, 175 150))")
                     .expect("Invalid WKT");
let geom2 = Geometry::new_from_wkt("POINT(110 170)").expect("Invalid WKT");
let geom2 = geom2.buffer(20., 8).expect("buffer failed");

let geom = geom1.union(&geom2).expect("union failed");

let final_geom = geom.delaunay_triangulation(0.001, false).expect("delaunay_triangulation failed");

Return an offset line at a given distance and side from an input line. All points of the returned geometries are not further than the given distance from the input geometry.

Parameters description:

width

  • If width is positive, the offset will be at the left side of the input line and retain the same direction.
  • If width is negative, it’ll be at the right side and in the opposite direction.

quadrant_segments

  • If quadrant_segments is >= 1, joins are round, and quadrant_segments indicates the number of segments to use to approximate a quarter-circle.
  • If quadrant_segments == 0, joins are bevelled (flat).
  • If quadrant_segments < 0, joins are mitred, and the value of quadrant_segments indicates the mitre ration limit as mitre_limit = |quadrant_segments|

mitre_limit

The mitre ratio is the ratio of the distance from the corner to the end of the mitred offset corner. When two line segments meet at a sharp angle, a miter join will extend far beyond the original geometry (and in the extreme case will be infinitely far). To prevent unreasonable geometry, the mitre limit allows controlling the maximum length of the join corner. Corners with a ratio which exceed the limit will be beveled.

Returns, in the tuple elements order:

  1. The polygonized geometry.
  2. The cuts geometries collection.
  3. The dangles geometries collection.
  4. The invalid geometries collection.

Converts a Geometry to the HEX format. For more control over the generated output, use the [WKBWriter] type.

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 WKB failed");

Converts a Geometry to the WKB format. For more control over the generated output, use the [WKBWriter] type.

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");

Creates a new PreparedGeometry from the current Geometry.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POINT (2.5 2.5)")
                          .expect("Invalid geometry");
let prepared_geom = point_geom.to_prepared_geom().expect("failed to create prepared geom");

Also passes the context to the newly created Geometry.

Returns the 1-based nth geometry.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("MULTIPOINT(1 1, 2 2, 3 3, 4 4)")
                    .expect("Invalid geometry");
let point_nb3 = geom
    .get_geometry_n(2)
    .expect("failed to get third point");
assert_eq!(
    point_nb3.to_wkt().unwrap(),
    "POINT (3.0000000000000000 3.0000000000000000)",
);

Returns the nth interior ring.

Example

use geos::{Geom, Geometry};

let geom = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),\
                                           (1 1, 2 1, 2 5, 1 5, 1 1),\
                                           (8 5, 8 4, 9 4, 9 5, 8 5))")
                    .expect("Invalid geometry");
let interior = geom
    .get_interior_ring_n(0)
    .expect("failed to get interior ring");
assert_eq!(interior.to_wkt().unwrap(),
           "LINEARRING (1.0000000000000000 1.0000000000000000, \
                        2.0000000000000000 1.0000000000000000, \
                        2.0000000000000000 5.0000000000000000, \
                        1.0000000000000000 5.0000000000000000, \
                        1.0000000000000000 1.0000000000000000)");

Returns the exterior ring.

Example

use geos::{Geom, Geometry};

let point_geom = Geometry::new_from_wkt("POLYGON((0 0, 10 0, 10 6, 0 6, 0 0),\
                                              (1 1, 2 1, 2 5, 1 5, 1 1))")
                          .expect("Invalid geometry");

let exterior = point_geom
    .get_exterior_ring()
    .expect("failed to get exterior ring");
assert_eq!(exterior.to_wkt().unwrap(),
           "LINEARRING (0.0000000000000000 0.0000000000000000, \
                        10.0000000000000000 0.0000000000000000, \
                        10.0000000000000000 6.0000000000000000, \
                        0.0000000000000000 6.0000000000000000, \
                        0.0000000000000000 0.0000000000000000)");

Implementors