1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use crate::vector::Geometry;
/// # Set Operations
///
/// These methods provide set operations over two geometries, producing a new geometry.
impl Geometry {
/// Computes the [geometric intersection][intersection] of `self` and `other`.
///
/// Generates a new geometry which is the region of intersection of the two geometries operated on.
///
/// # Notes
/// * If you only need to determine if two geometries intersect and don't require
/// the resultant region, use [`Geometry::intersects`].
/// * Geometry validity is not checked, and invalid geometry will generate unpredictable results.
/// Use [`Geometry::is_valid`] if validity might be in question.
/// * If GEOS is *not* enabled, this function will always return `None`.
/// You may check for GEOS support with [`VersionInfo::has_geos`][has_geos].
///
/// # Returns
/// * `Some(geometry)`: a new `Geometry` representing the computed intersection
/// * `None`: when the geometries do not intersect or result could not be computed
///
/// See: [`OGR_G_Intersection`][OGR_G_Intersection]
///
/// [OGR_G_Intersection]: https://gdal.org/api/vector_c_api.html#_CPPv418OGR_G_Intersection12OGRGeometryH12OGRGeometryH
/// [intersection]: https://en.wikipedia.org/wiki/Intersection_(geometry)
/// [has_geos]: crate::version::VersionInfo::has_geos
pub fn intersection(&self, other: &Self) -> Option<Self> {
if !self.has_gdal_ptr() {
return None;
}
if !other.has_gdal_ptr() {
return None;
}
let ogr_geom =
unsafe { gdal_sys::OGR_G_Intersection(self.c_geometry(), other.c_geometry()) };
if ogr_geom.is_null() {
return None;
}
Some(unsafe { Geometry::with_c_geometry(ogr_geom, true) })
}
/// Computes the [geometric union][union] of `self` and `other`.
///
/// Generates a new geometry which is the union of the two geometries operated on.
///
/// # Notes
/// * Geometry validity is not checked, and invalid geometry will generate unpredictable results.
/// Use [`Geometry::is_valid`] if validity might be in question.
/// * If GEOS is *not* enabled, this function will always return `None`.
/// You may check for GEOS support with [`VersionInfo::has_geos`][has_geos].
///
/// # Returns
/// * `Some(geometry)`: a new `Geometry` representing the computed union
/// * `None`: when the union could not be computed
///
/// See: [`OGR_G_Union`][OGR_G_Union]
///
/// [OGR_G_Union]: https://gdal.org/api/vector_c_api.html#_CPPv411OGR_G_Union12OGRGeometryH12OGRGeometryH
/// [union]: https://en.wikipedia.org/wiki/Constructive_solid_geometry#Workings
/// [has_geos]: crate::version::VersionInfo::has_geos
pub fn union(&self, other: &Self) -> Option<Self> {
if !self.has_gdal_ptr() {
return None;
}
if !other.has_gdal_ptr() {
return None;
}
unsafe {
let ogr_geom = gdal_sys::OGR_G_Union(self.c_geometry(), other.c_geometry());
if ogr_geom.is_null() {
return None;
}
Some(Geometry::with_c_geometry(ogr_geom, true))
}
}
/// Computes the [geometric difference][difference] of `self` and `other`.
///
/// Generates a new geometry which is the difference of the two geometries operated on.
///
/// # Notes
/// * Geometry validity is not checked, and invalid geometry will generate unpredictable results.
/// Use [`Geometry::is_valid`] if validity might be in question.
/// * If GEOS is *not* enabled, this function will always return `None`.
/// You may check for GEOS support with [`VersionInfo::has_geos`][has_geos].
///
/// # Returns
/// * `Some(geometry)`: a new `Geometry` representing the computed difference
/// * `None`: when the union could not be computed
///
/// See: [`OGR_G_Difference`][OGR_G_Difference]
///
/// [OGR_G_Difference]: https://gdal.org/api/vector_c_api.html#_CPPv416OGR_G_Difference12OGRGeometryH12OGRGeometryH
/// [difference]: https://en.wikipedia.org/wiki/Constructive_solid_geometry#Workings
/// [has_geos]: crate::version::VersionInfo::has_geos
pub fn difference(&self, other: &Self) -> Option<Self> {
if !self.has_gdal_ptr() {
return None;
}
if !other.has_gdal_ptr() {
return None;
}
unsafe {
let ogr_geom = gdal_sys::OGR_G_Difference(self.c_geometry(), other.c_geometry());
if ogr_geom.is_null() {
return None;
}
Some(Geometry::with_c_geometry(ogr_geom, true))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[allow(clippy::float_cmp)]
fn test_intersection_success() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other =
Geometry::from_wkt("POLYGON ((0.0 5.0, 0.0 0.0, 5.0 0.0, 5.0 5.0, 0.0 5.0))").unwrap();
let inter = geom.intersection(&other);
assert!(inter.is_some());
let inter = inter.unwrap();
assert_eq!(inter.area(), 25.0);
}
#[test]
fn test_intersection_no_gdal_ptr() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other = unsafe { Geometry::lazy_feature_geometry() };
let inter = geom.intersection(&other);
assert!(inter.is_none());
}
#[test]
#[allow(clippy::float_cmp)]
fn test_intersection_no_intersects() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 5.0, 0.0 0.0, 5.0 0.0, 5.0 5.0, 0.0 5.0))").unwrap();
let other =
Geometry::from_wkt("POLYGON ((15.0 15.0, 15.0 20.0, 20.0 20.0, 20.0 15.0, 15.0 15.0))")
.unwrap();
let inter = geom.intersection(&other);
assert!(inter.is_some());
assert_eq!(inter.unwrap().area(), 0.0);
}
#[test]
#[allow(clippy::float_cmp)]
fn test_union_success() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other = Geometry::from_wkt("POLYGON ((1 -5, 1 1, -5 1, -5 -5, 1 -5))").unwrap();
let res = geom.union(&other);
assert!(res.is_some());
let res = res.unwrap();
assert_eq!(res.area(), 135.0);
}
#[test]
fn test_union_no_gdal_ptr() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other = unsafe { Geometry::lazy_feature_geometry() };
let res = geom.union(&other);
assert!(res.is_none());
}
#[test]
#[allow(clippy::float_cmp)]
fn test_union_no_intersects() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 5.0, 0.0 0.0, 5.0 0.0, 5.0 5.0, 0.0 5.0))").unwrap();
let other =
Geometry::from_wkt("POLYGON ((15.0 15.0, 15.0 20.0, 20.0 20.0, 20.0 15.0, 15.0 15.0))")
.unwrap();
let res = geom.union(&other);
assert!(res.is_some());
assert_eq!(res.unwrap().area(), 50.0);
}
#[test]
#[allow(clippy::float_cmp)]
fn test_difference_success() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other = Geometry::from_wkt("POLYGON ((1 -5, 1 1, -5 1, -5 -5, 1 -5))").unwrap();
let res = geom.difference(&other).unwrap();
assert_eq!(res.area(), 99.0);
}
#[test]
fn test_difference_no_gdal_ptr() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 10.0, 0.0 0.0, 10.0 0.0, 10.0 10.0, 0.0 10.0))")
.unwrap();
let other = unsafe { Geometry::lazy_feature_geometry() };
let res = geom.difference(&other);
assert!(res.is_none());
}
#[test]
#[allow(clippy::float_cmp)]
fn test_difference_no_intersects() {
let geom =
Geometry::from_wkt("POLYGON ((0.0 5.0, 0.0 0.0, 5.0 0.0, 5.0 5.0, 0.0 5.0))").unwrap();
let other =
Geometry::from_wkt("POLYGON ((15.0 15.0, 15.0 20.0, 20.0 20.0, 20.0 15.0, 15.0 15.0))")
.unwrap();
let res = geom.difference(&other);
assert_eq!(res.unwrap().area(), 25.0);
}
}