Expand description

Examples

This crate uses serde for serialization and allows conversion to GeoJson objects.

To get started, add topojson to your Cargo.toml:

[dependencies]
topojson = "*"

Reading

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();

Writing

TopoJson can then be serialized by calling 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();

Converting to GeoJson

TopoJson can then be converted to GeoJson using the to_geojson` function:

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!(),
};

Structs

Geometry Objects

One member of the ‘objects’ member of a Topology

Topology object

Transforms

Enums

Error when reading to TopoJson

TopoJSON Objects (either Topology or Geometry)

The underlying Geometry value (which may contain Position or Arc indexes)

Functions

Convert a TopoJSON Topology object to a GeoJSON Feature collection.

Type Definitions

Arcs (an array of position which may have been quantized and delta-encoded)

Arc indexes (an array of indexes)

Bounding Boxes

Positions