Files
ocean-cleanup/src/utils/index.ts
Paul Bailey e18997ed95 feat: implement location selection interface with EXIF, GPS, and map picker
- Created LocationSelector component with three location input methods:
  * Auto-populate from photo EXIF data
  * Device GPS location capture with accuracy indicator
  * Interactive Leaflet map picker with draggable marker
- Added Ireland coordinate validation (lat: 51.4-55.4, lng: -10.5--5.5)
- Created coordinate utility functions for validation and formatting
- Added LocationSelectorDemo page for testing at /demo/location-selector
- Displays selected location with coordinates, source, and accuracy
- Provides clear error messages for validation failures
- Satisfies requirements 1.1, 1.2, 1.3, 8.2
2025-11-16 16:15:42 +00:00

62 lines
1.6 KiB
TypeScript

// Utility functions for the Beach Litter Mapping Platform
// Re-export coordinate utilities
export * from './coordinates';
/**
* Formats a date to ISO 8601 format
*/
export function formatDateISO(date: Date): string {
return date.toISOString();
}
/**
* Calculates distance between two coordinates in meters using Haversine formula
*/
export function calculateDistance(
lat1: number,
lng1: number,
lat2: number,
lng2: number
): number {
const R = 6371e3; // Earth's radius in meters
const φ1 = (lat1 * Math.PI) / 180;
const φ2 = (lat2 * Math.PI) / 180;
const Δφ = ((lat2 - lat1) * Math.PI) / 180;
const Δλ = ((lng2 - lng1) * Math.PI) / 180;
const a =
Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
/**
* Validates file type for photo uploads
*/
export function isValidImageFile(file: File): boolean {
const validTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
return validTypes.includes(file.type);
}
/**
* Validates file size (max 5MB)
*/
export function isValidFileSize(file: File, maxSizeMB: number = 5): boolean {
const maxSizeBytes = maxSizeMB * 1024 * 1024;
return file.size <= maxSizeBytes;
}
/**
* Formats bytes to human-readable size
*/
export function formatFileSize(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}