Skip to content

Commit

Permalink
locode-db: override collisions with file
Browse files Browse the repository at this point in the history
Add file override.csv, add logic to override longitude and latitude in record if it already exists and made the paths to be processed in order, so that the override file would be the last one.

Signed-off-by: Andrey Butusov <andrey@nspcc.io>
  • Loading branch information
End-rey committed Aug 13, 2024
1 parent 2a5ce57 commit 17e3b22
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ generate: unlocode in/airports.dat in/countries.dat in/continents.geojson $(LOCO
--airports in/airports.dat \
--continents in/continents.geojson \
--countries in/countries.dat \
--in in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart1.csv,in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart2.csv,in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart3.csv \
--in in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart1.csv \
--in in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart2.csv \
--in in/$(UNLOCODEPREFIX)\ UNLOCODE\ CodeListPart3.csv \
--in override.csv \
--subdiv in/$(UNLOCODEPREFIX)\ SubdivisionCodes.csv \
--out $(LOCODEDB)

Expand Down
10 changes: 6 additions & 4 deletions internal/parsers/db/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (db *CsvDB) Put(data []Data) error {
newRecordsLocode := make([][]string, 0, len(data))
newRecordsCountry := make([][]string, 0, 300)

uniqueKeys := make(map[string]struct{}, len(data))
uniqueKeys := make(map[string]int, len(data))

Check warning on line 29 in internal/parsers/db/put.go

View check run for this annotation

Codecov / codecov/patch

internal/parsers/db/put.go#L29

Added line #L29 was not covered by tests
uniqueKeysCountry := make(map[string]struct{}, 300)

for _, row := range data {
Expand All @@ -36,12 +36,14 @@ func (db *CsvDB) Put(data []Data) error {
// Calculate a unique index for each key
keyString := key.CountryCode().String() + key.LocationCode().String()

if _, exists := uniqueKeys[keyString]; exists {
continue // Skip duplicates
if index, exists := uniqueKeys[keyString]; exists {
newRecordsLocode[index][5] = strconv.FormatFloat(float64(rec.Point.Latitude), 'f', -1, 32)
newRecordsLocode[index][6] = strconv.FormatFloat(float64(rec.Point.Longitude), 'f', -1, 32)
continue

Check warning on line 42 in internal/parsers/db/put.go

View check run for this annotation

Codecov / codecov/patch

internal/parsers/db/put.go#L39-L42

Added lines #L39 - L42 were not covered by tests
}

// Mark the index as seen
uniqueKeys[keyString] = struct{}{}
uniqueKeys[keyString] = len(newRecordsLocode)

Check warning on line 46 in internal/parsers/db/put.go

View check run for this annotation

Codecov / codecov/patch

internal/parsers/db/put.go#L46

Added line #L46 was not covered by tests

newRecord := []string{
keyString,
Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/table/csv/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func New(prm Prm, opts ...Option) *Table {
}

return &Table{
paths: append(o.extraPaths, prm.Path),
paths: append([]string{prm.Path}, o.extraPaths...),

Check warning on line 71 in internal/parsers/table/csv/table.go

View check run for this annotation

Codecov / codecov/patch

internal/parsers/table/csv/table.go#L71

Added line #L71 was not covered by tests
mode: o.mode,
subDivPath: prm.SubDivPath,
}
Expand Down

0 comments on commit 17e3b22

Please sign in to comment.