Switch to using Antony's list of country coordinates, and load country code of each city.

This commit is contained in:
Mike Hearn 2017-04-13 22:10:02 +02:00
parent 1a2fe41330
commit dec2c82693
3 changed files with 22 additions and 5 deletions

View File

@ -40,14 +40,16 @@ data class WorldCoordinate(val latitude: Double, val longitude: Double) {
/**
* A labelled [WorldCoordinate], where the label is human meaningful. For example, the name of the nearest city.
* Labels should not refer to non-landmarks, for example, they should not contain the names of organisations.
* The [countryCode] field is a two letter ISO country code.
*/
@CordaSerializable
data class PhysicalLocation(val coordinate: WorldCoordinate, val description: String)
data class PhysicalLocation(val coordinate: WorldCoordinate, val description: String, val countryCode: String)
/**
* A simple lookup table of city names to their coordinates. Lookups are case insensitive.
*/
object CityDatabase {
private val matcher = Regex("^([a-zA-Z- ]*) \\((..)\\)$")
private val caseInsensitiveLookups = HashMap<String, PhysicalLocation>()
val cityMap = HashMap<String, PhysicalLocation>()
@ -56,9 +58,11 @@ object CityDatabase {
for (line in lines) {
if (line.startsWith("#")) continue
val (name, lng, lat) = line.split('\t')
val location = PhysicalLocation(WorldCoordinate(lat.toDouble(), lng.toDouble()), name)
caseInsensitiveLookups[name.toLowerCase()] = location
cityMap[name] = location
val matchResult = matcher.matchEntire(name) ?: throw Exception("Could not parse line: $line")
val (city, country) = matchResult!!.destructured
val location = PhysicalLocation(WorldCoordinate(lat.toDouble(), lng.toDouble()), city, country)
caseInsensitiveLookups[city.toLowerCase()] = location
cityMap[city] = location
}
}
}

View File

@ -777,7 +777,6 @@ Changwat Tak (TH) 99.12 16.87
Yangzhou (CN) 119.43 32.38
Novokuznetsk (RU) 87.08 53.75
Kisangani (CD) 25.18 0.52
Bor Sa`id (EG) 32.28 31.27
Warri (NG) 5.75 5.52
Yongkang (CN) 120.02 28.87
Tanggu (CN) 117.63 39.02

View File

@ -0,0 +1,14 @@
package net.corda.core.node
import org.junit.Assert.*
import org.junit.Test
class CityDatabaseTest {
@Test
fun lookups() {
val london = CityDatabase["London"]!!
assertEquals(WorldCoordinate(51.5, -0.12), london.coordinate)
assertEquals("GB", london.countryCode)
assertEquals("London", london.description)
}
}