6个版本 (破坏性更新)

0.5.1 2022年6月25日
0.5.0 2021年2月4日
0.4.0 2020年12月30日
0.3.0 2020年10月22日
0.1.0 2018年11月19日

#1540解析器实现

Download history 258/week @ 2024-04-20 103/week @ 2024-04-27 155/week @ 2024-05-04 161/week @ 2024-05-11 100/week @ 2024-05-18 155/week @ 2024-05-25 255/week @ 2024-06-01 248/week @ 2024-06-08 106/week @ 2024-06-15 174/week @ 2024-06-22 109/week @ 2024-06-29 129/week @ 2024-07-06 150/week @ 2024-07-13 129/week @ 2024-07-20 151/week @ 2024-07-27 121/week @ 2024-08-03

每月579 次下载
2 crates 中使用

MIT 许可证

255KB
1.5K SLoC

rust-topojson

Build status

TopoJSON 为Rust提供的工具

MIT许可证下授权。


lib.rs:

示例

此crate使用serde进行序列化,并允许转换为GeoJson对象。

要开始,请在您的Cargo.toml中添加topojson

[dependencies]
topojson = "*"

读取

use topojson::TopoJson;

let topojson_str = r#"
{
    "arcs": [[[0.0, 0.0], [0.0, 9999.0], [2000.0, 0.0], [0.0, -9999.0], [-2000.0, 0.0]]],
    "objects": {"example ": {
            "type": "GeometryCollection",
            "geometries": [
                {"coordinates": [4000.0, 5000.0],
                 "properties": {"prop0": "value0"},
                 "type": "Point"},
                {"arcs": [[0]],
                 "properties": {"prop0": "value0", "prop1": {"this": "that"}},
                 "type": "Polygon"}
            ]
     }},
    "transform": {"scale": [0.0005, 0.0001], "translate": [100.0, 0.0]},
    "type": "Topology"
}
"#;

let topo = topojson_str.parse::<TopoJson>().unwrap();

写入

TopoJson可以通过调用to_string进行序列化


use topojson::{TopoJson, Geometry, Value, Topology, NamedGeometry};
use serde_json;

let topo = Topology {
    arcs: vec![
        vec![vec![2.2, 2.2], vec![3.3, 3.3]]
    ],
    objects: vec![NamedGeometry {
        name: String::from("example"),
        geometry: Geometry {
            value: Value::LineString(vec![0]),
            bbox: None,
            id: None,
            properties: None,
            foreign_members: None,
        },
    }],
    bbox: None,
    transform: None,
    foreign_members: None,
};

let topojson_string = serde_json::to_string(&topo).unwrap();

转换为GeoJson

TopoJson可以通过使用`to_geojson`函数转换为GeoJson

extern crate geojson;

use topojson::{TopoJson, to_geojson};
use geojson::GeoJson;

let topojson_str = r#"
{
    "arcs": [[[0.0, 0.0], [0.0, 9999.0], [2000.0, 0.0], [0.0, -9999.0], [-2000.0, 0.0]]],
    "objects": {"example": { "type": "GeometryCollection", "geometries": [
        {"coordinates": [4000.0, 5000.0], "properties": {"prop0": "value0"}, "type": "Point"},
        {"arcs": [[0]], "properties": {"prop0": "value0", "prop1": {"this": "that"}}, "type": "Polygon"}]}},
    "transform": {"scale": [0.0005, 0.0001], "translate": [100.0, 0.0]},
    "type": "Topology"
}"#;
// Parse to Topology:
let topo = topojson_str.parse::<TopoJson>().unwrap();

// Conversion to GeoJson FeatureCollection for the "example" object:
let geojson = match topo {
    TopoJson::Topology(t) => {
        to_geojson(&t, &String::from("example"))
            .expect("Unable to convert TopoJSON to GeoJSON")
    },
    _ => unimplemented!(),
};

依赖项

~1.7–2.7MB
~56K SLoC