Skip to content

Commit

Permalink
Merge pull request #76 from osm-without-borders/ed2018
Browse files Browse the repository at this point in the history
Migrate to rust 2018
  • Loading branch information
amatissart authored Feb 22, 2019
2 parents 03b5143 + 3df8128 commit 882c2f0
Show file tree
Hide file tree
Showing 12 changed files with 612 additions and 476 deletions.
914 changes: 553 additions & 361 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ keywords = ["osm", "boundary", "geography"]
categories = ["science", "algorithms"]
description = "Provides geographical zones with a structured hierarchy"
readme = "README.md"
edition = "2018"


[badges]
Expand All @@ -19,8 +20,7 @@ log = "0.4"
env_logger = "0.5"
geo = "0.10.2"
geo-types = "0.2.0"
structopt = "0.1"
structopt-derive = "0.1"
structopt = "0.2"
osmpbfreader = "^0.11"
serde = {version = "1", features = ["rc"]}
serde_derive = "1"
Expand Down
41 changes: 12 additions & 29 deletions src/bin/cosmogony.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
extern crate cosmogony;
extern crate env_logger;
extern crate failure;
#[macro_use]
extern crate log;
extern crate serde_json;
extern crate structopt;
#[macro_use]
extern crate structopt_derive;
extern crate flate2;

use cosmogony::cosmogony::Cosmogony;
use cosmogony::{build_cosmogony, file_format::OutputFormat};
use failure::Error;
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use std::io::BufWriter;
use structopt::StructOpt;

use failure::Error;

#[derive(StructOpt, Debug)]
struct Args {
/// OSM PBF file.
Expand All @@ -34,7 +22,7 @@ Accepted extensions are '.json', '.json.gz', '.jsonl', '.jsonl.gz'
'jsonl' is json stream, each line is a zone as json
"#
)]
output: Option<String>,
output: String,
#[structopt(help = "Do not display the stats", long = "no-stats")]
no_stats: bool,
#[structopt(
Expand Down Expand Up @@ -63,7 +51,7 @@ fn to_json_stream(mut writer: impl std::io::Write, cosmogony: &Cosmogony) -> Res
}

// since we don't dump the metadata in json stream for the moment, we log them
info!("metadata: {:?}", &cosmogony.meta);
log::info!("metadata: {:?}", &cosmogony.meta);
Ok(())
}

Expand All @@ -72,7 +60,7 @@ fn serialize_cosmogony(
output_file: String,
format: OutputFormat,
) -> Result<(), Error> {
info!("writing the output file {}", output_file);
log::info!("writing the output file {}", output_file);
let file = File::create(output_file)?;
let stream = BufWriter::new(file);
match format {
Expand All @@ -95,11 +83,7 @@ fn serialize_cosmogony(
}

fn cosmogony(args: Args) -> Result<(), Error> {
let format = if let Some(ref output_filename) = args.output {
OutputFormat::from_filename(&output_filename)?
} else {
OutputFormat::Json
};
let format = OutputFormat::from_filename(&args.output)?;

let cosmogony = build_cosmogony(
args.input,
Expand All @@ -108,14 +92,13 @@ fn cosmogony(args: Args) -> Result<(), Error> {
args.country_code,
)?;

if let Some(output) = args.output {
serialize_cosmogony(&cosmogony, output, format)?;
}
serialize_cosmogony(&cosmogony, args.output, format)?;

if !args.no_stats {
info!(
log::info!(
"Statistics for {}:\n{}",
cosmogony.meta.osm_filename, cosmogony.meta.stats
cosmogony.meta.osm_filename,
cosmogony.meta.stats
);
}
Ok(())
Expand All @@ -135,11 +118,11 @@ fn main() {
let args = Args::from_args();
match cosmogony(args) {
Err(e) => {
error!("cosmogony in error! {:?}", e);
log::error!("cosmogony in error! {:?}", e);
e.iter_chain().for_each(|c| {
error!("{}", c);
log::error!("{}", c);
if let Some(b) = c.backtrace() {
error!(" - {}", b);
log::error!(" - {}", b);
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/cosmogony.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::zone::Zone;
use serde_derive::*;
use std::collections::BTreeMap;
use std::fmt;
use zone::Zone;
extern crate serde;

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Cosmogony {
Expand Down Expand Up @@ -47,7 +47,7 @@ impl CosmogonyStats {
}

impl fmt::Display for CosmogonyStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (level, count) in &self.level_counts {
let wd = self.wikidata_counts.get(level).unwrap_or(&0u64);
writeln!(f, "Admin level {}: {} element(s)", level, count)?;
Expand Down
7 changes: 2 additions & 5 deletions src/country_finder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
extern crate geos;

use std;
use crate::zone::{Zone, ZoneIndex};
use crate::zone_typer::ZoneTyper;
use std::collections::BTreeMap;
use zone::{Zone, ZoneIndex};
use zone_typer::ZoneTyper;

const COUNTRY_CODE_TAG: &str = "ISO3166-1:alpha2";

Expand Down
13 changes: 6 additions & 7 deletions src/hierarchy_builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
extern crate geo;

use crate::mutable_slice::MutableSlice;
use crate::utils::bbox_to_rect;
use crate::zone::{Zone, ZoneIndex};
use gst::rtree::RTree;
use mutable_slice::MutableSlice;
use log::{info, warn};
use std::iter::FromIterator;
use utils::bbox_to_rect;
use zone::{Zone, ZoneIndex};

pub struct ZonesTree {
tree: RTree<ZoneIndex>,
Expand Down Expand Up @@ -115,10 +114,10 @@ pub fn build_hierarchy(zones: &mut [Zone], inclusions: Vec<Vec<ZoneIndex>>) {

#[cfg(test)]
mod test {
use crate::hierarchy_builder::{build_hierarchy, find_inclusions};
use crate::zone::{Zone, ZoneType};
use geo::bounding_rect::BoundingRect;
use geo_types::{Coordinate, LineString, MultiPolygon, Polygon};
use hierarchy_builder::{build_hierarchy, find_inclusions};
use zone::{Zone, ZoneType};

fn zone_factory(idx: usize, ls: LineString<f64>, zone_type: Option<ZoneType>) -> Zone {
let p = Polygon::new(ls, vec![]);
Expand Down
37 changes: 8 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
#[macro_use]
extern crate failure;
extern crate geo;
extern crate geo_types;
extern crate gst;
#[macro_use]
extern crate log;
extern crate ordered_float;
extern crate osm_boundaries_utils;
extern crate osmpbfreader;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate regex;
extern crate serde_yaml;
extern crate structopt;
#[macro_use]
extern crate lazy_static;
extern crate flate2;
extern crate geos;
extern crate rayon;

use log::{debug, info};
pub mod cosmogony;
mod country_finder;
pub mod file_format;
Expand All @@ -29,19 +8,19 @@ mod utils;
pub mod zone;
pub mod zone_typer;

pub use cosmogony::{Cosmogony, CosmogonyMetadata, CosmogonyStats};
use country_finder::CountryFinder;
pub use crate::cosmogony::{Cosmogony, CosmogonyMetadata, CosmogonyStats};
use crate::country_finder::CountryFinder;
use crate::file_format::OutputFormat;
use crate::hierarchy_builder::{build_hierarchy, find_inclusions};
use crate::mutable_slice::MutableSlice;
use failure::Error;
use failure::ResultExt;
use file_format::OutputFormat;
use hierarchy_builder::{build_hierarchy, find_inclusions};
use mutable_slice::MutableSlice;
use osmpbfreader::{OsmObj, OsmPbfReader};
use std::collections::BTreeMap;
use std::fs::File;
use std::path::{Path, PathBuf};

pub use zone::{Zone, ZoneIndex, ZoneType};
pub use crate::zone::{Zone, ZoneIndex, ZoneType};

#[rustfmt::skip]
pub fn is_admin(obj: &OsmObj) -> bool {
Expand Down Expand Up @@ -300,7 +279,7 @@ pub fn load_cosmogony_from_file(input: &str) -> Result<Cosmogony, Error> {
/// if the input file is a json, the whole cosmogony is loaded
pub fn read_zones_from_file(
input: &str,
) -> Result<Box<std::iter::Iterator<Item = Result<Zone, Error>>>, Error> {
) -> Result<Box<dyn std::iter::Iterator<Item = Result<Zone, Error>>>, Error> {
let format = OutputFormat::from_filename(input)?;
let f = std::fs::File::open(&input)?;
let f = std::io::BufReader::new(f);
Expand Down
2 changes: 1 addition & 1 deletion src/mutable_slice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use zone::{Zone, ZoneIndex};
use crate::zone::{Zone, ZoneIndex};

// This struct is necessary to wrap the `zones` slice
// and keep a mutable reference to a zone (and set
Expand Down
4 changes: 0 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
extern crate geo;

use geo_types;
use gst;
use ordered_float::OrderedFloat;

pub fn bbox_to_rect(bbox: &geo_types::Rect<f64>) -> gst::rtree::Rect {
Expand Down
39 changes: 16 additions & 23 deletions src/zone.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
extern crate geo;
extern crate geojson;
extern crate geos;
extern crate itertools;
extern crate regex;
extern crate serde;
extern crate serde_json;

use self::itertools::Itertools;
use self::serde::Serialize;
use crate::mutable_slice::MutableSlice;
use geo::algorithm::bounding_rect::BoundingRect;
use geo_types::{Coordinate, Point, Rect};
use geos::GGeom;
use mutable_slice::MutableSlice;
use itertools::Itertools;
use log::{debug, info, warn};
use osm_boundaries_utils::build_boundary;
use osmpbfreader::objects::{OsmId, OsmObj, Relation, Tags};
use regex::Regex;
use serde::Serialize;
use serde_derive::*;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

Expand Down Expand Up @@ -91,7 +85,7 @@ pub struct Zone {
/// we don't add the international names that are equivalent to the default name
/// to reduce the size of the map
fn get_international_names(tags: &Tags, default_name: &str) -> BTreeMap<String, String> {
lazy_static! {
lazy_static::lazy_static! {
static ref LANG_NAME_REG: Regex = Regex::new("^name:(.+)").unwrap();
}

Expand Down Expand Up @@ -296,14 +290,14 @@ impl Zone {
}

/// iter_hierarchy gives an iterator over the whole hierachy including self
pub fn iter_hierarchy<'a>(&'a self, all_zones: &'a MutableSlice) -> HierarchyIterator<'a> {
pub fn iter_hierarchy<'a>(&'a self, all_zones: &'a MutableSlice<'_>) -> HierarchyIterator<'a> {
HierarchyIterator {
zone: Some(&self),
all_zones: all_zones,
}
}

fn create_lbl<'a, F>(&'a self, all_zones: &'a MutableSlice, f: F) -> String
fn create_lbl<'a, F>(&'a self, all_zones: &'a MutableSlice<'_>, f: F) -> String
where
F: Fn(&Zone) -> String,
{
Expand All @@ -328,7 +322,7 @@ impl Zone {
/// We compute a default label, and a label per language
/// Note: for the moment we use the same format for every language,
/// but in the future we might use opencage's configuration for this
pub fn compute_labels(&mut self, all_zones: &MutableSlice) {
pub fn compute_labels(&mut self, all_zones: &MutableSlice<'_>) {
let label = self.create_lbl(all_zones, |z: &Zone| z.name.clone());

// we compute a label per language
Expand Down Expand Up @@ -425,8 +419,8 @@ where
geojson::Value: From<&'a T>,
S: serde::Serializer,
{
use self::geojson::{GeoJson, Geometry, Value};
use self::serde::Serialize;
use geojson::{GeoJson, Geometry, Value};
use serde::Serialize;

match *multi_polygon_option {
Some(ref multi_polygon) => {
Expand All @@ -440,9 +434,8 @@ fn deserialize_geom<'de, D>(d: D) -> Result<Option<geo::Geometry<f64>>, D::Error
where
D: serde::Deserializer<'de>,
{
use self::geojson;
use self::geojson::conversion::TryInto;
use self::serde::Deserialize;
use geojson::conversion::TryInto;
use serde::Deserialize;

Option::<geojson::GeoJson>::deserialize(d).map(|option| {
option.and_then(|geojson| match geojson {
Expand Down Expand Up @@ -494,7 +487,7 @@ fn serialize_bbox_as_geojson<'a, S>(
where
S: serde::Serializer,
{
use self::geojson::Bbox as GeojsonBbox;
use geojson::Bbox as GeojsonBbox;
match bbox {
Some(b) => {
// bbox serialized as an array
Expand All @@ -511,7 +504,7 @@ fn deserialize_as_rect<'de, D>(d: D) -> Result<Option<Rect<f64>>, D::Error>
where
D: serde::Deserializer<'de>,
{
use self::serde::Deserialize;
use serde::Deserialize;
Option::<Vec<f64>>::deserialize(d).map(|option| match option {
Some(b) => Some(Rect {
min: Coordinate { x: b[0], y: b[1] },
Expand Down Expand Up @@ -553,7 +546,7 @@ impl<'de> serde::de::Visitor<'de> for ZoneIndexVisitor {
})
}

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a zone index")
}
}
Expand Down
Loading

0 comments on commit 882c2f0

Please sign in to comment.