photoprism/internal/search/geojson_result.go

95 lines
2.2 KiB
Go
Raw Normal View History

2021-11-26 13:59:10 +01:00
package search
import (
"time"
"github.com/gin-gonic/gin"
geojson "github.com/paulmach/go.geojson"
"github.com/photoprism/photoprism/internal/entity"
)
// GeoResult represents a photo geo search result.
type GeoResult struct {
ID string `json:"-"`
PhotoUID string `json:"UID"`
PhotoType string `json:"Type,omitempty"`
PhotoLat float32 `json:"Lat"`
PhotoLng float32 `json:"Lng"`
PhotoTitle string `json:"Title"`
PhotoDescription string `json:"Description,omitempty"`
PhotoFavorite bool `json:"Favorite,omitempty"`
FileHash string `json:"Hash"`
FileWidth int `json:"Width"`
FileHeight int `json:"Height"`
TakenAt time.Time `json:"TakenAt"`
}
// Lat returns the position latitude.
func (photo GeoResult) Lat() float64 {
return float64(photo.PhotoLat)
}
// Lng returns the position longitude.
func (photo GeoResult) Lng() float64 {
return float64(photo.PhotoLng)
}
// GeoResults represents a list of geo search results.
type GeoResults []GeoResult
// MarshalJSON returns results as Geo.
func (photos GeoResults) MarshalJSON() ([]byte, error) {
fc := geojson.NewFeatureCollection()
bbox := make([]float64, 4)
bboxMin := func(pos int, val float64) {
if bbox[pos] == 0.0 || bbox[pos] > val {
bbox[pos] = val
}
}
bboxMax := func(pos int, val float64) {
if bbox[pos] == 0.0 || bbox[pos] < val {
bbox[pos] = val
}
}
for _, p := range photos {
bboxMin(0, p.Lng())
bboxMin(1, p.Lat())
bboxMax(2, p.Lng())
bboxMax(3, p.Lat())
props := gin.H{
"UID": p.PhotoUID,
"Hash": p.FileHash,
"Width": p.FileWidth,
"Height": p.FileHeight,
"TakenAt": p.TakenAt,
"Title": p.PhotoTitle,
}
if p.PhotoDescription != "" {
props["Description"] = p.PhotoDescription
}
if p.PhotoType != entity.TypeImage && p.PhotoType != entity.TypeDefault {
props["Type"] = p.PhotoType
}
if p.PhotoFavorite {
props["Favorite"] = true
}
feat := geojson.NewPointFeature([]float64{p.Lng(), p.Lat()})
feat.ID = p.ID
feat.Properties = props
fc.AddFeature(feat)
}
fc.BoundingBox = bbox
return fc.MarshalJSON()
}