Skip to content

Instantly share code, notes, and snippets.

@prl900
Created April 12, 2018 06:56
Show Gist options
  • Save prl900/212969ead1320b309359194986247a1f to your computer and use it in GitHub Desktop.
Save prl900/212969ead1320b309359194986247a1f to your computer and use it in GitHub Desktop.
package main
import (
"encoding/gob"
"fmt"
"net/http"
"strconv"
itsk "github.com/prl900/intersekt"
"google.golang.org/appengine"
)
const (
sinuProj = "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs "
tileSize = 2400
)
var modisGeot = []float64{-20015109.3539999984204769, 463.3127165279165069, 0.0, 10007554.677005993, 0.0, -463.3127165279165069}
func GetChunkLoop(i, j int) (*itsk.Loop, error) {
id := float64(i)
jd := float64(j)
tl := itsk.Point{modisGeot[0] + id*tileSize*modisGeot[1], modisGeot[3] + jd*tileSize*modisGeot[5]}
tr := itsk.Point{modisGeot[0] + (id+1)*tileSize*modisGeot[1], modisGeot[3] + jd*tileSize*modisGeot[5]}
br := itsk.Point{modisGeot[0] + (id+1)*tileSize*modisGeot[1], modisGeot[3] + (jd+1)*tileSize*modisGeot[5]}
bl := itsk.Point{modisGeot[0] + id*tileSize*modisGeot[1], modisGeot[3] + (jd+1)*tileSize*modisGeot[5]}
return itsk.NewLoop([]itsk.Point{tl, tr, br, bl}, j, i)
}
func GetLoops() ([]itsk.Loop, error) {
loops := make([]itsk.Loop, 36*18)
for i := 0; i < 36; i++ {
for j := 0; j < 18; j++ {
l, err := GetChunkLoop(i, j)
if err != nil {
return loops, err
}
loops[36*j+i] = *l
}
}
return loops, nil
}
func GetMCD43A4Objects(x0, y0, res float64, xSize, ySize int) ([]string, error) {
out := []string{}
loops, err := GetLoops()
if err != nil {
return out, err
}
tl := itsk.Point{x0, y0}
tr := itsk.Point{x0 + float64(xSize)*res, y0}
br := itsk.Point{x0 + float64(xSize)*res, y0 - float64(ySize)*res}
bl := itsk.Point{x0, y0 - float64(ySize)*res}
a, err := itsk.NewLoop([]itsk.Point{tl, tr, br, bl}, 0, 0)
if err != nil {
return out, err
}
b := a.Transform(sinuProj)
//sa := a.Segmentize(2)
//sb := sa.Transform(sinuProj)
for _, l := range loops {
if b.Intersects(&l) {
out = append(out, fmt.Sprintf("MCD43A4.A2017007.h%02dv%02d.006_B01.TIF", l.H, l.V))
}
}
return out, nil
}
func handle(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
xSize, err := strconv.Atoi(q.Get("xsize"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Malformed request!"))
return
}
ySize, err := strconv.Atoi(q.Get("ysize"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Malformed request!"))
return
}
x0, err := strconv.ParseFloat(q.Get("x0"), 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Malformed request!"))
return
}
y0, err := strconv.ParseFloat(q.Get("y0"), 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Malformed request!"))
return
}
res, err := strconv.ParseFloat(q.Get("res"), 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("400 - Malformed request!"))
return
}
objs, err := GetMCD43A4Objects(x0, y0, res, xSize, ySize)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
return
}
w.Header().Set("Content-Type", "application/octet-stream")
enc := gob.NewEncoder(w)
err = enc.Encode(objs)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
}
func main() {
http.HandleFunc("/", handle)
appengine.Main()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment