open-geocode 0.1.0

Lightweight geocoding engine: build a binary Pack from OSM extracts and serve search, autocomplete, and reverse geocoding from a single Rust runtime.
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use std::collections::BTreeMap;

use geojson::{Geometry, GeometryValue};
use serde::{Deserialize, Serialize, Serializer, ser::SerializeStruct};

use crate::{labels, util::geo::point_lon_lat};

/// One normalized geocoding record, as the builder emits it and the Pack
/// stores it.
#[derive(Debug, Clone, PartialEq)]
pub enum Record {
    Address(AddressRecord),
    Interpolation(InterpolationRecord),
    Street(StreetRecord),
    Postcode(PostcodeRecord),
    Place(PlaceLayer, PlaceRecord),
}

#[derive(Debug, Clone, PartialEq)]
pub struct AddressRecord {
    pub address: AddressComponents,
    pub geometry: Geometry,
    pub location_precision: LocationPrecision,
    pub source: SourceProvenance,
}

#[derive(Debug, Clone, PartialEq)]
pub struct InterpolationRecord {
    pub address: InterpolationAddressComponents,
    pub interpolation: InterpolationRange,
    /// OSM node ids of the low and high numbered anchors.
    pub anchor_node_ids: [i64; 2],
    pub geometry: Geometry,
    pub representative_point: [f64; 2],
    pub source: SourceProvenance,
}

#[derive(Debug, Clone, PartialEq)]
pub struct StreetRecord {
    pub name: String,
    pub geometry: Geometry,
    pub representative_point: [f64; 2],
    pub source: SourceProvenance,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PostcodeRecord {
    pub postcode: String,
    pub geometry: Geometry,
    pub source: DerivedSourceProvenance,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PlaceRecord {
    pub name: String,
    pub place_type: String,
    pub geometry: Geometry,
    pub source: SourceProvenance,
}

/// Every record layer. Place layers are the context layers used to describe
/// where an address is.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "snake_case")]
pub enum Layer {
    Address,
    Country,
    District,
    Interpolation,
    Locality,
    Neighbourhood,
    Place,
    Postcode,
    Region,
    Street,
}

impl Layer {
    pub const ALL: [Layer; 10] = [
        Layer::Address,
        Layer::Country,
        Layer::District,
        Layer::Interpolation,
        Layer::Locality,
        Layer::Neighbourhood,
        Layer::Place,
        Layer::Postcode,
        Layer::Region,
        Layer::Street,
    ];

    pub const fn as_str(self) -> &'static str {
        match self {
            Layer::Address => "address",
            Layer::Country => "country",
            Layer::District => "district",
            Layer::Interpolation => "interpolation",
            Layer::Locality => "locality",
            Layer::Neighbourhood => "neighbourhood",
            Layer::Place => "place",
            Layer::Postcode => "postcode",
            Layer::Region => "region",
            Layer::Street => "street",
        }
    }

    pub fn parse(value: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|layer| layer.as_str() == value)
    }

    /// Layers that describe the surroundings of a point (admin areas, named
    /// places and postcodes) rather than a specific address or road.
    pub const fn is_context(self) -> bool {
        matches!(
            self,
            Layer::Country
                | Layer::District
                | Layer::Locality
                | Layer::Neighbourhood
                | Layer::Place
                | Layer::Postcode
                | Layer::Region
        )
    }

    pub const fn place_layer(self) -> Option<PlaceLayer> {
        match self {
            Layer::Country => Some(PlaceLayer::Country),
            Layer::Region => Some(PlaceLayer::Region),
            Layer::District => Some(PlaceLayer::District),
            Layer::Place => Some(PlaceLayer::Place),
            Layer::Locality => Some(PlaceLayer::Locality),
            Layer::Neighbourhood => Some(PlaceLayer::Neighbourhood),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PlaceLayer {
    Country,
    Region,
    District,
    Place,
    Locality,
    Neighbourhood,
}

impl PlaceLayer {
    pub const fn as_str(self) -> &'static str {
        Layer::from_place(self).as_str()
    }
}

impl Layer {
    pub const fn from_place(layer: PlaceLayer) -> Self {
        match layer {
            PlaceLayer::Country => Layer::Country,
            PlaceLayer::Region => Layer::Region,
            PlaceLayer::District => Layer::District,
            PlaceLayer::Place => Layer::Place,
            PlaceLayer::Locality => Layer::Locality,
            PlaceLayer::Neighbourhood => Layer::Neighbourhood,
        }
    }
}

impl From<PlaceLayer> for Layer {
    fn from(layer: PlaceLayer) -> Self {
        Layer::from_place(layer)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AddressComponents {
    pub number: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub street: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unit: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locality: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub postcode: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct InterpolationAddressComponents {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub street: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locality: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub postcode: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct InterpolationRange {
    #[serde(rename = "type")]
    pub kind: String,
    pub start: u32,
    pub end: u32,
    pub step: u32,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LocationPrecision {
    Point,
    Centroid,
}

pub fn point_geometry(lon: f64, lat: f64) -> Geometry {
    Geometry::new(GeometryValue::Point {
        coordinates: vec![lon, lat].into(),
    })
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SourceProvenance {
    pub dataset: String,
    pub object_type: OsmObjectType,
    pub object_id: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<BTreeMap<String, String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DerivedSourceProvenance {
    pub dataset: String,
    pub derived_from: String,
    pub record_count: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RejectedRecord {
    pub reason: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub layer_hint: Option<String>,
    pub source: SourceProvenance,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "snake_case")]
pub enum OsmObjectType {
    Node,
    Way,
    Relation,
}

impl Record {
    pub const fn layer(&self) -> Layer {
        match self {
            Record::Address(_) => Layer::Address,
            Record::Interpolation(_) => Layer::Interpolation,
            Record::Street(_) => Layer::Street,
            Record::Postcode(_) => Layer::Postcode,
            Record::Place(layer, _) => Layer::from_place(*layer),
        }
    }

    pub fn id(&self) -> String {
        match self {
            Record::Address(record) => record.id(),
            Record::Interpolation(record) => record.id(),
            Record::Street(record) => record.id(),
            Record::Postcode(record) => record.id(),
            Record::Place(_, record) => record.id(),
        }
    }

    pub fn label(&self) -> String {
        match self {
            Record::Address(record) => record.label(),
            Record::Interpolation(record) => record.label(),
            Record::Street(record) => record.label(),
            Record::Postcode(record) => record.label(),
            Record::Place(_, record) => record.label(),
        }
    }

    pub fn geometry(&self) -> &Geometry {
        match self {
            Record::Address(record) => &record.geometry,
            Record::Interpolation(record) => &record.geometry,
            Record::Street(record) => &record.geometry,
            Record::Postcode(record) => &record.geometry,
            Record::Place(_, record) => &record.geometry,
        }
    }

    /// The point used to place the record on a map: the point itself for point
    /// records, the representative point for lines.
    pub fn display_point(&self) -> Option<[f64; 2]> {
        match self {
            Record::Address(record) => point_lon_lat(&record.geometry),
            Record::Interpolation(record) => Some(record.representative_point),
            Record::Street(record) => Some(record.representative_point),
            Record::Postcode(record) => point_lon_lat(&record.geometry),
            Record::Place(_, record) => point_lon_lat(&record.geometry),
        }
    }
}

impl From<AddressRecord> for Record {
    fn from(record: AddressRecord) -> Self {
        Record::Address(record)
    }
}

impl From<InterpolationRecord> for Record {
    fn from(record: InterpolationRecord) -> Self {
        Record::Interpolation(record)
    }
}

impl From<StreetRecord> for Record {
    fn from(record: StreetRecord) -> Self {
        Record::Street(record)
    }
}

impl From<PostcodeRecord> for Record {
    fn from(record: PostcodeRecord) -> Self {
        Record::Postcode(record)
    }
}

impl Serialize for Record {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            Record::Address(record) => record.serialize(serializer),
            Record::Interpolation(record) => record.serialize(serializer),
            Record::Street(record) => record.serialize(serializer),
            Record::Postcode(record) => record.serialize(serializer),
            Record::Place(_, record) => record.serialize(serializer),
        }
    }
}

impl AddressRecord {
    pub const fn location_precision(&self) -> LocationPrecision {
        self.location_precision
    }

    pub fn id(&self) -> String {
        labels::osm_record_id(self.source.object_type, self.source.object_id)
    }

    pub fn name(&self) -> String {
        labels::address_name(&self.address)
    }

    pub fn label(&self) -> String {
        labels::address_label(&self.address)
    }
}

impl Serialize for AddressRecord {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("AddressRecord", 7)?;
        state.serialize_field("id", &self.id())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("name", &self.name())?;
        state.serialize_field("address", &self.address)?;
        state.serialize_field("geometry", &self.geometry)?;
        state.serialize_field("location_precision", &self.location_precision)?;
        state.serialize_field("source", &self.source)?;
        state.end()
    }
}

impl InterpolationRecord {
    pub fn id(&self) -> String {
        labels::interpolation_record_id(
            self.source.object_id,
            self.anchor_node_ids[0],
            self.anchor_node_ids[1],
        )
    }

    pub fn name(&self) -> String {
        labels::interpolation_name(&self.address)
    }

    pub fn label(&self) -> String {
        labels::interpolation_label(&self.name(), &self.interpolation, &self.address)
    }
}

impl Serialize for InterpolationRecord {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let anchor_ids = self
            .anchor_node_ids
            .map(|node_id| labels::osm_record_id(OsmObjectType::Node, node_id));
        let mut state = serializer.serialize_struct("InterpolationRecord", 9)?;
        state.serialize_field("id", &self.id())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("name", &self.name())?;
        state.serialize_field("address", &self.address)?;
        state.serialize_field("interpolation", &self.interpolation)?;
        state.serialize_field("anchor_ids", &anchor_ids)?;
        state.serialize_field("geometry", &self.geometry)?;
        state.serialize_field("representative_point", &self.representative_point)?;
        state.serialize_field("source", &self.source)?;
        state.end()
    }
}

impl StreetRecord {
    pub fn id(&self) -> String {
        labels::osm_record_id(self.source.object_type, self.source.object_id)
    }

    pub fn label(&self) -> String {
        self.name.clone()
    }
}

impl Serialize for StreetRecord {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("StreetRecord", 6)?;
        state.serialize_field("id", &self.id())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("name", &self.name)?;
        state.serialize_field("geometry", &self.geometry)?;
        state.serialize_field("representative_point", &self.representative_point)?;
        state.serialize_field("source", &self.source)?;
        state.end()
    }
}

impl PostcodeRecord {
    pub fn id(&self) -> String {
        labels::derived_postcode_id(&self.postcode)
    }

    pub fn name(&self) -> String {
        self.postcode.clone()
    }

    pub fn label(&self) -> String {
        self.postcode.clone()
    }
}

impl Serialize for PostcodeRecord {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("PostcodeRecord", 6)?;
        state.serialize_field("id", &self.id())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("name", &self.name())?;
        state.serialize_field("postcode", &self.postcode)?;
        state.serialize_field("geometry", &self.geometry)?;
        state.serialize_field("source", &self.source)?;
        state.end()
    }
}

impl PlaceRecord {
    pub fn id(&self) -> String {
        labels::place_record_id(
            self.source.object_type,
            self.source.object_id,
            &self.place_type,
        )
    }

    pub fn label(&self) -> String {
        self.name.clone()
    }
}

impl Serialize for PlaceRecord {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut state = serializer.serialize_struct("PlaceRecord", 6)?;
        state.serialize_field("id", &self.id())?;
        state.serialize_field("label", &self.label())?;
        state.serialize_field("name", &self.name)?;
        state.serialize_field("place_type", &self.place_type)?;
        state.serialize_field("geometry", &self.geometry)?;
        state.serialize_field("source", &self.source)?;
        state.end()
    }
}

impl SourceProvenance {
    pub fn osm(object_type: OsmObjectType, object_id: i64) -> Self {
        Self {
            dataset: "osm".to_string(),
            object_type,
            object_id,
            tags: None,
        }
    }

    pub fn osm_with_tags(
        object_type: OsmObjectType,
        object_id: i64,
        tags: BTreeMap<String, String>,
    ) -> Self {
        Self {
            dataset: "osm".to_string(),
            object_type,
            object_id,
            tags: Some(tags),
        }
    }
}

pub const DERIVED_FROM_ADDRESS_RECORDS: &str = "accepted_address_records";

impl DerivedSourceProvenance {
    pub fn osm_address_records(record_count: u64) -> Self {
        Self {
            dataset: "osm".to_string(),
            derived_from: DERIVED_FROM_ADDRESS_RECORDS.to_string(),
            record_count,
        }
    }
}