Files
turbomaps-er/cmd/run.go
2026-03-14 22:40:20 -06:00

70 lines
1.7 KiB
Go

package cmd
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"path"
)
func Run(ctx context.Context) error {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
searchRadius := fs.Float64("r", 5.0, "search radius in miles")
area := fs.Float64("a", 0.5, "result radius in miles")
doArea := fs.Bool("d", false, "do result radius in miles")
if err := fs.Parse(os.Args[1:]); err != nil {
panic(err)
}
radius := *area / convertToMiles
radiusX := radius / 2
radiusY := radius / 3
m, err := NewMapsOf(ctx, fs.Args()[0])
if err != nil {
return err
}
results, err := m.Search(ctx, fs.Args()[1], *searchRadius)
if err != nil {
return err
}
type geoJson struct {
Type string `json:"type"`
Properties struct {
Name string `json:"name"`
} `json:"properties"`
Geometry struct {
Type string `json:"type"`
Coordinates []any `json:"coordinates"`
} `json:"geometry"`
}
geoJsons := make([]geoJson, len(results))
for i := range results {
geoJsons[i].Type = "Feature"
geoJsons[i].Properties.Name = path.Join(fs.Args()[1], results[i].Name)
geoJsons[i].Geometry.Type = "Point"
geoJsons[i].Geometry.Coordinates = []any{results[i].Lng, results[i].Lat}
if *doArea {
geoJsons[i].Geometry.Type = "Polygon"
x, y := results[i].Lng, results[i].Lat
geoJsons[i].Geometry.Coordinates = []any{[]any{
[2]float64{x - radiusX, y + radiusY}, // top left
[2]float64{x + radiusX, y + radiusY}, // top righ
[2]float64{x + radiusX, y - radiusY}, // bot righ
[2]float64{x - radiusX, y - radiusY}, // bot left
[2]float64{x - radiusX, y + radiusY}, // top left
}}
}
}
b, _ := json.Marshal(map[string]any{
"features": geoJsons,
"type": "FeatureCollection",
})
fmt.Printf("%s\n", b)
return ctx.Err()
}