diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40467ef --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/turbomaps-er diff --git a/cmd/run.go b/cmd/run.go index 9337170..14373f1 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -31,9 +31,10 @@ func Run(ctx context.Context) error { } `json:"properties"` Geometry struct { Type string `json:"type"` - Coordinates []any `json:"coordinates"` + Coordinates any `json:"coordinates"` } `json:"geometry"` } + type Area [1][5][2]float64 geoJsons := []geoJson{} for _, town := range towns { for search, area := range searches { @@ -59,7 +60,7 @@ func Run(ctx context.Context) error { if *doArea { a.Geometry.Type = "Polygon" x, y := results[i].Lng, results[i].Lat - a.Geometry.Coordinates = []any{[]any{ + a.Geometry.Coordinates = Area{{ [2]float64{x, y + radiusY}, // top [2]float64{x + radiusX, y}, // right [2]float64{x, y - radiusY}, // bot @@ -78,6 +79,25 @@ func Run(ctx context.Context) error { geoJsons = slices.CompactFunc(geoJsons, func(a, b geoJson) bool { return fmt.Sprint(a.Geometry.Coordinates) == fmt.Sprint(b.Geometry.Coordinates) }) + if *doArea { + for i := 0; i < len(geoJsons); i++ { + areaI := geoJsons[i].Geometry.Coordinates.(Area)[0] + j := i + 1 + for j < len(geoJsons) { + areaJ := geoJsons[j].Geometry.Coordinates.(Area)[0] + // if diamond i contains j, then drop j + iAbove := areaI[0][1] > areaJ[0][1] + iRight := areaI[1][0] > areaJ[1][0] + iBelow := areaI[2][1] < areaJ[2][1] + iLeft := areaI[3][0] < areaJ[3][0] + if iAbove && iRight && iBelow && iLeft { + geoJsons = append(geoJsons[:j], geoJsons[j+1:]...) + } else { + j += 1 + } + } + } + } log.Println("COMPACTED", len(geoJsons)) b, _ := json.Marshal(map[string]any{ "features": geoJsons,