-
Notifications
You must be signed in to change notification settings - Fork 14
/
geocoding.go
153 lines (131 loc) · 4.17 KB
/
geocoding.go
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
package geocoding
import (
"context"
"fmt"
"time"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/service/component/clock"
)
type IGeocoding interface {
Geocode(ctx context.Context, ormService *beeorm.Engine, address string, language Language) (*Address, error)
ReverseGeocode(ctx context.Context, ormService *beeorm.Engine, latLng *LatLng, language Language) (*Address, error)
}
type Address struct {
FromCache bool
Address string
Language Language
Location *LatLng
}
type LatLng struct {
Lat float64
Lng float64
}
type Geocoding struct {
useCaching bool
cacheExpiryDays int64
cacheLatLngFloatingPointPrecision int
useCacheLatLngFloatingPointPrecision bool
clock clock.IClock
provider Provider
}
func NewGeocoding(
useCaching bool,
cacheExpiryDays int64,
cacheLatLngFloatingPointPrecision int,
useCacheLatLngFloatingPointPrecision bool,
clock clock.IClock,
provider Provider,
) IGeocoding {
return &Geocoding{
useCaching: useCaching,
cacheExpiryDays: cacheExpiryDays,
cacheLatLngFloatingPointPrecision: cacheLatLngFloatingPointPrecision,
useCacheLatLngFloatingPointPrecision: useCacheLatLngFloatingPointPrecision,
clock: clock,
provider: provider,
}
}
func (g *Geocoding) Geocode(ctx context.Context, ormService *beeorm.Engine, address string, language Language) (*Address, error) {
languageEnum, ok := languageToEnumMapping[language]
if !ok {
return nil, fmt.Errorf("language %s not supported", language)
}
if g.useCaching {
geocodingEntity := &entity.GeocodingEntity{}
if ormService.CachedSearchOne(geocodingEntity, "CachedQueryAddressLanguage", address, language) {
return &Address{
FromCache: true,
Address: geocodingEntity.Address,
Language: Language(geocodingEntity.Language),
Location: &LatLng{
Lat: geocodingEntity.Lat,
Lng: geocodingEntity.Lng,
},
}, nil
}
}
addressResult, rawResponse, err := g.provider.Geocode(ctx, address, language)
if err != nil {
return nil, err
}
now := g.clock.Now()
if g.useCaching {
ormService.Flush(&entity.GeocodingEntity{
Lat: addressResult.Location.Lat,
Lng: addressResult.Location.Lng,
Address: addressResult.Address,
Language: languageEnum,
Provider: g.provider.GetName(),
RawResponse: rawResponse,
ExpiresAt: now.Add(time.Duration(g.cacheExpiryDays) * time.Hour * 24),
CreatedAt: now,
})
}
return addressResult, nil
}
func (g *Geocoding) ReverseGeocode(ctx context.Context, ormService *beeorm.Engine, latLng *LatLng, language Language) (*Address, error) {
languageEnum, ok := languageToEnumMapping[language]
if !ok {
return nil, fmt.Errorf("language %s not supported", language)
}
cacheLat := latLng.Lat
cacheLng := latLng.Lng
if g.useCacheLatLngFloatingPointPrecision {
cacheLat = helper.ToFixed(cacheLat, g.cacheLatLngFloatingPointPrecision)
cacheLng = helper.ToFixed(cacheLng, g.cacheLatLngFloatingPointPrecision)
}
if g.useCaching {
reverseGeocodingEntity := &entity.ReverseGeocodingEntity{}
if ormService.CachedSearchOne(reverseGeocodingEntity, "CachedQueryLatLngLanguage", cacheLat, cacheLng, language) {
return &Address{
FromCache: true,
Address: reverseGeocodingEntity.Address,
Language: Language(reverseGeocodingEntity.Language),
Location: &LatLng{
Lat: reverseGeocodingEntity.Lat,
Lng: reverseGeocodingEntity.Lng,
},
}, nil
}
}
addressResult, rawResponse, err := g.provider.ReverseGeocode(ctx, latLng, language)
if err != nil {
return nil, err
}
now := g.clock.Now()
if g.useCaching {
ormService.Flush(&entity.ReverseGeocodingEntity{
Lat: cacheLat,
Lng: cacheLng,
Address: addressResult.Address,
Language: languageEnum,
Provider: g.provider.GetName(),
RawResponse: rawResponse,
ExpiresAt: now.Add(time.Duration(g.cacheExpiryDays) * time.Hour * 24),
CreatedAt: now,
})
}
return addressResult, nil
}