Skip to content

Commit

Permalink
More robustness for AWS Geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesname committed Mar 6, 2024
1 parent 38e2ebd commit 53bd388
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions modules/portal/app/services/geocoding/AwsGeocodingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ object AwsGeocodingService {

val credentials = StaticCredentialsProvider.create(new AwsCredentials {
override def accessKeyId(): String = config.getString("config.aws.credentials.access-key-id")

override def secretAccessKey(): String = config.getString("config.aws.credentials.secret-access-key")
})
val region: AwsRegionProvider = new AwsRegionProvider {
Expand All @@ -36,7 +35,19 @@ case class AwsGeocodingService @Inject()(credentials: AwsCredentialsProvider, re

private val logger = play.api.Logger(classOf[AwsGeocodingService])

override def geocode(address: AddressF): Future[Option[Point]] = {
override def geocode(address: AddressF): Future[Option[Point]] = Future {
if (address.postalAddress.trim.nonEmpty && address.countryCode3.nonEmpty) {
try runLocationGeocode(address) catch {
case e: Exception =>
logger.error(s"Error geocoding address: $address", e)
None
}
} else {
None
}
}

private def runLocationGeocode(address: AddressF): Option[Point] = {
val client: LocationClient = LocationClient
.builder()
.credentialsProvider(credentials)
Expand All @@ -50,19 +61,17 @@ case class AwsGeocodingService @Inject()(credentials: AwsCredentialsProvider, re
.maxResults(1)
.build()

Future {
client.searchPlaceIndexForText(request)
.results()
.asScala
.headOption
.map { result =>
logger.debug(s"Geocoding result: $result, point: ${result.place().geometry().point()}")
val lonLat = result.place().geometry().point()
Point(
BigDecimal(lonLat.get(1)),
BigDecimal(lonLat.get(0))
)
}
}
client.searchPlaceIndexForText(request)
.results()
.asScala
.headOption
.map { result =>
logger.debug(s"Geocoding result: $result, point: ${result.place().geometry().point()}")
val lonLat = result.place().geometry().point()
Point(
BigDecimal(lonLat.get(1)),
BigDecimal(lonLat.get(0))
)
}
}
}

0 comments on commit 53bd388

Please sign in to comment.