Nominatim geocoder try 'name' then 'housenumber'

pull/1381/head
Alex Baker 3 years ago
parent cd7bc0ecfe
commit 75cf22a997

@ -2,6 +2,7 @@ package org.tasks.location
import android.content.Context
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
@ -49,13 +50,10 @@ class GeocoderNominatim @Inject constructor(
.get("geocoding").asJsonObject
val geometry = feature.get("geometry").asJsonObject
newPlace().apply {
val type = geocoding.get("type").asString
name = if (type.equals("house")) {
"${geocoding.get("housenumber").asString} ${geocoding.get("street").asString}"
} else {
geocoding.get("name").asString
}
address = geocoding.get("label").asString
name = geocoding.getStringOrNull("name")
?: geocoding.getStringOrNull("housenumber")
?.let { "$it ${geocoding.get("street").asString}" }
address = geocoding.getOrNull("label")?.asString
geometry.get("coordinates").asCoordinates.let {
longitude = it.first
latitude = it.second
@ -65,5 +63,9 @@ class GeocoderNominatim @Inject constructor(
private val JsonElement.asCoordinates: Pair<Double, Double>
get() = asJsonArray.let { Pair(it[0].asDouble, it[1].asDouble) }
private fun JsonObject.getStringOrNull(key: String): String? = getOrNull(key)?.asString
private fun JsonObject.getOrNull(key: String) = if (has(key)) get(key) else null
}
}

@ -1,6 +1,7 @@
package org.tasks.location
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.tasks.TestUtilities.readFile
@ -30,4 +31,30 @@ class GeocoderNominatimTest {
place.address
)
}
@Test
fun residentialGeocode() {
val place = GeocoderNominatim.jsonToPlace(readFile("nominatim/residential.json"))!!
assertNull(place.name)
assertEquals(-9.553143, place.longitude, 0.0)
assertEquals(53.8946414, place.latitude, 0.0)
assertEquals(
"Newport East ED, Westport-Belmullet Municipal District, County Mayo, Connacht, Éire / Ireland",
place.address
)
}
@Test
fun busStopGeocode() {
val place = GeocoderNominatim.jsonToPlace(readFile("nominatim/bus_stop.json"))!!
assertEquals("Blessington Road", place.name)
assertEquals(-6.4154817, place.longitude, 0.0)
assertEquals(53.2751611, place.latitude, 0.0)
assertEquals(
"Blessington Road, Clondalkin ED, Tallaght, South Dublin, County Dublin, Leinster, D24 EP20, Éire / Ireland",
place.address
)
}
}

@ -0,0 +1,44 @@
{
"type": "FeatureCollection",
"geocoding": {
"version": "0.1.0",
"attribution": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"licence": "ODbL",
"query": "53.27478,-6.41509"
},
"features": [
{
"type": "Feature",
"properties": {
"geocoding": {
"place_id": 51801784,
"osm_type": "node",
"osm_id": 4386456055,
"type": "bus_stop",
"accuracy": 0,
"label": "Blessington Road, Clondalkin ED, Tallaght, South Dublin, County Dublin, Leinster, D24 EP20, Éire / Ireland",
"name": "Blessington Road",
"country": "Éire / Ireland",
"postcode": "D24 EP20",
"county": "County Dublin",
"city": "Tallaght",
"district": "Clondalkin ED",
"street": "Blessington Road",
"admin": {
"level5": "Leinster",
"level6": "County Dublin",
"level7": "South Dublin",
"level9": "Clondalkin ED"
}
}
},
"geometry": {
"type": "Point",
"coordinates": [
-6.4154817,
53.2751611
]
}
}
]
}

@ -0,0 +1,42 @@
{
"type": "FeatureCollection",
"geocoding": {
"version": "0.1.0",
"attribution": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"licence": "ODbL",
"query": "53.894495732719,-9.5531306587933"
},
"features": [
{
"type": "Feature",
"properties": {
"geocoding": {
"place_id": 104842902,
"osm_type": "way",
"osm_id": 58918090,
"type": "residential",
"accuracy": 0,
"label": "Newport East ED, Westport-Belmullet Municipal District, County Mayo, Connacht, Éire / Ireland",
"country": "Éire / Ireland",
"county": "County Mayo",
"city": "Westport-Belmullet Municipal District",
"district": "Newport East ED",
"admin": {
"level5": "Connacht",
"level6": "County Mayo",
"level7": "County Mayo",
"level8": "Westport-Belmullet Municipal District",
"level9": "Newport East ED"
}
}
},
"geometry": {
"type": "Point",
"coordinates": [
-9.553143,
53.8946414
]
}
}
]
}
Loading…
Cancel
Save