AchLabo

Expertise in Web, Security & AI Engineering

API Integration Mushi-Labe PHP Web Development

Enhancing Biological Data Accuracy: Integrating Exif Metadata and Elevation APIs

Enhancing Biological Data Accuracy: Integrating Exif Metadata and Elevation APIs | AchLabo

The Intersection of Entomology and Web Technology

In the field of entomology, a specimen without accurate data is scientifically worthless. The “label” must contain the precise date, location, and altitude of capture. For my project, Mushi-Labe, I focused on automating this data entry process to minimize human error and maximize scientific utility.

Leveraging Exif Metadata for Geolocation

Modern smartphones and cameras embed Exchangeable Image File Format (Exif) data into every photo. This includes GPS coordinates. Instead of making users manually type latitude and longitude, we can extract this using JavaScript.

Client-Side Extraction with Exif.js

By processing the image on the client side, we reduce server load and provide instant feedback to the user.

// Example of extracting GPS data from a file input
const imgElement = document.getElementById('upload');
EXIF.getData(imgElement, function() {
    let lat = EXIF.getTag(this, "GPSLatitude");
    let lon = EXIF.getTag(this, "GPSLongitude");
    let latRef = EXIF.getTag(this, "GPSLatitudeRef") || "N";
    let lonRef = EXIF.getTag(this, "GPSLongitudeRef") || "E";

    // Helper function to convert Rational format to Decimal
    const convertToDecimal = (gps, ref) => {
        let d = gps[0].numerator / gps[0].denominator;
        let m = gps[1].numerator / gps[1].denominator;
        let s = gps[2].numerator / gps[2].denominator;
        let decimal = d + (m / 60) + (s / 3600);
        return (ref === "S" || ref === "W") ? decimal * -1 : decimal;
    };

    console.log("Decimal Coordinates:", convertToDecimal(lat, latRef), convertToDecimal(lon, lonRef));
});

The Missing Link: Fetching Accurate Elevation

While GPS provides horizontal coordinates, many cameras fail to record accurate altitude (elevation). For insect specimens, altitude is critical for understanding habitat zones. To solve this, I integrated the Open-Elevation API.

Backend Implementation in PHP

Once the coordinates are sent to the server, the following PHP function (using CodeIgniter’s CURLRequest) fetches the altitude:

public function fetchAltitude(float $lat, float $lon)
{
    $client = \Config\Services::curlrequest();
    try {
        $response = $client->get("https://api.open-elevation.com/api/v1/lookup?locations={$lat},{$lon}", [
            'headers' => ['Accept' => 'application/json']
        ]);
        $data = json_decode($response->getBody(), true);
        return $data['results'][0]['elevation'] ?? 0;
    } catch (\Exception $e) {
        log_message('error', 'Elevation API failed: ' . $e->getMessage());
        return null;
    }
}

Results: Scientific Standardization

By combining these technologies, “Mushi-Labe” ensures that every specimen label generated is backed by verifiable digital evidence. This automation not only saves time but also promotes a higher standard of data integrity within the amateur entomology community.