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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
use crate::context_handle::PtrWrap;
use crate::error::{Error, GResult};
use crate::{
AsRaw, AsRawMut, ContextHandle, ContextHandling, ContextInteractions, CoordDimensions,
Geometry, Ordinate,
};
use geos_sys::*;
use std::convert::TryFrom;
use std::sync::Arc;
#[cfg(feature = "v3_10_0")]
type AsArrayOutput = (Vec<f64>, Vec<f64>, Option<Vec<f64>>, Option<Vec<f64>>);
/// `CoordSeq` represents a list of coordinates inside a [`Geometry`].
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::OneD)
/// .expect("failed to create CoordSeq");
/// coords.set_x(0, 10.);
/// assert_eq!(coords.get_x(0), Ok(10.));
/// ```
pub struct CoordSeq {
pub(crate) ptr: PtrWrap<*mut GEOSCoordSequence>,
pub(crate) context: Arc<ContextHandle>,
nb_dimensions: usize,
nb_lines: usize,
}
impl CoordSeq {
/// Creates a new `CoordSeq`.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coord_seq = CoordSeq::new(2, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
///
/// // Then you fill the positions of your `coord_seq`:
/// let positions: &[(f64, f64, f64)] = &[(0., 0., 0.), (1., 2., 1.)];
/// for (pos, (x, y, z)) in positions.into_iter().enumerate() {
/// coord_seq.set_x(pos, *x).expect("failed to set x...");
/// coord_seq.set_y(pos, *y).expect("failed to set y...");
/// coord_seq.set_z(pos, *z).expect("failed to set z...");
/// }
/// assert_eq!(coord_seq.get_z(1), Ok(1.));
///
/// // An example with 2 dimensions (and 3 lines) as well:
/// let mut coord_seq2 = CoordSeq::new(3, CoordDimensions::TwoD)
/// .expect("failed to create CoordSeq");
/// let positions2: &[(f64, f64)] = &[(0., 0.), (1., 2.), (14., 5.)];
/// for (pos, (x, y)) in positions2.into_iter().enumerate() {
/// coord_seq2.set_x(pos, *x).expect("failed to set x...");
/// coord_seq2.set_y(pos, *y).expect("failed to set y...");
/// }
/// assert_eq!(coord_seq2.get_x(1), Ok(1.));
/// ```
pub fn new(size: u32, dims: CoordDimensions) -> GResult<CoordSeq> {
let context_handle = ContextHandle::init_e(Some("CoordSeq::new"))?;
unsafe {
let ptr = GEOSCoordSeq_create_r(context_handle.as_raw(), size, dims.into());
CoordSeq::new_from_raw(ptr, Arc::new(context_handle), size, dims.into(), "new")
}
}
/// Creates a new `CoordSeq`.
///
/// # Example
///
/// ```
/// use geos::CoordSeq;
///
/// let coords = CoordSeq::new_from_vec(&[&[0., 1.], &[2., 3.]])
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.get_y(1), Ok(3.));
///
/// // Doing it from a Vec<Vec<f64>>.
/// let positions = vec![vec![0., 1.], vec![2., 3.]];
/// let s_positions = positions.iter().map(|x| x.as_slice()).collect::<Vec<_>>();
/// let coords = CoordSeq::new_from_vec(&s_positions)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.get_y(1), Ok(3.));
///
/// // All vectors don't have the same length, this is an error!
/// assert!(CoordSeq::new_from_vec(&[vec![0., 1.], vec![3.]]).is_err());
///
/// // An empty vector is an error as well since we can't figure out its dimensions!
/// let x: &[f64] = &[];
/// assert!(CoordSeq::new_from_vec(&[x]).is_err());
/// ```
pub fn new_from_vec<T: AsRef<[f64]>>(data: &[T]) -> GResult<CoordSeq> {
let size = data.len();
if size > 0 {
let dims = data[0].as_ref().len();
if let Err(e) = CoordDimensions::try_from(dims as u32) {
return Err(Error::GenericError(e.to_owned()));
}
if !data.iter().skip(1).all(|x| x.as_ref().len() == dims) {
return Err(Error::GenericError(
"All vec entries must have the same size!".into(),
));
}
match ContextHandle::init_e(Some("CoordSeq::new_from_vec")) {
Ok(context_handle) => unsafe {
let ptr = GEOSCoordSeq_create_r(context_handle.as_raw(), size as _, dims as _);
CoordSeq::new_from_raw(
ptr,
Arc::new(context_handle),
size as _,
dims as _,
"new_from_vec",
)
},
Err(e) => return Err(e),
}
.and_then(|mut coord| {
let raw_context = coord.get_raw_context();
let raw_coord = coord.as_raw_mut();
let funcs = [
GEOSCoordSeq_setX_r,
GEOSCoordSeq_setY_r,
GEOSCoordSeq_setZ_r,
];
for (line, line_data) in data.iter().enumerate() {
for (pos, elem) in line_data.as_ref().iter().enumerate() {
unsafe {
if funcs[pos](raw_context, raw_coord, line as _, *elem) == 0 {
let err = format!(
"Failed to set value at position {pos} on line {line}",
);
return Err(Error::GenericError(err));
}
}
}
}
Ok(coord)
})
} else {
Err(Error::GenericError(
"Can't determine dimension for the CoordSeq".to_owned(),
))
}
}
/// Creates a new `CoordSeq` from an interleaved coordinate buffer.
///
/// # Parameters
///
/// - `data`: The data buffer as a contiguous f64 slice.
/// - `size`: The number of coordinates in the provided data buffer.
/// - `has_z`: Whether the data buffer contains `z` coordinates. If `false`, the coordinate
/// buffer must be either `XY` or `XYM`. If `true`, the coordinate buffer must be either
/// `XYZ` or `XYZM`.
/// - `has_m`: Whether the data buffer contains `m` coordinates. If `false`, the coordinate
/// buffer must be either `XY` or `XYZ`. If `true`, the coordinate buffer must be either
/// `XYM` or `XYZM`.
///
/// # Example
///
/// ```
/// use geos::CoordSeq;
///
/// let buffer = vec![0., 1., 2., 3., 4., 5.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 3, false, false)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.get_y(1), Ok(3.));
/// assert_eq!(coords.get_x(2), Ok(4.));
/// ```
#[cfg(feature = "v3_10_0")]
pub fn new_from_buffer(
data: &[f64],
size: usize,
has_z: bool,
has_m: bool,
) -> GResult<CoordSeq> {
let mut dims: u32 = 2;
if has_z {
dims += 1;
}
if has_m {
dims += 1;
}
assert_eq!(data.len(), size * dims as usize, "Incorrect buffer length");
let context_handle = ContextHandle::init_e(Some("CoordSeq::new_from_buffer"))?;
unsafe {
let ptr = GEOSCoordSeq_copyFromBuffer_r(
context_handle.as_raw(),
data.as_ptr(),
size as _,
has_z as _,
has_m as _,
);
CoordSeq::new_from_raw(
ptr,
Arc::new(context_handle),
size as _,
dims,
"new_from_buffer",
)
}
}
/// Creates a new `CoordSeq` from separated coordinate buffers.
///
/// # Parameters
///
/// - `x`: A slice of x coordinates.
/// - `y`: A slice of y coordinates.
/// - `z`: An optional slice of z coordinates.
/// - `m`: An optional slice of m coordinates.
///
/// # Example
///
/// ```
/// use geos::CoordSeq;
///
/// let x = vec![0., 2., 4.];
/// let y = vec![1., 3., 5.];
/// let coords = CoordSeq::new_from_arrays(&x, &y, None, None)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.get_y(1), Ok(3.));
/// assert_eq!(coords.get_x(2), Ok(4.));
/// ```
#[cfg(feature = "v3_10_0")]
pub fn new_from_arrays(
x: &[f64],
y: &[f64],
z: Option<&[f64]>,
m: Option<&[f64]>,
) -> GResult<CoordSeq> {
assert_eq!(x.len(), y.len(), "Arrays have different lengths.");
let mut dims: u32 = 2;
let z_ptr = if let Some(z) = z {
assert_eq!(x.len(), z.len(), "Arrays have different lengths.");
dims += 1;
z.as_ptr()
} else {
std::ptr::null()
};
let m_ptr = if let Some(m) = m {
assert_eq!(x.len(), m.len(), "Arrays have different lengths.");
dims += 1;
m.as_ptr()
} else {
std::ptr::null()
};
let context_handle = ContextHandle::init_e(Some("CoordSeq::new_from_arrays"))?;
unsafe {
let ptr = GEOSCoordSeq_copyFromArrays_r(
context_handle.as_raw(),
x.as_ptr(),
y.as_ptr(),
z_ptr,
m_ptr,
x.len() as _,
);
CoordSeq::new_from_raw(
ptr,
Arc::new(context_handle),
x.len() as u32,
dims,
"new_from_buffer",
)
}
}
pub(crate) unsafe fn new_from_raw(
ptr: *mut GEOSCoordSequence,
context: Arc<ContextHandle>,
size: u32,
dims: u32,
caller: &str,
) -> GResult<CoordSeq> {
if ptr.is_null() {
let extra = if let Some(x) = context.get_last_error() {
format!("\nLast error: {x}")
} else {
String::new()
};
return Err(Error::NoConstructionFromNullPtr(format!(
"CoordSeq::{caller}{extra}",
)));
}
Ok(CoordSeq {
ptr: PtrWrap(ptr),
context,
nb_dimensions: dims as _,
nb_lines: size as _,
})
}
/// Sets the X position value at the given `line`.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::OneD)
/// .expect("failed to create CoordSeq");
/// coords.set_x(0, 10.);
/// assert_eq!(coords.get_x(0), Ok(10.));
/// ```
pub fn set_x(&mut self, line: usize, val: f64) -> GResult<()> {
assert!(line < self.nb_lines);
let ret_val = unsafe {
GEOSCoordSeq_setX_r(self.get_raw_context(), self.as_raw_mut(), line as _, val)
};
if ret_val == 0 {
Err(Error::GeosError("impossible to set x for coord".into()))
} else {
Ok(())
}
}
/// Sets the Y position value at the given `line`.
///
/// Note: your `CoordSeq` object must have at least two dimensions!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::TwoD)
/// .expect("failed to create CoordSeq");
/// coords.set_y(0, 10.);
/// assert_eq!(coords.get_y(0), Ok(10.));
/// ```
pub fn set_y(&mut self, line: usize, val: f64) -> GResult<()> {
assert!(line < self.nb_lines);
assert!(self.nb_dimensions >= 2);
let ret_val = unsafe {
GEOSCoordSeq_setY_r(self.get_raw_context(), self.as_raw_mut(), line as _, val)
};
if ret_val == 0 {
Err(Error::GeosError("impossible to set y for coord".into()))
} else {
Ok(())
}
}
/// Sets the Z position value at the given `line`.
///
/// Note: your `CoordSeq` object must have three dimensions!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// coords.set_z(0, 10.);
/// assert_eq!(coords.get_z(0), Ok(10.));
/// ```
pub fn set_z(&mut self, line: usize, val: f64) -> GResult<()> {
assert!(line < self.nb_lines);
assert!(self.nb_dimensions >= 3);
let ret_val = unsafe {
GEOSCoordSeq_setZ_r(self.get_raw_context(), self.as_raw_mut(), line as _, val)
};
if ret_val == 0 {
Err(Error::GeosError("impossible to set z for coord".into()))
} else {
Ok(())
}
}
/// Sets the value at the given `ordinate` (aka position).
///
/// Note: your `CoordSeq` object must have enough dimensions to set at the given `ordinate`!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq, Ordinate};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// coords.set_ordinate(0, Ordinate::Z, 10.);
/// assert_eq!(coords.get_z(0), Ok(10.));
/// assert_eq!(coords.get_ordinate(0, Ordinate::Z), Ok(10.));
/// ```
pub fn set_ordinate(&mut self, line: usize, ordinate: Ordinate, val: f64) -> GResult<()> {
let ordinate: u32 = ordinate.into();
assert!(line < self.nb_lines);
assert!(self.nb_dimensions > ordinate as _);
let ret_val = unsafe {
GEOSCoordSeq_setOrdinate_r(
self.get_raw_context(),
self.as_raw_mut(),
line as _,
ordinate,
val,
)
};
if ret_val == 0 {
Err(Error::GeosError(format!(
"impossible to set value for ordinate {ordinate}",
)))
} else {
Ok(())
}
}
/// Gets the X position value at the given `line`.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::OneD)
/// .expect("failed to create CoordSeq");
/// coords.set_x(0, 10.);
/// assert_eq!(coords.get_x(0), Ok(10.));
/// ```
pub fn get_x(&self, line: usize) -> GResult<f64> {
assert!(line < self.nb_lines);
let mut n = 0.;
let ret_val = unsafe {
GEOSCoordSeq_getX_r(self.get_raw_context(), self.as_raw(), line as _, &mut n)
};
if ret_val == 0 {
Err(Error::GeosError(
"failed to get coordinates from CoordSeq".into(),
))
} else {
Ok(n as _)
}
}
/// Gets the Y position value at the given `line`.
///
/// Note: your `CoordSeq` object must have at least two dimensions!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::TwoD)
/// .expect("failed to create CoordSeq");
/// coords.set_y(0, 10.);
/// assert_eq!(coords.get_y(0), Ok(10.));
/// ```
pub fn get_y(&self, line: usize) -> GResult<f64> {
assert!(line < self.nb_lines);
assert!(self.nb_dimensions >= 2);
let mut n = 0.;
let ret_val = unsafe {
GEOSCoordSeq_getY_r(self.get_raw_context(), self.as_raw(), line as _, &mut n)
};
if ret_val == 0 {
Err(Error::GeosError(
"failed to get coordinates from CoordSeq".into(),
))
} else {
Ok(n as _)
}
}
/// Gets the Z position value at the given `line`.
///
/// Note: your `CoordSeq` object must have three dimensions!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// coords.set_z(0, 10.);
/// assert_eq!(coords.get_z(0), Ok(10.));
/// ```
pub fn get_z(&self, line: usize) -> GResult<f64> {
assert!(line < self.nb_lines);
assert!(self.nb_dimensions >= 3);
let mut n = 0.;
let ret_val = unsafe {
GEOSCoordSeq_getZ_r(self.get_raw_context(), self.as_raw(), line as _, &mut n)
};
if ret_val == 0 {
Err(Error::GeosError(
"failed to get coordinates from CoordSeq".into(),
))
} else {
Ok(n as _)
}
}
/// Gets the entire `CoordSeq` object as an interleaved buffer.
///
/// # Parameters:
///
/// - `dims`: Optionally, the number of dimensions to include in the output buffer. If `None`,
/// will be inferred from the number of dimensions on the geometry. A user may want to
/// override this for performance because GEOS always stores coordinates internally with 3 or
/// 4 dimensions. So copying 2-dimensional GEOS coordinates to a 3-dimensional output buffer
/// is slightly faster (a straight `memcpy`) than copying 2-dimensionalal GEOS coordinates to
/// a 2-dimensional output buffer (iterating over every coordinate and copying only the XY
/// values).
///
/// # Example
///
/// ```
/// use geos::CoordSeq;
///
/// // Create a two-dimensional `CoordSeq` from a buffer with three points
/// let buffer = vec![0., 1., 2., 3., 4., 5.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 3, false, false)
/// .expect("failed to create CoordSeq");
///
/// // Return an output buffer, inferring the number of coordinates (2)
/// let output_buffer = coords.as_buffer(None)
/// .expect("failed to get buffer");
///
/// let expected_output_buffer = vec![0., 1., 2., 3., 4., 5.];
/// assert_eq!(output_buffer, expected_output_buffer);
/// ```
///
/// You can also force GEOS to return a 3D buffer from 2D coordinates
///
/// ```
/// use geos::CoordSeq;
///
/// let buffer = vec![0., 1., 2., 3., 4., 5.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 3, false, false)
/// .expect("failed to create CoordSeq");
///
/// // Return an output buffer, forcing it to be 3-dimensional
/// let output_buffer = coords.as_buffer(Some(3))
/// .expect("failed to get buffer");
///
/// let expected_output_buffer = vec![0., 1., f64::NAN, 2., 3., f64::NAN, 4., 5., f64::NAN];
/// assert_eq!(output_buffer[0], expected_output_buffer[0]);
/// assert_eq!(output_buffer[3], expected_output_buffer[3]);
/// ```
///
/// You can also force GEOS to return a 2D buffer from 3D coordinates
///
/// ```
/// use geos::CoordSeq;
///
/// let buffer = vec![0., 1., 100., 2., 3., 200.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 2, true, false)
/// .expect("failed to create CoordSeq");
///
/// // Return an output buffer, forcing it to be 2-dimensional
/// let output_buffer = coords.as_buffer(Some(2))
/// .expect("failed to get buffer");
///
/// let expected_output_buffer = vec![0., 1., 2., 3.];
/// assert_eq!(output_buffer, expected_output_buffer);
/// ```
#[cfg(feature = "v3_10_0")]
pub fn as_buffer(&self, dims: Option<usize>) -> GResult<Vec<f64>> {
let size = self.nb_lines;
let dims = dims.unwrap_or(self.nb_dimensions);
let has_z = dims >= 3;
let has_m = dims >= 4;
let mut output_buffer = vec![0.; size * dims];
unsafe {
GEOSCoordSeq_copyToBuffer_r(
self.get_raw_context(),
self.as_raw(),
output_buffer.as_mut_ptr(),
has_z as _,
has_m as _,
);
}
Ok(output_buffer)
}
/// Gets the entire `CoordSeq` object as individual coordinate arrays.
///
/// Returns a tuple with four vectors. The first and second vectors correspond to `x` and `y`
/// coordinates. The third corresponds to an optional third dimension if it exists (`z` or
/// `m`). The fourth corresponds to an optional fourth dimension if it exists (`m` in the case
/// of an `XYZM` geometry).
///
/// # Example
///
/// ```
/// use geos::CoordSeq;
///
/// let buffer = vec![0., 1., 2., 3., 4., 5.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 3, false, false)
/// .expect("failed to create CoordSeq");
///
/// let output_arrays = coords.as_arrays()
/// .expect("failed to get arrays");
///
/// assert_eq!(output_arrays.0, vec![0., 2., 4.]);
/// assert_eq!(output_arrays.1, vec![1., 3., 5.]);
/// assert_eq!(output_arrays.2, None);
/// assert_eq!(output_arrays.3, None);
/// ```
///
/// If the `CoordSeq` contains three dimensions, the third dimension will be returned as the
/// third value in the tuple:
///
/// ```
/// use geos::CoordSeq;
///
/// let buffer = vec![0., 1., 100., 3., 4., 200., 6., 7., 300.];
/// let coords = CoordSeq::new_from_buffer(&buffer, 3, true, false)
/// .expect("failed to create CoordSeq");
///
/// let output_arrays = coords.as_arrays()
/// .expect("failed to get arrays");
///
/// assert_eq!(output_arrays.0, vec![0., 3., 6.]);
/// assert_eq!(output_arrays.1, vec![1., 4., 7.]);
/// assert_eq!(output_arrays.2, Some(vec![100., 200., 300.]));
/// assert_eq!(output_arrays.3, None);
/// ```
#[cfg(feature = "v3_10_0")]
pub fn as_arrays(&self) -> GResult<AsArrayOutput> {
let size = self.nb_lines;
let mut x = vec![0.; size];
let mut y = vec![0.; size];
let mut z = if self.nb_dimensions == 3 {
Some(vec![0.; size])
} else {
None
};
let mut m = if self.nb_dimensions == 4 {
Some(vec![0.; size])
} else {
None
};
unsafe {
GEOSCoordSeq_copyToArrays_r(
self.get_raw_context(),
self.as_raw(),
x.as_mut_ptr(),
y.as_mut_ptr(),
z.as_mut()
.map(|arr| arr.as_mut_ptr())
.unwrap_or(std::ptr::null_mut()),
m.as_mut()
.map(|arr| arr.as_mut_ptr())
.unwrap_or(std::ptr::null_mut()),
);
}
Ok((x, y, z, m))
}
/// Gets the value at the given `ordinate` (aka position).
///
/// Note: your `CoordSeq` object must have enough dimensions to access the given `ordinate`!
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq, Ordinate};
///
/// let mut coords = CoordSeq::new(1, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// coords.set_ordinate(0, Ordinate::Z, 10.);
/// assert_eq!(coords.get_z(0), Ok(10.));
/// assert_eq!(coords.get_ordinate(0, Ordinate::Z), Ok(10.));
/// ```
pub fn get_ordinate(&self, line: usize, ordinate: Ordinate) -> GResult<f64> {
let ordinate: u32 = ordinate.into();
let mut val = 0f64;
assert!(line < self.nb_lines);
assert!(self.nb_dimensions > ordinate as _);
if unsafe {
GEOSCoordSeq_getOrdinate_r(
self.get_raw_context(),
self.as_raw(),
line as _,
ordinate,
&mut val,
)
} != 1
{
Err(Error::GeosError("getting size from CoordSeq".into()))
} else {
Ok(val)
}
}
/// Returns the number of lines of the `CoordSeq` object.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let coords = CoordSeq::new(2, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.size(), Ok(2));
///
/// let coords = CoordSeq::new_from_vec(&[&[1f64], &[2.], &[3.], &[4.]])
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.size(), Ok(4));
/// ```
pub fn size(&self) -> GResult<usize> {
let mut n = 0;
let ret_val =
unsafe { GEOSCoordSeq_getSize_r(self.get_raw_context(), self.as_raw(), &mut n) };
if ret_val == 0 {
Err(Error::GeosError("getting size from CoordSeq".into()))
} else {
Ok(n as usize)
}
}
/// Returns the number of lines of the `CoordSeq` object.
///
/// Note: This is an alias to the [`size`](#method.size) method.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let coords = CoordSeq::new(2, CoordDimensions::ThreeD)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.number_of_lines(), Ok(2));
///
/// let coords = CoordSeq::new_from_vec(&[&[1f64], &[2.], &[3.], &[4.]])
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.number_of_lines(), Ok(4));
/// ```
pub fn number_of_lines(&self) -> GResult<usize> {
self.size()
}
/// Returns the number of dimensions of the `CoordSeq` object.
///
/// # Example
///
/// ```
/// use geos::{CoordDimensions, CoordSeq};
///
/// let coords = CoordSeq::new(2, CoordDimensions::OneD)
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.dimensions(), Ok(CoordDimensions::OneD));
///
/// let coords = CoordSeq::new_from_vec(&[&[1., 2.], &[3. ,4.]])
/// .expect("failed to create CoordSeq");
/// assert_eq!(coords.dimensions(), Ok(CoordDimensions::TwoD));
/// ```
pub fn dimensions(&self) -> GResult<CoordDimensions> {
let mut n = 0;
let ret_val =
unsafe { GEOSCoordSeq_getDimensions_r(self.get_raw_context(), self.as_raw(), &mut n) };
if ret_val == 0 {
Err(Error::GeosError("getting dimensions from CoordSeq".into()))
} else {
Ok(CoordDimensions::try_from(n).expect("Failed to convert to CoordDimensions"))
}
}
/// Returns `true` if the geometry has a counter-clockwise orientation.
///
/// Available using the `v3_7_0` feature.
#[cfg(any(feature = "v3_7_0", feature = "dox"))]
pub fn is_ccw(&self) -> GResult<bool> {
unsafe {
let mut is_ccw = 0;
if GEOSCoordSeq_isCCW_r(self.get_raw_context(), self.as_raw(), &mut is_ccw) != 1 {
Err(Error::GenericError(
"GEOSCoordSeq_isCCW_r failed".to_owned(),
))
} else {
Ok(is_ccw == 1)
}
}
}
/// 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 point");
///
/// assert_eq!(geom.to_wkt().unwrap(), "POINT (1.0000000000000000 2.0000000000000000)");
/// ```
pub fn create_point(self) -> GResult<Geometry> {
Geometry::create_point(self)
}
/// 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 line string");
///
/// assert_eq!(geom.to_wkt().unwrap(),
/// "LINESTRING (1.0000000000000000 2.0000000000000000, \
/// 3.0000000000000000 4.0000000000000000)");
/// ```
pub fn create_line_string(self) -> GResult<Geometry> {
Geometry::create_line_string(self)
}
/// Creates a linear ring geometry.
pub fn create_linear_ring(self) -> GResult<Geometry> {
Geometry::create_linear_ring(self)
}
}
unsafe impl Send for CoordSeq {}
unsafe impl Sync for CoordSeq {}
impl Drop for CoordSeq {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
unsafe { GEOSCoordSeq_destroy_r(self.get_raw_context(), self.as_raw_mut()) };
}
}
impl Clone for CoordSeq {
/// Also pass the context to the newly created `CoordSeq`.
fn clone(&self) -> CoordSeq {
let ptr = unsafe { GEOSCoordSeq_clone_r(self.get_raw_context(), self.as_raw()) };
if ptr.is_null() {
panic!("Couldn't clone CoordSeq...");
}
CoordSeq {
ptr: PtrWrap(ptr),
context: self.clone_context(),
nb_dimensions: self.nb_dimensions,
nb_lines: self.nb_lines,
}
}
}
impl ContextInteractions for CoordSeq {
/// Set the context handle to the `CoordSeq`.
///
/// ```
/// use geos::{ContextInteractions, CoordDimensions, CoordSeq, ContextHandle};
///
/// let context_handle = ContextHandle::init().expect("invalid init");
/// context_handle.set_notice_message_handler(Some(Box::new(|s| println!("new message: {}", s))));
/// let mut coord_seq = CoordSeq::new(2, CoordDimensions::TwoD).expect("failed to create CoordSeq");
/// coord_seq.set_context_handle(context_handle);
/// ```
fn set_context_handle(&mut self, context: ContextHandle) {
self.context = Arc::new(context);
}
/// Get the context handle of the `CoordSeq`.
///
/// ```
/// use geos::{ContextInteractions, CoordDimensions, CoordSeq};
///
/// let coord_seq = CoordSeq::new(2, CoordDimensions::TwoD).expect("failed to create CoordSeq");
/// let context = coord_seq.get_context_handle();
/// context.set_notice_message_handler(Some(Box::new(|s| println!("new message: {}", s))));
/// ```
fn get_context_handle(&self) -> &ContextHandle {
&self.context
}
}
impl AsRaw for CoordSeq {
type RawType = GEOSCoordSequence;
fn as_raw(&self) -> *const Self::RawType {
*self.ptr
}
}
impl AsRawMut for CoordSeq {
type RawType = GEOSCoordSequence;
unsafe fn as_raw_mut_override(&self) -> *mut Self::RawType {
*self.ptr
}
}
impl ContextHandling for CoordSeq {
type Context = Arc<ContextHandle>;
fn get_raw_context(&self) -> GEOSContextHandle_t {
self.context.as_raw()
}
fn clone_context(&self) -> Arc<ContextHandle> {
Arc::clone(&self.context)
}
}