Skip to content
Merged
Prev Previous commit
Next Next commit
#3408 Refactoring the FileProcessor and GPSExtractor classes - refact…
…or and rename GpsExtractor
  • Loading branch information
macgills committed Mar 19, 2020
commit e8c1cd1a24de97d02049720be9b7ebcab517198a
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.free.nrw.commons.repository;

import fr.free.nrw.commons.upload.GPSExtractor;
import fr.free.nrw.commons.upload.ImageCoordinates;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -204,7 +204,7 @@ public Place getNearbyPlaces(double latitude, double longitude) {
}
}

public void usePictureCoordinatesFrom(GPSExtractor gpsExtractor) {
uploadModel.usePictureCoordinatesFrom(gpsExtractor);
public void usePictureCoordinatesFrom(ImageCoordinates imageCoordinates) {
uploadModel.usePictureCoordinatesFrom(imageCoordinates);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.free.nrw.commons.repository;

import fr.free.nrw.commons.upload.GPSExtractor;
import fr.free.nrw.commons.upload.ImageCoordinates;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -273,7 +273,7 @@ public Place checkNearbyPlaces(double decLatitude, double decLongitude) {
return remoteDataSource.getNearbyPlaces(decLatitude, decLongitude);
}

public void usePictureCoordinatesFrom(GPSExtractor gpsExtractor) {
remoteDataSource.usePictureCoordinatesFrom(gpsExtractor);
public void usePictureCoordinatesFrom(ImageCoordinates imageCoordinates) {
remoteDataSource.usePictureCoordinatesFrom(imageCoordinates);
}
}
42 changes: 24 additions & 18 deletions app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void cleanup() {
/**
* Processes filePath coordinates, either from EXIF data or user location
*/
GPSExtractor processFileCoordinates(SimilarImageInterface similarImageInterface,
ImageCoordinates processFileCoordinates(SimilarImageInterface similarImageInterface,
String filePath) {
ExifInterface exifInterface = null;
try {
Expand All @@ -69,8 +69,8 @@ GPSExtractor processFileCoordinates(SimilarImageInterface similarImageInterface,
redactExifTags(exifInterface, getExifTagsToRedact());

Timber.d("Calling GPSExtractor");
GPSExtractor originalImageExtractor = new GPSExtractor(exifInterface);
String decimalCoords = originalImageExtractor.getCoords();
ImageCoordinates originalImageExtractor = new ImageCoordinates(exifInterface);
String decimalCoords = originalImageExtractor.getDecimalCoords();
if (decimalCoords == null || !originalImageExtractor.imageCoordsExists) {
//Find other photos taken around the same time which has gps coordinates
findOtherImages(originalImageExtractor, new File(filePath),
Expand Down Expand Up @@ -143,7 +143,7 @@ private void redactTag(ExifInterface exifInterface, String tag) {
* @param similarImageInterface
*/
private void findOtherImages(
GPSExtractor originalImageExtractor,
ImageCoordinates originalImageExtractor,
File fileBeingProcessed,
SimilarImageInterface similarImageInterface) {
//Time when the original image was created
Expand All @@ -156,10 +156,10 @@ private void findOtherImages(
&& file.lastModified() - timeOfCreation >= -(120 * 1000)) {
//Make sure the photos were taken within 20seconds
Timber.d("fild date:" + file.lastModified() + " time of creation" + timeOfCreation);
GPSExtractor similarPictureExtractor = createGpsExtractor(file);
ImageCoordinates similarPictureExtractor = createGpsExtractor(file);
Timber.d("not null fild EXIF" + similarPictureExtractor.imageCoordsExists + " coords"
+ similarPictureExtractor.getCoords());
if (similarPictureExtractor.getCoords() != null
+ similarPictureExtractor.getDecimalCoords());
if (similarPictureExtractor.getDecimalCoords() != null
&& similarPictureExtractor.imageCoordsExists) {
// Current image has gps coordinates and it's not current gps locaiton
Timber.d("This filePath has image coords:" + file.getAbsolutePath());
Expand All @@ -176,35 +176,41 @@ private void findOtherImages(
}

@NotNull
private GPSExtractor createGpsExtractor(File file) {
private ImageCoordinates createGpsExtractor(File file) {
try {
return new GPSExtractor(contentResolver.openInputStream(Uri.fromFile(file)));
return new ImageCoordinates(contentResolver.openInputStream(Uri.fromFile(file)));
} catch (IOException e) {
e.printStackTrace();
return new GPSExtractor(file.getAbsolutePath());
Timber.e(e);
try {
return new ImageCoordinates(file.getAbsolutePath());
} catch (IOException ex) {
Timber.e(ex);
return null;
}
}
}

/**
* Initiates retrieval of image coordinates or user coordinates, and caching of coordinates. Then
* initiates the calls to MediaWiki API through an instance of CategoryApi.
*
* @param gpsExtractor
* @param imageCoordinates
*/
@SuppressLint("CheckResult")
public void useImageCoords(GPSExtractor gpsExtractor) {
useImageCoords(gpsExtractor, gpsExtractor.getCoords());
public void useImageCoords(ImageCoordinates imageCoordinates) {
useImageCoords(imageCoordinates, imageCoordinates.getDecimalCoords());
}

private void useImageCoords(GPSExtractor gpsExtractor, String decimalCoords) {
private void useImageCoords(ImageCoordinates imageCoordinates, String decimalCoords) {
if (decimalCoords != null) {
Timber.d("Decimal coords of image: %s", decimalCoords);
Timber.d("is EXIF data present:" + gpsExtractor.imageCoordsExists +
Timber.d("is EXIF data present:" + imageCoordinates.imageCoordsExists +
" from findOther image");

// Only set cache for this point if image has coords
if (gpsExtractor.imageCoordsExists) {
cacheController.setQtPoint(gpsExtractor.getDecLongitude(), gpsExtractor.getDecLatitude());
if (imageCoordinates.imageCoordsExists) {
cacheController
.setQtPoint(imageCoordinates.getDecLongitude(), imageCoordinates.getDecLatitude());
}

List<String> displayCatList = cacheController.findCategory();
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ static String getGeolocationOfFile(String filePath) {

try {
ExifInterface exifInterface = new ExifInterface(filePath);
GPSExtractor imageObj = new GPSExtractor(exifInterface);
ImageCoordinates imageObj = new ImageCoordinates(exifInterface);
if (imageObj.imageCoordsExists) { // If image has geolocation information in its EXIF
return imageObj.getCoords();
return imageObj.getDecimalCoords();
} else {
return "";
}
Expand Down Expand Up @@ -178,4 +178,4 @@ public static boolean fileExists(Uri localUri) {
return false;
}
}
}
}
152 changes: 0 additions & 152 deletions app/src/main/java/fr/free/nrw/commons/upload/GPSExtractor.java

This file was deleted.

92 changes: 92 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/upload/ImageCoordinates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fr.free.nrw.commons.upload;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.exifinterface.media.ExifInterface;
import java.io.IOException;
import java.io.InputStream;
import timber.log.Timber;

/**
* Extracts geolocation to be passed to API for category suggestions. If a picture with geolocation
* is uploaded, extract latitude and longitude from EXIF data of image.
*/
public class ImageCoordinates {

private double decLatitude;
private double decLongitude;
public boolean imageCoordsExists;
private String decimalCoords;

/**
* Construct from the file path of the image.
* @param exif exif interface of the image
*
*/
ImageCoordinates(ExifInterface exif){
//If image has no EXIF data and user has enabled GPS setting, get user's location
//Always return null as a temporary fix for #1599
if (exif != null && exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null) {
//If image has EXIF data, extract image coords
imageCoordsExists = true;
Timber.d("EXIF data has location info");

String latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String latitudeRef = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String longitudeRef = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
decLatitude = "N".equals(latitudeRef) ? convertToDegree(latitude) :
0 - convertToDegree(latitude);
decLongitude = "E".equals(longitudeRef) ? convertToDegree(longitude)
: 0 - convertToDegree(longitude);

decimalCoords = decLatitude + "|" + decLongitude;
}
}

/**
* Construct from a stream.
*/
ImageCoordinates(@NonNull InputStream stream) throws IOException {
this(new ExifInterface(stream));
}

/**
* Construct from the file path of the image.
* @param path file path of the image
*
*/
ImageCoordinates(@NonNull String path) throws IOException{
this(new ExifInterface(path));
}

/**
* Extracts geolocation (either of image from EXIF data, or of user)
* @return coordinates as string (needs to be passed as a String in API query)
*/
@Nullable
String getDecimalCoords() {
return decimalCoords;
}

public double getDecLatitude() {
return decLatitude;
}

public double getDecLongitude() {
return decLongitude;
}

private double convertToDegree(String stringDMS) {
String[] DMS = stringDMS.split(",", 3);
double degrees = divideComponents(DMS[0]);
double minutes = divideComponents(DMS[1]);
double seconds = divideComponents(DMS[2]);
return degrees + (minutes/60) + (seconds/3600);
}

private double divideComponents(String dm) {
String[] stringD = dm.split("/", 2);
return Double.parseDouble(stringD[0]) / Double.parseDouble(stringD[1]);
}
}
Loading