diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java index 5e3028af3..2a55eec2c 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java @@ -412,63 +412,44 @@ public class OkHttpJsonApiClient { "\n" + "\n" + " "; + List placeBindings = runQuery(leftLatLng, + rightLatLng); + if (placeBindings != null) { + for (PlaceBindings item : placeBindings) { + if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) { + String input = item.getLocation().getValue(); + Pattern pattern = Pattern.compile( + "Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)"); + Matcher matcher = pattern.matcher(input); - int increment = 1; - double longitude = leftLatLng.getLongitude(); + if (matcher.find()) { + String longStr = matcher.group(1); + String latStr = matcher.group(2); + String itemUrl = item.getItem().getValue(); + String itemName = item.getLabel().getValue().replace("&", "&"); + String itemLatitude = latStr; + String itemLongitude = longStr; + String itemClass = item.getClas().getValue(); - while (longitude <= rightLatLng.getLongitude()) { - double NEXT_LONGITUDE = - (increment + longitude) >= 0.0 && (increment + longitude) <= 1.0 ? 0.0 - : increment + longitude; + String formattedItemName = + !itemClass.isEmpty() ? itemName + " (" + itemClass + ")" + : itemName; - double latitude = leftLatLng.getLatitude(); - - while (latitude <= rightLatLng.getLatitude()) { - double NEXT_LATITUDE = - (increment + latitude) >= 0.0 && (increment + latitude) <= 1.0 ? 0.0 - : increment + latitude; - List placeBindings = runQuery(new LatLng(latitude, longitude, 0), - new LatLng(NEXT_LATITUDE, NEXT_LONGITUDE, 0)); - if (placeBindings != null) { - for (PlaceBindings item : placeBindings) { - if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) { - String input = item.getLocation().getValue(); - Pattern pattern = Pattern.compile( - "Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)"); - Matcher matcher = pattern.matcher(input); - - if (matcher.find()) { - String longStr = matcher.group(1); - String latStr = matcher.group(2); - String itemUrl = item.getItem().getValue(); - String itemName = item.getLabel().getValue().replace("&", "&"); - String itemLatitude = latStr; - String itemLongitude = longStr; - String itemClass = item.getClas().getValue(); - - String formattedItemName = - !itemClass.isEmpty() ? itemName + " (" + itemClass + ")" - : itemName; - - String kmlEntry = "\n \n" + - " " + formattedItemName + "\n" + - " " + itemUrl + "\n" + - " \n" + - " " + itemLongitude + "," - + itemLatitude - + "\n" + - " \n" + - " "; - kmlString = kmlString + kmlEntry; - } else { - Timber.e("No match found"); - } - } + String kmlEntry = "\n \n" + + " " + formattedItemName + "\n" + + " " + itemUrl + "\n" + + " \n" + + " " + itemLongitude + "," + + itemLatitude + + "\n" + + " \n" + + " "; + kmlString = kmlString + kmlEntry; + } else { + Timber.e("No match found"); } } - latitude += increment; } - longitude += increment; } kmlString = kmlString + "\n \n" + "\n"; @@ -495,60 +476,42 @@ public class OkHttpJsonApiClient { " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">" + "\n"; - int increment = 1; - double longitude = leftLatLng.getLongitude(); + List placeBindings = runQuery(leftLatLng,rightLatLng); + if (placeBindings != null) { + for (PlaceBindings item : placeBindings) { + if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) { + String input = item.getLocation().getValue(); + Pattern pattern = Pattern.compile( + "Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)"); + Matcher matcher = pattern.matcher(input); - while (longitude <= rightLatLng.getLongitude()) { - double NEXT_LONGITUDE = - (increment + longitude) >= 0.0 && (increment + longitude) <= 1.0 ? 0.0 - : increment + longitude; + if (matcher.find()) { + String longStr = matcher.group(1); + String latStr = matcher.group(2); + String itemUrl = item.getItem().getValue(); + String itemName = item.getLabel().getValue().replace("&", "&"); + String itemLatitude = latStr; + String itemLongitude = longStr; + String itemClass = item.getClas().getValue(); - double latitude = leftLatLng.getLatitude(); + String formattedItemName = + !itemClass.isEmpty() ? itemName + " (" + itemClass + ")" + : itemName; - while (latitude <= rightLatLng.getLatitude()) { - double NEXT_LATITUDE = - (increment + latitude) >= 0.0 && (increment + latitude) <= 1.0 ? 0.0 - : increment + latitude; - List placeBindings = runQuery(new LatLng(latitude, longitude, 0), - new LatLng(NEXT_LATITUDE, NEXT_LONGITUDE, 0)); - if (placeBindings != null) { - for (PlaceBindings item : placeBindings) { - if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) { - String input = item.getLocation().getValue(); - Pattern pattern = Pattern.compile( - "Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)"); - Matcher matcher = pattern.matcher(input); + String gpxEntry = + "\n \n" + + " " + itemName + "\n" + + " " + itemUrl + "\n" + + " "; + gpxString = gpxString + gpxEntry; - if (matcher.find()) { - String longStr = matcher.group(1); - String latStr = matcher.group(2); - String itemUrl = item.getItem().getValue(); - String itemName = item.getLabel().getValue().replace("&", "&"); - String itemLatitude = latStr; - String itemLongitude = longStr; - String itemClass = item.getClas().getValue(); - - String formattedItemName = - !itemClass.isEmpty() ? itemName + " (" + itemClass + ")" - : itemName; - - String gpxEntry = - "\n \n" + - " " + itemName + "\n" + - " " + itemUrl + "\n" + - " "; - gpxString = gpxString + gpxEntry; - - } else { - Timber.e("No match found"); - } - } + } else { + Timber.e("No match found"); } } - latitude += increment; } - longitude += increment; + } gpxString = gpxString + "\n"; return gpxString; diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java index 61d749147..d650f2869 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java @@ -1,8 +1,10 @@ package fr.free.nrw.commons.nearby; +import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween; +import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; + import androidx.annotation.MainThread; import androidx.annotation.Nullable; - import fr.free.nrw.commons.BaseMarker; import fr.free.nrw.commons.MapController; import fr.free.nrw.commons.location.LatLng; @@ -16,9 +18,6 @@ import java.util.Map; import javax.inject.Inject; import timber.log.Timber; -import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween; -import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; - public class NearbyController extends MapController { private static final int MAX_RESULTS = 1000; @@ -39,7 +38,7 @@ public class NearbyController extends MapController { /** * Prepares Place list to make their distance information update later. * - * @param currentLatLng current location for user + * @param currentLatLng current location for user * @param searchLatLng the location user wants to search around * @param returnClosestResult if this search is done to find closest result or all results * @param customQuery if this search is done via an advanced query @@ -118,18 +117,44 @@ public class NearbyController extends MapController { return nearbyPlacesInfo; } - public String getPlacesAsKML(LatLng leftLatLng, LatLng rightLatLng) throws Exception { - return nearbyPlaces.getPlacesAsKML(leftLatLng, rightLatLng); + public String getPlacesAsKML(LatLng currentLocation) throws Exception { + return nearbyPlaces.getPlacesAsKML( + calculateSouthWest(currentLocation.getLatitude(), currentLocation.getLongitude(), 10), + calculateNorthEast(currentLocation.getLatitude(), currentLocation.getLongitude(), 10) + ); } - public String getPlacesAsGPX(LatLng leftLatLng, LatLng rightLatLng) throws Exception { - return nearbyPlaces.getPlacesAsGPX(leftLatLng, rightLatLng); + public String getPlacesAsGPX(LatLng currentLocation) throws Exception { + return nearbyPlaces.getPlacesAsGPX( + calculateSouthWest(currentLocation.getLatitude(), currentLocation.getLongitude(), 10), + calculateNorthEast(currentLocation.getLatitude(), currentLocation.getLongitude(), 10) + ); + } + + public static LatLng calculateNorthEast(double latitude, double longitude, double distance) { + double lat1 = Math.toRadians(latitude); + double deltaLat = distance * 0.008; + double deltaLon = distance / Math.cos(lat1)*0.008; + double lat2 = latitude + deltaLat; + double lon2 = longitude + deltaLon; + + return new LatLng(lat2, lon2, 0); + } + + public static LatLng calculateSouthWest(double latitude, double longitude, double distance) { + double lat1 = Math.toRadians(latitude); + double deltaLat = distance * 0.008; + double deltaLon = distance / Math.cos(lat1)*0.008; + double lat2 = latitude - deltaLat; + double lon2 = longitude - deltaLon; + + return new LatLng(lat2, lon2, 0); } /** * Prepares Place list to make their distance information update later. * - * @param currentLatLng The current latitude and longitude. + * @param currentLatLng The current latitude and longitude. * @param screenTopRight The top right corner of the screen (latitude, * longitude). * @param screenBottomLeft The bottom left corner of the screen (latitude, @@ -221,7 +246,7 @@ public class NearbyController extends MapController { /** * Prepares Place list to make their distance information update later. * - * @param currentLatLng current location for user + * @param currentLatLng current location for user * @param searchLatLng the location user wants to search around * @param returnClosestResult if this search is done to find closest result or all results * @return NearbyPlacesInfo a variable holds Place list without distance information and @@ -239,12 +264,12 @@ public class NearbyController extends MapController { * Loads attractions from location for map view, we need to return BaseMarkerOption data type. * * @param currentLatLng users current location - * @param placeList list of nearby places in Place data type + * @param placeList list of nearby places in Place data type * @return BaseMarkerOptions list that holds nearby places */ public static List loadAttractionsFromLocationToBaseMarkerOptions( - LatLng currentLatLng, - List placeList) { + LatLng currentLatLng, + List placeList) { List baseMarkersList = new ArrayList<>(); if (placeList == null) { @@ -259,7 +284,7 @@ public class NearbyController extends MapController { baseMarker.setPosition( new fr.free.nrw.commons.location.LatLng( place.location.getLatitude(), - place.location.getLongitude(),0)); + place.location.getLongitude(), 0)); baseMarker.setPlace(place); baseMarkersList.add(baseMarker); } @@ -267,8 +292,6 @@ public class NearbyController extends MapController { } - - /** * Updates makerLabelList item isBookmarked value * @@ -280,7 +303,8 @@ public class NearbyController extends MapController { for (ListIterator iter = markerLabelList.listIterator(); iter.hasNext(); ) { MarkerPlaceGroup markerPlaceGroup = iter.next(); - if (markerPlaceGroup.getPlace().getWikiDataEntityId().equals(place.getWikiDataEntityId())) { + if (markerPlaceGroup.getPlace().getWikiDataEntityId() + .equals(place.getWikiDataEntityId())) { iter.set(new MarkerPlaceGroup(isBookmarked, place)); } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java index 01b11b5a4..a23de2471 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java @@ -289,15 +289,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment @Override public boolean onMenuItemClick(@NonNull MenuItem item) { try { - IGeoPoint screenTopRight = binding.map.getProjection().fromPixels(binding.map.getWidth(), 0); - IGeoPoint screenBottomLeft = binding.map.getProjection().fromPixels(0, binding.map.getHeight()); - fr.free.nrw.commons.location.LatLng screenTopRightLatLng = new fr.free.nrw.commons.location.LatLng( - screenBottomLeft.getLatitude(), screenBottomLeft.getLongitude(), 0); - fr.free.nrw.commons.location.LatLng screenBottomLeftLatLng = new fr.free.nrw.commons.location.LatLng( - screenTopRight.getLatitude(), screenTopRight.getLongitude(), 0); progressDialog.setTitle(getString(R.string.saving_gpx_file)); progressDialog.show(); - savePlacesAsGPX(screenTopRightLatLng, screenBottomLeftLatLng); + savePlacesAsGPX(); } catch (Exception e) { throw new RuntimeException(e); } @@ -309,15 +303,9 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment @Override public boolean onMenuItemClick(@NonNull MenuItem item) { try { - IGeoPoint screenTopRight = binding.map.getProjection().fromPixels(binding.map.getWidth(), 0); - IGeoPoint screenBottomLeft = binding.map.getProjection().fromPixels(0, binding.map.getHeight()); - fr.free.nrw.commons.location.LatLng screenTopRightLatLng = new fr.free.nrw.commons.location.LatLng( - screenBottomLeft.getLatitude(), screenBottomLeft.getLongitude(), 0); - fr.free.nrw.commons.location.LatLng screenBottomLeftLatLng = new fr.free.nrw.commons.location.LatLng( - screenTopRight.getLatitude(), screenTopRight.getLongitude(), 0); progressDialog.setTitle(getString(R.string.saving_kml_file)); progressDialog.show(); - savePlacesAsKML(screenTopRightLatLng, screenBottomLeftLatLng); + savePlacesAsKML(); } catch (Exception e) { throw new RuntimeException(e); } @@ -1163,10 +1151,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment } } - private void savePlacesAsKML(LatLng latLng, LatLng nextlatLng) { + private void savePlacesAsKML() { final Observable savePlacesObservable = Observable .fromCallable(() -> nearbyController - .getPlacesAsKML(latLng, nextlatLng)); + .getPlacesAsKML(getMapFocus())); compositeDisposable.add(savePlacesObservable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -1197,10 +1185,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment })); } - private void savePlacesAsGPX(LatLng latLng, LatLng nextlatLng) { + private void savePlacesAsGPX() { final Observable savePlacesObservable = Observable .fromCallable(() -> nearbyController - .getPlacesAsGPX(latLng, nextlatLng)); + .getPlacesAsGPX(getMapFocus())); compositeDisposable.add(savePlacesObservable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())