From 45bbcdeb8a03d7f2e5682168681fff17fb1cb824 Mon Sep 17 00:00:00 2001 From: Alberto Date: Thu, 11 Jan 2024 23:19:32 +0100 Subject: [PATCH] Find events in the same area. --- .../albertus82/eqbulletin/gui/MenuBar.java | 4 +- .../eqbulletin/gui/ResultsTable.java | 4 +- ...dEarthquakesSameAreaSelectionListener.java | 44 ---------- .../FindEventsSameAreaSelectionListener.java | 86 +++++++++++++++++++ 4 files changed, 90 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEarthquakesSameAreaSelectionListener.java create mode 100644 src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEventsSameAreaSelectionListener.java diff --git a/src/main/java/io/github/albertus82/eqbulletin/gui/MenuBar.java b/src/main/java/io/github/albertus82/eqbulletin/gui/MenuBar.java index fb916f8b..d49c441c 100644 --- a/src/main/java/io/github/albertus82/eqbulletin/gui/MenuBar.java +++ b/src/main/java/io/github/albertus82/eqbulletin/gui/MenuBar.java @@ -16,7 +16,7 @@ import io.github.albertus82.eqbulletin.gui.listener.ExitListener; import io.github.albertus82.eqbulletin.gui.listener.ExportCsvSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.FileMenuListener; -import io.github.albertus82.eqbulletin.gui.listener.FindEarthquakesSameAreaSelectionListener; +import io.github.albertus82.eqbulletin.gui.listener.FindEventsSameAreaSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.GoogleMapsBrowserSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.OpenInBrowserSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.PreferencesListener; @@ -121,7 +121,7 @@ public class MenuBar extends AbstractMenu { // Find events in the same area findEventsSameAreaMenuItem = newLocalizedMenuItem(eventMenu, SWT.PUSH, LABEL_MENU_ITEM_FIND_EVENTS_SAME_AREA); - findEventsSameAreaMenuItem.addSelectionListener(new FindEarthquakesSameAreaSelectionListener(gui::getResultsTable, gui::getSearchForm)); + findEventsSameAreaMenuItem.addSelectionListener(new FindEventsSameAreaSelectionListener(gui::getResultsTable, gui::getSearchForm)); // Tools final Menu toolsMenu = new Menu(shell, SWT.DROP_DOWN); diff --git a/src/main/java/io/github/albertus82/eqbulletin/gui/ResultsTable.java b/src/main/java/io/github/albertus82/eqbulletin/gui/ResultsTable.java index e2be8ceb..71ff73f9 100644 --- a/src/main/java/io/github/albertus82/eqbulletin/gui/ResultsTable.java +++ b/src/main/java/io/github/albertus82/eqbulletin/gui/ResultsTable.java @@ -52,7 +52,7 @@ import io.github.albertus82.eqbulletin.gui.listener.CopyLinkSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.EpicenterMapSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.ExportCsvSelectionListener; -import io.github.albertus82.eqbulletin.gui.listener.FindEarthquakesSameAreaSelectionListener; +import io.github.albertus82.eqbulletin.gui.listener.FindEventsSameAreaSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.GoogleMapsBrowserSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.OpenInBrowserSelectionListener; import io.github.albertus82.eqbulletin.gui.listener.ResultsTableContextMenuDetectListener; @@ -544,7 +544,7 @@ private ContextMenu(@NonNull final ResultsTable rt) { // Find events in the same area findEventsSameAreaMenuItem = newLocalizedMenuItem(menu, SWT.PUSH, LABEL_MENU_ITEM_FIND_EVENTS_SAME_AREA); - findEventsSameAreaMenuItem.addSelectionListener(new FindEarthquakesSameAreaSelectionListener(() -> rt, () -> searchForm)); + findEventsSameAreaMenuItem.addSelectionListener(new FindEventsSameAreaSelectionListener(() -> rt, () -> searchForm)); new MenuItem(menu, SWT.SEPARATOR); diff --git a/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEarthquakesSameAreaSelectionListener.java b/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEarthquakesSameAreaSelectionListener.java deleted file mode 100644 index a08329c0..00000000 --- a/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEarthquakesSameAreaSelectionListener.java +++ /dev/null @@ -1,44 +0,0 @@ -package io.github.albertus82.eqbulletin.gui.listener; - -import java.util.function.Supplier; - -import org.eclipse.jface.viewers.TableViewer; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.widgets.Table; - -import io.github.albertus82.eqbulletin.gui.ResultsTable; -import io.github.albertus82.eqbulletin.gui.SearchForm; -import io.github.albertus82.eqbulletin.model.Earthquake; -import io.github.albertus82.jface.maps.MapBounds; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -public class FindEarthquakesSameAreaSelectionListener extends SelectionAdapter { - - @NonNull - private final Supplier resultsTableSupplier; - - @NonNull - private final Supplier searchFormSupplier; - - @Override - public void widgetSelected(final SelectionEvent se) { - final TableViewer tableViewer = resultsTableSupplier.get().getTableViewer(); - final Earthquake selection = (Earthquake) tableViewer.getStructuredSelection().getFirstElement(); - final Table table = tableViewer.getTable(); - if (selection != null && table != null && !table.isDisposed()) { - final SearchForm form = searchFormSupplier.get(); - final float lat = Math.min(MapBounds.LATITUDE_MAX_VALUE - 1f, Math.max(MapBounds.LATITUDE_MIN_VALUE + 1f, selection.getLatitude().getValue())); - form.setLatitudeFrom(lat - 1); - form.setLatitudeTo(lat + 1); - final float lon = Math.min(MapBounds.LONGITUDE_MAX_VALUE - 1f, Math.max(MapBounds.LONGITUDE_MIN_VALUE + 1f, selection.getLongitude().getValue())); - form.setLongitudeFrom(lon - 1); - form.setLongitudeTo(lon + 1); - form.getSearchButton().notifyListeners(SWT.Selection, null); - } - } - -} diff --git a/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEventsSameAreaSelectionListener.java b/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEventsSameAreaSelectionListener.java new file mode 100644 index 00000000..006a20b3 --- /dev/null +++ b/src/main/java/io/github/albertus82/eqbulletin/gui/listener/FindEventsSameAreaSelectionListener.java @@ -0,0 +1,86 @@ +package io.github.albertus82.eqbulletin.gui.listener; + +import java.util.function.Supplier; + +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.Table; + +import io.github.albertus82.eqbulletin.gui.ResultsTable; +import io.github.albertus82.eqbulletin.gui.SearchForm; +import io.github.albertus82.eqbulletin.model.Earthquake; +import io.github.albertus82.jface.maps.MapBounds; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RequiredArgsConstructor +public class FindEventsSameAreaSelectionListener extends SelectionAdapter { + + private static final double AUTHALIC_RADIUS = 6371.0072; + + @NonNull + private final Supplier resultsTableSupplier; + + @NonNull + private final Supplier searchFormSupplier; + + @Override + public void widgetSelected(final SelectionEvent se) { + final TableViewer tableViewer = resultsTableSupplier.get().getTableViewer(); + final Earthquake selection = (Earthquake) tableViewer.getStructuredSelection().getFirstElement(); + final Table table = tableViewer.getTable(); + if (selection != null && table != null && !table.isDisposed()) { + final SearchForm form = searchFormSupplier.get(); + final float offset = 1; + + // Latitude (parallels) + final float lat = Math.min(MapBounds.LATITUDE_MAX_VALUE - offset, Math.max(MapBounds.LATITUDE_MIN_VALUE + offset, selection.getLatitude().getValue())); + final float lat0 = lat - offset; + final float lat1 = lat + offset; + form.setLatitudeFrom(lat0); + form.setLatitudeTo(lat1); + + // Longitude (meridians) + final float lon = selection.getLongitude().getValue(); + float lon0 = lon; + float lon1 = lon; + final double targetArea = computeArea(-1, 1, -1, 1); + double actualArea; + do { + final float step = 0.01f; + lon0 -= step; + lon1 += step; + actualArea = computeArea(lat0, lat1, lon0, lon1); + log.debug("lon0={}, lon1={} -> actualArea={}", lon0, lon1, actualArea); + } + while (actualArea < targetArea); + if (lon0 < -180) { + lon0 += 360; + } + if (lon1 > 180) { + lon1 -= 360; + } + log.debug("lon0={}, lon1={}", lon0, lon1); + form.setLongitudeFrom(lon0); + form.setLongitudeTo(lon1); + + form.getSearchButton().notifyListeners(SWT.Selection, null); + } + } + + private static double computeArea(final double lat0deg, final double lat1deg, final double lon0deg, final double lon1deg) { + final double lat0rad = Math.toRadians(lat0deg); + final double lat1rad = Math.toRadians(lat1deg); + final double lon0rad = Math.toRadians(lon0deg); + final double lon1rad = Math.toRadians(lon1deg); + final double a = Math.sin(lat1rad) - Math.sin(lat0rad); + final double b = lon1rad - lon0rad; + final double c = Math.pow(AUTHALIC_RADIUS, 2); + return a * b * c; + } + +}