Skip to content

Commit

Permalink
Output as GeoJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Mar 28, 2021
1 parent 1961956 commit 0ec7209
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use geo::{map_coords::MapCoordsInplace, LineString, Point, Polygon};
use geojson::{GeoJson, Feature, FeatureCollection, Geometry};
use std::convert::TryInto;
use std::default::Default;

Expand Down Expand Up @@ -39,7 +40,7 @@ pub fn clockboard(
centerpoint: Point<f64>,
params: Params,
//boundary: Option<Polygon<f64>>,
) -> Vec<Polygon<f64>> {
) -> GeoJson {
let mut polygons = Vec::new();
for i in params.distances {
// println!("{}", i); // debugging
Expand All @@ -51,7 +52,25 @@ pub fn clockboard(
round(polygon, params.precision);
}

polygons
let mut features: Vec<Feature> = polygons
.iter()
.map(|poly| Feature {
bbox: None,
geometry: Some(Geometry::from(poly)),
id: None,
properties: None,
foreign_members: None,
})
.collect();

let fc = FeatureCollection {
bbox: None,
features,
foreign_members: None,
};

let gj = GeoJson::from(fc);
gj
}

fn makecircle(centerpoint: Point<f64>, radius: f64, num_vertices: usize) -> Polygon<f64> {
Expand Down
16 changes: 9 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use geo::Point;
// use serde_json::to_string_pretty;
use serde_json;
use serde_json::{to_string_pretty};
use zonebuilder::clockboard;
use zonebuilder::Params;

fn main() {
let polygon_list = clockboard(Point::new(0.0, 0.0), Params::default());
let gj = clockboard(Point::new(0.0, 0.0), Params::default());

// Attempt to print pretty json - not outputting valid json currently
// See https://github.com/georust/geojson/issues/161 for details
// let geojson_list = geojson::Value::from(&polygon_list[0]);
// let result = serde_json::to_string_pretty(&geojson_list);
// println!("{}", result.unwrap());

// Brute force approach: for loop
for polygon in polygon_list {
let result = geojson::Value::from(&polygon);
println!("{}", result);
}
let gjstring = to_string_pretty(&gj);

// Which we can print / dump / etc:

println!("{}", &gjstring);

}

0 comments on commit 0ec7209

Please sign in to comment.