我有一个 geojson 文档,我想对其执行一些 GEOS 转换,例如:计算交点、从另一个多边形中减去多边形等。
我已经能够创建 geo_types::Polygon
来自文档,但无法将其转换为 GEOS 多边形。 geos 库的文档说这是可能的,但我遇到了编译错误。
use serde_json::{Result, Value};
use geo_geojson;
use geos::from_geo::TryInto;
use geos::{Error, Geometry};
fn main() {
let data = r#"
{
"type" : "Feature",
"properties" : {},
"geometry" : {
"type" : "Polygon",
"coordinates" : [ [ [ -80.2006099057282, 25.7667778809006], [ -80.2005810927863, 25.7667893295156],
[ -80.2005511360631, 25.7667981904308], [ -80.2005203313322, 25.7668043699427], [ -80.2004889842378, 25.7668078025078],
[ -80.2004574067358, 25.766808451653], [ -80.2004259134638, 25.7668063104759], [ -80.2003948180789, 25.7668014017381],
[ -80.2003644296081, 25.7667937775553], [ -80.2003350488499, 25.7667835186779], [ -80.2003069648777, 25.7667707333574],
[ -80.2002804517018, 25.7667555557905], [ -80.2002557651654, 25.7667381441435], [ -80.2002331401646, 25.7667186781729],
[ -80.2002127882898, 25.7666973564876], [ -80.200194895997, 25.7666743935394], [ -80.2001796233871, 25.7666500164743],
[ -80.2001671036392, 25.7666244620256], [ -80.2001574430594, 25.7665979736533], [ -80.2001507216193, 25.7665707991263],
[ -80.2001469937692, 25.7665431886774], [ -80.2001462892496, 25.766515393747], [ -80.2001486136429, 25.7664876661955],
[ -80.200153948486, 25.7664602577369], [ -80.2001622509086, 25.7664334192908], [ -80.2001734529129, 25.7664073999664],
[ -80.2001874605259, 25.7663824454984], [ -80.2002041531028, 25.7663587960835], [ -80.2002233830273, 25.7663366837049],
[ -80.2002449759842, 25.7663163291135], [ -80.2002687318761, 25.7662979386737], [ -80.2002944263789, 25.7662817012691],
[ -80.2003218130656, 25.7662677854259], [ -80.2003506260038, 25.7662563367582], [ -80.2003805827209, 25.7662474758012],
[ -80.2004113874437, 25.7662412962597], [ -80.2004427345288, 25.766237863678], [ -80.2004743120208, 25.7662372145296],
[ -80.200505805283, 25.7662393557171], [ -80.2005369006592, 25.7662442644785], [ -80.2005672891229, 25.7662518886976],
[ -80.2005966698762, 25.7662621476228], [ -80.200624753846, 25.7662749330014], [ -80.2006512670223, 25.7662901106348],
[ -80.2006759535611, 25.7663075223545], [ -80.2006985785666, 25.7663269884019], [ -80.200718930447, 25.7663483101652],
[ -80.2007368227461, 25.7663712731897], [ -80.2007520953618, 25.7663956503261], [ -80.2007646151143, 25.7664212048378],
[ -80.2007742756976, 25.766447693262], [ -80.2007809971394, 25.7664748678266], [ -80.20078472499, 25.766502478297],
[ -80.2007854295097, 25.7665302732315], [ -80.2007831051166, 25.7665580007696], [ -80.2007777702746, 25.7665854091976],
[ -80.2007694678545, 25.7666122475983], [ -80.2007582658544, 25.7666382668644], [ -80.2007442582467, 25.7666632212646],
[ -80.2007275656759, 25.7666868706053], [ -80.2007083357575, 25.7667089829062], [ -80.2006867428059, 25.7667293374198],
[ -80.2006629869175, 25.7667477277848], [ -80.200637292416, 25.7667639651196], [ -80.2006099057282, 25.7667778809006]]]
}
}"#;
// Parse the string of data into serde_json::Value.
let serialized: Value = serde_json::from_str(data).unwrap();
let collection: geo_types::GeometryCollection<f64> = geo_geojson::from_str(&serialized.to_string()).unwrap();
for geom in collection {
let poly = geom.into_polygon().unwrap();
let converted_poly: geos::Geometry = (&poly).try_into().expect("failed conversion");
}
}
我希望它能编译并得到 converted_poly
成为geos::Polygon
.相反,我从编译器那里得到这个:
could not find from_geo in geos
no method named try_into found for type &geo_types::polygon::Polygon<f64> in the current scope
import
和 try_into
在 the documentation for the geos crate 的第一页上引用了调用在“从 rust-geo 转换”部分下。
最佳答案
from_geo
模块 is behind a feature flag :
#[cfg(any(feature = "geo", feature = "dox"))]
pub mod from_geo;
当您将箱子添加到您的 Cargo.toml 时,您需要指定该功能:
[dependencies]
geos = { version = "5.0.0", features = ["geo"] }
您还应该将问题提交给 crate 以记录这一点。
另见:
关于rust - 无法将 geo_type::Polygon 转换为 geos::Polygon:在 geos 中找不到 from_geo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477992/