Repository navigation
feat(address): Nominatim API #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package net.buildtheearth.buildteamtools.modules.network.api; | ||
|
|
||
| import com.alpsbte.alpslib.utils.ChatHelper; | ||
| import net.buildtheearth.buildteamtools.BuildTeamTools; | ||
| import net.buildtheearth.model.GeographicalCoordinate; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.json.simple.JSONObject; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public class NominatimAPI { | ||
| private static final String BASE_URL = "https://nominatim.openstreetmap.org/"; | ||
|
|
||
| public static @NotNull CompletableFuture<String> getAddressFromCoordinatesAsync( | ||
| @NotNull GeographicalCoordinate coordinates | ||
| ) { | ||
| CompletableFuture<String> future = new CompletableFuture<>(); | ||
|
|
||
| String url = BASE_URL + "reverse?format=jsonv2" | ||
| + "&lat=" + coordinates.latitude() | ||
| + "&lon=" + coordinates.longitude() | ||
| + "&addressdetails=1" | ||
| + "&accept-language=en"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making that optional would be helpful. I would suggest that by default it uses local language and only with a flag uses en. If you have another suggestion/Opinion lmk |
||
| ChatHelper.getStandardComponent(false, url); | ||
| String userAgent = BuildTeamTools.getInstance().getMainConfig().userAgent(); | ||
| API.getAsync(url, userAgent, new API.ApiResponseCallback() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Requests are sent without shared rate limiting. Concurrent or repeated address lookups can exceed Nominatim’s public API limit of one request per second, leading to rejected requests or blocked access. Add server-wide throttling before sending requests; caching repeated lookups could also help |
||
| @Override | ||
| public void onResponse(String response) { | ||
| try { | ||
| String address = formatAddressFromResponse(response); | ||
| future.complete(address); | ||
| } catch (Exception e) { | ||
| future.completeExceptionally(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(IOException e) { | ||
| future.completeExceptionally(e); | ||
| } | ||
| }); | ||
|
|
||
| return future; | ||
| } | ||
|
|
||
| private static String formatAddressFromResponse(String response) { | ||
| JSONObject jsonObject = API.createJSONObject(response); | ||
|
|
||
| ChatHelper.logDebug( | ||
| "Response from Nominatim: %s", | ||
| jsonObject | ||
| ); | ||
|
|
||
| Object displayName = jsonObject.get("display_name"); | ||
|
|
||
| if (displayName == null || displayName.toString().isBlank()) { | ||
| ChatHelper.logError( | ||
| "No address data found for these coordinates." | ||
| ); | ||
| return ""; | ||
| } | ||
|
|
||
| return displayName.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,11 @@ default String apiKey() { | |
| return "00000000-0000-0000-0000-000000000000"; | ||
| } | ||
|
|
||
| @Comments("User Agent for NominatimAPI") | ||
| default String userAgent() { | ||
| return "BuildTeamTools/1.0 (contact: email@example.com)"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should provide a default E-Mail which is also reachable. |
||
| } | ||
|
|
||
|
Comment on lines
+22
to
+26
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is in the wrong config and should be added/moved to the NavigationConfig or also be used by/for the other api's. It should also only allow to set a different e-mail/comment (i think the part in () is called that). The |
||
| @Comments("The main world using the custom BTE Dynmaxion projection") | ||
| default String earthWorld() { | ||
| return "world"; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be out of scope - you can decide yourself. But i think we should set a user agent for all api calls. Also for the other where it is not required - if we do that the option can stay at the Main Config