-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the repository geocoder using AWS Location service
The old Bing one seems sadly defunct
- Loading branch information
Showing
10 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
modules/portal/app/services/geocoding/AwsGeocodingService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package services.geocoding | ||
|
||
import models.AddressF | ||
import software.amazon.awssdk.auth.credentials.{AwsCredentials, AwsCredentialsProvider, StaticCredentialsProvider} | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.regions.providers.AwsRegionProvider | ||
import software.amazon.awssdk.services.location.LocationClient | ||
import software.amazon.awssdk.services.location.model.SearchPlaceIndexForTextRequest | ||
|
||
import javax.inject.Inject | ||
import scala.collection.JavaConverters._ | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
|
||
object AwsGeocodingService { | ||
def apply(config: com.typesafe.config.Config)(implicit ec: ExecutionContext): 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 { | ||
override def getRegion: Region = Region.of(config.getString("config.aws.region.default-region")) | ||
} | ||
|
||
new AwsGeocodingService( | ||
credentials, | ||
region, | ||
config.getString("config.aws.index-name") | ||
) | ||
} | ||
} | ||
|
||
case class AwsGeocodingService @Inject()(credentials: AwsCredentialsProvider, region: AwsRegionProvider, indexName: String)(implicit executionContext: ExecutionContext) extends GeocodingService { | ||
|
||
private val logger = play.api.Logger(classOf[AwsGeocodingService]) | ||
|
||
override def geocode(address: AddressF): Future[Option[Point]] = { | ||
val client: LocationClient = LocationClient | ||
.builder() | ||
.credentialsProvider(credentials) | ||
.region(region.getRegion) | ||
.build() | ||
|
||
val request = SearchPlaceIndexForTextRequest.builder() | ||
.indexName(indexName) | ||
.text(address.postalAddress) | ||
.filterCountries(address.countryCode3.toList: _*) | ||
.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)) | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
package services.geocoding | ||
|
||
import com.google.inject.ImplementedBy | ||
import models.AddressF | ||
|
||
import scala.concurrent.Future | ||
|
||
case class Point(latitude: BigDecimal, longitude: BigDecimal) | ||
|
||
@ImplementedBy(classOf[BingGeocodingService]) | ||
trait GeocodingService { | ||
def geocode(address: AddressF): Future[Option[Point]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package services.geocoding | ||
|
||
import models.AddressF | ||
import play.api.Logger | ||
|
||
import javax.inject.{Inject, Singleton} | ||
import scala.concurrent.Future | ||
|
||
@Singleton | ||
case class NoopGeocodingService @Inject()() extends GeocodingService { | ||
private val logger = Logger(classOf[NoopGeocodingService]) | ||
override def geocode(address: AddressF): Future[Option[Point]] = { | ||
logger.info(s"Geocoding disabled: $address") | ||
Future.successful(None) | ||
} | ||
} |