Skip to content

Commit

Permalink
Merge pull request #534 from mapzen/524-confidence-score-geocode
Browse files Browse the repository at this point in the history
Use confidence score
  • Loading branch information
ecgreb committed Apr 6, 2016
2 parents 755b3e8 + 590e8e2 commit c1621f3
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 53 deletions.
74 changes: 60 additions & 14 deletions app/src/main/kotlin/com/mapzen/erasermap/controller/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.mapzen.erasermap.R
import com.mapzen.erasermap.model.AndroidAppSettings
import com.mapzen.erasermap.model.ApiKeys
import com.mapzen.erasermap.model.AppSettings
import com.mapzen.erasermap.model.ConfidenceHandler
import com.mapzen.erasermap.model.MapzenLocation
import com.mapzen.erasermap.model.RouteManager
import com.mapzen.erasermap.model.TileHttpHandler
Expand Down Expand Up @@ -75,6 +76,8 @@ import com.mapzen.valhalla.Router
import retrofit.Callback
import retrofit.RetrofitError
import retrofit.client.Response
import java.math.RoundingMode
import java.text.DecimalFormat
import java.util.ArrayList
import javax.inject.Inject

Expand Down Expand Up @@ -118,6 +121,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
var searchView: PeliasSearchView? = null
var voiceNavigationController: VoiceNavigationController? = null
var notificationCreator: NotificationCreator? = null
lateinit var confidenceHandler: ConfidenceHandler

// activity_main
val findMeButton: ImageButton by lazy { findViewById(R.id.find_me) as ImageButton }
Expand Down Expand Up @@ -164,10 +168,13 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
initCompass()
initReverseButton()
initMapRotateListener()
initConfidenceHandler()
presenter.onCreate()
presenter.onRestoreViewState()
supportActionBar?.setDisplayShowTitleEnabled(false)
settings.initTangramDebugFlags()
settings.initSearchResultVersion(this, savedSearch)
initVoiceNavigationController()
initNotificationCreator()
}

Expand Down Expand Up @@ -242,17 +249,23 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
mapView.setZOrderMediaOverlay(true) //so that white bg shows, not window when launching
mapController = MapController(this, mapView, "style/bubble-wrap.yaml")
mapController?.setLongPressResponder({
x, y -> presenter.onReverseGeoRequested(x, y)
x, y ->
confidenceHandler.longPressed = true
presenter.onReverseGeoRequested(x, y)
})
mapController?.setTapResponder(object: TouchInput.TapResponder {
override fun onSingleTapUp(x: Float, y: Float): Boolean = false
override fun onSingleTapConfirmed(x: Float, y: Float): Boolean {
confidenceHandler.longPressed = false
var coords = mapController?.coordinatesAtScreenPosition(x.toDouble(), y.toDouble())
presenter?.reverseGeoLngLat = coords
poiTapPoint = floatArrayOf(x, y)
mapController?.pickFeature(x, y)
return true
}
})
mapController?.setDoubleTapResponder({ x, y ->
confidenceHandler.longPressed = false
val tappedPos = mapController?.coordinatesAtScreenPosition(x.toDouble(), y.toDouble())
val currentPos = mapController?.mapPosition
if (tappedPos != null && currentPos != null) {
Expand All @@ -266,6 +279,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
})
mapController?.setFeatureTouchListener({
properties, positionX, positionY ->
confidenceHandler.longPressed = false
// Reassign tapPoint to center of the feature tapped
// Also used in placing the pin
poiTapPoint = floatArrayOf(positionX, positionY)
Expand Down Expand Up @@ -338,6 +352,10 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
notificationCreator = NotificationCreator(this)
}

private fun initConfidenceHandler() {
confidenceHandler = ConfidenceHandler(presenter)
}

override fun centerMapOnLocation(location: Location, zoom: Float) {
mapController?.setMapPosition(location.longitude, location.latitude)
mapController?.mapZoom = zoom
Expand Down Expand Up @@ -408,7 +426,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
searchView?.setOnPeliasFocusChangeListener { view, b ->
if (b) {
expandSearchView()
} else if(presenter.resultListVisible) {
} else if (presenter.resultListVisible) {
onCloseAllSearchResults()
} else {
searchView?.setQuery(presenter.currentSearchTerm, false)
Expand Down Expand Up @@ -447,7 +465,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
}

override fun showAllSearchResults(features: List<Feature>) {
if(presenter.resultListVisible as Boolean) {
if (presenter.resultListVisible) {
onCloseAllSearchResults()
} else {
saveCurrentSearchTerm()
Expand Down Expand Up @@ -559,7 +577,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
}

private fun showSearchResultsView(features: List<Feature>) {
searchResultsView.setAdapter(SearchResultsAdapter(this, features))
searchResultsView.setAdapter(SearchResultsAdapter(this, features, confidenceHandler))
searchResultsView.visibility = View.VISIBLE
searchResultsView.onSearchResultsSelectedListener = this
}
Expand Down Expand Up @@ -606,17 +624,14 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,

if (poiTapFallback) return

val simpleFeature = SimpleFeature.fromFeature(features.get(0))
val lngLat = LngLat(simpleFeature.lng(), simpleFeature.lat())

val properties = com.mapzen.tangram.Properties()
properties.set(MAP_DATA_PROP_STATE, MAP_DATA_PROP_STATE_ACTIVE)
if (reverseGeocodeData == null) {
reverseGeocodeData = MapData("reverse_geocode")
Tangram.addDataSource(reverseGeocodeData)
}
reverseGeocodeData?.clear()
reverseGeocodeData?.addPoint(properties, lngLat)
reverseGeocodeData?.addPoint(properties, presenter?.reverseGeoLngLat)

mapController?.requestRender()
}
Expand Down Expand Up @@ -648,7 +663,8 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
}

override fun showPlaceSearchFeature(features: List<Feature>) {
searchResultsView.setAdapter(SearchResultsAdapter(this, features.subList(0, 1)))
searchResultsView.setAdapter(SearchResultsAdapter(this, features.subList(0, 1),
confidenceHandler))
searchResultsView.visibility = View.VISIBLE
searchResultsView.onSearchResultsSelectedListener = this
}
Expand Down Expand Up @@ -708,6 +724,7 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
override fun reverseGeolocate(screenX: Float, screenY: Float) {
pelias.setLocationProvider(presenter.getPeliasLocationProvider())
var coords = mapController?.coordinatesAtScreenPosition(screenX.toDouble(), screenY.toDouble())
presenter?.reverseGeoLngLat = coords
presenter.currentFeature = getGenericLocationFeature(coords?.latitude as Double,
coords?.longitude as Double)
pelias.reverse(coords?.latitude as Double, coords?.longitude as Double,
Expand Down Expand Up @@ -777,20 +794,42 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,

override fun showRoutePreview(location: Location, feature: Feature) {
showCurrentLocation(location)

routeManager.origin = location
routeManager.destination = feature

if (location.hasBearing()) {
routeManager.bearing = location.bearing
} else {
routeManager.bearing = null
}

routePreviewView.destination = SimpleFeature.fromFeature(feature)
if (!confidenceHandler.useRawLatLng(feature.properties.confidence)) {
routePreviewView.destination = SimpleFeature.fromFeature(feature)
routeManager.destination = feature
} else {
val rawFeature = generateRawFeature()
routePreviewView.destination = SimpleFeature.fromFeature(rawFeature)
routeManager.destination = rawFeature
}
route()
}

private fun generateRawFeature(): Feature {
var rawFeature: Feature = Feature()
rawFeature.geometry = Geometry()
val coords = ArrayList<Double>()
coords.add(presenter?.reverseGeoLngLat?.longitude as Double)
coords.add(presenter?.reverseGeoLngLat?.latitude as Double)
rawFeature.geometry.coordinates = coords
val properties = Properties()
val formatter = DecimalFormat(".####")
formatter.roundingMode = RoundingMode.HALF_UP
val lng = formatter.format(presenter?.reverseGeoLngLat?.longitude as Double)
val lat = formatter.format(presenter?.reverseGeoLngLat?.latitude as Double)
properties.name = "$lng, $lat"
rawFeature.properties = properties
return rawFeature
}

override fun drawRoute(route: Route) {
routeModeView.drawRoute(route)
}
Expand Down Expand Up @@ -1056,8 +1095,14 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
}

override fun startRoutingMode(feature: Feature) {
showRoutingMode(feature)
routeModeView.startRoute(feature, routeManager.route)
if (confidenceHandler.useRawLatLng(feature.properties.confidence)) {
val rawFeature = generateRawFeature()
showRoutingMode(rawFeature)
routeModeView.startRoute(rawFeature, routeManager.route)
} else {
showRoutingMode(feature)
routeModeView.startRoute(feature, routeManager.route)
}
setRoutingCamera()
hideRoutePins()
}
Expand Down Expand Up @@ -1189,3 +1234,4 @@ class MainActivity : AppCompatActivity(), MainViewController, RouteCallback,
endPin = null
}
}

Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package com.mapzen.erasermap.model

import android.content.Context
import android.location.Location
import android.preference.PreferenceManager
import com.mapzen.erasermap.EraserMapApplication
import com.mapzen.erasermap.R
import com.mapzen.pelias.SavedSearch
import com.mapzen.tangram.DebugFlags
import com.mapzen.tangram.Tangram
import com.mapzen.valhalla.Router
import java.io.File
import java.util.Locale

public class AndroidAppSettings(val application: EraserMapApplication) : AppSettings {
class AndroidAppSettings(val application: EraserMapApplication) : AppSettings {
companion object {
public val KEY_DISTANCE_UNITS: String = "list_distance_units"
public val KEY_MOCK_LOCATION_ENABLED: String = "checkbox_mock_location"
public val KEY_MOCK_LOCATION_VALUE: String = "edittext_mock_location"
public val KEY_MOCK_ROUTE_ENABLED: String = "checkbox_mock_route"
public val KEY_MOCK_ROUTE_VALUE: String = "edittext_mock_route"
public val KEY_TILE_DEBUG_ENABLED: String = "checkbox_tile_debug"
public val KEY_LABEL_DEBUG_ENABLED: String = "checkbox_label_debug"
public val KEY_TANGRAM_INFOS_DEBUG_ENABLED: String = "checkbox_tangram_infos_debug"
public val KEY_BUILD_NUMBER: String = "edittext_build_number"
public val KEY_ERASE_HISTORY: String = "edittext_erase_history"
public val KEY_CACHE_SEARCH_HISTORY: String = "checkbox_cache_search_results"
public val SHOW_DEBUG_SETTINGS_QUERY = "!!!!!!!!"
public val KEY_SHOW_DEBUG_SETTINGS = "show_debug_settings"
val KEY_DISTANCE_UNITS: String = "list_distance_units"
val KEY_MOCK_LOCATION_ENABLED: String = "checkbox_mock_location"
val KEY_MOCK_LOCATION_VALUE: String = "edittext_mock_location"
val KEY_MOCK_ROUTE_ENABLED: String = "checkbox_mock_route"
val KEY_MOCK_ROUTE_VALUE: String = "edittext_mock_route"
val KEY_TILE_DEBUG_ENABLED: String = "checkbox_tile_debug"
val KEY_LABEL_DEBUG_ENABLED: String = "checkbox_label_debug"
val KEY_TANGRAM_INFOS_DEBUG_ENABLED: String = "checkbox_tangram_infos_debug"
val KEY_BUILD_NUMBER: String = "edittext_build_number"
val KEY_ERASE_HISTORY: String = "edittext_erase_history"
val KEY_CACHE_SEARCH_HISTORY: String = "checkbox_cache_search_results"
val SHOW_DEBUG_SETTINGS_QUERY = "!!!!!!!!"
val KEY_SHOW_DEBUG_SETTINGS = "show_debug_settings"
val KEY_SEARCH_RESULTS_VERSION = "search_results_version"
val SEARCH_RESULTS_VERSION_UNKNOWN = -1
val SEARCH_RESULTS_VERSION = 1
}

private val prefs = PreferenceManager.getDefaultSharedPreferences(application)
Expand Down Expand Up @@ -66,12 +71,12 @@ public class AndroidAppSettings(val application: EraserMapApplication) : AppSett
val values = prefs.getString(KEY_MOCK_LOCATION_VALUE, default)
val split = values?.split(",")
val location = Location("mock")
location.setLatitude(split?.get(0)?.toDouble() as Double)
location.setLongitude(split?.get(1)?.toDouble() as Double)
location.latitude = split?.get(0)?.toDouble() as Double
location.longitude = split?.get(1)?.toDouble() as Double
return location
}
set(value) {
val string = value.getLatitude().toString() + ", " + value.getLongitude().toString()
val string = value.latitude.toString() + ", " + value.longitude.toString()
prefs.edit().putString(KEY_MOCK_LOCATION_VALUE, string).commit()
}

Expand All @@ -96,7 +101,7 @@ public class AndroidAppSettings(val application: EraserMapApplication) : AppSett
return File(application.getExternalFilesDir(null), value)
}
set(value) {
prefs.edit().putString(KEY_MOCK_ROUTE_VALUE, value.getName()).commit()
prefs.edit().putString(KEY_MOCK_ROUTE_VALUE, value.name).commit()
}

/**
Expand Down Expand Up @@ -158,4 +163,33 @@ public class AndroidAppSettings(val application: EraserMapApplication) : AppSett
Tangram.setDebugFlag(DebugFlags.LABELS, isLabelDebugEnabled)
Tangram.setDebugFlag(DebugFlags.TANGRAM_INFOS, isTangramInfosDebugEnabled)
}

override fun initSearchResultVersion(context: Context, savedSearch: SavedSearch) {
if (newSearchResultVersion(context)) {
setSearchResultVersion(context, SEARCH_RESULTS_VERSION)
clearHistory(context, savedSearch)
}
}

private fun newSearchResultVersion(context: Context): Boolean {
val prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.contains(KEY_SEARCH_RESULTS_VERSION)) {
return true
}
val version = prefs.getInt(KEY_SEARCH_RESULTS_VERSION, SEARCH_RESULTS_VERSION_UNKNOWN);
return version < SEARCH_RESULTS_VERSION;
}

private fun setSearchResultVersion(context: Context, version: Int) {
val prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt(KEY_SEARCH_RESULTS_VERSION, version).commit()
}

private fun clearHistory(context: Context, savedSearch: SavedSearch) {
savedSearch.clear()
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(SavedSearch.TAG, savedSearch.serialize())
.commit()
}
}
25 changes: 14 additions & 11 deletions app/src/main/kotlin/com/mapzen/erasermap/model/AppSettings.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.mapzen.erasermap.model

import android.content.Context
import android.location.Location
import com.mapzen.pelias.SavedSearch
import com.mapzen.valhalla.Router
import java.io.File

public interface AppSettings {
public var distanceUnits: Router.DistanceUnits
public var isMockLocationEnabled: Boolean
public var mockLocation: Location
public var isMockRouteEnabled: Boolean
public var mockRoute: File
public var isTileDebugEnabled: Boolean
public var isLabelDebugEnabled: Boolean
public var isTangramInfosDebugEnabled: Boolean
public var isCacheSearchResultsEnabled: Boolean
interface AppSettings {
var distanceUnits: Router.DistanceUnits
var isMockLocationEnabled: Boolean
var mockLocation: Location
var isMockRouteEnabled: Boolean
var mockRoute: File
var isTileDebugEnabled: Boolean
var isLabelDebugEnabled: Boolean
var isTangramInfosDebugEnabled: Boolean
var isCacheSearchResultsEnabled: Boolean

public fun initTangramDebugFlags()
fun initTangramDebugFlags()
fun initSearchResultVersion(context: Context, savedSearch: SavedSearch)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mapzen.erasermap.model

import com.mapzen.erasermap.presenter.MainPresenter

class ConfidenceHandler(val presenter: MainPresenter) {

var longPressed = false;

companion object {
const val CONFIDENCE_THRESHOLD = 0.8
const val CONFIDENCE_MISSING = -1.0
}

fun useRawLatLng(confidence: Double): Boolean {
if (confidence == CONFIDENCE_MISSING || !longPressed) {
return false;
}
return confidence < CONFIDENCE_THRESHOLD
&& presenter?.reverseGeoLngLat != null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.mapzen.erasermap.view.RouteViewController
import com.mapzen.pelias.PeliasLocationProvider
import com.mapzen.pelias.gson.Feature
import com.mapzen.pelias.gson.Result
import com.mapzen.tangram.LngLat

public interface MainPresenter {
companion object {
Expand All @@ -22,7 +23,7 @@ public interface MainPresenter {
public var routingEnabled: Boolean
public var resultListVisible: Boolean
public var reverseGeo: Boolean

public var reverseGeoLngLat: LngLat?

public fun onSearchResultsAvailable(result: Result?)
public fun onReverseGeocodeResultsAvailable(searchResults: Result?)
Expand Down

0 comments on commit c1621f3

Please sign in to comment.