Skip to content

Commit

Permalink
Now stores the whole nominatim response in teh cache for later proces…
Browse files Browse the repository at this point in the history
…sing
  • Loading branch information
amnuts committed Oct 30, 2023
1 parent 570357f commit cbe8716
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (cache *CacheDatabase) initCache() error {
division TEXT NOT NULL,
city TEXT NOT NULL,
place TEXT NOT NULL,
url TEXT NOT NULL
url TEXT NOT NULL,
json JSON NOT NULL
);`

_, err := cache.DB.Exec(createTableSql)
Expand All @@ -40,8 +41,8 @@ func (cache *CacheDatabase) initCache() error {
}

func (cache *CacheDatabase) InsertLocation(url string, location GeoLocationMetadata) error {
stmt, err := cache.DB.Prepare("INSERT INTO locations(geohash, latitude, longitude, country, division, city, place, url) VALUES(?, ?, ?, ?, ?, ?, ?, ?)")
_, err = stmt.Exec(location.Geohash, location.Latitude, location.Longitude, location.Country, location.Division, location.City, location.Place, url)
stmt, err := cache.DB.Prepare("INSERT INTO locations(geohash, latitude, longitude, country, division, city, place, url, json) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")
_, err = stmt.Exec(location.Geohash, location.Latitude, location.Longitude, location.Country, location.Division, location.City, location.Place, url, location.Json)
return err
}

Expand All @@ -55,8 +56,9 @@ func (cache *CacheDatabase) GetLocation(geohash string) (*GeoLocationMetadata, e
var rowCity string
var rowPlace string
var rowUrl string
var rowJson string

if err := cache.DB.QueryRow("SELECT * FROM locations WHERE geohash = ?", geohash).Scan(&rowGeohash, &rowLatitude, &rowLongitude, &rowCountry, &rowDivision, &rowCity, &rowPlace, &rowUrl); err != nil {
if err := cache.DB.QueryRow("SELECT * FROM locations WHERE geohash = ?", geohash).Scan(&rowGeohash, &rowLatitude, &rowLongitude, &rowCountry, &rowDivision, &rowCity, &rowPlace, &rowUrl, &rowJson); err != nil {
return nil, err
}

Expand All @@ -69,6 +71,7 @@ func (cache *CacheDatabase) GetLocation(geohash string) (*GeoLocationMetadata, e
Division: rowDivision,
City: rowCity,
Place: rowPlace,
Json: rowJson,
}

return location, nil
Expand Down
2 changes: 2 additions & 0 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export namespace main {
division: string;
city: string;
place: string;
json: string;

static createFrom(source: any = {}) {
return new GeoLocationMetadata(source);
Expand All @@ -36,6 +37,7 @@ export namespace main {
this.division = source["division"];
this.city = source["city"];
this.place = source["place"];
this.json = source["json"];
}
}
export class ImageMetadata {
Expand Down
14 changes: 14 additions & 0 deletions path_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type GeoLocationMetadata struct {
Division string `json:"division"`
City string `json:"city"`
Place string `json:"place"`
Json string `json:"json"`
}

type PlaceholderMap map[string]string
Expand Down Expand Up @@ -275,15 +276,24 @@ func RelocateFiles(a *App) {

func DoLocationLookup(latitude float64, longitude float64, hash string, cache *CacheDatabase) *GeoLocationMetadata {
type LocationResponseStruct struct {
PlaceId int `json:"place_id"`
License string `json:"licence"`
OsmType string `json:"osm_type"`
OsmId int `json:"osm_id"`
Latitude string `json:"lat"`
Longitude string `json:"lon"`
Name string `json:"name"`
Category string `json:"category"`
Type string `json:"type"`
AddressType string `json:"addresstype"`
DisplayName string `json:"display_name"`
PlaceRank int `json:"place_rank"`
Importance float64
Address struct {
Amenity string `json:"amenity"`
Leisure string `json:"leisure"`
Road string `json:"road"`
Residential string `json:"residential"`
Village string `json:"village"`
Suburb string `json:"suburb"`
Town string `json:"town"`
Expand All @@ -296,6 +306,7 @@ func DoLocationLookup(latitude float64, longitude float64, hash string, cache *C
Country string `json:"country"`
CountryCode string `json:"country_code"`
} `json:"address"`
BoundingBox []string `json:"boundingbox"`
}

// Check the cache first
Expand Down Expand Up @@ -326,12 +337,15 @@ func DoLocationLookup(latitude float64, longitude float64, hash string, cache *C
// Construct GeoLocationMetadata from LocationResponse
locationResponse := &LocationResponseStruct{}
_ = json.NewDecoder(resp.Body).Decode(locationResponse)
jsonMarshaled, _ := json.Marshal(locationResponse)

data := GeoLocationMetadata{
Latitude: latitude,
Longitude: longitude,
Country: locationResponse.Address.Country,
Place: locationResponse.Name,
Geohash: hash,
Json: string(jsonMarshaled),
}

// Sort out some (hopefully) sensible mappings
Expand Down

0 comments on commit cbe8716

Please sign in to comment.