Hack around the double </osm> bug in clip_map.
use std::collections::HashMap;
+use std::fs::File;
+use std::io::{BufRead, BufReader, BufWriter, Write};
use anyhow::Result;
use geo::prelude::Contains;
@@ -35,7 +37,7 @@ fn clip(pbf_path: &str, boundary: &Polygon<f64>, out_path: &str) -> Result<()> {
let mut relations: HashMap<i64, RcRelation> = HashMap::new();
// TODO Buffer?
- let mut reader = osmio::pbf::PBFReader::new(std::fs::File::open(pbf_path)?);
+ let mut reader = osmio::pbf::PBFReader::new(File::open(pbf_path)?);
for obj in reader.objects() {
match obj.object_type() {
OSMObjectType::Node => {
@@ -68,7 +70,7 @@ fn clip(pbf_path: &str, boundary: &Polygon<f64>, out_path: &str) -> Result<()> {
}
// TODO Buffer?
- let mut writer = osmio::xml::XMLWriter::new(std::fs::File::create(out_path)?);
+ let mut writer = osmio::xml::XMLWriter::new(File::create("tmp.osm")?);
// TODO Nondetermistic output because of HashMap!
for (_, node) in nodes {
writer.write_obj(&RcOSMObj::Node(node))?;
@@ -81,6 +83,26 @@ fn clip(pbf_path: &str, boundary: &Polygon<f64>, out_path: &str) -> Result<()> {
}
writer.close()?;
+
+ // TODO osmio seems to have a bug; it's writing 2 </osm> tags. Fix upstream, but in the
+ // meantime, just delete the last line from the file.
+ {
+ let reader = BufReader::new(File::open("tmp.osm")?);
+ let mut writer = BufWriter::new(File::create(out_path)?);
+ let mut last_line: Option<String> = None;
+ for line in reader.lines() {
+ let line = line?;
+ if let Some(l) = last_line.take() {
+ writer.write(l.as_bytes())?;
+ writer.write(b"\n")?;
+ }
+ last_line = Some(line);
+ }
+ // Drop the last line? Except somehow the last line is "</osm>\n</osm>", so...
+ writer.write(b"</osm>")?;
+ std::fs::remove_file("tmp.osm")?;
+ }
+
Ok(())
}
