switch to carta for geocoding but keep docker nominatim as option in case we want to use it
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CartaLocationClient implements GeocodingClient {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CartaLocationClient.class);
|
||||
|
||||
private final WebClient cartaLocationClient;
|
||||
|
||||
public CartaLocationClient(WebClient cartaLocationClient) {
|
||||
this.cartaLocationClient = cartaLocationClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<GeocodingResponse> reverse(double lat, double lon) {
|
||||
String query = lat + "," + lon;
|
||||
logger.debug("Calling Carta location API with query: {}, type: BUILDING, limit: 3", query);
|
||||
|
||||
return cartaLocationClient.get()
|
||||
.uri(uriBuilder -> {
|
||||
var uri = uriBuilder.path("/location/search")
|
||||
.queryParam("q", query)
|
||||
.queryParam("type", "BUILDING")
|
||||
.queryParam("limit", 3)
|
||||
.build();
|
||||
logger.debug("Full Carta location API URL: {}", uri);
|
||||
return uri;
|
||||
})
|
||||
.retrieve()
|
||||
.bodyToMono(CartaLocationResponse[].class)
|
||||
.doOnNext(responses -> logger.debug("Received {} results from Carta location API",
|
||||
responses != null ? responses.length : 0))
|
||||
.map(responses -> {
|
||||
if (responses == null || responses.length == 0) {
|
||||
logger.error("Carta location API returned empty response for coordinates: lat={}, lon={}", lat, lon);
|
||||
return new GeocodingResponse(null);
|
||||
}
|
||||
logger.debug("Using first result: {}", responses[0]);
|
||||
return mapToGeocodingResponse(responses[0]);
|
||||
})
|
||||
.onErrorResume(error -> {
|
||||
logger.error("Error calling Carta location API for coordinates: lat={}, lon={}", lat, lon, error);
|
||||
return Mono.just(new GeocodingResponse(null));
|
||||
});
|
||||
}
|
||||
|
||||
private GeocodingResponse mapToGeocodingResponse(CartaLocationResponse response) {
|
||||
Map<String, String> addressMap = new HashMap<>();
|
||||
|
||||
// Map Carta fields to Nominatim-style address format
|
||||
if (response.building() != null) {
|
||||
addressMap.put("house_number", response.building());
|
||||
}
|
||||
if (response.street() != null) {
|
||||
addressMap.put("road", response.street());
|
||||
}
|
||||
if (response.suburb() != null) {
|
||||
addressMap.put("suburb", response.suburb());
|
||||
}
|
||||
if (response.city() != null) {
|
||||
addressMap.put("city", response.city());
|
||||
}
|
||||
if (response.state() != null) {
|
||||
addressMap.put("state", response.state());
|
||||
}
|
||||
if (response.postcode() != null) {
|
||||
addressMap.put("postcode", response.postcode());
|
||||
}
|
||||
if (response.country() != null) {
|
||||
addressMap.put("country", response.country());
|
||||
}
|
||||
|
||||
return new GeocodingResponse(addressMap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record CartaLocationResponse(
|
||||
Double longitude,
|
||||
Double latitude,
|
||||
String country,
|
||||
String state,
|
||||
String county,
|
||||
String city,
|
||||
String suburb,
|
||||
String neighborhood,
|
||||
String locality,
|
||||
String street,
|
||||
String building,
|
||||
String postcode,
|
||||
String name
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import SuMoC.backend.nominatim.NominatimClient;
|
||||
import SuMoC.backend.nominatim.ReverseResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class GeoCodingService {
|
||||
|
||||
private final NominatimClient nominatimClient;
|
||||
private final GeocodingClient geocodingClient;
|
||||
|
||||
public GeoCodingService(NominatimClient nominatimClient) {
|
||||
this.nominatimClient = nominatimClient;
|
||||
public GeoCodingService(GeocodingClient geocodingClient) {
|
||||
this.geocodingClient = geocodingClient;
|
||||
}
|
||||
|
||||
public Mono<AddressDTO> getAddress(double lat, double lon) {
|
||||
return nominatimClient.reverse(lat, lon)
|
||||
return geocodingClient.reverse(lat, lon)
|
||||
.map(this::mapToAddressDTO);
|
||||
}
|
||||
|
||||
private AddressDTO mapToAddressDTO(ReverseResponse response) {
|
||||
private AddressDTO mapToAddressDTO(GeocodingResponse response) {
|
||||
var addressMap = response.address();
|
||||
if (addressMap == null) {
|
||||
return new AddressDTO(null, null, null, null, null, null, null, null, null, null);
|
||||
}
|
||||
return new AddressDTO(
|
||||
addressMap.get("house_number"),
|
||||
addressMap.get("road"),
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public interface GeocodingClient {
|
||||
Mono<GeocodingResponse> reverse(double lat, double lon);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(GeocodingProperties.class)
|
||||
public class GeocodingConfig {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "geocoding.provider", havingValue = "docker", matchIfMissing = true)
|
||||
public WebClient nominatimWebClient(GeocodingProperties properties) {
|
||||
return WebClient.builder()
|
||||
.baseUrl(properties.nominatim().baseUrl())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean(name = "geocodingClient")
|
||||
@ConditionalOnProperty(name = "geocoding.provider", havingValue = "docker", matchIfMissing = true)
|
||||
public GeocodingClient nominatimClient(WebClient nominatimWebClient) {
|
||||
return new NominatimClient(nominatimWebClient);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "geocoding.provider", havingValue = "carta")
|
||||
public WebClient cartaLocationWebClient(GeocodingProperties properties) {
|
||||
return WebClient.builder()
|
||||
.baseUrl(properties.carta().baseUrl())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean(name = "geocodingClient")
|
||||
@ConditionalOnProperty(name = "geocoding.provider", havingValue = "carta")
|
||||
public GeocodingClient cartaLocationClient(WebClient cartaLocationWebClient) {
|
||||
return new CartaLocationClient(cartaLocationWebClient);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "geocoding")
|
||||
public record GeocodingProperties(
|
||||
String provider,
|
||||
NominatimConfig nominatim,
|
||||
CartaConfig carta
|
||||
) {
|
||||
public record NominatimConfig(
|
||||
String baseUrl
|
||||
) {
|
||||
}
|
||||
|
||||
public record CartaConfig(
|
||||
String baseUrl
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public record GeocodingResponse(
|
||||
Map<String, String> address
|
||||
) {
|
||||
}
|
||||
|
||||
+8
-8
@@ -1,20 +1,18 @@
|
||||
package SuMoC.backend.nominatim;
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class NominatimClient {
|
||||
public class NominatimClient implements GeocodingClient {
|
||||
|
||||
private final WebClient nominatimClient;
|
||||
|
||||
public NominatimClient(@Qualifier("nominatimWebClient") WebClient nominatimClient) {
|
||||
public NominatimClient(WebClient nominatimClient) {
|
||||
this.nominatimClient = nominatimClient;
|
||||
}
|
||||
|
||||
public Mono<ReverseResponse> reverse(double lat, double lon) {
|
||||
@Override
|
||||
public Mono<GeocodingResponse> reverse(double lat, double lon) {
|
||||
return nominatimClient.get()
|
||||
.uri(uriBuilder -> uriBuilder.path("/reverse")
|
||||
.queryParam("lat", lat)
|
||||
@@ -22,6 +20,8 @@ public class NominatimClient {
|
||||
.queryParam("format", "jsonv2")
|
||||
.build())
|
||||
.retrieve()
|
||||
.bodyToMono(ReverseResponse.class);
|
||||
.bodyToMono(ReverseResponse.class)
|
||||
.map(response -> new GeocodingResponse(response.address()));
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.nominatim;
|
||||
package SuMoC.backend.geocoding;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@@ -15,3 +15,4 @@ public record ReverseResponse(
|
||||
@JsonProperty("address") Map<String, String> address
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package SuMoC.backend.nominatim;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(NominatimProperties.class)
|
||||
public class NominatimClientConfig {
|
||||
|
||||
@Bean
|
||||
public WebClient nominatimWebClient(NominatimProperties properties) {
|
||||
return WebClient.builder()
|
||||
.baseUrl(properties.baseUrl())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package SuMoC.backend.nominatim;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "nominatim")
|
||||
public record NominatimProperties(
|
||||
String baseUrl
|
||||
) {
|
||||
}
|
||||
Reference in New Issue
Block a user