Skip to main content

SingleId

Struct SingleId 

Source
pub struct SingleId { /* private fields */ }
Expand description

SingleIdは標準的な時空間 ID を表す型。

内部的には下記のような構造体で構成されている。

この型は PartialOrd / Ord を実装していますが、これは主にBTreeSetBTreeMap などの順序付きコレクションでの格納・探索用であり、実際の空間的な「大小」を意味するものではない。

pub struct SingleId {
    z: u8,
    f: i32,
    x: u32,
    y: u32,
    temporal_id: TemporalId,
}

Implementations§

Source§

impl SingleId

Source

pub fn new(z: u8, f: i32, x: u32, y: u32) -> Result<SingleId, Error>

指定された値から SingleId を作成する。このコンストラクタは、与えられた z, f, x, y が 各ズームレベルにおける範囲内にあるかを検証し、範囲外の場合は Error を返す。

§パラメータ
  • z — ズームレベル(0–MAX_ZOOM_LEVELの範囲が有効)
  • f — Fインデックス(鉛直方向)
  • x — Xインデックス(東西方向)
  • y — Yインデックス(南北方向)
§バリデーション

IDの作成:

let id = SingleId::new(5, 3, 2, 10).unwrap();
assert_eq!(id.to_string(), "5/3/2/10".to_string());

次元の範囲外の検知:

let id = SingleId::new(3, 3, 2, 10);
assert_eq!(id, Err(SpatialIdError::YOutOfRange{z:3,y:10}.into()));

ズームレベルの範囲外の検知:

let id = SingleId::new(68, 3, 2, 10);
assert_eq!(id, Err(SpatialIdError::ZOutOfRange { z:68 }.into()));
Source

pub unsafe fn new_unchecked(z: u8, f: i32, x: u32, y: u32) -> SingleId

検証を行わずに SingleId を作成する。

この関数は SingleId::new と異なり、与えられた z, f, x, y に対して一切の範囲チェックや整合性チェックを行わない。そのため、高速に ID を生成できるが、不正なパラメータを与えた場合の動作は未定義である

§注意

呼び出し側は、以下をすべて満たすことを保証しなければならない。

  • z が有効なズームレベル(0–MAX_ZOOM_LEVEL)であること
  • f が与えられた z に応じて F_MIN[z]..=F_MAX[z] の範囲内であること
  • x および y0..=XY_MAX[z] の範囲内であること

これらが保証されない場合、パニック・不正メモリアクセス・未定義動作を引き起こす可能性がある。

// パラメータが妥当であることを呼び出し側が保証する必要がある
let id = unsafe { SingleId::new_unchecked(5, 3, 2, 10) };

assert_eq!(id.z(), 5u8);
assert_eq!(id.f(), 3i32);
assert_eq!(id.x(), 2u32);
assert_eq!(id.y(), 10u32);
Source§

impl SingleId

Source

pub fn random() -> Self

ランダムなSingleIdを作成する

Source

pub fn random_at(z: u8) -> Self

特定のズームレベルにおいて、ランダムなSingleIdを作成する

Source

pub fn random_within(z_range: RangeInclusive<u8>) -> Self

特定のズームレベル間において、ランダムなSingleIdを作成する

Source

pub fn random_using<R: Rng>(rng: &mut R) -> Self

外部から渡された乱数生成器を使って、全ズーム範囲からランダムな SingleId を生成します。

Source

pub fn random_at_using<R: Rng>(rng: &mut R, z: u8) -> Self

外部から渡された乱数生成器を使って、指定ズームのランダムな SingleId を生成します。

Source

pub fn random_within_using<R: Rng>( rng: &mut R, z_range: RangeInclusive<u8>, ) -> Self

外部から渡された乱数生成器を使って、指定ズーム範囲のランダムな SingleId を生成します。

z_range の終端は MAX_ZOOM_LEVEL でクリップされ、start > end の場合は end を採用します。

Source§

impl SingleId

Source

pub fn z(&self) -> u8

この SingleId が保持しているズームレベル z を返します。

let id = SingleId::new(5, 3, 2, 10).unwrap();
assert_eq!(id.z(), 5u8);
Source

pub fn f(&self) -> i32

この SingleId が保持している F インデックス f を返します。

let id = SingleId::new(5, 3, 2, 10).unwrap();
assert_eq!(id.f(), 3i32);
Source

pub fn x(&self) -> u32

この SingleId が保持している X インデックス x を返します。

let id = SingleId::new(5, 3, 2, 10).unwrap();
assert_eq!(id.x(), 2u32);
Source

pub fn y(&self) -> u32

この SingleId が保持している Y インデックス y を返します。

let id = SingleId::new(5, 3, 2, 10).unwrap();
assert_eq!(id.y(), 10u32);
Source

pub fn set_f(&mut self, value: i32) -> Result<(), Error>

F インデックスを更新します。

与えられた value が、現在のズームレベル z に対応する F_MIN[z]..=F_MAX[z] の範囲内にあるかを検証し、範囲外の場合は Error を返します。

§パラメータ
  • value — 新しい F インデックス
§バリデーション

正常な更新:

let mut id = SingleId::new(5, 3, 2, 10).unwrap();
id.set_f(4).unwrap();
assert_eq!(id.f(), 4);

範囲外の検知:

let mut id = SingleId::new(3, 3, 2, 7).unwrap();
let result = id.set_f(999);
assert!(matches!(result, Err(Error::SpatialId(SpatialIdError::FOutOfRange { z: 3, f: 999 }))));
Source

pub fn set_x(&mut self, value: u32) -> Result<(), Error>

X インデックスを更新します。

与えられた value が、現在のズームレベル z に対応する 0..=XY_MAX[z] の範囲内にあるかを検証し、範囲外の場合は Error を返します。

§パラメータ
  • value — 新しい X インデックス
§バリデーション

正常な更新:

let mut id = SingleId::new(5, 3, 2, 10).unwrap();
id.set_x(4).unwrap();
assert_eq!(id.x(), 4);

範囲外の検知

let mut id = SingleId::new(3, 3, 2, 7).unwrap();
let result = id.set_x(999);
assert!(matches!(result, Err(Error::SpatialId(SpatialIdError::XOutOfRange { z: 3, x: 999 }))));
Source

pub fn set_y(&mut self, value: u32) -> Result<(), Error>

Y インデックスを更新します。

与えられた value が、現在のズームレベル z に対応する 0..=XY_MAX[z] の範囲内にあるかを検証し、範囲外の場合は Error を返します。

§パラメータ
  • value — 新しい Y インデックス
§バリデーション

正常な更新

let mut id = SingleId::new(5, 3, 2, 10).unwrap();
id.set_y(8).unwrap();
assert_eq!(id.y(), 8);

範囲外の検知

let mut id = SingleId::new(3, 3, 2, 7).unwrap();
let result = id.set_y(999);
assert!(matches!(result, Err(Error::SpatialId(SpatialIdError::YOutOfRange { z: 3, y: 999 }))));
Source

pub fn spatial_children_at_zoom( &self, target_z: u8, ) -> Result<impl Iterator<Item = SingleId>, Error>

指定したズームレベル target_z に細分化した、この SingleId を含むすべての子 SingleId を生成します。

§パラメータ
  • target_z — 生成したい子 SingleId のズームレベル
§バリデーション

1段深いズームへの細分化

let id = SingleId::new(3, 3, 2, 7).unwrap();

// target_z = 4 のため F, X, Y はそれぞれ 2 分割される
let children: Vec<_> = id.spatial_children_at_zoom(4).unwrap().collect();

assert_eq!(children.len(), 8); // 2 × 2 × 2

// 最初の要素を確認(f, x, y の下限側)
let first = &children[0];
assert_eq!(first.z(), 4);
assert_eq!(first.f(), 3 * 2);   // 2
assert_eq!(first.x(), 2 * 2);   // 6
assert_eq!(first.y(), 7 * 2);   // 8

現在より浅いズームを指定した場合

let id = SingleId::new(3, 3, 2, 7).unwrap();
let result = id.spatial_children_at_zoom(2);
assert!(matches!(result, Err(Error::SpatialId(SpatialIdError::ZoomLevelTransitionOutOfRange { current_z: 3, target_z: 2 }))));
Source

pub fn spatial_parent_at_zoom(&self, target_z: u8) -> Result<SingleId, Error>

指定したズームレベル target_z に縮約した、この SingleId の親 SingleId を返します。

§パラメータ
  • target_z — 取得したい親 SingleId のズームレベル
§バリデーション

1段浅いズームへの縮約

let id = SingleId::new(4, 6, 9, 14).unwrap();

let parent = id.spatial_parent_at_zoom(3).unwrap();

assert_eq!(parent.z(), 3u8);
assert_eq!(parent.f(), 3i32);
assert_eq!(parent.x(), 4u32);
assert_eq!(parent.y(), 7u32);

Fが負の場合の挙動

let id = SingleId::new(4, -1, 8, 12).unwrap();

let parent = id.spatial_parent_at_zoom(3).unwrap();

assert_eq!(parent.z(), 3u8);
assert_eq!(parent.f(), -1i32);
assert_eq!(parent.x(), 4u32);
assert_eq!(parent.y(), 6u32);

現在より深いズームを指定した場合:

let id = SingleId::new(3, 3, 2, 7).unwrap();
let result = id.spatial_parent_at_zoom(4);
assert!(matches!(result, Err(Error::SpatialId(SpatialIdError::ZoomLevelTransitionOutOfRange { current_z: 3, target_z: 4 }))));

Trait Implementations§

Source§

impl Clone for SingleId

Source§

fn clone(&self) -> SingleId

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SingleId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SingleId

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SingleId

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

SingleId を文字列形式で表示する。

形式は "{z}/{f}/{x}/{y}"

let id = SingleId::new(4, 6, 9, 10).unwrap();
let s = format!("{}", id);
assert_eq!(s, "4/6/9/10");
Source§

impl Eq for SingleId

Source§

impl From<&SingleId> for FlexId

Source§

fn from(value: &SingleId) -> Self

Converts to this type from the input type.
Source§

impl From<&SingleId> for RangeId

Source§

fn from(id: &SingleId) -> Self

Converts to this type from the input type.
Source§

impl From<SingleId> for FlexId

Source§

fn from(value: SingleId) -> Self

Converts to this type from the input type.
Source§

impl From<SingleId> for RangeId

Source§

fn from(id: SingleId) -> Self

Converts to this type from the input type.
Source§

impl Hash for SingleId

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoFlexIds for SingleId

Source§

type IntoIter = Once<FlexId>

Source§

fn into_flex_ids(self) -> Self::IntoIter

所有権ごと FlexId の列へ変換する。
Source§

impl IntoSingleIds for SingleId

Source§

type IntoIter = Once<SingleId>

Source§

fn into_single_ids(self) -> Self::IntoIter

所有権ごと SingleId の列へ変換する。
Source§

impl IterFlexIds for SingleId

Source§

type Iter<'a> = Once<FlexId>

Source§

fn iter_flex_ids(&self) -> Self::Iter<'_>

参照から FlexId の列を列挙する。
Source§

impl IterSingleIds for SingleId

Source§

type Iter<'a> = Once<SingleId>

Source§

fn iter_single_ids(&self) -> Self::Iter<'_>

参照から SingleId の列を列挙する。
Source§

impl Ord for SingleId

Source§

fn cmp(&self, other: &SingleId) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SingleId

Source§

fn eq(&self, other: &SingleId) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SingleId

Source§

fn partial_cmp(&self, other: &SingleId) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for SingleId

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl SpatialId for SingleId

Source§

fn move_f(&mut self, by: i32) -> Result<(), Error>

指定したインデックス差 by に基づき、この SingleId を垂直上下方向に動かします。

§パラメータ
  • by — インデックス差
§バリデーション

移動

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.f(), 6);

let _ = id.move_f(-10).unwrap();
assert_eq!(id.f(), -4);

範囲外の検知によるエラー

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.f(), 6);
assert_eq!(id.move_f(50), Err(SpatialIdError::FOutOfRange { z: 4, f: 56 }.into()));
Source§

fn move_x(&mut self, by: i32)

指定したインデックス差 by に基づき、この SingleId を東西方向に動かします。WEBメルカトル図法において、東西方向は循環しているためどのような値を指定してもエラーは発生しません。

§パラメータ
  • by — インデックス差

移動

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.x(), 9);

let _ = id.move_x(-3);
assert_eq!(id.x(), 6);

循環による移動

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.x(), 9);

let _ = id.move_x(100);
assert_eq!(id.x(), 13);
Source§

fn move_y(&mut self, by: i32) -> Result<(), Error>

指定したインデックス差 by に基づき、この SingleId を南北方向に動かします。

§パラメータ
  • by — インデックス差
§バリデーション

移動

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.y(), 10);

let _ = id.move_y(-3).unwrap();
assert_eq!(id.y(), 7);

範囲外の検知によるエラー

let mut id = SingleId::new(4, 6, 9, 10).unwrap();
assert_eq!(id.y(), 10);
assert_eq!(id.move_y(-20), Err(SpatialIdError::YOutOfRange { z: 4, y: 0 }.into()));
Source§

fn spatial_center(&self) -> Coordinate

SingleId の中心座標をCoordinate型で返します。

中心座標は空間IDの最も外側の頂点の8点の平均座標です。現実空間における空間IDは完全な直方体ではなく、緯度や高度によって歪みが発生していることに注意する必要があります。

let id = SingleId::new(4, 6, 9, 14).unwrap();
let center: Coordinate = id.spatial_center();
println!("{:?}", center);
// Coordinate { latitude: -81.09321385260839, longitude: 33.75, altitude: 13631488.0 }
Source§

fn spatial_vertices(&self) -> [Coordinate; 8]

SingleId の最も外側の頂点の8点の座標をCoordinate型の配列として返します。

現実空間における空間IDは完全な直方体ではなく、緯度や高度によって歪みが発生していることに注意する必要があります。

let id = SingleId::new(4, 6, 9, 14).unwrap();
let vertices: [Coordinate; 8] = id.spatial_vertices();
println!("{:?}", vertices);

 //[Coordinate { latitude: -79.17133464081945, longitude: 22.5, altitude: 12582912.0 }, Coordinate { latitude: -79.17133464081945, longitude: 45.0, altitude: 12582912.0 }, Coordinate { latitude: -82.67628497834903, longitude: 22.5, altitude: 12582912.0 }, Coordinate { latitude: -82.67628497834903, longitude: 45.0, altitude: 12582912.0 }, Coordinate { latitude: -79.17133464081945, longitude: 22.5, altitude: 14680064.0 }, Coordinate { latitude: -79.17133464081945, longitude: 45.0, altitude: 14680064.0 }, Coordinate { latitude: -82.67628497834903, longitude: 22.5, altitude: 14680064.0 }, Coordinate { latitude: -82.67628497834903, longitude: 45.0, altitude: 14680064.0 }]
Source§

fn length_f_meters(&self) -> f64

その空間IDのF方向の長さをメートル単位で計算する関数

Source§

fn length_x_meters(&self) -> f64

その空間IDのX方向の長さをメートル単位で計算する関数

Source§

fn length_y_meters(&self) -> f64

その空間IDのY方向の長さをメートル単位で計算する関数

Source§

fn f_min(&self) -> i32

ズームレベルにおける最小のFインデックスを返す。 Read more
Source§

fn f_max(&self) -> i32

ズームレベルにおける最大のFインデックスを返す。 Read more
Source§

fn x_max(&self) -> u32

ズームレベルにおける最大のXインデックスを返す。 Read more
Source§

fn y_max(&self) -> u32

ズームレベルにおける最大のYインデックスを返す。 Read more
Source§

fn temporal(&self) -> &TemporalId

時間 ID を参照で返す。
Source§

fn temporal_mut(&mut self) -> &mut TemporalId

時間 ID を可変参照で返す。
Source§

fn x_min(&self) -> u32

ズームレベルにおける最小のXインデックスを返す。全てのIDにおいて必ず0を返す。 Read more
Source§

fn y_min(&self) -> u32

ズームレベルにおける最小のYインデックスを返す。全てのIDにおいて必ず0を返す。 Read more
Source§

impl StructuralPartialEq for SingleId

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V