diff --git a/db/dbinfo/address.bob.go b/db/dbinfo/address.bob.go index effe360e..6ad02a97 100644 --- a/db/dbinfo/address.bob.go +++ b/db/dbinfo/address.bob.go @@ -114,24 +114,6 @@ var Addresses = Table[ Generated: false, AutoIncr: false, }, - LocationX: column{ - Name: "location_x", - DBType: "double precision", - Default: "GENERATED", - Comment: "", - Nullable: true, - Generated: true, - AutoIncr: false, - }, - LocationY: column{ - Name: "location_y", - DBType: "double precision", - Default: "GENERATED", - Comment: "", - Nullable: true, - Generated: true, - AutoIncr: false, - }, Gid: column{ Name: "gid", DBType: "text", @@ -141,6 +123,24 @@ var Addresses = Table[ Generated: false, AutoIncr: false, }, + LocationLatitude: column{ + Name: "location_latitude", + DBType: "double precision", + Default: "GENERATED", + Comment: "", + Nullable: true, + Generated: true, + AutoIncr: false, + }, + LocationLongitude: column{ + Name: "location_longitude", + DBType: "double precision", + Default: "GENERATED", + Comment: "", + Nullable: true, + Generated: true, + AutoIncr: false, + }, }, Indexes: addressIndexes{ AddressPkey: index{ @@ -213,25 +213,25 @@ var Addresses = Table[ } type addressColumns struct { - Country column - Created column - Location column - H3cell column - ID column - Locality column - PostalCode column - Street column - Unit column - Region column - Number column - LocationX column - LocationY column - Gid column + Country column + Created column + Location column + H3cell column + ID column + Locality column + PostalCode column + Street column + Unit column + Region column + Number column + Gid column + LocationLatitude column + LocationLongitude column } func (c addressColumns) AsSlice() []column { return []column{ - c.Country, c.Created, c.Location, c.H3cell, c.ID, c.Locality, c.PostalCode, c.Street, c.Unit, c.Region, c.Number, c.LocationX, c.LocationY, c.Gid, + c.Country, c.Created, c.Location, c.H3cell, c.ID, c.Locality, c.PostalCode, c.Street, c.Unit, c.Region, c.Number, c.Gid, c.LocationLatitude, c.LocationLongitude, } } diff --git a/db/migrations/00134_address_location_names.sql b/db/migrations/00134_address_location_names.sql new file mode 100644 index 00000000..46c44a53 --- /dev/null +++ b/db/migrations/00134_address_location_names.sql @@ -0,0 +1,14 @@ +-- +goose Up +ALTER TABLE address + DROP COLUMN location_x, + DROP COLUMN location_y; +ALTER TABLE address + ADD COLUMN location_latitude DOUBLE PRECISION GENERATED ALWAYS AS (ST_Y(location)) STORED, + ADD COLUMN location_longitude DOUBLE PRECISION GENERATED ALWAYS AS (ST_X(location)) STORED; +-- +goose Down +ALTER TABLE address + DROP COLUMN location_latitude, + DROP COLUMN location_longitude; +ALTER TABLE address + ADD COLUMN location_y DOUBLE PRECISION GENERATED ALWAYS AS (ST_Y(location)) STORED, + ADD COLUMN location_x DOUBLE PRECISION GENERATED ALWAYS AS (ST_X(location)) STORED; diff --git a/db/models/address.bob.go b/db/models/address.bob.go index 5f3e612b..2e1ed705 100644 --- a/db/models/address.bob.go +++ b/db/models/address.bob.go @@ -25,20 +25,20 @@ import ( // Address is an object representing the database table. type Address struct { - Country string `db:"country" ` - Created time.Time `db:"created" ` - Location string `db:"location" ` - H3cell string `db:"h3cell" ` - ID int32 `db:"id,pk" ` - Locality string `db:"locality" ` - PostalCode string `db:"postal_code" ` - Street string `db:"street" ` - Unit string `db:"unit" ` - Region string `db:"region" ` - Number string `db:"number_" ` - LocationX null.Val[float64] `db:"location_x,generated" ` - LocationY null.Val[float64] `db:"location_y,generated" ` - Gid string `db:"gid" ` + Country string `db:"country" ` + Created time.Time `db:"created" ` + Location string `db:"location" ` + H3cell string `db:"h3cell" ` + ID int32 `db:"id,pk" ` + Locality string `db:"locality" ` + PostalCode string `db:"postal_code" ` + Street string `db:"street" ` + Unit string `db:"unit" ` + Region string `db:"region" ` + Number string `db:"number_" ` + Gid string `db:"gid" ` + LocationLatitude null.Val[float64] `db:"location_latitude,generated" ` + LocationLongitude null.Val[float64] `db:"location_longitude,generated" ` R addressR `db:"-" ` } @@ -66,43 +66,43 @@ type addressR struct { func buildAddressColumns(alias string) addressColumns { return addressColumns{ ColumnsExpr: expr.NewColumnsExpr( - "country", "created", "location", "h3cell", "id", "locality", "postal_code", "street", "unit", "region", "number_", "location_x", "location_y", "gid", + "country", "created", "location", "h3cell", "id", "locality", "postal_code", "street", "unit", "region", "number_", "gid", "location_latitude", "location_longitude", ).WithParent("address"), - tableAlias: alias, - Country: psql.Quote(alias, "country"), - Created: psql.Quote(alias, "created"), - Location: psql.Quote(alias, "location"), - H3cell: psql.Quote(alias, "h3cell"), - ID: psql.Quote(alias, "id"), - Locality: psql.Quote(alias, "locality"), - PostalCode: psql.Quote(alias, "postal_code"), - Street: psql.Quote(alias, "street"), - Unit: psql.Quote(alias, "unit"), - Region: psql.Quote(alias, "region"), - Number: psql.Quote(alias, "number_"), - LocationX: psql.Quote(alias, "location_x"), - LocationY: psql.Quote(alias, "location_y"), - Gid: psql.Quote(alias, "gid"), + tableAlias: alias, + Country: psql.Quote(alias, "country"), + Created: psql.Quote(alias, "created"), + Location: psql.Quote(alias, "location"), + H3cell: psql.Quote(alias, "h3cell"), + ID: psql.Quote(alias, "id"), + Locality: psql.Quote(alias, "locality"), + PostalCode: psql.Quote(alias, "postal_code"), + Street: psql.Quote(alias, "street"), + Unit: psql.Quote(alias, "unit"), + Region: psql.Quote(alias, "region"), + Number: psql.Quote(alias, "number_"), + Gid: psql.Quote(alias, "gid"), + LocationLatitude: psql.Quote(alias, "location_latitude"), + LocationLongitude: psql.Quote(alias, "location_longitude"), } } type addressColumns struct { expr.ColumnsExpr - tableAlias string - Country psql.Expression - Created psql.Expression - Location psql.Expression - H3cell psql.Expression - ID psql.Expression - Locality psql.Expression - PostalCode psql.Expression - Street psql.Expression - Unit psql.Expression - Region psql.Expression - Number psql.Expression - LocationX psql.Expression - LocationY psql.Expression - Gid psql.Expression + tableAlias string + Country psql.Expression + Created psql.Expression + Location psql.Expression + H3cell psql.Expression + ID psql.Expression + Locality psql.Expression + PostalCode psql.Expression + Street psql.Expression + Unit psql.Expression + Region psql.Expression + Number psql.Expression + Gid psql.Expression + LocationLatitude psql.Expression + LocationLongitude psql.Expression } func (c addressColumns) Alias() string { @@ -1150,20 +1150,20 @@ func (address0 *Address) AttachSite(ctx context.Context, exec bob.Executor, site } type addressWhere[Q psql.Filterable] struct { - Country psql.WhereMod[Q, string] - Created psql.WhereMod[Q, time.Time] - Location psql.WhereMod[Q, string] - H3cell psql.WhereMod[Q, string] - ID psql.WhereMod[Q, int32] - Locality psql.WhereMod[Q, string] - PostalCode psql.WhereMod[Q, string] - Street psql.WhereMod[Q, string] - Unit psql.WhereMod[Q, string] - Region psql.WhereMod[Q, string] - Number psql.WhereMod[Q, string] - LocationX psql.WhereNullMod[Q, float64] - LocationY psql.WhereNullMod[Q, float64] - Gid psql.WhereMod[Q, string] + Country psql.WhereMod[Q, string] + Created psql.WhereMod[Q, time.Time] + Location psql.WhereMod[Q, string] + H3cell psql.WhereMod[Q, string] + ID psql.WhereMod[Q, int32] + Locality psql.WhereMod[Q, string] + PostalCode psql.WhereMod[Q, string] + Street psql.WhereMod[Q, string] + Unit psql.WhereMod[Q, string] + Region psql.WhereMod[Q, string] + Number psql.WhereMod[Q, string] + Gid psql.WhereMod[Q, string] + LocationLatitude psql.WhereNullMod[Q, float64] + LocationLongitude psql.WhereNullMod[Q, float64] } func (addressWhere[Q]) AliasedAs(alias string) addressWhere[Q] { @@ -1172,20 +1172,20 @@ func (addressWhere[Q]) AliasedAs(alias string) addressWhere[Q] { func buildAddressWhere[Q psql.Filterable](cols addressColumns) addressWhere[Q] { return addressWhere[Q]{ - Country: psql.Where[Q, string](cols.Country), - Created: psql.Where[Q, time.Time](cols.Created), - Location: psql.Where[Q, string](cols.Location), - H3cell: psql.Where[Q, string](cols.H3cell), - ID: psql.Where[Q, int32](cols.ID), - Locality: psql.Where[Q, string](cols.Locality), - PostalCode: psql.Where[Q, string](cols.PostalCode), - Street: psql.Where[Q, string](cols.Street), - Unit: psql.Where[Q, string](cols.Unit), - Region: psql.Where[Q, string](cols.Region), - Number: psql.Where[Q, string](cols.Number), - LocationX: psql.WhereNull[Q, float64](cols.LocationX), - LocationY: psql.WhereNull[Q, float64](cols.LocationY), - Gid: psql.Where[Q, string](cols.Gid), + Country: psql.Where[Q, string](cols.Country), + Created: psql.Where[Q, time.Time](cols.Created), + Location: psql.Where[Q, string](cols.Location), + H3cell: psql.Where[Q, string](cols.H3cell), + ID: psql.Where[Q, int32](cols.ID), + Locality: psql.Where[Q, string](cols.Locality), + PostalCode: psql.Where[Q, string](cols.PostalCode), + Street: psql.Where[Q, string](cols.Street), + Unit: psql.Where[Q, string](cols.Unit), + Region: psql.Where[Q, string](cols.Region), + Number: psql.Where[Q, string](cols.Number), + Gid: psql.Where[Q, string](cols.Gid), + LocationLatitude: psql.WhereNull[Q, float64](cols.LocationLatitude), + LocationLongitude: psql.WhereNull[Q, float64](cols.LocationLongitude), } } diff --git a/platform/publicreport/report.go b/platform/publicreport/report.go index e24769cb..87207403 100644 --- a/platform/publicreport/report.go +++ b/platform/publicreport/report.go @@ -139,6 +139,8 @@ func reportQuery() bob.BaseQuery[*dialect.SelectQuery] { "COALESCE(a.country, '') AS \"address.country\"", "a.id AS \"address.id\"", "COALESCE(a.gid, '') AS \"address.gid\"", + "COALESCE(a.location_latitude, 0) AS \"address.location.latitude\"", + "COALESCE(a.location_longitude, 0) AS \"address.location.longitude\"", "COALESCE(a.locality, '') AS \"address.locality\"", "COALESCE(a.number_, '') AS \"address.number\"", "COALESCE(a.postal_code, '') AS \"address.postal_code\"", diff --git a/platform/signal.go b/platform/signal.go index 5a0d1aa0..e3e1f3c6 100644 --- a/platform/signal.go +++ b/platform/signal.go @@ -70,8 +70,8 @@ func SignalCreateFromPublicreport(ctx context.Context, user User, report_id stri return nil, fmt.Errorf("site from address: %w", err) } site_id = site.ID - lat := address.LocationY.GetOr(0.0) - lng := address.LocationX.GetOr(0.0) + lat := address.LocationLatitude.GetOr(0.0) + lng := address.LocationLongitude.GetOr(0.0) location = fmt.Sprintf("POINT(%f %f)", lng, lat) } else if report.LocationLatitude.IsValue() && report.LocationLongitude.IsValue() { lat := report.LocationLatitude.MustGet() @@ -97,8 +97,8 @@ func SignalCreateFromPublicreport(ctx context.Context, user User, report_id stri return nil, fmt.Errorf("find address from raw: %w", err) } site_id = site.ID - lat := address.LocationY.GetOr(0.0) - lng := address.LocationX.GetOr(0.0) + lat := address.LocationLatitude.GetOr(0.0) + lng := address.LocationLongitude.GetOr(0.0) location = fmt.Sprintf("POINT(%f %f)", lng, lat) } else { // We have no structured address, no GPS, no unstructued address. diff --git a/platform/types/address.go b/platform/types/address.go index ccedebd4..682b6e9a 100644 --- a/platform/types/address.go +++ b/platform/types/address.go @@ -25,15 +25,15 @@ func (a Address) String() string { return fmt.Sprintf("%s %s, %s, %s, %s, %s", a.Number, a.Street, a.Locality, a.Region, a.PostalCode, a.Country) } func AddressFromModel(m *models.Address) Address { - log.Debug().Int32("id", m.ID).Float64("lat", m.LocationY.GetOr(0.0)).Float64("lng", m.LocationX.GetOr(0.0)).Msg("converting address") + log.Debug().Int32("id", m.ID).Float64("lat", m.LocationLatitude.GetOr(0.0)).Float64("lng", m.LocationLongitude.GetOr(0.0)).Msg("converting address") return Address{ Country: m.Country, GID: m.Gid, ID: &m.ID, Locality: m.Locality, Location: &Location{ - Latitude: m.LocationY.GetOr(0.0), - Longitude: m.LocationX.GetOr(0.0), + Latitude: m.LocationLatitude.GetOr(0.0), + Longitude: m.LocationLongitude.GetOr(0.0), }, Number: m.Number, PostalCode: m.PostalCode,