use std::sync::Arc;
use geo::{BooleanOps, BoundingRect, Buffer, Intersects};
use crate::layers::{Layer, mark_foreign, take_owner};
use crate::source::input::{DataSource, InputFeature};
use crate::tile::feature::PropValue;
use super::buildings::ProcessedFeature;
const MASK_MARGIN_DEG: f64 = 0.005;
const INNER_MARGIN_DEG: f64 = 0.02;
const CLAIM_MARGIN_DEG: f64 = 0.02;
#[derive(Debug)]
pub struct IndiaMask {
land: geo::MultiPolygon<f64>,
inner: geo::MultiPolygon<f64>,
claim_land: geo::MultiPolygon<f64>,
bounds: geo::Rect<f64>,
claim_bounds: geo::Rect<f64>,
}
impl IndiaMask {
pub fn from_features(features: &[InputFeature]) -> Option<Self> {
let pieces: Vec<geo::Polygon<f64>> = features
.iter()
.flat_map(|f| closed_rings(&f.geometry))
.map(|ring| geo::Polygon::new(ring, vec![]))
.collect();
if pieces.is_empty() {
return None;
}
let pieces = geo::MultiPolygon::new(pieces);
let land = pieces.buffer(MASK_MARGIN_DEG);
let inner = pieces.buffer(-INNER_MARGIN_DEG);
let claim_land = pieces.buffer(CLAIM_MARGIN_DEG);
let bounds = land.bounding_rect()?;
let claim_bounds = claim_land.bounding_rect()?;
Some(Self {
land,
inner,
claim_land,
bounds,
claim_bounds,
})
}
pub fn clip(&self, feature: ProcessedFeature) -> Option<ProcessedFeature> {
let (feature, foreign) = take_owner(feature);
match feature.layer {
Layer::Places => self.clip_label(feature, foreign),
Layer::Boundaries if carries_claim(&feature) => self.clip_claim_line(feature),
Layer::Boundaries if is_country(&feature) => self.clip_country_line(feature),
Layer::Boundaries => self.clip_admin_line(feature, foreign),
_ => Some(feature),
}
}
fn clip_label(&self, feature: ProcessedFeature, foreign: bool) -> Option<ProcessedFeature> {
use geo::Contains;
if !foreign {
return Some(feature);
}
match feature.geometry.as_ref() {
geo::Geometry::Point(point)
if self.bounds.contains(point) && self.land.contains(point) =>
{
None
}
_ => Some(feature),
}
}
fn clip_country_line(&self, feature: ProcessedFeature) -> Option<ProcessedFeature> {
let lines = match feature.geometry.as_ref() {
geo::Geometry::LineString(line) => geo::MultiLineString::new(vec![line.clone()]),
geo::Geometry::MultiLineString(lines) => lines.clone(),
_ => return Some(feature),
};
match lines.bounding_rect() {
Some(rect) if rect.intersects(&self.bounds) => {}
_ => return Some(feature),
}
let outside = self.land.clip(&lines, true);
if outside.0.is_empty() {
return None;
}
Some(ProcessedFeature {
geometry: Arc::new(geo::Geometry::MultiLineString(outside)),
..feature
})
}
fn clip_claim_line(&self, feature: ProcessedFeature) -> Option<ProcessedFeature> {
let lines = match feature.geometry.as_ref() {
geo::Geometry::LineString(line) => geo::MultiLineString::new(vec![line.clone()]),
geo::Geometry::MultiLineString(lines) => lines.clone(),
_ => return Some(feature),
};
match lines.bounding_rect() {
Some(rect) if rect.intersects(&self.claim_bounds) => {}
_ => return Some(feature),
}
let outside = self.claim_land.clip(&lines, true);
if outside.0.is_empty() {
return None;
}
Some(ProcessedFeature {
geometry: Arc::new(geo::Geometry::MultiLineString(outside)),
..feature
})
}
fn clip_admin_line(
&self,
feature: ProcessedFeature,
foreign: bool,
) -> Option<ProcessedFeature> {
if !foreign {
return Some(feature);
}
let lines = match feature.geometry.as_ref() {
geo::Geometry::LineString(line) => geo::MultiLineString::new(vec![line.clone()]),
geo::Geometry::MultiLineString(lines) => lines.clone(),
_ => return Some(feature),
};
match lines.bounding_rect() {
Some(rect) if rect.intersects(&self.bounds) => {}
_ => return Some(feature),
}
let outside = self.inner.clip(&lines, true);
if outside.0.is_empty() {
return None;
}
Some(ProcessedFeature {
geometry: Arc::new(geo::Geometry::MultiLineString(outside)),
..feature
})
}
}
fn carries_claim(feature: &ProcessedFeature) -> bool {
feature.properties.iter().any(|(k, v)| {
matches!(k.as_str(), "disputed_by" | "claimed_by")
|| (k == "disputed" && *v == PropValue::Bool(true))
})
}
fn closed_rings(geometry: &geo::Geometry<f64>) -> Vec<geo::LineString<f64>> {
let is_ring = |line: &&geo::LineString<f64>| line.is_closed() && line.0.len() >= 4;
match geometry {
geo::Geometry::LineString(line) => [line].into_iter().filter(is_ring).cloned().collect(),
geo::Geometry::MultiLineString(lines) => lines.0.iter().filter(is_ring).cloned().collect(),
geo::Geometry::Polygon(polygon) => vec![polygon.exterior().clone()],
geo::Geometry::MultiPolygon(polygons) => {
polygons.0.iter().map(|p| p.exterior().clone()).collect()
}
_ => Vec::new(),
}
}
fn is_country(feature: &ProcessedFeature) -> bool {
feature
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
}
const INDIA_BBOX: [f64; 4] = [68.0, 6.5, 97.5, 37.2];
pub fn process(feature: &InputFeature) -> Option<ProcessedFeature> {
match &feature.source {
DataSource::Overture { .. } => process_overture(feature),
DataSource::NaturalEarth { .. } => process_ne(feature),
DataSource::Osm { .. } => process_osm(feature),
DataSource::IndiaBoundary => process_india(feature),
_ => None,
}
}
fn process_overture(feature: &InputFeature) -> Option<ProcessedFeature> {
let (kind, admin_level, min_zoom) = overture_subtype_to_kind(feature.get_str("subtype")?)?;
let disputed = matches!(
feature.properties.get("is_disputed"),
Some(PropValue::Bool(true))
);
let sort_rank = if disputed { 288 } else { 289 };
let mut properties = Vec::with_capacity(4);
properties.push(("kind".into(), PropValue::Str(kind.into())));
properties.push(("kind_detail".into(), PropValue::I64(admin_level)));
if disputed {
properties.push(("disputed".into(), PropValue::Bool(true)));
}
if feature
.get_str("country")
.is_some_and(|c| !c.eq_ignore_ascii_case("IN"))
{
mark_foreign(&mut properties);
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Boundaries,
min_zoom,
max_zoom: 15,
sort_rank,
properties,
})
}
fn process_ne(feature: &InputFeature) -> Option<ProcessedFeature> {
let source_layer = feature.ne_source_layer()?;
let is_boundary_layer = source_layer.starts_with("ne_10m_admin_0_boundary")
|| source_layer.starts_with("ne_10m_admin_1");
if !is_boundary_layer {
return None;
}
let featurecla = feature.get_str("featurecla").unwrap_or("");
let (kind, admin_level, disputed) = match featurecla {
"International boundary (verify)" => ("country", 2i64, false),
"Disputed (please verify)" => ("country", 2, true),
"Line of control (please verify)" => ("country", 2, true),
"Indefinite (please verify)" => ("country", 2, true),
"Lease limit" => ("country", 2, true),
"Overlay limit" => ("country", 2, true),
"Admin-1 boundary" => ("region", 4, false),
"Admin-1 statistical boundary" => ("region", 4, false),
"Admin-1 region boundary" => ("region", 4, false),
"Unrecognized" => ("country", 2, true),
_ if featurecla.contains("disputed") || featurecla.contains("Disputed") => {
("country", 2, true)
}
_ if featurecla.contains("Admin-1") => ("region", 4, false),
_ => ("country", 2, false),
};
let sort_rank = if disputed { 288 } else { 289 };
let mut properties = Vec::with_capacity(4);
properties.push(("kind".into(), PropValue::Str(kind.into())));
properties.push(("kind_detail".into(), PropValue::I64(admin_level)));
if disputed {
properties.push(("disputed".into(), PropValue::Bool(true)));
}
if kind == "region"
&& feature
.get_str("adm0_a3")
.is_some_and(|c| !c.eq_ignore_ascii_case("IND"))
{
mark_foreign(&mut properties);
}
let ne_min_zoom = feature
.get_f64("min_zoom")
.map(|v| (v as u8).min(5))
.or_else(|| feature.get_i64("min_zoom").map(|v| (v as u8).min(5)))
.unwrap_or(0);
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Boundaries,
min_zoom: ne_min_zoom,
max_zoom: 5,
sort_rank,
properties,
})
}
fn process_osm(feature: &InputFeature) -> Option<ProcessedFeature> {
if !feature.can_be_line() {
return None;
}
let relations = feature.osm_relations();
if relations.is_empty() {
if feature.has_tag("boundary", "administrative") || feature.has_tag("boundary", "disputed")
{
let admin_level = feature
.get_str("admin_level")
.and_then(|v| v.parse::<i64>().ok())
.unwrap_or(2);
let disputed = feature.has_tag("boundary", "disputed")
|| feature.get_str("disputed").is_some_and(|v| v == "yes")
|| feature
.get_str("boundary")
.is_some_and(|v| v == "disputed" || v == "claim");
if admin_level == 2 && is_within_india_bbox(feature) && !disputed {
return None;
}
let (kind, min_zoom) = admin_level_to_kind(admin_level)?;
let sort_rank = if disputed { 288 } else { 289 };
let mut properties = Vec::with_capacity(4);
properties.push(("kind".into(), PropValue::Str(kind.into())));
properties.push(("kind_detail".into(), PropValue::I64(admin_level)));
if disputed {
properties.push(("disputed".into(), PropValue::Bool(true)));
}
push_claim_tags(feature, &mut properties);
return Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Boundaries,
min_zoom,
max_zoom: 15,
sort_rank,
properties,
});
}
return None;
}
let best_relation = relations
.iter()
.filter(|r| {
r.tags.get("type").is_some_and(|t| t == "boundary")
|| r.tags
.get("boundary")
.is_some_and(|b| b == "administrative" || b == "disputed")
})
.min_by_key(|r| r.admin_level.unwrap_or(99));
let rel = best_relation?;
let admin_level = i64::from(rel.admin_level.unwrap_or(2));
let disputed = rel.disputed
|| rel
.tags
.get("boundary")
.is_some_and(|v| v == "disputed" || v == "claim")
|| rel.tags.get("disputed").is_some_and(|v| v == "yes");
if admin_level == 2 && is_within_india_bbox(feature) && !disputed {
return None;
}
let (kind, min_zoom) = admin_level_to_kind(admin_level)?;
let sort_rank = if disputed { 288 } else { 289 };
let mut properties = Vec::with_capacity(5);
properties.push(("kind".into(), PropValue::Str(kind.into())));
properties.push(("kind_detail".into(), PropValue::I64(admin_level)));
if disputed {
properties.push(("disputed".into(), PropValue::Bool(true)));
}
push_claim_tags(feature, &mut properties);
if relations
.iter()
.filter_map(|r| relation_country(&r.tags))
.any(|code| code != "IN")
{
mark_foreign(&mut properties);
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Boundaries,
min_zoom,
max_zoom: 15,
sort_rank,
properties,
})
}
fn push_claim_tags(feature: &InputFeature, properties: &mut Vec<(String, PropValue)>) {
for key in ["disputed_by", "claimed_by"] {
if let Some(value) = feature.get_str(key) {
properties.push((key.into(), PropValue::Str(value.to_string())));
}
}
}
fn relation_country(tags: &std::collections::HashMap<String, String>) -> Option<String> {
let code = ["ISO3166-1:alpha2", "ISO3166-1"]
.iter()
.find_map(|key| tags.get(*key))
.or_else(|| tags.get("ISO3166-2"))?;
let country = code.split(['-', ';']).next()?.trim().to_ascii_uppercase();
(country.len() == 2).then_some(country)
}
fn process_india(feature: &InputFeature) -> Option<ProcessedFeature> {
let kind = feature.get_str("kind").unwrap_or("country");
let kind_detail = feature.get_i64("kind_detail").unwrap_or(2);
let mut properties = Vec::with_capacity(3);
properties.push(("kind".into(), PropValue::Str(kind.to_string())));
properties.push(("kind_detail".into(), PropValue::I64(kind_detail)));
if let Some(name) = feature.get_str("name") {
properties.push(("name".into(), PropValue::Str(name.to_string())));
}
Some(ProcessedFeature {
id: feature.id,
geometry: feature.geometry.clone(),
layer: Layer::Boundaries,
min_zoom: 0,
max_zoom: 15,
sort_rank: 289,
properties,
})
}
fn overture_subtype_to_kind(subtype: &str) -> Option<(&'static str, i64, u8)> {
match subtype {
"country" | "dependency" => Some(("country", 2, 6)),
"macroregion" | "region" => Some(("region", 4, 6)),
"macrocounty" | "county" => Some(("county", 6, 8)),
"localadmin" | "locality" | "borough" => Some(("locality", 8, 10)),
_ => None,
}
}
const fn admin_level_to_kind(admin_level: i64) -> Option<(&'static str, u8)> {
match admin_level {
2 => Some(("country", 6)),
3 | 4 => Some(("region", 6)),
5 | 6 => Some(("county", 8)),
7 | 8 => Some(("locality", 10)),
_ => None,
}
}
fn is_within_india_bbox(feature: &InputFeature) -> bool {
if let Some(bbox) = feature.bbox {
let cx = (bbox[0] + bbox[2]) / 2.0;
let cy = (bbox[1] + bbox[3]) / 2.0;
return (INDIA_BBOX[0]..=INDIA_BBOX[2]).contains(&cx)
&& (INDIA_BBOX[1]..=INDIA_BBOX[3]).contains(&cy);
}
match feature.geometry.as_ref() {
geo::Geometry::LineString(ls) => {
if let Some(coord) = ls.0.first() {
return coord.x >= INDIA_BBOX[0]
&& coord.x <= INDIA_BBOX[2]
&& coord.y >= INDIA_BBOX[1]
&& coord.y <= INDIA_BBOX[3];
}
}
geo::Geometry::Point(pt) => {
return pt.x() >= INDIA_BBOX[0]
&& pt.x() <= INDIA_BBOX[2]
&& pt.y() >= INDIA_BBOX[1]
&& pt.y() <= INDIA_BBOX[3];
}
_ => {}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
use crate::source::input::{DataSource, OsmElementType, RelationInfo};
use std::collections::HashMap;
use std::sync::Arc;
fn props_map(props: &[(&str, PropValue)]) -> HashMap<String, PropValue> {
let mut m = HashMap::new();
for (k, v) in props {
m.insert(k.to_string(), v.clone());
}
m
}
fn line_geom() -> Arc<geo::Geometry<f64>> {
Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
])))
}
fn make_feature(props: &[(&str, PropValue)]) -> InputFeature {
InputFeature {
id: 1,
geometry: line_geom(),
properties: props_map(props),
bbox: None,
source: DataSource::Overture {
theme: String::new(),
otype: String::new(),
},
}
}
fn make_ne_feature(layer: &str, props: &[(&str, PropValue)]) -> InputFeature {
InputFeature {
id: 1,
geometry: line_geom(),
properties: props_map(props),
bbox: None,
source: DataSource::NaturalEarth {
source_layer: layer.to_string(),
},
}
}
fn make_osm_way(props: &[(&str, PropValue)], relations: Vec<RelationInfo>) -> InputFeature {
InputFeature {
id: 1,
geometry: line_geom(),
properties: props_map(props),
bbox: None,
source: DataSource::Osm {
element_type: OsmElementType::Way,
relations,
},
}
}
fn make_india_feature(props: &[(&str, PropValue)]) -> InputFeature {
InputFeature {
id: 1,
geometry: line_geom(),
properties: props_map(props),
bbox: None,
source: DataSource::IndiaBoundary,
}
}
fn rel_with(admin_level: Option<i32>, disputed: bool, tags: &[(&str, &str)]) -> RelationInfo {
let mut t = HashMap::new();
for (k, v) in tags {
t.insert(k.to_string(), v.to_string());
}
RelationInfo {
id: 1,
admin_level,
disputed,
role: String::new(),
tags: t,
}
}
#[test]
fn dispatch_unsupported_source_returns_none() {
let feat = InputFeature {
id: 1,
geometry: line_geom(),
properties: HashMap::new(),
bbox: None,
source: DataSource::LandPolygons,
};
assert!(process(&feat).is_none());
let feat2 = InputFeature {
source: DataSource::WaterPolygons,
..feat
};
assert!(process(&feat2).is_none());
}
fn kind_of(result: &ProcessedFeature) -> Option<&PropValue> {
result
.properties
.iter()
.find(|(k, _)| k == "kind")
.map(|(_, v)| v)
}
fn detail_of(result: &ProcessedFeature) -> Option<&PropValue> {
result
.properties
.iter()
.find(|(k, _)| k == "kind_detail")
.map(|(_, v)| v)
}
#[test]
fn overture_county_with_relative_admin_level_2_is_not_a_country() {
let feat = make_feature(&[
("subtype", PropValue::Str("county".into())),
("admin_level", PropValue::I64(2)),
]);
let result = process(&feat).unwrap();
assert_eq!(kind_of(&result), Some(&PropValue::Str("county".into())));
assert_eq!(detail_of(&result), Some(&PropValue::I64(6)));
assert_eq!(result.min_zoom, 8);
}
#[test]
fn overture_country_with_relative_admin_level_1_is_kept() {
let feat = make_feature(&[
("subtype", PropValue::Str("country".into())),
("admin_level", PropValue::I64(1)),
]);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Boundaries);
assert_eq!(kind_of(&result), Some(&PropValue::Str("country".into())));
assert_eq!(detail_of(&result), Some(&PropValue::I64(2)));
assert_eq!(result.min_zoom, 6);
assert_eq!(result.sort_rank, 289);
}
#[test]
fn overture_region_with_relative_admin_level_1_is_kept() {
let feat = make_feature(&[
("subtype", PropValue::Str("region".into())),
("admin_level", PropValue::I64(1)),
]);
let result = process(&feat).unwrap();
assert_eq!(kind_of(&result), Some(&PropValue::Str("region".into())));
assert_eq!(detail_of(&result), Some(&PropValue::I64(4)));
}
#[test]
fn overture_subtype_levels_follow_the_osm_scale() {
let cases = [
("dependency", "country", 2, 6),
("macroregion", "region", 4, 6),
("macrocounty", "county", 6, 8),
("localadmin", "locality", 8, 10),
("locality", "locality", 8, 10),
("borough", "locality", 8, 10),
];
for (subtype, kind, level, zoom) in cases {
let feat = make_feature(&[("subtype", PropValue::Str(subtype.into()))]);
let result = process(&feat).unwrap();
assert_eq!(
kind_of(&result),
Some(&PropValue::Str(kind.into())),
"{subtype}"
);
assert_eq!(
detail_of(&result),
Some(&PropValue::I64(level)),
"{subtype}"
);
assert_eq!(result.min_zoom, zoom, "{subtype}");
}
}
#[test]
fn overture_neighborhood_boundaries_are_dropped() {
for subtype in ["macrohood", "neighborhood", "microhood"] {
let feat = make_feature(&[("subtype", PropValue::Str(subtype.into()))]);
assert!(process(&feat).is_none(), "{subtype}");
}
}
#[test]
fn overture_boundary_without_subtype_is_dropped() {
let feat = make_feature(&[("admin_level", PropValue::I64(2))]);
assert!(process(&feat).is_none());
}
#[test]
fn overture_disputed_boolean_lowers_sort_rank() {
let feat = make_feature(&[
("subtype", PropValue::Str("country".into())),
("is_disputed", PropValue::Bool(true)),
]);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "disputed" && *v == PropValue::Bool(true))
);
}
#[test]
fn overture_undisputed_boolean_keeps_default_sort_rank() {
let feat = make_feature(&[
("subtype", PropValue::Str("country".into())),
("is_disputed", PropValue::Bool(false)),
]);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 289);
assert!(!result.properties.iter().any(|(k, _)| k == "disputed"));
}
#[test]
fn ne_non_boundary_layer_returns_none() {
let feat = make_ne_feature("ne_10m_lakes", &[]);
assert!(process(&feat).is_none());
}
#[test]
fn ne_featurecla_values_map_to_kind_level_and_disputed() {
let cases = [
("Disputed (please verify)", "country", 2, true, 288),
("Line of control (please verify)", "country", 2, true, 288),
("Indefinite (please verify)", "country", 2, true, 288),
("Lease limit", "country", 2, true, 288),
("Overlay limit", "country", 2, true, 288),
("Unrecognized", "country", 2, true, 288),
("Some weird disputed thing", "country", 2, true, 288),
("Something Disputed weird", "country", 2, true, 288),
("Admin-1 boundary", "region", 4, false, 289),
("Admin-1 statistical boundary", "region", 4, false, 289),
("Admin-1 region boundary", "region", 4, false, 289),
("Custom Admin-1 thing", "region", 4, false, 289),
];
for (featurecla, kind, level, disputed, rank) in cases {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[("featurecla", PropValue::Str(featurecla.into()))],
);
let result = process(&feat).expect(featurecla);
let prop = |key: &str| {
result
.properties
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v.clone())
};
assert_eq!(
prop("kind"),
Some(PropValue::Str(kind.into())),
"{featurecla}"
);
assert_eq!(
prop("kind_detail"),
Some(PropValue::I64(level)),
"{featurecla}"
);
assert_eq!(
prop("disputed"),
disputed.then_some(PropValue::Bool(true)),
"{featurecla}"
);
assert_eq!(result.sort_rank, rank, "{featurecla}");
}
}
#[test]
fn ne_admin_0_international_boundary() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[(
"featurecla",
PropValue::Str("International boundary (verify)".into()),
)],
);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Boundaries);
assert_eq!(result.max_zoom, 5);
assert_eq!(result.sort_rank, 289);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
);
}
#[test]
fn ne_unknown_default_country() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[("featurecla", PropValue::Str("Mystery line".into()))],
);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
);
assert_eq!(result.sort_rank, 289);
}
#[test]
fn ne_missing_featurecla_default_country() {
let feat = make_ne_feature("ne_10m_admin_0_boundary_lines_land", &[]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
);
}
#[test]
fn ne_min_zoom_from_f64_clamped() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[
("featurecla", PropValue::Str("Mystery".into())),
("min_zoom", PropValue::F64(8.0)),
],
);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 5);
}
#[test]
fn ne_min_zoom_from_f64_within_range() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[
("featurecla", PropValue::Str("Mystery".into())),
("min_zoom", PropValue::F64(3.0)),
],
);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 3);
}
#[test]
fn ne_min_zoom_from_i64() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[
("featurecla", PropValue::Str("Mystery".into())),
("min_zoom", PropValue::I64(2)),
],
);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 2);
}
#[test]
fn ne_min_zoom_default_zero() {
let feat = make_ne_feature(
"ne_10m_admin_0_boundary_lines_land",
&[("featurecla", PropValue::Str("Mystery".into()))],
);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 0);
}
#[test]
fn osm_non_line_returns_none() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0))),
properties: HashMap::new(),
bbox: None,
source: DataSource::Osm {
element_type: OsmElementType::Way,
relations: vec![],
},
};
assert!(process(&feat).is_none());
}
#[test]
fn osm_no_relations_no_tag_returns_none() {
let feat = make_osm_way(&[], vec![]);
assert!(process(&feat).is_none());
}
#[test]
fn osm_no_relations_with_admin_tag() {
let feat = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("4".into())),
],
vec![],
);
let result = process(&feat).unwrap();
assert_eq!(result.min_zoom, 6);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("region".into()))
);
}
#[test]
fn osm_no_relations_missing_or_unparseable_admin_level_is_a_country() {
let missing = vec![("boundary", PropValue::Str("administrative".into()))];
let mut unparseable = missing.clone();
unparseable.push(("admin_level", PropValue::Str("not-a-number".into())));
for tags in [missing, unparseable] {
let result = process(&make_osm_way(&tags, vec![])).expect("a boundary");
assert_eq!(
kind_of(&result),
Some(&PropValue::Str("country".into())),
"{tags:?}"
);
}
}
#[test]
fn osm_no_relations_dropped_admin_level_returns_none() {
let feat = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("9".into())),
],
vec![],
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_no_relations_boundary_disputed_tag() {
let feat = make_osm_way(&[("boundary", PropValue::Str("disputed".into()))], vec![]);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "disputed" && *v == PropValue::Bool(true))
);
}
#[test]
fn osm_no_relations_disputed_yes_tag() {
let feat = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("2".into())),
("disputed", PropValue::Str("yes".into())),
],
vec![],
);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
}
#[test]
fn osm_no_relations_boundary_claim() {
let feat = make_osm_way(
&[
("boundary", PropValue::Str("claim".into())),
("admin_level", PropValue::Str("2".into())),
],
vec![],
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_no_relations_india_level_2_suppressed() {
let mut feat = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("2".into())),
],
vec![],
);
feat.bbox = Some([78.0, 20.0, 80.0, 22.0]); assert!(process(&feat).is_none());
}
#[test]
fn osm_no_relations_india_level_2_disputed_kept() {
let mut feat = make_osm_way(
&[
("boundary", PropValue::Str("disputed".into())),
("admin_level", PropValue::Str("2".into())),
],
vec![],
);
feat.bbox = Some([78.0, 20.0, 80.0, 22.0]);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
}
#[test]
fn osm_no_relations_india_level_4_kept() {
let mut feat = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("4".into())),
],
vec![],
);
feat.bbox = Some([78.0, 20.0, 80.0, 22.0]);
assert!(process(&feat).is_some());
}
#[test]
fn osm_relations_picks_lowest_admin_level() {
let feat = make_osm_way(
&[],
vec![
rel_with(Some(6), false, &[("type", "boundary")]),
rel_with(Some(2), false, &[("type", "boundary")]),
rel_with(Some(4), false, &[("type", "boundary")]),
],
);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
);
}
#[test]
fn osm_relations_filter_boundary_administrative() {
let feat = make_osm_way(
&[],
vec![rel_with(Some(4), false, &[("boundary", "administrative")])],
);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("region".into()))
);
}
#[test]
fn osm_relations_filter_boundary_disputed() {
let feat = make_osm_way(
&[],
vec![rel_with(Some(2), false, &[("boundary", "disputed")])],
);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
}
#[test]
fn osm_relations_no_match_returns_none() {
let feat = make_osm_way(
&[],
vec![rel_with(Some(2), false, &[("type", "multipolygon")])],
);
assert!(process(&feat).is_none());
}
#[test]
fn osm_relations_admin_level_default_when_missing() {
let feat = make_osm_way(&[], vec![rel_with(None, false, &[("type", "boundary")])]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind_detail" && *v == PropValue::I64(2))
);
}
#[test]
fn osm_relations_dropped_admin_level_returns_none() {
let feat = make_osm_way(&[], vec![rel_with(Some(9), false, &[("type", "boundary")])]);
assert!(process(&feat).is_none());
}
#[test]
fn osm_relations_disputed_by_flag_claim_or_disputed_tag() {
let relations = [
rel_with(Some(2), true, &[("type", "boundary")]),
rel_with(
Some(2),
false,
&[("type", "boundary"), ("boundary", "claim")],
),
rel_with(Some(2), false, &[("type", "boundary"), ("disputed", "yes")]),
];
for rel in relations {
let tags = format!("{:?}", rel.tags);
let result = process(&make_osm_way(&[], vec![rel])).expect("a boundary");
assert_eq!(result.sort_rank, 288, "{tags}");
}
}
#[test]
fn osm_relations_india_level_2_suppressed() {
let mut feat = make_osm_way(&[], vec![rel_with(Some(2), false, &[("type", "boundary")])]);
feat.bbox = Some([78.0, 20.0, 80.0, 22.0]);
assert!(process(&feat).is_none());
}
#[test]
fn osm_relations_india_level_2_disputed_kept() {
let mut feat = make_osm_way(&[], vec![rel_with(Some(2), true, &[("type", "boundary")])]);
feat.bbox = Some([78.0, 20.0, 80.0, 22.0]);
let result = process(&feat).unwrap();
assert_eq!(result.sort_rank, 288);
}
#[test]
fn osm_relations_disputed_by_property_added() {
let feat = make_osm_way(
&[("disputed_by", PropValue::Str("CountryX".into()))],
vec![rel_with(Some(2), true, &[("type", "boundary")])],
);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "disputed_by" && *v == PropValue::Str("CountryX".into()))
);
}
#[test]
fn osm_relations_claimed_by_property_added() {
let feat = make_osm_way(
&[("claimed_by", PropValue::Str("CountryY".into()))],
vec![rel_with(Some(2), true, &[("type", "boundary")])],
);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "claimed_by" && *v == PropValue::Str("CountryY".into()))
);
}
fn square_mask() -> IndiaMask {
let ring = geo::LineString::from(vec![
(0.0, 0.0),
(10.0, 0.0),
(10.0, 10.0),
(0.0, 10.0),
(0.0, 0.0),
]);
let feat = InputFeature {
geometry: Arc::new(geo::Geometry::MultiLineString(geo::MultiLineString::new(
vec![ring],
))),
..make_india_feature(&[])
};
IndiaMask::from_features(&[feat]).unwrap()
}
fn country_line(coords: Vec<(f64, f64)>, disputed: bool) -> ProcessedFeature {
let mut props = vec![("subtype", PropValue::Str("country".into()))];
if disputed {
props.push(("is_disputed", PropValue::Bool(true)));
}
let mut feat = make_feature(&props);
feat.geometry = Arc::new(geo::Geometry::LineString(geo::LineString::from(coords)));
process(&feat).unwrap()
}
fn total_length(feature: &ProcessedFeature) -> f64 {
use geo::{Euclidean, Length};
match feature.geometry.as_ref() {
geo::Geometry::MultiLineString(m) => Euclidean.length(m),
geo::Geometry::LineString(l) => Euclidean.length(l),
other => panic!("unexpected geometry {other:?}"),
}
}
#[test]
fn india_mask_keeps_only_the_country_line_outside_india() {
let line = country_line(vec![(-5.0, 5.0), (15.0, 5.0)], false);
let clipped = square_mask().clip(line).unwrap();
let expected = 2.0 * (5.0 - MASK_MARGIN_DEG);
assert!((total_length(&clipped) - expected).abs() < 1e-6);
}
#[test]
fn india_mask_drops_country_lines_inside_india() {
let mask = square_mask();
for disputed in [false, true] {
let line = country_line(vec![(2.0, 2.0), (8.0, 8.0)], disputed);
assert!(mask.clip(line).is_none(), "disputed={disputed}");
}
}
#[test]
fn india_mask_drops_a_copy_of_the_border_inside_the_margin() {
let line = country_line(vec![(-0.001, 2.0), (-0.001, 8.0)], false);
assert!(square_mask().clip(line).is_none());
}
#[test]
fn india_mask_leaves_regions_and_far_lines_alone() {
let mask = square_mask();
let mut region = make_feature(&[("subtype", PropValue::Str("region".into()))]);
region.geometry = Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(2.0, 2.0),
(8.0, 8.0),
])));
let region = process(®ion).unwrap();
let kept = mask.clip(region.clone()).unwrap();
assert!(Arc::ptr_eq(&kept.geometry, ®ion.geometry));
let far = country_line(vec![(40.0, 40.0), (50.0, 50.0)], false);
let kept = mask.clip(far.clone()).unwrap();
assert!(Arc::ptr_eq(&kept.geometry, &far.geometry));
}
#[test]
fn india_mask_needs_a_closed_ring() {
assert!(IndiaMask::from_features(&[make_india_feature(&[])]).is_none());
}
#[test]
fn india_default_kind_and_detail() {
let feat = make_india_feature(&[]);
let result = process(&feat).unwrap();
assert_eq!(result.layer, Layer::Boundaries);
assert_eq!(result.min_zoom, 0);
assert_eq!(result.max_zoom, 15);
assert_eq!(result.sort_rank, 289);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("country".into()))
);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind_detail" && *v == PropValue::I64(2))
);
}
#[test]
fn india_custom_kind_and_detail() {
let feat = make_india_feature(&[
("kind", PropValue::Str("region".into())),
("kind_detail", PropValue::I64(4)),
]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind" && *v == PropValue::Str("region".into()))
);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "kind_detail" && *v == PropValue::I64(4))
);
}
#[test]
fn india_with_name_property() {
let feat = make_india_feature(&[("name", PropValue::Str("India".into()))]);
let result = process(&feat).unwrap();
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "name" && *v == PropValue::Str("India".into()))
);
}
#[test]
fn admin_level_to_kind_all_branches() {
assert_eq!(admin_level_to_kind(2), Some(("country", 6)));
assert_eq!(admin_level_to_kind(3), Some(("region", 6)));
assert_eq!(admin_level_to_kind(4), Some(("region", 6)));
assert_eq!(admin_level_to_kind(5), Some(("county", 8)));
assert_eq!(admin_level_to_kind(6), Some(("county", 8)));
assert_eq!(admin_level_to_kind(7), Some(("locality", 10)));
assert_eq!(admin_level_to_kind(8), Some(("locality", 10)));
assert_eq!(admin_level_to_kind(0), None);
assert_eq!(admin_level_to_kind(1), None);
assert_eq!(admin_level_to_kind(9), None);
assert_eq!(admin_level_to_kind(100), None);
assert_eq!(admin_level_to_kind(-1), None);
}
#[test]
fn india_bbox_centroid_inside() {
let feat = InputFeature {
id: 1,
geometry: line_geom(),
properties: HashMap::new(),
bbox: Some([77.0, 20.0, 79.0, 22.0]),
source: DataSource::IndiaBoundary,
};
assert!(is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_centroid_outside() {
let feat = InputFeature {
id: 1,
geometry: line_geom(),
properties: HashMap::new(),
bbox: Some([0.0, 0.0, 1.0, 1.0]),
source: DataSource::IndiaBoundary,
};
assert!(!is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_linestring_inside() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(78.0, 20.0),
(79.0, 22.0),
]))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_linestring_outside() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(0.0, 0.0),
(1.0, 1.0),
]))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(!is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_empty_linestring() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::LineString(geo::LineString::from(Vec::<(
f64,
f64,
)>::new(
)))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(!is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_point_inside() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::Point(geo::Point::new(78.0, 20.0))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_point_outside() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::Point(geo::Point::new(0.0, 0.0))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(!is_within_india_bbox(&feat));
}
#[test]
fn india_bbox_fallback_other_geom() {
let feat = InputFeature {
id: 1,
geometry: Arc::new(geo::Geometry::Polygon(geo::Polygon::new(
geo::LineString::from(vec![(78.0, 20.0), (79.0, 20.0), (79.0, 21.0), (78.0, 20.0)]),
vec![],
))),
properties: HashMap::new(),
bbox: None,
source: DataSource::IndiaBoundary,
};
assert!(!is_within_india_bbox(&feat));
}
fn overture_line(
subtype: &str,
coords: Vec<(f64, f64)>,
extra: &[(&str, PropValue)],
) -> ProcessedFeature {
let mut props = vec![("subtype", PropValue::Str(subtype.into()))];
props.extend(extra.iter().cloned());
let mut feat = make_feature(&props);
feat.geometry = Arc::new(geo::Geometry::LineString(geo::LineString::from(coords)));
process(&feat).unwrap()
}
fn country(code: &str) -> (&'static str, PropValue) {
("country", PropValue::Str(code.into()))
}
fn has_key(feature: &ProcessedFeature, key: &str) -> bool {
feature.properties.iter().any(|(k, _)| k == key)
}
#[test]
fn a_foreign_region_line_inside_india_is_dropped() {
let mask = square_mask();
let inside = overture_line("region", vec![(2.0, 2.0), (8.0, 8.0)], &[country("PK")]);
assert!(mask.clip(inside).is_none());
let far = overture_line("region", vec![(40.0, 40.0), (50.0, 50.0)], &[country("PK")]);
let kept = mask.clip(far).unwrap();
assert!(!has_key(&kept, crate::layers::OWNER_KEY));
assert!((total_length(&kept) - 10.0 * 2f64.sqrt()).abs() < 1e-9);
}
#[test]
fn a_foreign_line_along_the_border_stays() {
let line = overture_line("region", vec![(10.0, 2.0), (10.0, 8.0)], &[country("PK")]);
let kept = square_mask().clip(line).unwrap();
assert!((total_length(&kept) - 6.0).abs() < 1e-6);
}
#[test]
fn a_claim_carrying_line_inside_india_is_dropped() {
let mask = square_mask();
let claim = overture_line(
"county",
vec![(2.0, 2.0), (8.0, 8.0)],
&[country("IN"), ("is_disputed", PropValue::Bool(true))],
);
assert!(mask.clip(claim).is_none());
let plain = overture_line("county", vec![(2.0, 2.0), (8.0, 8.0)], &[country("IN")]);
let kept = mask.clip(plain).unwrap();
assert!(!has_key(&kept, "disputed"));
assert!((total_length(&kept) - 6.0 * 2f64.sqrt()).abs() < 1e-9);
}
#[test]
fn an_osm_line_of_an_admin_relation_of_another_country_is_foreign() {
let mut tags = std::collections::HashMap::new();
assert_eq!(relation_country(&tags), None);
tags.insert("ISO3166-2".to_string(), "PK-GB".to_string());
assert_eq!(relation_country(&tags).as_deref(), Some("PK"));
tags.insert("ISO3166-1:alpha2".to_string(), "IN".to_string());
assert_eq!(relation_country(&tags).as_deref(), Some("IN"));
}
fn label(subtype: &str, props: &[(&str, &str)], at: (f64, f64)) -> ProcessedFeature {
let mut all: Vec<(&str, PropValue)> = vec![
("subtype", PropValue::Str(subtype.into())),
("names.primary", PropValue::Str("X".into())),
];
all.extend(props.iter().map(|(k, v)| (*k, PropValue::Str((*v).into()))));
let mut feat = make_feature(&all);
feat.geometry = Arc::new(geo::Geometry::Point(geo::Point::new(at.0, at.1)));
crate::layers::places::process(&feat).unwrap()
}
#[test]
fn foreign_admin_labels_on_india_land_are_dropped() {
let mask = square_mask();
let at = (5.0, 5.0);
let claim = label("region", &[("country", "IN")], at);
let tibet = label("region", &[("country", "CN"), ("region", "CN-XZ")], at);
let division = label("county", &[("country", "PK"), ("region", "PK-GB")], at);
let pakistan = label("country", &[("country", "PK")], at);
for (name, feature) in [
("claim region", claim),
("Chinese region", tibet),
("Pakistani division", division),
("Pakistan", pakistan),
] {
assert!(mask.clip(feature).is_none(), "{name}");
}
let state = label("region", &[("country", "IN"), ("region", "IN-AR")], at);
let county = label("county", &[("country", "IN"), ("region", "IN-AR")], at);
let city = label("locality", &[("country", "PK")], at);
for (name, feature) in [("state", state), ("county", county), ("city", city)] {
let kept = mask.clip(feature).unwrap_or_else(|| panic!("{name} stays"));
assert!(!has_key(&kept, crate::layers::OWNER_KEY), "{name}");
}
let outside = label(
"region",
&[("country", "PK"), ("region", "PK-GB")],
(40.0, 40.0),
);
assert!(
mask.clip(outside).is_some(),
"outside India nothing changes"
);
}
#[test]
fn the_owner_mark_is_stripped_without_a_mask() {
let foreign = overture_line("region", vec![(2.0, 2.0), (8.0, 8.0)], &[country("PK")]);
assert!(has_key(&foreign, crate::layers::OWNER_KEY));
let (clean, was_foreign) = crate::layers::take_owner(foreign);
assert!(was_foreign);
assert!(!has_key(&clean, crate::layers::OWNER_KEY));
}
fn admin_line(
kind: &str,
coords: Vec<(f64, f64)>,
extra: &[(&str, PropValue)],
foreign: bool,
) -> ProcessedFeature {
let mut properties: Vec<(String, PropValue)> = vec![
("kind".into(), PropValue::Str(kind.into())),
("kind_detail".into(), PropValue::I64(4)),
];
for (k, v) in extra {
properties.push(((*k).into(), v.clone()));
}
if foreign {
mark_foreign(&mut properties);
}
ProcessedFeature {
id: 1,
geometry: Arc::new(geo::Geometry::LineString(geo::LineString::from(coords))),
layer: Layer::Boundaries,
min_zoom: 0,
max_zoom: 15,
sort_rank: 288,
properties,
}
}
fn vertical(
kind: &str,
x: f64,
extra: &[(&str, PropValue)],
foreign: bool,
) -> ProcessedFeature {
admin_line(kind, vec![(x, 2.0), (x, 8.0)], extra, foreign)
}
const CLAIM_MARKERS: [&str; 3] = ["disputed", "disputed_by", "claimed_by"];
fn marker(name: &str) -> (&'static str, PropValue) {
match name {
"disputed" => ("disputed", PropValue::Bool(true)),
"disputed_by" => ("disputed_by", PropValue::Str("PK".into())),
_ => ("claimed_by", PropValue::Str("CN".into())),
}
}
#[test]
fn a_claim_marked_line_in_the_band_inside_india_is_dropped_for_every_kind_and_marker() {
let mask = square_mask();
for kind in ["country", "region", "county", "locality"] {
for name in CLAIM_MARKERS {
for foreign in [false, true] {
let (key, value) = marker(name);
let line = vertical(kind, 0.01, &[(key, value)], foreign);
assert!(
mask.clip(line).is_none(),
"{kind} with {name} (foreign={foreign}) at 0.01 deg inside survives"
);
}
}
}
}
#[test]
fn a_claim_marked_line_within_the_margin_outside_india_is_dropped_too() {
let mask = square_mask();
for kind in ["country", "region", "county", "locality"] {
for x in [-0.001, -0.015, -0.019] {
let line = vertical(kind, x, &[marker("disputed_by")], true);
assert!(mask.clip(line).is_none(), "{kind} at {x}");
}
let line = vertical(kind, -0.03, &[marker("disputed_by")], true);
let kept = mask
.clip(line)
.expect("a claim line 0.03 deg outside stays");
assert!((total_length(&kept) - 6.0).abs() < 1e-9, "{kind}");
}
}
#[test]
fn a_claim_marked_line_crossing_the_edge_keeps_only_the_part_beyond_the_outward_band() {
let mask = square_mask();
let line = admin_line(
"region",
vec![(-5.0, 5.0), (15.0, 5.0)],
&[marker("claimed_by")],
true,
);
let kept = mask.clip(line).expect("the outside parts stay");
let expected = 2.0 * (5.0 - CLAIM_MARGIN_DEG);
assert!(
(total_length(&kept) - expected).abs() < 1e-6,
"{}",
total_length(&kept)
);
}
#[test]
fn unclaimed_lines_keep_the_band_rule() {
let mask = square_mask();
let unmarked = vertical("region", 0.01, &[], false);
let kept = mask.clip(unmarked.clone()).expect("unmarked stays");
assert!(Arc::ptr_eq(&kept.geometry, &unmarked.geometry));
let foreign_band = vertical("region", 0.01, &[], true);
let kept = mask
.clip(foreign_band)
.expect("a foreign line in the band stays");
assert!((total_length(&kept) - 6.0).abs() < 1e-9);
let foreign_deep = vertical("region", 0.03, &[], true);
assert!(
mask.clip(foreign_deep).is_none(),
"a foreign line deeper than the band goes"
);
}
#[test]
fn a_line_between_indian_divisions_carries_no_claim_marker_and_stays_whole() {
let mask = square_mask();
let inside = Arc::new(geo::Geometry::LineString(geo::LineString::from(vec![
(2.0, 2.0),
(8.0, 8.0),
])));
let mut overture = make_feature(&[
("subtype", PropValue::Str("region".into())),
("country", PropValue::Str("IN".into())),
]);
overture.geometry = inside.clone();
let overture = process(&overture).expect("an Indian region line");
assert!(!carries_claim(&overture), "{:?}", overture.properties);
let kept = mask.clip(overture.clone()).expect("stays");
assert!(Arc::ptr_eq(&kept.geometry, &overture.geometry));
let mut way = make_osm_way(
&[],
vec![
rel_with(
Some(4),
false,
&[("type", "boundary"), ("ISO3166-2", "IN-MH")],
),
rel_with(
Some(4),
false,
&[("type", "boundary"), ("ISO3166-2", "IN-GJ")],
),
],
);
way.geometry = inside;
let way = process(&way).expect("a line between two Indian states");
assert!(!carries_claim(&way), "{:?}", way.properties);
let (unmarked, foreign) = take_owner(way.clone());
assert!(!foreign, "a line of two Indian relations is not foreign");
let kept = mask.clip(way).expect("stays");
assert!(Arc::ptr_eq(&kept.geometry, &unmarked.geometry) || total_length(&kept) > 8.0);
}
#[test]
fn a_way_without_a_relation_keeps_its_claim_tags() {
let way = make_osm_way(
&[
("boundary", PropValue::Str("administrative".into())),
("admin_level", PropValue::Str("4".into())),
("disputed_by", PropValue::Str("PK".into())),
("claimed_by", PropValue::Str("CN".into())),
],
vec![],
);
let result = process(&way).expect("a region line");
assert!(carries_claim(&result));
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "disputed_by" && *v == PropValue::Str("PK".into()))
);
assert!(
result
.properties
.iter()
.any(|(k, v)| k == "claimed_by" && *v == PropValue::Str("CN".into()))
);
}
}