diff --git a/platform/geocode/geocode.go b/platform/geocode/geocode.go index 8dcfc4b4..1d42e9a8 100644 --- a/platform/geocode/geocode.go +++ b/platform/geocode/geocode.go @@ -57,7 +57,7 @@ func EnsureAddress(ctx context.Context, txn bob.Tx, a types.Address, l types.Loc row, err := bob.One(ctx, txn, psql.Insert( im.Into("address", "country", "created", "h3cell", "id", "locality", "location", "number_", "postal_code", "region", "street", "unit"), im.Values( - psql.Arg(a.Country), + psql.Arg(a.CountryEnum()), psql.Arg(created), psql.Arg(cell), psql.Raw("DEFAULT"), diff --git a/platform/publicreport/image.go b/platform/publicreport/image.go index 35f4f46c..4188c004 100644 --- a/platform/publicreport/image.go +++ b/platform/publicreport/image.go @@ -33,8 +33,9 @@ func loadImagesForReportNuisance(ctx context.Context, org_id int32, report_ids [ rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select( sm.Columns( "i.storage_uuid AS uuid", - "COALESCE(ST_X(location), 0) AS \"location.longitude\"", - "COALESCE(ST_Y(location), 0) AS \"location.latitude\"", + "COALESCE(ST_X(i.location), 0) AS \"location.longitude\"", + "COALESCE(ST_Y(i.location), 0) AS \"location.latitude\"", + "ST_Distance(i.location::geography, n.location::geography) AS \"distance_to_reporter_meters\"", "COALESCE(MAX(e.value) FILTER (WHERE e.name = 'Make'), '') AS exif_make", "COALESCE(MAX(e.value) FILTER (WHERE e.name = 'Model'), '') AS exif_model", "COALESCE(MAX(e.value) FILTER (WHERE e.name = 'DateTime'), '') AS exif_datetime", @@ -49,11 +50,15 @@ func loadImagesForReportNuisance(ctx context.Context, org_id int32, report_ids [ psql.Quote("ni", "image_id"), psql.Quote("i", "id"), ), + sm.InnerJoin("publicreport.nuisance").As("n").OnEQ( + psql.Quote("ni", "nuisance_id"), + psql.Quote("n", "id"), + ), sm.Where(psql.Quote("ni", "nuisance_id").EQ(psql.Any(report_ids))), sm.GroupBy( //psql.Quote("i", "id"), //psql.Quote("ni", "nuisance_id"), - psql.Raw("i.id, ni.nuisance_id"), + psql.Raw("i.id, ni.nuisance_id, n.location"), ), ), scan.StructMapper[types.Image]()) if err != nil { diff --git a/platform/types/image.go b/platform/types/image.go index 81e7da7d..ac85631a 100644 --- a/platform/types/image.go +++ b/platform/types/image.go @@ -2,6 +2,8 @@ package types import ( "encoding/json" + "fmt" + "time" "github.com/Gleipnir-Technology/nidus-sync/config" "github.com/google/uuid" @@ -12,19 +14,41 @@ type Exif struct { Make string `json:"make"` Model string `json:"model"` } + +func (e *Exif) MarshalJSON() ([]byte, error) { + to_marshal := make(map[string]interface{}, 0) + if e.Created != "" { + layout := "2006:01:02 15:04:05" + + t, err := time.Parse(layout, e.Created) + if err != nil { + fmt.Println("Error parsing date:", err) + return nil, fmt.Errorf("parse created exif: %w", err) + } + to_marshal["created"] = t + } else { + to_marshal["created"] = e.Created + } + to_marshal["make"] = e.Make + to_marshal["model"] = e.Model + return json.Marshal(to_marshal) +} + type Image struct { - Exif Exif `db:"-" json:"exif"` - ExifMake string `db:"exif_make" json:"-"` - ExifModel string `db:"exif_model" json:"-"` - ExifDateTime string `db:"exif_datetime" json:"-"` - Location Location `db:"location"` - NuisanceID int32 `db:"nuisance_id"` - URLContent string `db:"-" json:"url_content"` - UUID uuid.UUID `db:"uuid"` + DistanceToReporterMeters float64 `db:"distance_to_reporter_meters"` + Exif Exif `db:"-" json:"exif"` + ExifMake string `db:"exif_make" json:"-"` + ExifModel string `db:"exif_model" json:"-"` + ExifDateTime string `db:"exif_datetime" json:"-"` + Location Location `db:"location"` + NuisanceID int32 `db:"nuisance_id"` + URLContent string `db:"-" json:"url_content"` + UUID uuid.UUID `db:"uuid"` } func (i *Image) MarshalJSON() ([]byte, error) { to_marshal := make(map[string]interface{}, 0) + to_marshal["distance_to_reporter_meters"] = i.DistanceToReporterMeters to_marshal["exif"] = Exif{ Created: i.ExifDateTime, Make: i.ExifMake,