-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3408 Refactoring the FileProcessor and GPSExtractor classes - conver…
…t ImageCoordinates to kotlin
- Loading branch information
Showing
2 changed files
with
72 additions
and
92 deletions.
There are no files selected for viewing
92 changes: 0 additions & 92 deletions
92
app/src/main/java/fr/free/nrw/commons/upload/ImageCoordinates.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
app/src/main/java/fr/free/nrw/commons/upload/ImageCoordinates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package fr.free.nrw.commons.upload | ||
|
||
import androidx.exifinterface.media.ExifInterface | ||
import timber.log.Timber | ||
import java.io.InputStream | ||
|
||
/** | ||
* 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. | ||
*/ | ||
class ImageCoordinates internal constructor(exif: ExifInterface?) { | ||
var decLatitude = 0.0 | ||
var decLongitude = 0.0 | ||
var imageCoordsExists = false | ||
/** | ||
* @return string of `"[decLatitude]|[decLongitude]"` or null if coordinates do not exist | ||
*/ | ||
var decimalCoords: String? = null | ||
|
||
/** | ||
* Construct from a stream. | ||
*/ | ||
internal constructor(stream: InputStream) : this(ExifInterface(stream)) | ||
|
||
/** | ||
* Construct from the file path of the image. | ||
* @param path file path of the image | ||
*/ | ||
internal constructor(path: String) : this(ExifInterface(path)) | ||
|
||
|
||
|
||
init { | ||
//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) { | ||
val latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) | ||
val latitudeRef = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF) | ||
val longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) | ||
val longitudeRef = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF) | ||
if (latitude != null && longitude != null && latitudeRef != null && longitudeRef != null) { | ||
//If image has EXIF data, extract image coords | ||
imageCoordsExists = true | ||
Timber.d("EXIF data has location info") | ||
decLatitude = | ||
if (ExifInterface.LATITUDE_NORTH == latitudeRef) convertToDegree(latitude) | ||
else 0 - convertToDegree(latitude) | ||
decLongitude = | ||
if (ExifInterface.LONGITUDE_EAST == longitudeRef) convertToDegree(longitude) | ||
else 0 - convertToDegree(longitude) | ||
decimalCoords = "$decLatitude|$decLongitude" | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Convert a string to an accurate Degree | ||
* | ||
* @param degreeMinuteSecondString - template string "a/b,c/d,e/f" where the letters represent numbers | ||
* @return the degree accurate to the second | ||
*/ | ||
private fun convertToDegree(degreeMinuteSecondString: String) = | ||
degreeMinuteSecondString.split(",").let { | ||
val degrees = evaluateExpression(it[0]) | ||
val minutes = evaluateExpression(it[1]) | ||
val seconds = evaluateExpression(it[2]) | ||
degrees + minutes / 60 + seconds / 3600 | ||
} | ||
|
||
private fun evaluateExpression(dm: String) = | ||
dm.split("/").let { it[0].toDouble() / it[1].toDouble() } | ||
} |