Skip to content

Commit

Permalink
Find events in the same area.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertus82 committed Jan 11, 2024
1 parent b66d7c5 commit 45bbcde
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ResultsTable> resultsTableSupplier;

@NonNull
private final Supplier<SearchForm> 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;
}

}

0 comments on commit 45bbcde

Please sign in to comment.