geo: offline IP locations and Iran's borders, for the swarm map

DB-IP Lite (CC BY 4.0) for Iranian towns and other countries, Natural
Earth for the borders; scripts/geo/build.py makes both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vvvomitandClaude Opus 5.5 committed 2026-10-30 19:22:59 +03:30
1 parent 5add571571
commit 0ccda18a8d
5 files changed
+344

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
// Package geo places IP addresses on the Host screen's map of Iran, offline:
// Iranian addresses to a town, others to a country. The data is DB-IP Lite
// (https://db-ip.com, CC BY 4.0) and Natural Earth's borders, made by
// scripts/geo/build.py.
package geo
import (
"bufio"
"bytes"
"compress/gzip"
_ "embed"
"encoding/binary"
"io"
"net/netip"
"sort"
"sync"
)
// Credit is the attribution DB-IP's license asks for.
const Credit = "IP locations by DB-IP.com"
// Shape is a country's border ring, lon/lat pairs.
type Shape struct {
Country string
Ring [][2]float64
}
// Place is where an address is.
type Place struct {
Country string // ISO 3166 code
Town string // in Iran only
Province string
Lat, Lon float64
}
//go:embed ip.bin
var raw []byte
type span struct {
start, end uint64
v int
}
type table struct {
places []Place
countries []Place
ir4, ir6 []span
world []span
}
var (
once sync.Once
data *table
)
func load() *table {
once.Do(func() {
zr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
panic(err)
}
r := bufio.NewReader(zr)
var magic [4]byte
if _, err := io.ReadFull(r, magic[:]); err != nil || string(magic[:]) != "IZG1" {
panic("geo: bad ip.bin")
}
u := func() uint64 { n, _ := binary.ReadUvarint(r); return n }
s := func() float64 { n, _ := binary.ReadVarint(r); return float64(n) / 100 }
str := func() string {
b := make([]byte, u())
_, _ = io.ReadFull(r, b)
return string(b)
}
t := &table{}
for n := u(); n > 0; n-- {
lat, lon := s(), s()
town := str()
t.places = append(t.places, Place{Country: "IR", Lat: lat, Lon: lon, Town: town, Province: str()})
}
for n := u(); n > 0; n-- {
var cc [2]byte
_, _ = io.ReadFull(r, cc[:])
lat, lon := s(), s()
t.countries = append(t.countries, Place{Country: string(cc[:]), Lat: lat, Lon: lon})
}
spans := func() []span {
out := make([]span, u())
last := ^uint64(0) // -1, so the first gap is the first start
for i := range out {
a := last + 1 + u()
b := a + u()
out[i], last = span{a, b, int(u())}, b
}
return out
}
t.ir4, t.ir6, t.world = spans(), spans(), spans()
data = t
})
return data
}
func find(spans []span, ip uint64) (int, bool) {
i := sort.Search(len(spans), func(i int) bool { return spans[i].end >= ip })
if i < len(spans) && spans[i].start <= ip {
return spans[i].v, true
}
return 0, false
}
// Locate says where an address is; false for private and unknown ones.
func Locate(a netip.Addr) (Place, bool) {
a = a.Unmap()
if !a.IsGlobalUnicast() || a.IsPrivate() {
return Place{}, false
}
t := load()
if a.Is4() {
ip := uint64(binary.BigEndian.Uint32(a.AsSlice()))
if i, ok := find(t.ir4, ip); ok {
return t.places[i], true
}
if i, ok := find(t.world, ip); ok {
return t.countries[i], true
}
return Place{}, false
}
b := a.As16()
if i, ok := find(t.ir6, binary.BigEndian.Uint64(b[:8])); ok {
return t.places[i], true
}
return Place{}, false
}
+31
View File
@@ -0,0 +1,31 @@
package geo
import (
"net/netip"
"testing"
)
func TestLocate(t *testing.T) {
for _, c := range []struct{ ip, country string }{
{"2.176.0.1", "IR"}, // TCI, Iran
{"5.160.0.1", "IR"}, // Respina, Iran
{"8.8.8.8", "US"}, // Google
{"81.169.145.1", "DE"}, // Strato, Germany
} {
p, ok := Locate(netip.MustParseAddr(c.ip))
if !ok || p.Country != c.country {
t.Errorf("%s: %+v %v, want %s", c.ip, p, ok, c.country)
}
if c.country == "IR" && (p.Town == "" || p.Lat < 25 || p.Lat > 40 || p.Lon < 44 || p.Lon > 64) {
t.Errorf("%s: %+v isn't a place in Iran", c.ip, p)
}
}
for _, ip := range []string{"192.168.1.1", "127.0.0.1", "10.0.0.1", "fe80::1"} {
if p, ok := Locate(netip.MustParseAddr(ip)); ok {
t.Errorf("%s: %+v, want none", ip, p)
}
}
if len(Shapes) == 0 || Shapes[0].Country != "IR" {
t.Fatal("Iran's border should come first")
}
}
Binary file not shown.
+32
View File
@@ -0,0 +1,32 @@
// Code generated by scripts/geo/build.py from Natural Earth (public domain). DO NOT EDIT.
package geo
// Shapes are Iran's border and its neighbours', as lon/lat rings; Iran's first.
var Shapes = []Shape{
{"IR", [][2]float64{{56.19, 26.92}, {55.95, 26.70}, {55.85, 26.73}, {55.42, 26.58}, {55.30, 26.64}, {55.76, 26.81}, {55.76, 26.95}, {55.91, 26.91}, {56.21, 27.00}, {56.28, 26.95}, {56.19, 26.92}}},
{"IR", [][2]float64{{53.91, 37.34}, {54.19, 37.33}, {54.70, 37.47}, {54.90, 37.78}, {55.38, 38.05}, {55.58, 38.10}, {56.27, 38.08}, {56.32, 38.19}, {56.44, 38.25}, {57.19, 38.22}, {57.31, 38.13}, {57.35, 37.97}, {57.98, 37.83}, {58.26, 37.67}, {58.82, 37.68}, {59.30, 37.51}, {59.45, 37.25}, {60.06, 36.96}, {60.34, 36.64}, {61.12, 36.64}, {61.17, 36.57}, {61.21, 36.10}, {61.15, 35.98}, {61.25, 35.87}, {61.28, 35.51}, {61.19, 35.31}, {61.10, 35.27}, {61.15, 35.09}, {61.08, 34.86}, {60.95, 34.65}, {60.73, 34.52}, {60.89, 34.32}, {60.64, 34.31}, {60.49, 34.09}, {60.53, 33.84}, {60.49, 33.71}, {60.57, 33.59}, {60.92, 33.51}, {60.56, 33.14}, {60.83, 32.25}, {60.82, 31.50}, {61.66, 31.38}, {61.76, 31.29}, {61.81, 30.91}, {60.84, 29.86}, {61.32, 29.37}, {61.62, 28.79}, {61.89, 28.55}, {62.35, 28.41}, {62.56, 28.24}, {62.76, 28.24}, {62.81, 27.50}, {62.75, 27.27}, {62.92, 27.22}, {63.17, 27.25}, {63.31, 27.12}, {63.24, 27.08}, {63.25, 26.88}, {63.19, 26.84}, {63.16, 26.65}, {62.44, 26.56}, {62.31, 26.49}, {62.24, 26.36}, {62.13, 26.37}, {61.84, 26.23}, {61.75, 25.84}, {61.66, 25.75}, {61.59, 25.20}, {61.41, 25.10}, {60.66, 25.28}, {60.51, 25.44}, {60.40, 25.31}, {59.62, 25.40}, {59.46, 25.48}, {59.05, 25.42}, {58.80, 25.55}, {58.20, 25.59}, {57.94, 25.69}, {57.80, 25.65}, {57.73, 25.72}, {57.33, 25.79}, {57.10, 26.37}, {57.04, 26.80}, {56.91, 26.99}, {56.73, 27.13}, {56.36, 27.20}, {56.12, 27.14}, {55.94, 27.04}, {55.65, 26.98}, {55.42, 26.77}, {55.29, 26.79}, {54.90, 26.56}, {54.64, 26.51}, {54.25, 26.70}, {53.71, 26.73}, {53.45, 26.94}, {52.69, 27.32}, {52.48, 27.62}, {52.03, 27.82}, {51.59, 27.86}, {51.28, 28.13}, {51.09, 28.51}, {51.06, 28.73}, {50.87, 28.87}, {50.88, 29.06}, {50.68, 29.15}, {50.65, 29.42}, {50.17, 29.92}, {50.07, 30.20}, {49.55, 30.03}, {49.03, 30.33}, {49.05, 30.40}, {49.25, 30.41}, {49.22, 30.47}, {49.00, 30.51}, {49.04, 30.45}, {48.89, 30.33}, {48.92, 30.12}, {48.87, 30.06}, {48.55, 29.96}, {48.43, 30.04}, {48.33, 30.29}, {48.01, 30.47}, {48.01, 30.99}, {47.68, 31.00}, {47.68, 31.40}, {47.83, 31.79}, {47.51, 32.15}, {47.37, 32.42}, {47.12, 32.47}, {46.38, 32.93}, {46.11, 32.96}, {46.08, 33.09}, {46.15, 33.23}, {46.02, 33.42}, {45.87, 33.49}, {45.88, 33.61}, {45.74, 33.60}, {45.40, 33.97}, {45.54, 34.22}, {45.44, 34.42}, {45.50, 34.58}, {45.64, 34.57}, {45.68, 34.80}, {45.92, 35.03}, {46.13, 35.13}, {46.11, 35.32}, {45.98, 35.48}, {46.00, 35.61}, {46.27, 35.77}, {45.78, 35.82}, {45.56, 35.98}, {45.36, 36.02}, {45.24, 36.36}, {45.08, 36.43}, {45.02, 36.70}, {44.88, 36.80}, {44.77, 37.14}, {44.79, 37.29}, {44.57, 37.44}, {44.55, 37.66}, {44.59, 37.71}, {44.22, 37.88}, {44.45, 38.33}, {44.30, 38.39}, {44.27, 38.84}, {44.17, 38.93}, {44.18, 39.14}, {44.08, 39.22}, {44.02, 39.38}, {44.39, 39.42}, {44.46, 39.67}, {44.59, 39.77}, {44.82, 39.65}, {45.14, 39.25}, {45.48, 39.01}, {46.17, 38.87}, {46.55, 38.90}, {47.34, 39.42}, {47.77, 39.65}, {48.00, 39.68}, {48.32, 39.40}, {48.10, 39.24}, {48.29, 39.02}, {48.05, 38.94}, {48.00, 38.85}, {48.59, 38.41}, {48.87, 38.44}, {48.93, 38.02}, {49.08, 37.67}, {49.37, 37.52}, {50.13, 37.41}, {50.53, 37.01}, {51.12, 36.74}, {51.76, 36.61}, {52.19, 36.62}, {53.77, 36.93}, {53.92, 36.93}, {53.68, 36.85}, {53.97, 36.82}, {54.02, 36.90}, {53.91, 37.34}}},
{"AE", [][2]float64{{56.30, 25.65}, {56.39, 24.98}, {56.06, 24.74}, {56.00, 24.95}, {55.80, 24.87}, {55.76, 24.24}, {55.93, 24.22}, {55.99, 24.06}, {55.47, 23.94}, {55.51, 23.72}, {55.12, 22.62}, {52.56, 22.93}, {51.57, 24.13}, {51.61, 24.34}, {51.91, 23.99}, {52.65, 24.15}, {53.89, 24.08}, {54.40, 24.28}, {54.75, 24.81}, {55.94, 25.79}, {56.08, 26.06}, {56.17, 26.05}, {56.14, 25.69}, {56.30, 25.65}}},
{"AE", [][2]float64{{53.33, 24.26}, {53.19, 24.29}, {53.41, 24.41}, {53.33, 24.26}}},
{"AE", [][2]float64{{53.93, 24.18}, {53.63, 24.17}, {53.83, 24.26}, {53.93, 24.18}}},
{"AF", [][2]float64{{66.52, 37.35}, {67.70, 37.23}, {68.07, 36.95}, {68.91, 37.33}, {69.30, 37.12}, {69.49, 37.55}, {70.19, 37.58}, {70.21, 37.92}, {70.88, 38.46}, {71.26, 38.31}, {71.28, 37.92}, {71.58, 37.91}, {71.43, 37.13}, {71.67, 36.70}, {72.66, 37.03}, {72.90, 37.27}, {73.38, 37.46}, {73.72, 37.42}, {73.65, 37.24}, {74.35, 37.42}, {74.89, 37.23}, {74.73, 37.29}, {74.37, 37.16}, {74.54, 37.02}, {74.04, 36.83}, {73.12, 36.87}, {72.25, 36.73}, {71.23, 36.12}, {71.19, 36.04}, {71.57, 35.55}, {71.62, 35.18}, {70.97, 34.53}, {71.10, 34.37}, {71.05, 34.05}, {70.65, 33.95}, {69.89, 34.01}, {70.26, 33.29}, {69.50, 33.02}, {69.24, 32.43}, {69.28, 31.94}, {68.87, 31.63}, {68.60, 31.80}, {68.16, 31.80}, {67.58, 31.51}, {67.74, 31.34}, {67.45, 31.23}, {66.83, 31.26}, {66.35, 30.80}, {66.24, 30.11}, {66.31, 29.97}, {66.18, 29.84}, {65.10, 29.56}, {64.39, 29.54}, {64.10, 29.39}, {63.57, 29.50}, {62.48, 29.41}, {60.84, 29.86}, {61.81, 30.91}, {61.66, 31.38}, {60.82, 31.50}, {60.83, 32.25}, {60.56, 33.06}, {60.92, 33.51}, {60.51, 33.64}, {60.49, 34.09}, {60.64, 34.31}, {60.89, 34.32}, {60.73, 34.52}, {61.08, 34.86}, {61.26, 35.62}, {61.62, 35.43}, {61.98, 35.44}, {62.31, 35.17}, {62.69, 35.26}, {63.06, 35.45}, {63.13, 35.85}, {64.01, 36.01}, {64.51, 36.34}, {64.82, 37.13}, {65.55, 37.25}, {65.77, 37.57}, {66.52, 37.35}}},
{"AM", [][2]float64{{44.77, 39.70}, {44.29, 40.04}, {43.67, 40.13}, {43.57, 40.48}, {43.72, 40.72}, {43.44, 41.11}, {45.00, 41.29}, {45.19, 41.15}, {45.07, 41.08}, {45.59, 40.85}, {45.38, 40.64}, {45.97, 40.17}, {45.89, 40.02}, {45.58, 39.98}, {46.20, 39.59}, {46.48, 39.56}, {46.37, 39.40}, {46.58, 39.22}, {46.40, 39.19}, {46.49, 38.91}, {46.11, 38.88}, {45.75, 39.56}, {45.46, 39.49}, {45.15, 39.58}, {45.03, 39.77}, {44.77, 39.70}}},
{"AZ", [][2]float64{{44.82, 39.65}, {45.03, 39.77}, {45.17, 39.57}, {45.78, 39.55}, {45.77, 39.38}, {45.98, 39.24}, {46.11, 38.88}, {45.48, 39.01}, {44.82, 39.65}}},
{"AZ", [][2]float64{{48.87, 38.44}, {48.59, 38.41}, {48.00, 38.85}, {48.29, 39.02}, {48.10, 39.24}, {48.32, 39.40}, {48.00, 39.68}, {46.49, 38.91}, {46.40, 39.19}, {46.58, 39.22}, {46.37, 39.40}, {46.48, 39.56}, {46.20, 39.59}, {45.58, 39.98}, {45.89, 40.02}, {45.96, 40.23}, {45.45, 40.53}, {45.38, 40.67}, {45.59, 40.85}, {45.07, 41.08}, {45.19, 41.15}, {45.00, 41.29}, {45.28, 41.45}, {46.53, 41.09}, {46.67, 41.29}, {46.18, 41.66}, {46.43, 41.89}, {46.75, 41.81}, {47.32, 41.28}, {47.79, 41.20}, {48.57, 41.84}, {48.66, 41.79}, {49.46, 40.80}, {50.18, 40.50}, {50.37, 40.28}, {49.92, 40.32}, {49.55, 40.19}, {49.32, 39.61}, {49.36, 39.35}, {49.17, 39.03}, {49.01, 39.13}, {48.85, 38.84}, {48.87, 38.44}}},
{"BH", [][2]float64{{50.61, 25.88}, {50.57, 25.81}, {50.47, 25.97}, {50.47, 26.23}, {50.59, 26.24}, {50.61, 25.88}}},
{"GE", [][2]float64{{43.44, 41.11}, {42.75, 41.58}, {42.47, 41.44}, {41.51, 41.52}, {41.76, 41.97}, {41.49, 42.66}, {39.98, 43.42}, {40.15, 43.57}, {40.65, 43.53}, {41.58, 43.22}, {42.76, 43.17}, {43.78, 42.75}, {43.83, 42.57}, {44.51, 42.75}, {44.77, 42.62}, {44.87, 42.76}, {45.71, 42.50}, {45.64, 42.21}, {46.43, 41.89}, {46.18, 41.66}, {46.67, 41.29}, {46.53, 41.09}, {45.28, 41.45}, {44.84, 41.21}, {43.44, 41.11}}},
{"IQ", [][2]float64{{42.36, 37.11}, {42.77, 37.37}, {43.68, 37.23}, {44.11, 37.30}, {44.28, 36.98}, {44.73, 37.17}, {45.36, 36.02}, {45.78, 35.82}, {46.27, 35.77}, {46.00, 35.61}, {46.13, 35.13}, {45.68, 34.80}, {45.64, 34.57}, {45.50, 34.58}, {45.44, 34.42}, {45.54, 34.22}, {45.40, 33.97}, {46.02, 33.42}, {46.15, 33.23}, {46.11, 32.96}, {47.37, 32.42}, {47.83, 31.79}, {47.68, 31.00}, {48.01, 30.99}, {48.01, 30.47}, {48.33, 30.29}, {48.55, 29.96}, {47.67, 30.10}, {47.22, 30.04}, {46.53, 29.10}, {44.72, 29.19}, {42.07, 31.08}, {40.37, 31.94}, {39.15, 32.12}, {39.29, 32.24}, {39.25, 32.35}, {39.04, 32.31}, {38.77, 33.37}, {40.99, 34.43}, {41.19, 34.77}, {41.35, 35.64}, {41.30, 36.38}, {41.79, 36.60}, {42.36, 37.11}}},
{"KW", [][2]float64{{48.28, 29.62}, {48.08, 29.80}, {48.18, 29.98}, {48.35, 29.78}, {48.28, 29.62}}},
{"KW", [][2]float64{{48.44, 28.54}, {47.67, 28.53}, {47.43, 28.99}, {46.53, 29.10}, {47.15, 30.00}, {47.75, 30.08}, {47.98, 29.98}, {48.14, 29.57}, {47.97, 29.62}, {47.72, 29.39}, {48.05, 29.36}, {48.44, 28.54}}},
{"KZ", [][2]float64{{70.95, 42.25}, {69.15, 41.43}, {68.58, 40.88}, {68.57, 40.62}, {68.42, 40.62}, {68.05, 40.81}, {68.11, 41.03}, {67.94, 41.20}, {66.71, 41.18}, {66.50, 41.99}, {66.01, 42.00}, {66.10, 42.99}, {65.80, 42.88}, {65.50, 43.31}, {64.91, 43.71}, {64.44, 43.55}, {63.21, 43.63}, {61.99, 43.49}, {61.01, 44.39}, {58.56, 45.56}, {55.98, 44.99}, {55.98, 41.32}, {55.43, 41.30}, {54.85, 41.97}, {54.12, 42.34}, {53.06, 42.15}, {52.49, 41.78}, {52.60, 42.76}, {51.90, 42.87}, {51.62, 43.16}, {51.30, 43.17}, {51.30, 43.48}, {50.83, 44.19}, {50.33, 44.33}, {50.25, 44.46}, {50.41, 44.62}, {51.54, 44.53}, {51.01, 44.92}, {51.42, 45.36}, {53.20, 45.33}, {52.77, 45.57}, {53.14, 46.19}, {53.06, 46.48}, {53.17, 46.67}, {53.07, 46.86}, {52.48, 46.99}, {52.14, 46.83}, {51.18, 47.11}, {49.89, 46.60}, {49.35, 46.52}, {49.23, 46.34}, {48.61, 46.57}, {48.52, 46.73}, {48.96, 46.77}, {48.17, 47.71}, {47.29, 47.74}, {47.09, 47.95}, {47.06, 48.23}, {46.66, 48.41}, {46.70, 48.81}, {47.03, 49.15}, {46.80, 49.37}, {46.82, 49.50}, {47.50, 50.40}, {47.71, 50.38}, {48.33, 49.86}, {48.76, 49.93}, {48.84, 50.01}, {48.63, 50.61}, {49.32, 50.85}, {49.50, 51.08}, {50.31, 51.32}, {50.79, 51.73}, {51.16, 51.65}, {51.34, 51.48}, {52.22, 51.71}, {52.57, 51.48}, {53.34, 51.48}, {54.14, 51.04}, {54.56, 50.54}, {54.65, 50.66}, {54.55, 50.95}, {54.64, 51.01}, {55.69, 50.58}, {56.49, 51.02}, {57.01, 51.07}, {57.44, 50.89}, {57.84, 51.09}, {58.36, 51.06}, {58.88, 50.69}, {59.45, 50.62}, {59.52, 50.49}, {59.81, 50.58}, {60.06, 50.85}, {60.42, 50.68}, {60.94, 50.70}, {61.39, 50.86}, {61.55, 51.32}, {60.46, 51.65}, {60.03, 51.93}, {60.99, 52.34}, {60.77, 52.68}, {61.05, 52.97}, {62.08, 53.01}, {61.66, 53.23}, {61.20, 53.29}, {61.23, 53.45}, {61.53, 53.52}, {60.98, 53.62}, {61.23, 54.02}, {61.93, 53.95}, {64.46, 54.38}, {65.09, 54.34}, {65.48, 54.62}, {68.16, 54.98}, {68.21, 55.16}, {68.98, 55.39}, {70.18, 55.16}, {70.74, 55.31}, {71.19, 54.60}, {71.09, 54.21}, {72.00, 54.21}, {72.19, 54.33}, {72.45, 53.94}, {72.62, 54.13}, {73.23, 53.96}, {73.67, 54.06}, {73.68, 53.93}, {73.31, 53.71}, {73.41, 53.45}, {73.86, 53.62}, {74.35, 53.49}, {74.45, 53.65}, {75.22, 53.89}, {75.44, 54.09}, {76.84, 54.44}, {76.65, 54.15}, {76.42, 54.15}, {76.48, 54.02}, {77.86, 53.27}, {79.99, 50.77}, {80.42, 50.95}, {80.45, 51.18}, {80.74, 51.29}, {81.13, 51.19}, {81.07, 50.97}, {81.39, 50.96}, {81.47, 50.74}, {82.49, 50.73}, {83.36, 50.99}, {83.95, 50.77}, {84.32, 50.24}, {84.99, 50.06}, {85.23, 49.62}, {86.18, 49.50}, {86.68, 49.78}, {86.63, 49.56}, {87.32, 49.09}, {86.81, 49.05}, {86.55, 48.53}, {85.75, 48.39}, {85.53, 47.92}, {85.66, 47.25}, {85.48, 47.06}, {84.79, 46.83}, {84.67, 46.97}, {84.02, 46.97}, {83.03, 47.19}, {82.32, 45.59}, {82.61, 45.42}, {82.52, 45.13}, {81.94, 45.16}, {81.69, 45.35}, {79.87, 44.88}, {80.48, 44.71}, {80.36, 44.55}, {80.36, 44.10}, {80.79, 43.16}, {80.39, 43.04}, {80.54, 42.87}, {80.20, 42.73}, {80.21, 42.19}, {79.92, 42.41}, {79.43, 42.48}, {79.13, 42.78}, {76.99, 42.97}, {75.37, 42.84}, {74.21, 43.24}, {73.56, 43.00}, {73.42, 42.59}, {73.49, 42.41}, {71.76, 42.82}, {71.26, 42.73}, {70.89, 42.34}, {70.95, 42.25}}},
{"OM", [][2]float64{{53.09, 16.65}, {51.98, 19.00}, {54.98, 20.00}, {55.64, 22.00}, {55.19, 22.70}, {55.20, 23.03}, {55.51, 23.72}, {55.47, 23.94}, {55.99, 24.06}, {55.93, 24.22}, {55.76, 24.24}, {55.80, 24.87}, {56.00, 24.95}, {56.06, 24.74}, {56.39, 24.98}, {56.64, 24.47}, {57.12, 23.98}, {58.77, 23.52}, {59.43, 22.66}, {59.82, 22.51}, {59.80, 22.22}, {58.47, 20.41}, {58.21, 20.42}, {58.17, 20.59}, {57.86, 20.24}, {57.71, 19.68}, {57.81, 19.02}, {56.83, 18.75}, {56.38, 17.99}, {55.48, 17.84}, {55.26, 17.59}, {55.28, 17.32}, {55.06, 17.04}, {54.07, 17.01}, {53.09, 16.65}}},
{"OM", [][2]float64{{56.30, 25.65}, {56.14, 25.69}, {56.17, 26.05}, {56.08, 26.06}, {56.38, 26.36}, {56.30, 25.65}}},
{"PK", [][2]float64{{76.77, 35.66}, {77.05, 35.11}, {76.59, 34.74}, {75.71, 34.50}, {74.30, 34.77}, {73.96, 34.65}, {73.79, 34.38}, {73.97, 34.24}, {73.90, 34.08}, {74.25, 33.95}, {73.98, 33.72}, {74.15, 33.51}, {74.00, 33.19}, {74.30, 32.99}, {74.35, 32.77}, {74.66, 32.76}, {74.69, 32.49}, {75.33, 32.28}, {74.56, 31.82}, {74.52, 31.19}, {74.63, 31.03}, {73.90, 30.44}, {73.93, 30.22}, {73.81, 30.09}, {73.38, 29.93}, {72.90, 29.03}, {72.34, 28.75}, {71.87, 27.96}, {70.80, 27.71}, {70.40, 28.03}, {69.54, 27.12}, {69.51, 26.74}, {70.15, 26.51}, {70.10, 25.91}, {70.26, 25.71}, {70.65, 25.67}, {70.65, 25.42}, {71.05, 24.69}, {71.04, 24.40}, {70.72, 24.24}, {70.49, 24.41}, {69.81, 24.17}, {68.78, 24.31}, {68.72, 23.96}, {68.28, 23.93}, {68.12, 23.75}, {67.50, 23.94}, {67.31, 24.17}, {67.17, 24.76}, {66.70, 24.86}, {66.70, 25.23}, {66.32, 25.60}, {66.13, 25.49}, {66.47, 25.45}, {64.78, 25.31}, {64.66, 25.18}, {64.06, 25.40}, {63.56, 25.35}, {63.49, 25.21}, {61.57, 25.19}, {61.84, 26.23}, {62.24, 26.36}, {62.44, 26.56}, {63.16, 26.65}, {63.30, 27.15}, {62.76, 27.25}, {62.76, 28.24}, {61.89, 28.55}, {60.84, 29.86}, {62.48, 29.41}, {63.57, 29.50}, {64.10, 29.39}, {64.39, 29.54}, {65.10, 29.56}, {66.23, 29.87}, {66.35, 30.80}, {66.83, 31.26}, {67.45, 31.23}, {67.74, 31.34}, {67.58, 31.51}, {68.16, 31.80}, {68.60, 31.80}, {68.87, 31.63}, {69.28, 31.94}, {69.24, 32.43}, {69.50, 33.02}, {70.26, 33.29}, {69.89, 34.01}, {70.65, 33.95}, {71.05, 34.05}, {71.10, 34.37}, {70.97, 34.53}, {71.62, 35.18}, {71.43, 35.83}, {71.19, 36.04}, {71.62, 36.44}, {72.16, 36.70}, {73.12, 36.87}, {74.04, 36.83}, {74.60, 37.04}, {75.15, 36.97}, {75.35, 36.91}, {75.42, 36.74}, {75.88, 36.60}, {75.91, 36.05}, {76.15, 35.83}, {76.55, 35.89}, {76.77, 35.66}}},
{"QA", [][2]float64{{51.27, 24.61}, {50.97, 24.57}, {50.86, 24.68}, {50.75, 25.40}, {51.00, 25.98}, {51.26, 26.15}, {51.54, 25.90}, {51.49, 25.52}, {51.61, 25.05}, {51.43, 24.67}, {51.27, 24.61}}},
{"RU", [][2]float64{{130.69, 42.30}, {130.42, 42.73}, {131.07, 42.90}, {131.26, 43.38}, {131.17, 43.70}, {131.26, 44.07}, {130.98, 44.84}, {131.45, 44.98}, {131.85, 45.33}, {132.94, 45.03}, {133.11, 45.13}, {133.19, 45.49}, {133.44, 45.60}, {133.51, 45.88}, {133.86, 46.25}, {133.87, 46.50}, {134.20, 47.13}, {134.17, 47.30}, {134.75, 47.72}, {134.57, 48.02}, {134.67, 48.25}, {134.29, 48.37}, {133.47, 48.10}, {133.14, 48.11}, {132.71, 47.95}, {132.48, 47.71}, {130.96, 47.71}, {130.73, 48.02}, {130.80, 48.34}, {130.55, 48.60}, {130.55, 48.86}, {130.20, 48.89}, {129.50, 49.39}, {129.07, 49.37}, {128.70, 49.60}, {128.00, 49.57}, {127.55, 49.80}, {127.59, 50.21}, {127.34, 50.35}, {127.31, 50.71}, {126.92, 51.10}, {126.34, 52.36}, {126.02, 52.61}, {126.05, 52.74}, {125.65, 53.04}, {125.08, 53.20}, {124.81, 53.13}, {123.61, 53.55}, {120.99, 53.28}, {120.09, 52.79}, {120.07, 52.63}, {120.66, 52.57}, {120.68, 51.97}, {120.07, 51.60}, {119.16, 50.41}, {119.35, 50.28}, {119.26, 50.07}, {118.45, 49.84}, {117.87, 49.51}, {116.22, 50.01}, {115.43, 49.90}, {114.74, 50.23}, {114.30, 50.27}, {113.57, 50.01}, {112.81, 49.52}, {111.34, 49.36}, {110.71, 49.14}, {108.61, 49.32}, {107.97, 49.65}, {107.92, 49.95}, {107.23, 49.99}, {106.71, 50.31}, {105.38, 50.47}, {104.08, 50.15}, {103.30, 50.20}, {102.29, 50.59}, {102.11, 51.35}, {99.92, 51.76}, {98.89, 52.12}, {98.64, 51.80}, {98.04, 51.45}, {97.84, 51.05}, {98.03, 50.64}, {98.28, 50.53}, {98.25, 50.30}, {98.00, 50.01}, {97.36, 49.74}, {96.07, 50.00}, {95.52, 49.91}, {94.61, 50.02}, {94.35, 50.22}, {94.25, 50.56}, {93.10, 50.60}, {92.94, 50.78}, {92.63, 50.69}, {92.35, 50.86}, {92.19, 50.70}, {91.80, 50.69}, {91.42, 50.47}, {90.05, 50.09}, {89.64, 49.90}, {89.65, 49.72}, {89.11, 49.50}, {88.19, 49.45}, {87.99, 49.19}, {87.42, 49.08}, {86.63, 49.56}, {86.68, 49.78}, {86.18, 49.50}, {85.23, 49.62}, {84.99, 50.06}, {84.32, 50.24}, {83.95, 50.77}, {83.36, 50.99}, {82.49, 50.73}, {81.47, 50.74}, {81.39, 50.96}, {81.07, 50.97}, {81.13, 51.19}, {80.74, 51.29}, {80.45, 51.18}, {80.42, 50.95}, {79.99, 50.77}, {77.86, 53.27}, {76.48, 54.02}, {76.42, 54.15}, {76.65, 54.15}, {76.84, 54.44}, {75.44, 54.09}, {75.22, 53.89}, {74.45, 53.65}, {74.35, 53.49}, {73.86, 53.62}, {73.41, 53.45}, {73.31, 53.71}, {73.68, 53.93}, {73.67, 54.06}, {73.23, 53.96}, {72.62, 54.13}, {72.45, 53.94}, {72.19, 54.33}, {72.00, 54.21}, {71.09, 54.21}, {71.19, 54.60}, {70.74, 55.31}, {70.18, 55.16}, {68.98, 55.39}, {68.21, 55.16}, {68.16, 54.98}, {65.48, 54.62}, {65.09, 54.34}, {64.46, 54.38}, {61.93, 53.95}, {61.23, 54.02}, {60.98, 53.62}, {61.53, 53.52}, {61.23, 53.45}, {61.20, 53.29}, {61.66, 53.23}, {62.08, 53.01}, {61.05, 52.97}, {60.77, 52.68}, {60.99, 52.34}, {60.03, 51.93}, {60.46, 51.65}, {61.55, 51.32}, {61.39, 50.86}, {60.94, 50.70}, {60.42, 50.68}, {60.06, 50.85}, {59.81, 50.58}, {59.52, 50.49}, {59.45, 50.62}, {58.88, 50.69}, {58.36, 51.06}, {57.84, 51.09}, {57.44, 50.89}, {57.01, 51.07}, {56.49, 51.02}, {55.69, 50.58}, {54.64, 51.01}, {54.55, 50.95}, {54.65, 50.66}, {54.56, 50.54}, {54.14, 51.04}, {53.34, 51.48}, {52.57, 51.48}, {52.22, 51.71}, {51.34, 51.48}, {51.16, 51.65}, {50.79, 51.73}, {50.31, 51.32}, {49.50, 51.08}, {49.32, 50.85}, {48.63, 50.61}, {48.84, 50.01}, {48.76, 49.93}, {48.33, 49.86}, {47.71, 50.38}, {47.43, 50.36}, {47.25, 50.00}, {46.89, 49.70}, {46.80, 49.37}, {47.03, 49.15}, {46.70, 48.81}, {46.66, 48.41}, {47.06, 48.23}, {47.09, 47.95}, {47.29, 47.74}, {48.17, 47.71}, {48.96, 46.77}, {48.56, 46.76}, {48.54, 46.61}, {49.25, 46.29}, {48.68, 46.09}, {48.73, 45.90}, {48.49, 45.93}, {47.63, 45.58}, {47.46, 45.68}, {47.51, 45.49}, {47.08, 44.82}, {47.00, 44.88}, {46.72, 44.56}, {46.75, 44.42}, {47.31, 44.10}, {47.46, 43.56}, {47.65, 43.88}, {47.46, 43.04}, {48.57, 41.84}, {47.86, 41.21}, {47.52, 41.23}, {47.26, 41.32}, {46.75, 41.81}, {45.64, 42.21}, {45.66, 42.52}, {44.87, 42.76}, {44.77, 42.62}, {44.51, 42.75}, {43.83, 42.57}, {43.78, 42.75}, {42.76, 43.17}, {41.58, 43.22}, {40.65, 43.53}, {40.15, 43.57}, {39.98, 43.42}, {38.72, 44.29}, {38.18, 44.42}, {37.85, 44.70}, {37.50, 44.70}, {37.20, 44.97}, {36.65, 45.13}, {36.94, 45.29}, {36.79, 45.41}, {37.21, 45.27}, {37.65, 45.38}, {37.61, 45.56}, {37.93, 46.00}, {38.08, 45.93}, {38.18, 46.09}, {38.49, 46.09}, {37.91, 46.41}, {37.77, 46.64}, {38.50, 46.66}, {38.44, 46.81}, {39.27, 47.04}, {39.20, 47.27}, {38.67, 47.14}, {38.55, 47.15}, {38.76, 47.26}, {38.58, 47.24}, {38.21, 47.09}, {38.20, 47.32}, {38.29, 47.56}, {38.82, 47.84}, {39.78, 47.89}, {39.96, 48.27}, {39.85, 48.30}, {39.84, 48.54}, {39.64, 48.59}, {39.79, 48.81}, {40.00, 48.82}, {39.69, 49.01}, {40.11, 49.25}, {40.08, 49.58}, {39.78, 49.57}, {39.17, 49.86}, {38.92, 49.82}, {38.26, 50.05}, {38.05, 49.92}, {37.42, 50.41}, {36.62, 50.21}, {36.12, 50.41}, {35.59, 50.37}, {35.41, 50.54}, {35.31, 51.04}, {35.06, 51.20}, {34.21, 51.26}, {34.12, 51.68}, {34.40, 51.78}, {33.74, 52.34}, {32.44, 52.31}, {32.12, 52.05}, {31.76, 52.10}, {31.58, 52.31}, {31.56, 52.76}, {31.26, 53.02}, {31.4Line truncated
{"SA", [][2]float64{{51.98, 19.00}, {49.04, 18.58}, {48.17, 18.16}, {47.44, 17.11}, {47.14, 16.95}, {46.98, 16.95}, {46.73, 17.27}, {45.54, 17.30}, {45.15, 17.43}, {43.92, 17.32}, {43.42, 17.52}, {43.19, 17.36}, {43.17, 16.69}, {42.80, 16.37}, {42.29, 17.43}, {41.75, 17.89}, {41.23, 18.68}, {40.76, 19.76}, {39.73, 20.39}, {39.28, 20.97}, {38.99, 21.88}, {39.06, 22.59}, {38.46, 23.71}, {37.92, 24.19}, {37.54, 24.29}, {37.18, 24.82}, {37.27, 24.96}, {37.15, 25.29}, {35.18, 28.03}, {34.62, 28.06}, {34.95, 29.35}, {36.07, 29.20}, {36.76, 29.87}, {37.47, 30.00}, {37.65, 30.33}, {37.98, 30.50}, {36.96, 31.49}, {39.15, 32.12}, {40.37, 31.94}, {42.07, 31.08}, {44.69, 29.20}, {47.43, 28.99}, {47.67, 28.53}, {48.44, 28.54}, {48.80, 27.72}, {49.24, 27.49}, {49.18, 27.44}, {49.41, 27.18}, {50.15, 26.66}, {50.01, 26.68}, {50.21, 26.31}, {50.16, 26.10}, {50.03, 26.11}, {50.08, 25.96}, {50.86, 24.68}, {50.97, 24.57}, {51.41, 24.57}, {51.31, 24.34}, {51.57, 24.29}, {51.59, 24.08}, {52.56, 22.93}, {55.10, 22.62}, {55.19, 22.70}, {55.64, 22.00}, {54.98, 20.00}, {51.98, 19.00}}},
{"SY", [][2]float64{{35.89, 35.92}, {36.15, 35.83}, {36.38, 36.17}, {36.64, 36.23}, {36.54, 36.46}, {36.66, 36.80}, {37.44, 36.64}, {38.19, 36.90}, {38.77, 36.69}, {39.36, 36.68}, {40.71, 37.10}, {41.52, 37.09}, {42.20, 37.30}, {42.36, 37.11}, {41.79, 36.60}, {41.30, 36.38}, {41.35, 35.64}, {41.19, 34.77}, {40.99, 34.43}, {36.82, 32.32}, {36.37, 32.39}, {35.79, 32.73}, {35.89, 32.94}, {35.79, 33.33}, {36.03, 33.59}, {35.97, 33.73}, {36.37, 33.84}, {36.28, 33.93}, {36.58, 34.22}, {36.50, 34.43}, {36.33, 34.50}, {36.38, 34.66}, {35.98, 34.63}, {35.90, 35.42}, {35.76, 35.57}, {35.89, 35.92}}},
{"TM", [][2]float64{{55.98, 41.32}, {57.02, 41.26}, {57.12, 41.35}, {56.96, 41.86}, {57.29, 42.12}, {57.81, 42.19}, {58.03, 42.49}, {58.47, 42.30}, {58.15, 42.63}, {58.59, 42.78}, {59.35, 42.32}, {59.99, 42.21}, {59.94, 41.97}, {60.20, 41.80}, {60.08, 41.76}, {60.09, 41.40}, {60.45, 41.22}, {61.50, 41.28}, {61.90, 41.09}, {62.48, 39.98}, {63.76, 39.16}, {65.61, 38.24}, {65.97, 38.24}, {66.57, 38.01}, {66.52, 37.35}, {65.77, 37.57}, {65.55, 37.25}, {64.82, 37.13}, {64.51, 36.34}, {64.01, 36.01}, {63.13, 35.85}, {63.06, 35.45}, {62.31, 35.17}, {61.98, 35.44}, {61.62, 35.43}, {61.26, 35.62}, {61.12, 36.64}, {60.34, 36.64}, {60.06, 36.96}, {59.45, 37.25}, {59.30, 37.51}, {58.82, 37.68}, {58.26, 37.67}, {57.35, 37.97}, {57.19, 38.22}, {56.44, 38.25}, {56.27, 38.08}, {55.38, 38.05}, {54.90, 37.78}, {54.70, 37.47}, {53.91, 37.34}, {53.87, 38.95}, {53.70, 39.21}, {53.34, 39.34}, {53.16, 39.26}, {53.24, 39.61}, {53.60, 39.55}, {53.47, 39.67}, {53.49, 39.91}, {52.99, 39.99}, {53.04, 39.77}, {52.80, 40.05}, {52.73, 40.40}, {52.94, 41.04}, {53.15, 40.82}, {53.62, 40.82}, {53.87, 40.65}, {54.38, 40.69}, {54.32, 40.83}, {54.69, 40.87}, {54.70, 41.07}, {54.09, 41.52}, {53.80, 42.12}, {53.16, 42.09}, {52.97, 41.98}, {52.81, 41.71}, {52.85, 41.20}, {52.49, 41.78}, {53.06, 42.15}, {54.12, 42.34}, {54.85, 41.97}, {55.43, 41.30}, {55.98, 41.32}}},
{"TR", [][2]float64{{41.51, 41.52}, {42.47, 41.44}, {42.79, 41.56}, {43.63, 40.93}, {43.72, 40.72}, {43.57, 40.48}, {43.67, 40.13}, {44.40, 40.00}, {44.82, 39.65}, {44.59, 39.77}, {44.39, 39.42}, {44.02, 39.38}, {44.27, 38.84}, {44.30, 38.39}, {44.45, 38.33}, {44.21, 37.91}, {44.56, 37.74}, {44.57, 37.44}, {44.80, 37.27}, {44.77, 37.14}, {44.61, 37.18}, {44.28, 36.98}, {44.11, 37.30}, {43.68, 37.23}, {42.77, 37.37}, {42.36, 37.11}, {42.20, 37.30}, {41.52, 37.09}, {40.71, 37.10}, {39.36, 36.68}, {38.77, 36.69}, {38.19, 36.90}, {37.44, 36.64}, {36.66, 36.80}, {36.54, 36.46}, {36.64, 36.23}, {36.38, 36.17}, {36.13, 35.83}, {35.89, 35.92}, {35.81, 36.31}, {36.19, 36.66}, {36.05, 36.91}, {35.39, 36.58}, {34.70, 36.82}, {33.69, 36.18}, {32.79, 36.04}, {31.35, 36.80}, {30.64, 36.87}, {30.45, 36.27}, {29.69, 36.16}, {29.22, 36.32}, {28.97, 36.72}, {28.30, 36.81}, {28.02, 36.63}, {28.08, 36.75}, {27.45, 36.71}, {28.01, 36.83}, {28.24, 37.03}, {27.26, 36.98}, {27.30, 37.13}, {27.54, 37.16}, {27.07, 37.66}, {27.22, 37.73}, {27.23, 37.98}, {26.29, 38.28}, {26.42, 38.37}, {26.44, 38.64}, {26.67, 38.34}, {27.14, 38.45}, {26.91, 38.48}, {26.76, 38.71}, {27.01, 38.89}, {26.81, 38.96}, {26.85, 39.12}, {26.68, 39.29}, {26.90, 39.55}, {26.11, 39.47}, {26.18, 39.99}, {26.74, 40.40}, {27.28, 40.46}, {27.73, 40.33}, {27.85, 40.38}, {27.73, 40.48}, {27.87, 40.51}, {27.99, 40.49}, {27.96, 40.37}, {29.01, 40.39}, {28.79, 40.53}, {28.96, 40.63}, {29.85, 40.76}, {29.11, 40.94}, {29.15, 41.22}, {31.25, 41.11}, {32.31, 41.73}, {33.28, 42.00}, {34.75, 41.96}, {35.01, 42.06}, {35.15, 42.03}, {35.12, 41.89}, {35.30, 41.73}, {36.05, 41.68}, {36.41, 41.27}, {36.78, 41.36}, {37.07, 41.18}, {38.38, 40.92}, {39.43, 41.11}, {40.27, 40.96}, {41.51, 41.52}}},
{"UZ", [][2]float64{{70.95, 42.25}, {71.23, 42.16}, {70.86, 42.03}, {70.20, 41.51}, {70.69, 41.45}, {70.86, 41.22}, {71.39, 41.12}, {71.42, 41.34}, {71.59, 41.33}, {71.66, 41.54}, {71.88, 41.20}, {72.16, 41.17}, {72.19, 41.03}, {73.14, 40.81}, {72.60, 40.53}, {72.40, 40.58}, {72.36, 40.40}, {72.13, 40.44}, {71.69, 40.15}, {71.30, 40.29}, {70.60, 40.21}, {70.38, 40.44}, {70.75, 40.74}, {70.40, 41.04}, {69.71, 40.66}, {69.41, 40.80}, {69.21, 40.57}, {69.27, 40.20}, {68.63, 40.17}, {68.97, 40.09}, {68.80, 40.05}, {68.87, 39.91}, {68.64, 39.84}, {68.51, 39.56}, {67.72, 39.62}, {67.43, 39.47}, {67.36, 39.22}, {67.65, 39.13}, {67.69, 38.99}, {68.13, 38.93}, {68.09, 38.47}, {68.35, 38.21}, {67.81, 37.49}, {67.76, 37.17}, {66.52, 37.35}, {66.57, 38.01}, {65.97, 38.24}, {65.61, 38.24}, {63.76, 39.16}, {62.48, 39.98}, {61.90, 41.09}, {61.50, 41.28}, {60.45, 41.22}, {60.09, 41.40}, {60.08, 41.76}, {60.20, 41.80}, {59.94, 41.97}, {59.99, 42.21}, {59.35, 42.32}, {58.59, 42.78}, {58.15, 42.63}, {58.47, 42.30}, {58.03, 42.49}, {57.81, 42.19}, {57.29, 42.12}, {56.96, 41.86}, {57.12, 41.35}, {57.02, 41.26}, {55.98, 41.32}, {55.98, 44.99}, {58.56, 45.56}, {61.01, 44.39}, {61.99, 43.49}, {63.21, 43.63}, {64.44, 43.55}, {64.91, 43.71}, {65.50, 43.31}, {65.80, 42.88}, {66.10, 42.99}, {66.01, 42.00}, {66.50, 41.99}, {66.71, 41.18}, {67.94, 41.20}, {68.11, 41.03}, {68.05, 40.81}, {68.42, 40.62}, {68.57, 40.62}, {68.58, 40.88}, {69.15, 41.43}, {70.95, 42.25}}},
}
+149
View File
@@ -0,0 +1,149 @@
# build.py DBIP_CITY.csv.gz DBIP_COUNTRY.csv.gz NE_COUNTRIES.geojson
# Writes internal/geo/ip.bin and internal/geo/shapes.go, what the Host
# screen's swarm map needs, from:
# DB-IP Lite, City and Country (CC BY 4.0, https://db-ip.com/db/lite.php):
# Iran's IPv4 and IPv6 ranges to a place, the world's IPv4 ranges to a
# country, and each country's middle (its rows' mean position);
# Natural Earth's 1:50m countries (public domain): Iran's border and its
# neighbours', simplified.
# Run it from the repository's root; refresh now and then, as IPs move.
import csv, gzip, io, json, math, re, struct, sys, unicodedata
from collections import defaultdict
from ipaddress import ip_address
city_csv, country_csv, ne = sys.argv[1:4]
BOX = (40.0, 22.0, 67.0, 42.0) # lon/lat the map can show, with room around Iran
def plain(s):
s = re.sub(r"\s*\(.*?\)", "", s) # "Tehran (District 6)"
s = "".join(c for c in unicodedata.normalize("NFD", s) if not unicodedata.combining(c))
return re.sub(r"\s+Province$", "", s.replace("'", "").replace("`", "")).strip()
def uvarint(n):
out = bytearray()
while True:
b = n & 0x7F
n >>= 7
if n:
out.append(b | 0x80)
else:
out.append(b)
return bytes(out)
def svarint(n):
return uvarint((n << 1) ^ (n >> 63) if n < 0 else n << 1)
def text(s):
b = s.encode()
return uvarint(len(b)) + b
places, place_ids, sums = [], {}, defaultdict(lambda: [0.0, 0.0, 0])
ir4, ir6 = [], []
with gzip.open(city_csv, "rt", encoding="utf-8") as f:
for row in csv.reader(f):
start, end, _, cc, prov, city, lat, lon = row
lat, lon = float(lat), float(lon)
if cc not in ("ZZ", ""):
s = sums[cc]
s[0] += lat; s[1] += lon; s[2] += 1
if cc != "IR":
continue
key = (plain(prov), plain(city) or plain(prov))
if key not in place_ids:
place_ids[key] = len(places)
places.append([key, 0.0, 0.0, 0])
p = places[place_ids[key]]
p[1] += lat; p[2] += lon; p[3] += 1
a, b = ip_address(start), ip_address(end)
if a.version == 4:
ir4.append((int(a), int(b), place_ids[key]))
else:
ir6.append((int(a) >> 64, int(b) >> 64, place_ids[key]))
countries = sorted(c for c, s in sums.items() if s[2])
cid = {c: i for i, c in enumerate(countries)}
world = []
with gzip.open(country_csv, "rt", encoding="utf-8") as f:
for start, end, cc in csv.reader(f):
a = ip_address(start)
if a.version == 4 and cc in cid:
world.append((int(a), int(ip_address(end)), cid[cc]))
def ranges(rs):
"""Sorted, adjacent ranges of one value merged, as gap, length, value."""
rs.sort()
merged = []
for a, b, v in rs:
if merged and merged[-1][2] == v and merged[-1][1] + 1 >= a:
merged[-1][1] = max(merged[-1][1], b)
elif merged and a <= merged[-1][1]:
continue # overlapping /64s of different places: the first wins
else:
merged.append([a, b, v])
out, last = bytearray(uvarint(len(merged))), -1
for a, b, v in merged:
out += uvarint(a - last - 1) + uvarint(b - a) + uvarint(v)
last = b
return bytes(out)
blob = bytearray(b"IZG1")
blob += uvarint(len(places))
for (prov, city), lat, lon, n in places:
blob += svarint(round(lat / n * 100)) + svarint(round(lon / n * 100)) + text(city) + text(prov)
blob += uvarint(len(countries))
for c in countries:
s = sums[c]
blob += c.encode() + svarint(round(s[0] / s[2] * 100)) + svarint(round(s[1] / s[2] * 100))
blob += ranges(ir4) + ranges(ir6) + ranges(world)
with open("internal/geo/ip.bin", "wb") as f:
f.write(gzip.compress(bytes(blob), 9, mtime=0))
def simplify(pts, tol):
"""Douglas-Peucker."""
if len(pts) < 3:
return pts
(x1, y1), (x2, y2) = pts[0], pts[-1]
dx, dy = x2 - x1, y2 - y1
norm = math.hypot(dx, dy) or 1e-9
far, at = 0, 0
for i in range(1, len(pts) - 1):
d = abs(dy * (pts[i][0] - x1) - dx * (pts[i][1] - y1)) / norm if (dx or dy) else math.hypot(pts[i][0] - x1, pts[i][1] - y1)
if d > far:
far, at = d, i
if far <= tol:
return [pts[0], pts[-1]]
return simplify(pts[: at + 1], tol)[:-1] + simplify(pts[at:], tol)
shapes = []
for feat in json.load(open(ne))["features"]:
code = feat["properties"]["ISO_A2"]
g = feat["geometry"]
polys = g["coordinates"] if g["type"] == "MultiPolygon" else [g["coordinates"]]
for poly in polys:
ring = poly[0]
if not any(BOX[0] <= x <= BOX[2] and BOX[1] <= y <= BOX[3] for x, y in ring):
continue
ring = simplify(ring, 0.04 if code == "IR" else 0.08)
if len(ring) >= 4:
shapes.append((code, ring))
shapes.sort(key=lambda s: (s[0] != "IR", s[0]))
with open("internal/geo/shapes.go", "w") as f:
f.write("// Code generated by scripts/geo/build.py from Natural Earth (public domain). DO NOT EDIT.\n\n")
f.write("package geo\n\n// Shapes are Iran's border and its neighbours', as lon/lat rings; Iran's first.\n")
f.write("var Shapes = []Shape{\n")
for code, ring in shapes:
pts = ", ".join(f"{{{x:.2f}, {y:.2f}}}" for x, y in ring)
f.write(f'\t{{"{code}", [][2]float64{{{pts}}}}},\n')
f.write("}\n")
print(f"{len(places)} places, {len(countries)} countries, {len(ir4)}+{len(ir6)} Iranian and {len(world)} world rows, "
f"{len(shapes)} shapes", file=sys.stderr)