accept flags and -d for a box
This commit is contained in:
@@ -39,7 +39,7 @@ type Location struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Maps) textSearchOne(ctx context.Context, query string) (Location, error) {
|
func (m *Maps) textSearchOne(ctx context.Context, query string) (Location, error) {
|
||||||
results, err := m.Search(ctx, query)
|
results, err := m.Search(ctx, query, 1.0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Location{}, err
|
return Location{}, err
|
||||||
} else if len(results) < 1 {
|
} else if len(results) < 1 {
|
||||||
@@ -48,12 +48,13 @@ func (m *Maps) textSearchOne(ctx context.Context, query string) (Location, error
|
|||||||
return results[0], nil
|
return results[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var convertToMiles = 69.0
|
||||||
|
|
||||||
func (m *Maps) Search(ctx context.Context, query string, radius_miles float64) ([]Location, error) {
|
func (m *Maps) Search(ctx context.Context, query string, radius_miles float64) ([]Location, error) {
|
||||||
results, err := m.search(ctx, query)
|
results, err := m.search(ctx, query)
|
||||||
for i := len(results) - 1; i >= 0; i-- {
|
for i := len(results) - 1; i >= 0; i-- {
|
||||||
shouldKeep := true
|
shouldKeep := true
|
||||||
if m.around != (Location{}) {
|
if m.around != (Location{}) {
|
||||||
convertToMiles := 69.0
|
|
||||||
a := m.around.Lat - results[i].Lat
|
a := m.around.Lat - results[i].Lat
|
||||||
b := m.around.Lng - results[i].Lng
|
b := m.around.Lng - results[i].Lng
|
||||||
dist := math.Sqrt(a*a + b*b)
|
dist := math.Sqrt(a*a + b*b)
|
||||||
|
|||||||
33
cmd/run.go
33
cmd/run.go
@@ -3,18 +3,30 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run(ctx context.Context) error {
|
func Run(ctx context.Context) error {
|
||||||
m, err := NewMapsOf(ctx, os.Args[1])
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := m.Search(ctx, os.Args[2], 5.0)
|
results, err := m.Search(ctx, fs.Args()[1], *searchRadius)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -26,15 +38,26 @@ func Run(ctx context.Context) error {
|
|||||||
} `json:"properties"`
|
} `json:"properties"`
|
||||||
Geometry struct {
|
Geometry struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Coordinates []float64 `json:"coordinates"`
|
Coordinates []any `json:"coordinates"`
|
||||||
} `json:"geometry"`
|
} `json:"geometry"`
|
||||||
}
|
}
|
||||||
geoJsons := make([]geoJson, len(results))
|
geoJsons := make([]geoJson, len(results))
|
||||||
for i := range results {
|
for i := range results {
|
||||||
geoJsons[i].Type = "Feature"
|
geoJsons[i].Type = "Feature"
|
||||||
geoJsons[i].Properties.Name = path.Join(os.Args[2], results[i].Name)
|
geoJsons[i].Properties.Name = path.Join(fs.Args()[1], results[i].Name)
|
||||||
geoJsons[i].Geometry.Type = "Point"
|
geoJsons[i].Geometry.Type = "Point"
|
||||||
geoJsons[i].Geometry.Coordinates = []float64{results[i].Lng, results[i].Lat}
|
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{
|
b, _ := json.Marshal(map[string]any{
|
||||||
"features": geoJsons,
|
"features": geoJsons,
|
||||||
|
|||||||
Reference in New Issue
Block a user