-
Notifications
You must be signed in to change notification settings - Fork 5
Add local reverse-geocode database for better performance #87
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 |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| package net.buildtheearth.buildteamtools.modules.network.api; | ||
|
|
||
| import com.alpsbte.alpslib.geo.AdminLevel; | ||
| import com.alpsbte.alpslib.utils.ChatHelper; | ||
| import net.buildtheearth.buildteamtools.BuildTeamTools; | ||
| import net.buildtheearth.buildteamtools.modules.navigation.NavigationModule; | ||
| import org.bukkit.Bukkit; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.json.simple.JSONArray; | ||
| import org.json.simple.JSONObject; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| public class OpenStreetMapAPI extends API { | ||
|
|
@@ -16,6 +21,37 @@ | |
| */ | ||
| public static @NotNull CompletableFuture<String[]> getCountryFromLocationAsync(double @NotNull [] coordinates) { | ||
| CompletableFuture<String[]> future = new CompletableFuture<>(); | ||
|
|
||
| if (NavigationModule.getInstance().isEnabled() && NavigationModule.getInstance().getRgcHandler() != null) { | ||
|
Collaborator
Author
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. Quite sure that will not cause issues |
||
| ChatHelper.logDebug("Using custom file API to get country from location: %s, %s", coordinates[0], coordinates[1]); | ||
|
|
||
| // If we are not on main thread, schedule lookup on main thread and complete the outer future there | ||
| if (!Bukkit.isPrimaryThread()) { | ||
| ChatHelper.logDebug("Not on main thread: scheduling RGC lookup on main thread..."); | ||
| Bukkit.getScheduler().runTask(BuildTeamTools.getInstance(), () -> { | ||
| try { | ||
| var rgcGeoLocation = NavigationModule.getInstance().getRgcHandler() | ||
| .locationFromCoordinates((float) coordinates[0], (float) coordinates[1]); | ||
| ChatHelper.logDebug("RGC lookup successful: %s", rgcGeoLocation); | ||
|
Check notice on line 35 in src/main/java/net/buildtheearth/buildteamtools/modules/network/api/OpenStreetMapAPI.java
|
||
| future.complete(new String[]{rgcGeoLocation.get(AdminLevel.COUNTRY), ""}); | ||
| } catch (Exception ex) { | ||
| future.completeExceptionally(ex); | ||
| } | ||
| }); | ||
| return future; | ||
| } | ||
|
|
||
| // We're on the main thread already — run synchronously | ||
| try { | ||
| var location = Objects.requireNonNull(NavigationModule.getInstance().getRgcHandler()).locationFromCoordinates((float) coordinates[0], (float) coordinates[1]); | ||
| ChatHelper.logDebug("RGC lookup successful: %s", location); | ||
|
Check notice on line 47 in src/main/java/net/buildtheearth/buildteamtools/modules/network/api/OpenStreetMapAPI.java
|
||
| future.complete(new String[]{location.get(AdminLevel.COUNTRY), ""}); | ||
| } catch (Exception ex) { | ||
| future.completeExceptionally(ex); | ||
| } | ||
| return future; | ||
| } | ||
|
|
||
| String url = "https://photon.komoot.io/reverse?lat=" + coordinates[0] + "&lon=" + coordinates[1] + "&lang=en"; | ||
|
|
||
| ChatHelper.logDebug("Requesting country from location: %s", url); | ||
|
|
||
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.
Quite sure that will not cause issues because we always read from main thread. also Rgc handler itself is not thread save