|
| 1 | + |
| 2 | +#[ |
| 3 | +
|
| 4 | +This started out as a Python script, but I rewrote it in NimScript to |
| 5 | +reduce dependencies and pave the way for native compilation on Windows |
| 6 | +with fewer headaches. |
| 7 | +
|
| 8 | +Potentially, this could be done during compilation, however, the NimScript |
| 9 | +implementation is a lot slower than Python (surprisingly) so it stays where |
| 10 | +it is. |
| 11 | +
|
| 12 | +]# |
| 13 | + |
| 14 | +import os |
| 15 | +import streams |
| 16 | +import json |
| 17 | +import parsecsv |
| 18 | +import parseutils |
| 19 | +import strutils |
| 20 | +import algorithm |
| 21 | + |
| 22 | +const |
| 23 | + srcPath = "vendor" |
| 24 | + dbPath = "db" |
| 25 | + |
| 26 | +# Slightly different from the types used in the actual program. |
| 27 | +type |
| 28 | + Geo = tuple[lat: float, lon: float] |
| 29 | + CityRecord = tuple[ |
| 30 | + name: string, |
| 31 | + region: int, |
| 32 | + country: int, |
| 33 | + radius: float, |
| 34 | + loc: Geo |
| 35 | + ] |
| 36 | + |
| 37 | +var |
| 38 | + cities: seq[CityRecord] |
| 39 | + countries: seq[string] |
| 40 | + regions: seq[string] |
| 41 | + |
| 42 | +var |
| 43 | + csv: CsvParser |
| 44 | + # A bit silly that this is how you have to do it in nimscript, but whatever. |
| 45 | + db = newStringStream(readFile(os.joinpath(srcPath, "worldcities.csv"))) |
| 46 | + |
| 47 | +csv.open(db, "worldcities.csv", ',', '\"') |
| 48 | + |
| 49 | +csv.readHeaderRow() |
| 50 | + |
| 51 | +while csv.readRow(): |
| 52 | + var |
| 53 | + population = 0 |
| 54 | + |
| 55 | + # Clean up the population value: some entries in the database |
| 56 | + # have a decimal point in there for some silly reason. |
| 57 | + try: |
| 58 | + population = parseInt(csv.rowEntry("population").replace(".", "")) |
| 59 | + except ValueError: |
| 60 | + continue |
| 61 | + |
| 62 | + # We skip cities with population < 20000 |
| 63 | + # unless they're also marked as region capitals. |
| 64 | + if len(csv.rowEntry("capital")) == 0 and population <= 20000: |
| 65 | + continue |
| 66 | + |
| 67 | + # Now take a guess at a city's effective radius, which |
| 68 | + # we are using to solve the agglomeration problem. |
| 69 | + # I am only guessing here, but I know Moscow's |
| 70 | + # radius is about 15.3km, |
| 71 | + # and the population is listed as 17125000. |
| 72 | + let radius = float(population) / (17125000 / 15.3) |
| 73 | + |
| 74 | + var |
| 75 | + city: CityRecord |
| 76 | + |
| 77 | + # Here we also clean up some bogus entries in regions: |
| 78 | + # I'm not going to believe any country uses slashes to *start* |
| 79 | + # their region names. |
| 80 | + regionString = csv.rowEntry("admin_name").replace("//", "") |
| 81 | + |
| 82 | + countryString = csv.rowEntry("country") |
| 83 | + countryIndex = countries.find(countryString) |
| 84 | + regionIndex = regions.find(regionString) |
| 85 | + |
| 86 | + city.name = csv.rowEntry("city") |
| 87 | + city.radius = radius |
| 88 | + |
| 89 | + city.loc.lat = parseFloat(csv.rowEntry("lat")) |
| 90 | + city.loc.lon = parseFloat(csv.rowEntry("lng")) |
| 91 | + |
| 92 | + # Cities with an empty region name get the region name equal to the city itself. |
| 93 | + if len(regionString) == 0: |
| 94 | + regionString = csv.rowEntry("city") |
| 95 | + |
| 96 | + if regionIndex > -1: |
| 97 | + city.region = regionIndex |
| 98 | + else: |
| 99 | + regions.add(regionString) |
| 100 | + city.region = len(regions)-1 |
| 101 | + |
| 102 | + if countryIndex > -1: |
| 103 | + city.country = countryIndex |
| 104 | + else: |
| 105 | + countries.add(countryString) |
| 106 | + city.country = len(countries)-1 |
| 107 | + |
| 108 | + cities.add(city) |
| 109 | + |
| 110 | +csv.close() |
| 111 | + |
| 112 | +# Sort the cities by population, highest first, |
| 113 | +# so that if the search lands inside the radius of two cities, |
| 114 | +# the bigger one wins. |
| 115 | +func compareCities(a: CityRecord, b: CityRecord): int = |
| 116 | + if a.radius < b.radius: 1 |
| 117 | + elif a.radius == b.radius: 0 |
| 118 | + else: -1 |
| 119 | + |
| 120 | +cities.sort(compareCities) |
| 121 | + |
| 122 | +# Now write our json files. |
| 123 | +# Simple with regions and countries, a bit more complicated for cities, |
| 124 | +# since they're not a simple structure. |
| 125 | + |
| 126 | +var |
| 127 | + citiesJson = newJArray() |
| 128 | + |
| 129 | +for city in cities: |
| 130 | + citiesJson.add( %* { |
| 131 | + "Field0": city.name, |
| 132 | + "Field1": city.region, |
| 133 | + "Field2": city.country, |
| 134 | + "Field3": city.radius, |
| 135 | + "Field4": { |
| 136 | + "Field0": city.loc.lat, |
| 137 | + "Field1": city.loc.lon |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + |
| 142 | +writeFile(os.joinpath(dbPath, "cities.json"), pretty(citiesJson)) |
| 143 | +writeFile(os.joinpath(dbPath, "regions.json"), pretty(%regions)) |
| 144 | +writeFile(os.joinpath(dbPath, "countries.json"), pretty(%countries)) |
| 145 | + |
| 146 | +echo("Database preparation complete.") |
0 commit comments