Outreachy Week #2

Boy, has it been a busy week! Implemented GPS coordinate extraction from uploaded photos, which was a much more windy road than it would seem  at first glance. We had to alter the code to temporarily stop uploads while testing (to prevent me from uploading dozens of nonsensical pictures to the actual Commons database), and then there were issues with finding where to insert the code in the app, how to convert the image Uri to a file path, issues with refactoring…

But we did it, woohoo! 🙂 Two brand new classes for the app:

  1. FilePathConverter.java
public class FilePathConverter {

    private Uri uri;
    private Context context;

    public FilePathConverter(Context context, Uri uri) {
        this.context = context;
        this.uri = uri;
    }

    /**
     * Gets file path of image from its Uri
     * May return null
     */
    public String getFilePath(){
        String filePath ="";
        String wholeID = DocumentsContract.getDocumentId(uri);

        String id = wholeID.split(":")[1];
        String[] column = { MediaStore.Images.Media.DATA };

String sel = MediaStore.Images.Media._ID + "=?";
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{id}, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();

        Log.d("Image", "File path: " + filePath);
        return filePath;
    }
}

2. GPSExtractor.java

public class GPSExtractor {

    private String filePath;

    public GPSExtractor(String filePath){
        this.filePath = filePath;
    }

    //Extract GPS coords of image
    public String getCoords() {

        ExifInterface exif;
        String latitude = "";
        String longitude = "";
        String latitude_ref = "";
        String longitude_ref = "";
        String decimalCoords = "";

        try {
            exif = new ExifInterface(filePath);
            latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
            longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

            Log.d("Image", "Latitude: " + latitude + " " + latitude_ref);
            Log.d("Image", "Longitude: " + longitude + " " + longitude_ref);

            decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);

        } catch (IOException e) {
            Log.w("Image", e);
        }
        return decimalCoords;
    }

    //Converts format of coords into decimal coords as required by API for next step
    private String getDecimalCoords(String latitude, String latitude_ref, String longitude, String longitude_ref) {

        double decLatitude, decLongitude;

        if(latitude_ref.equals("N")){
            decLatitude = convertToDegree(latitude);
        }
        else{
            decLatitude = 0 - convertToDegree(latitude);
        }

        if(longitude_ref.equals("E")){
            decLongitude = convertToDegree(longitude);
        }
        else{
            decLongitude = 0 - convertToDegree(longitude);
        }

        return (String.valueOf(decLatitude) + "|" + String.valueOf(decLongitude));
    }

    private double convertToDegree(String stringDMS){
        double result;
        String[] DMS = stringDMS.split(",", 3);

        String[] stringD = DMS[0].split("/", 2);
        double d0 = Double.parseDouble(stringD[0]);
        double d1 = Double.parseDouble(stringD[1]);
        double degrees = d0/d1;

        String[] stringM = DMS[1].split("/", 2);
        double m0 = Double.parseDouble(stringM[0]);
        double m1 = Double.parseDouble(stringM[1]);
        double minutes = m0/m1;

        String[] stringS = DMS[2].split("/", 2);
        double s0 = Double.parseDouble(stringS[0]);
        double s1 = Double.parseDouble(stringS[1]);
        double seconds = s0/s1;

        result = degrees + (minutes/60) + (seconds/3600);
        return result;
    }
}
Advertisements
Outreachy Week #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s