gsi 1.0.0

Implementation of a map tile retriever for 'Chi-ri-in-tile' ('地理院タイル'; ja-JP) of 'Geospatial Information Authority of Japan' (GSI).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::{
 error::GsiError,
 http
};
use serde::{
 Deserialize,
 Serialize
};
use std::collections::HashMap;

pub const URL_ROOT_DIR_GITHUB: &str = "https://raw.githubusercontent.com/gsi-cyberjapan/gsimaps/gh-pages/layers_txt/";
pub const URL_ROOT_DIR_GSI: &str = "https://maps.gsi.go.jp/layers_txt/";
/// Reference: <https://github.com/gsi-cyberjapan/layers-dot-txt-spec/blob/master/list.md>
pub const PATH_FIRST_LAYERS: [&str; 8] = [
 // ベースマップ
 "layers0.txt",
 // 年代別の写真
 "layers1.txt",
 // 標高・土地の凹凸
 "layers2.txt",
 // 土地の成り立ち・土地利用
 "layers3.txt",
 // 基準点・地磁気・地殻変動
 "layers4.txt",
 // 災害伝承・避難場所
 "layers5.txt",
 // 近年の災害
 "layers6.txt",
 // その他
 "layers7.txt"
];

pub type LeafletLatLngBounds = [[f64; 2]; 2];

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum LeafletStringOrStringArray
{
 String(String),
 StringArray(Vec<String>)
}

/// note: https://github.com/serde-rs/serde/issues/1030#issuecomment-522278006
fn default_true() -> bool
{
 true
}

/// note: https://github.com/serde-rs/serde/issues/368
fn default_subdomains() -> LeafletStringOrStringArray
{
 LeafletStringOrStringArray::String(String::from("abc"))
}

fn is_default_subdomains(a: &LeafletStringOrStringArray) -> bool
{
 match a
 {
  LeafletStringOrStringArray::String(s) if &s[..] == "abc" => true,
  _ => false
 }
}

fn is_true(a: &bool) -> bool
{
 a.clone()
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Layer
{
 #[serde(rename = "type")]
 pub ty: String,
 pub id: String,
 pub title: String,
 pub url: String,

 #[serde(default = "default_subdomains", skip_serializing_if = "is_default_subdomains")]
 pub subdomains: LeafletStringOrStringArray,
 #[serde(default, skip_serializing_if = "String::is_empty")]
 pub attribution: String,
 #[serde(default = "default_true", skip_serializing_if = "is_true")]
 pub cocotile: bool,
 #[serde(rename = "minZoom", default, skip_serializing_if = "Option::is_none")]
 pub min_zoom: Option<u8>,
 #[serde(rename = "maxZoom", default, skip_serializing_if = "Option::is_none")]
 pub max_zoom: Option<u8>,
 #[serde(rename = "maxNativeZoom", default, skip_serializing_if = "Option::is_none")]
 pub max_native_zoom: Option<u8>,

 #[serde(rename = "iconUrl", default, skip_serializing_if = "String::is_empty")]
 pub icon_url: String,
 #[serde(rename = "legendUrl", default, skip_serializing_if = "String::is_empty")]
 pub legend_url: String,
 /// note: https://github.com/gsi-cyberjapan/layers-dot-txt-spec/issues/1
 #[serde(rename = "styleurl", default, skip_serializing_if = "String::is_empty")]
 pub style_url: String,

 #[serde(rename = "errorTileUrl", default, skip_serializing_if = "String::is_empty")]
 pub error_tile_url: String,
 #[serde(default, skip_serializing_if = "Option::is_none")]
 pub bounds: Option<LeafletLatLngBounds>,

 #[serde(rename = "html", default, skip_serializing_if = "String::is_empty")]
 pub html: String
}

impl Layer
{
 /// Get the {ext} part. ( It's not same to the extension part of filepath. )
 /// eg.1: url = "https://maps.gsi.go.jp/xyz/dem/{z}/{x}/{y}.txt" => ".txt"
 /// eg.2: url = "https://maps.gsi.go.jp/xyz/std/{z}/{x}/{y}.png?_=20200803a" => ".png?_=20200803a"
 /// eg.1: url = "https://example.com/{x}/{y}/{z}" => ""
 pub fn ext(&self) -> String
 {
  let p = std::path::Path::new(&self.url);
  match p.extension()
  {
   Some(ext) => format!(".{}", ext.to_string_lossy()),
   None => "".into()
  }
 }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LayerGroup
{
 #[serde(rename = "type")]
 pub ty: String,
 /// note: https://github.com/gsi-cyberjapan/layers-dot-txt-spec/issues/2
 #[serde(default)]
 pub id: String,
 pub title: String,
 #[serde(default)]
 pub toggleall: bool,
 #[serde(default)]
 pub entries: Vec<LayerVariant>,
 #[serde(default)]
 pub src: String,
 #[serde(rename = "iconUrl", default)]
 pub icon_url: String,
 #[serde(rename = "legendUrl", default)]
 pub legend_url: String,
 #[serde(rename = "html", default)]
 pub html: String
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum LayerVariant
{
 Layer(Layer),
 LayerGroup(LayerGroup)
}

impl LayerVariant
{
 pub fn is_layer(&self) -> bool
 {
  match self
  {
   LayerVariant::Layer(_) => true,
   _ => false
  }
 }

 pub fn is_layer_group(&self) -> bool
 {
  match self
  {
   LayerVariant::LayerGroup(_) => true,
   _ => false
  }
 }

 pub fn as_layer(&self) -> Option<&Layer>
 {
  match self
  {
   LayerVariant::Layer(l) => Some(l),
   _ => None
  }
 }

 pub fn as_layer_group(&self) -> Option<&LayerGroup>
 {
  match self
  {
   LayerVariant::LayerGroup(g) => Some(g),
   _ => None
  }
 }

 pub fn ty(&self) -> &str
 {
  match self
  {
   LayerVariant::Layer(l) => &l.ty[..],
   LayerVariant::LayerGroup(g) => &g.ty[..]
  }
 }

 pub fn title(&self) -> &str
 {
  match self
  {
   LayerVariant::Layer(l) => &l.title[..],
   LayerVariant::LayerGroup(g) => &g.title[..]
  }
 }

 pub fn icon_url(&self) -> &str
 {
  match self
  {
   LayerVariant::Layer(l) => &l.icon_url[..],
   LayerVariant::LayerGroup(g) => &g.icon_url[..]
  }
 }

 pub fn legend_url(&self) -> &str
 {
  match self
  {
   LayerVariant::Layer(l) => &l.legend_url[..],
   LayerVariant::LayerGroup(g) => &g.legend_url[..]
  }
 }

 pub fn html(&self) -> &str
 {
  match self
  {
   LayerVariant::Layer(l) => &l.html[..],
   LayerVariant::LayerGroup(g) => &g.html[..]
  }
 }

 pub fn iter<'a>(&'a self) -> Box<dyn std::iter::Iterator<Item = &'a Self> + 'a>
 {
  match self
  {
   LayerVariant::Layer(_) => Box::new(std::iter::once(self)),
   LayerVariant::LayerGroup(g) => Box::new(std::iter::once(self).chain(Box::new(g.entries.iter().flat_map(|v| v.iter()))))
  }
 }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Layers
{
 pub layers: Vec<LayerVariant>
}

impl Layers
{
 pub fn iter<'a>(&'a self) -> Box<dyn std::iter::Iterator<Item = &'a LayerVariant> + 'a>
 {
  Box::new(self.layers.iter().flat_map(|a| a.iter()))
 }
}

/// { key: id -> value: Layer } mapper
pub type CachedLayers = HashMap<String, Layer>;

pub async fn get_first_layers_from(base_url: &str) -> Vec<Result<Layers, GsiError>>
{
 let mapper = PATH_FIRST_LAYERS.iter().map(|path| {
  let url = format!("{}{}", base_url, path);
  async move { get_layers_from_url(&url).await }
 });

 let results = mapper.collect::<Vec<_>>();
 let results = futures::future::join_all(results).await;

 results
}

pub async fn get_first_layers_from_github() -> Vec<Result<Layers, GsiError>>
{
 get_first_layers_from(URL_ROOT_DIR_GITHUB).await
}

pub async fn get_first_layers_from_gsi() -> Vec<Result<Layers, GsiError>>
{
 get_first_layers_from(URL_ROOT_DIR_GSI).await
}

pub fn get_layers_from_json(source: &str) -> Result<Layers, GsiError>
{
 let layers = serde_json::from_str::<Layers>(source).map_err(|e| GsiError::SerdeJsonError(e))?;
 Ok(layers)
}

pub async fn get_layers_from_url(url: &str) -> Result<Layers, GsiError>
{
 http::get_json::<Layers>(url).await
}

pub async fn get_layers_from_github(path: &str) -> Result<Layers, GsiError>
{
 let url = format!("{}{}", URL_ROOT_DIR_GITHUB, path);
 get_layers_from_url(&url).await
}

pub async fn get_layers_from_gsi(path: &str) -> Result<Layers, GsiError>
{
 let url = format!("{}{}", URL_ROOT_DIR_GSI, path);
 get_layers_from_url(&url).await
}

pub async fn merge_layer_cache_from_url(cached_layers: &mut CachedLayers, layers_txt_url: &str) -> Result<Vec<LayerGroup>, GsiError>
{
 eprintln!("merge_layer_cache_from_url, layers_txt_url={}", layers_txt_url);

 let layers = get_layers_from_url(layers_txt_url).await?;

 let mut has_src_groups: Vec<LayerGroup> = vec![];

 for layer_variant in layers.iter()
 {
  match layer_variant
  {
   LayerVariant::Layer(l) =>
   {
    cached_layers.insert(l.id.clone(), l.clone());
   },
   LayerVariant::LayerGroup(g) if !g.src.is_empty() => has_src_groups.push(g.clone()),
   _ => ()
  }
 }

 Ok(has_src_groups)
}

pub async fn make_cached_layers_from_urls(layers_txt_urls: &[&str], resolve_src: bool) -> Result<CachedLayers, GsiError>
{
 let mut cached_layers = CachedLayers::new();

 let mut url_queue = std::collections::vec_deque::VecDeque::<String>::new();
 layers_txt_urls.iter().for_each(|&url| url_queue.push_back(url.into()));

 while let Some(url) = url_queue.pop_front()
 {
  let has_src_groups = merge_layer_cache_from_url(&mut cached_layers, &url).await?;
  if resolve_src
  {
   let base_url = url.rsplitn(2, '/').collect::<Vec<_>>()[1];
   has_src_groups.iter().for_each(|g| {
    match g.src.starts_with("./")
    {
     true => url_queue.push_back(format!("{}/{}", base_url, &g.src)),
     false => url_queue.push_back(g.src.clone())
    }
   })
  }
 }
 Ok(cached_layers)
}

pub async fn make_cached_layers_from_url(layers_txt_url: &str, resolve_src: bool) -> Result<CachedLayers, GsiError>
{
 make_cached_layers_from_urls(&[layers_txt_url], resolve_src).await
}

pub async fn make_cached_layers_github(resolve_src: bool) -> Result<CachedLayers, GsiError>
{
 let layers_txt_urls = PATH_FIRST_LAYERS
  .iter()
  .map(|&path| format!("{}{}", URL_ROOT_DIR_GITHUB, path))
  .collect::<Vec<_>>();
 let layers_txt_urls = layers_txt_urls.iter().map(AsRef::as_ref).collect::<Vec<_>>();
 make_cached_layers_from_urls(&layers_txt_urls, resolve_src).await
}

pub async fn make_cached_layers_gsi(resolve_src: bool) -> Result<CachedLayers, GsiError>
{
 let layers_txt_urls = PATH_FIRST_LAYERS
  .iter()
  .map(|&path| format!("{}{}", URL_ROOT_DIR_GSI, path))
  .collect::<Vec<_>>();
 let layers_txt_urls = layers_txt_urls.iter().map(AsRef::as_ref).collect::<Vec<_>>();
 make_cached_layers_from_urls(&layers_txt_urls, resolve_src).await
}

pub trait CachedLayersSerializeDeserialize
{
 fn to_json_string(&self) -> Result<String, serde_json::Error>;
 fn to_json_vec(&self) -> Result<Vec<u8>, serde_json::Error>;
 fn from_json_string(json_string: &str) -> Result<CachedLayers, serde_json::Error>;
 fn from_json_slice(json_slice: &[u8]) -> Result<CachedLayers, serde_json::Error>;
}

impl CachedLayersSerializeDeserialize for CachedLayers
{
 fn to_json_string(&self) -> Result<String, serde_json::Error>
 {
  serde_json::to_string(&self)
 }

 fn to_json_vec(&self) -> Result<Vec<u8>, serde_json::Error>
 {
  serde_json::to_vec(&self)
 }

 fn from_json_string(json_string: &str) -> Result<CachedLayers, serde_json::Error>
 {
  serde_json::from_str(json_string)
 }

 fn from_json_slice(json_slice: &[u8]) -> Result<CachedLayers, serde_json::Error>
 {
  serde_json::from_slice(json_slice)
 }
}