multi town
This commit is contained in:
62
cmd/run.go
62
cmd/run.go
@@ -5,8 +5,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(ctx context.Context) error {
|
func Run(ctx context.Context) error {
|
||||||
@@ -14,6 +17,8 @@ func Run(ctx context.Context) error {
|
|||||||
searchRadius := fs.Float64("r", 5.0, "search radius in miles")
|
searchRadius := fs.Float64("r", 5.0, "search radius in miles")
|
||||||
area := fs.Float64("a", 0.5, "result radius in miles")
|
area := fs.Float64("a", 0.5, "result radius in miles")
|
||||||
doArea := fs.Bool("d", false, "do result radius in miles")
|
doArea := fs.Bool("d", false, "do result radius in miles")
|
||||||
|
var towns FlagStringArray
|
||||||
|
fs.Var(&towns, "t", "list of towns to search around")
|
||||||
if err := fs.Parse(os.Args[1:]); err != nil {
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -21,16 +26,6 @@ func Run(ctx context.Context) error {
|
|||||||
radiusX := 2 * radius / 2
|
radiusX := 2 * radius / 2
|
||||||
radiusY := 2 * radius / 3
|
radiusY := 2 * 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 geoJson struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Properties struct {
|
Properties struct {
|
||||||
@@ -41,16 +36,28 @@ func Run(ctx context.Context) error {
|
|||||||
Coordinates []any `json:"coordinates"`
|
Coordinates []any `json:"coordinates"`
|
||||||
} `json:"geometry"`
|
} `json:"geometry"`
|
||||||
}
|
}
|
||||||
geoJsons := make([]geoJson, len(results))
|
geoJsons := []geoJson{}
|
||||||
|
for _, town := range towns {
|
||||||
|
m, err := NewMapsOf(ctx, town)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := m.Search(ctx, fs.Args()[0], *searchRadius)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for i := range results {
|
for i := range results {
|
||||||
geoJsons[i].Type = "Feature"
|
var a geoJson
|
||||||
geoJsons[i].Properties.Name = path.Join(fs.Args()[1], results[i].Name)
|
a.Type = "Feature"
|
||||||
geoJsons[i].Geometry.Type = "Point"
|
a.Properties.Name = path.Join(fs.Args()[0], results[i].Name)
|
||||||
geoJsons[i].Geometry.Coordinates = []any{results[i].Lng, results[i].Lat}
|
a.Geometry.Type = "Point"
|
||||||
|
a.Geometry.Coordinates = []any{results[i].Lng, results[i].Lat}
|
||||||
if *doArea {
|
if *doArea {
|
||||||
geoJsons[i].Geometry.Type = "Polygon"
|
a.Geometry.Type = "Polygon"
|
||||||
x, y := results[i].Lng, results[i].Lat
|
x, y := results[i].Lng, results[i].Lat
|
||||||
geoJsons[i].Geometry.Coordinates = []any{[]any{
|
a.Geometry.Coordinates = []any{[]any{
|
||||||
[2]float64{x, y + radiusY}, // top
|
[2]float64{x, y + radiusY}, // top
|
||||||
[2]float64{x + radiusX, y}, // right
|
[2]float64{x + radiusX, y}, // right
|
||||||
[2]float64{x, y - radiusY}, // bot
|
[2]float64{x, y - radiusY}, // bot
|
||||||
@@ -58,7 +65,17 @@ func Run(ctx context.Context) error {
|
|||||||
[2]float64{x, y + radiusY}, // top
|
[2]float64{x, y + radiusY}, // top
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
geoJsons = append(geoJsons, a)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
log.Println(len(geoJsons))
|
||||||
|
slices.SortFunc(geoJsons, func(a, b geoJson) int {
|
||||||
|
return strings.Compare(fmt.Sprint(a.Geometry.Coordinates), fmt.Sprint(b.Geometry.Coordinates))
|
||||||
|
})
|
||||||
|
geoJsons = slices.CompactFunc(geoJsons, func(a, b geoJson) bool {
|
||||||
|
return fmt.Sprint(a.Geometry.Coordinates) == fmt.Sprint(b.Geometry.Coordinates)
|
||||||
|
})
|
||||||
|
log.Println("COMPACTED", len(geoJsons))
|
||||||
b, _ := json.Marshal(map[string]any{
|
b, _ := json.Marshal(map[string]any{
|
||||||
"features": geoJsons,
|
"features": geoJsons,
|
||||||
"type": "FeatureCollection",
|
"type": "FeatureCollection",
|
||||||
@@ -67,3 +84,14 @@ func Run(ctx context.Context) error {
|
|||||||
|
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FlagStringArray []string
|
||||||
|
|
||||||
|
func (array *FlagStringArray) String() string {
|
||||||
|
return strings.Join(*array, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (array *FlagStringArray) Set(s string) error {
|
||||||
|
*array = append(*array, s)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user