2020-01-15 04:04:33 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-05-23 20:58:58 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-05-08 15:41:01 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2020-01-15 04:04:33 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
|
2020-01-15 19:59:25 +01:00
|
|
|
geojson "github.com/paulmach/go.geojson"
|
2020-01-15 04:04:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GET /api/v1/geo
|
|
|
|
func GetGeo(router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
router.GET("/geo", func(c *gin.Context) {
|
2020-01-22 13:43:07 +01:00
|
|
|
if Unauthorized(c, conf) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, ErrUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:04:33 +01:00
|
|
|
var f form.GeoSearch
|
|
|
|
|
|
|
|
err := c.MustBindWith(&f, binding.Form)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
photos, err := query.Geo(f)
|
2020-01-15 04:04:33 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fc := geojson.NewFeatureCollection()
|
|
|
|
|
|
|
|
bbox := make([]float64, 4)
|
|
|
|
|
2020-01-15 19:59:25 +01:00
|
|
|
bboxMin := func(pos int, val float64) {
|
2020-01-15 04:04:33 +01:00
|
|
|
if bbox[pos] == 0.0 || bbox[pos] > val {
|
|
|
|
bbox[pos] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:59:25 +01:00
|
|
|
bboxMax := func(pos int, val float64) {
|
2020-01-15 04:04:33 +01:00
|
|
|
if bbox[pos] == 0.0 || bbox[pos] < val {
|
|
|
|
bbox[pos] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range photos {
|
2020-04-26 11:41:54 +02:00
|
|
|
bboxMin(0, p.Lng())
|
|
|
|
bboxMin(1, p.Lat())
|
|
|
|
bboxMax(2, p.Lng())
|
|
|
|
bboxMax(3, p.Lat())
|
2020-01-15 04:04:33 +01:00
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:41:54 +02:00
|
|
|
feat := geojson.NewPointFeature([]float64{p.Lng(), p.Lat()})
|
2020-01-15 19:45:54 +01:00
|
|
|
feat.ID = p.ID
|
2020-05-23 20:58:58 +02:00
|
|
|
feat.Properties = props
|
2020-01-15 04:04:33 +01:00
|
|
|
fc.AddFeature(feat)
|
|
|
|
}
|
|
|
|
|
|
|
|
fc.BoundingBox = bbox
|
|
|
|
|
|
|
|
resp, err := fc.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(400, gin.H{"error": txt.UcFirst(err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data(http.StatusOK, "application/json", resp)
|
|
|
|
})
|
|
|
|
}
|