【问题标题】:Unable to convert geo_type::Polygon into geos::Polygon: could not find from_geo in geos无法将 geo_type::Polygon 转换为 geos::Polygon:在 geos 中找不到 from_geo
【发布时间】:2019-08-13 22:02:02
【问题描述】:

我有一个 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 &amp;geo_types::polygon::Polygon&lt;f64&gt; in the current scope

importtry_into 调用均在 the documentation for the geos crate 的第一页“从 rust-geo 转换”部分下引用。

【问题讨论】:

  • 请粘贴您收到的准确和完整的错误 - 这将有助于我们了解问题所在,以便我们提供最佳帮助。有时试图解释错误消息很棘手,实际上错误消息的不同部分很重要。

标签: rust geojson geos


【解决方案1】:

from_geo 模块is behind a feature flag

#[cfg(any(feature = "geo", feature = "dox"))]
pub mod from_geo;

您需要在将 crate 添加到 Cargo.toml 时指定该功能:

[dependencies]
geos = { version = "5.0.0", features = ["geo"] }

您还应该向 crate 提交问题以记录这一点。

另见:

【讨论】:

  • 非常感谢!这确实解决了问题!我对 Rust 很陌生,所以这特别令人困惑。
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多