weather-station/internal/database/stations_coords.go

39 lines
940 B
Go

package database
import (
"context"
"database/sql"
)
// StationCoord holds a station_id with geographic coordinates.
type StationCoord struct {
StationID string
Lat float64
Lon float64
}
// ListWH65LPStationsWithLatLon returns WH65LP stations that have non-null and non-zero lat/lon.
func ListWH65LPStationsWithLatLon(ctx context.Context, db *sql.DB) ([]StationCoord, error) {
const q = `
SELECT station_id, latitude, longitude
FROM stations
WHERE device_type = 'WH65LP'
AND latitude IS NOT NULL AND longitude IS NOT NULL
AND latitude <> 0 AND longitude <> 0
ORDER BY station_id`
rows, err := db.QueryContext(ctx, q)
if err != nil {
return nil, err
}
defer rows.Close()
var out []StationCoord
for rows.Next() {
var s StationCoord
if err := rows.Scan(&s.StationID, &s.Lat, &s.Lon); err != nil {
continue
}
out = append(out, s)
}
return out, nil
}