如何在rust中迭代geos::Polygon的外环中的点

gjmwrych  于 2023-05-07  发布在  Go
关注(0)|答案(1)|浏览(124)

我有一个表示多边形的几何体,我想迭代外环中的点。我试着遵循与this question相同的方法,但它似乎在rust中不起作用。
get_exterior_ring函数返回LinearRing类型的ConstGeometry。如果在此几何上调用get_num_points,则会返回错误

Err(
    GenericError(
        "Geometry must be a LineString",
    ),
)
let test_polygon: Polygon = Polygon::new(
  LineString(vec![
    Coord::from((4.9147899, 52.3735245)),
    Coord::from((4.9148563, 52.3735048)),
    Coord::from((4.9148865, 52.3735437)),
    Coord::from((4.9148248, 52.3735613)),
    Coord::from((4.9147899, 52.3735245)),
  ]),
  vec![LineString(vec![])],
);
let geometry: geos::Geometry = (&test_polygon).try_into().unwrap();
let linear_ring: ConstGeometry  = geometry.get_exterior_ring().unwrap();
dbg!(linear_ring.get_num_points().err());
ac1kyiln

ac1kyiln1#

在此回答:https://github.com/georust/geos/issues/132

use geos::{ConstGeometry, CoordSeq, Geom};

let test_polygon: Polygon = Polygon::new(
    LineString(vec![
        Coord::from((4.9147899, 52.3735245)),
        Coord::from((4.9148563, 52.3735048)),
        Coord::from((4.9148865, 52.3735437)),
        Coord::from((4.9148248, 52.3735613)),
        Coord::from((4.9147899, 52.3735245)),
    ]),
    vec![LineString(vec![])],
);
let geometry: geos::Geometry = (&test_polygon).try_into().unwrap();
let linear_ring: ConstGeometry  = geometry.get_exterior_ring().unwrap();
let coord_seq: CoordSeq = linear_ring.get_coord_seq().unwrap();

let size = coord_seq.size().unwrap();
println!("coord_seq size: {}", size);

for i in 0..size {
    let (x, y) = (coord_seq.get_x(i).unwrap(), coord_seq.get_y(i).unwrap());
    println!("coord of point n°{}: {:?}", i, (x, y));
}

相关问题