managarten/memoro/apps/mobile/features/location/utils/locationHelpers.ts
Till-JS e7f5f942f3 chore: initial commit - consolidate 4 projects into monorepo
Projects included:
- maerchenzauber (NestJS backend + Expo mobile + SvelteKit web + Astro landing)
- manacore (Expo mobile + SvelteKit web + Astro landing)
- manadeck (NestJS backend + Expo mobile + SvelteKit web)
- memoro (Expo mobile + SvelteKit web + Astro landing)

This commit preserves the current state before monorepo restructuring.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-22 23:38:24 +01:00

73 lines
2 KiB
TypeScript

/**
* Helper functions for working with location data
*/
/**
* Parse PostGIS POINT string to coordinates
* @param pointString - PostGIS POINT format: "POINT(longitude latitude)"
* @returns Object with latitude and longitude or null if invalid
*/
export function parsePostGISPoint(
pointString: string | null | undefined
): { latitude: number; longitude: number } | null {
if (!pointString) return null;
// Match POINT(longitude latitude) format
const match = pointString.match(/POINT\(([-\d.]+)\s+([-\d.]+)\)/);
if (!match) return null;
const longitude = parseFloat(match[1]);
const latitude = parseFloat(match[2]);
// Validate coordinates
if (isNaN(longitude) || isNaN(latitude)) return null;
if (latitude < -90 || latitude > 90) return null;
if (longitude < -180 || longitude > 180) return null;
return { latitude, longitude };
}
/**
* Convert coordinates to PostGIS POINT format
* @param latitude
* @param longitude
* @returns PostGIS POINT string
*/
export function toPostGISPoint(latitude: number, longitude: number): string {
return `POINT(${longitude} ${latitude})`;
}
/**
* Get location data from memo using the PostGIS location field and address from metadata
* @param memo - The memo object
* @returns Location data with latitude, longitude, and address or null
*/
export function getMemoLocation(
memo: any
): { latitude?: number; longitude?: number; address?: any } | null {
// Check if we have address data in metadata
const hasAddress = memo?.metadata?.address;
// Parse location coordinates if available
const parsed = memo?.location ? parsePostGISPoint(memo.location) : null;
// If we have neither coordinates nor address, return null
if (!parsed && !hasAddress) {
return null;
}
// Build result with available data
const result: { latitude?: number; longitude?: number; address?: any } = {};
if (parsed) {
result.latitude = parsed.latitude;
result.longitude = parsed.longitude;
}
if (hasAddress) {
result.address = memo.metadata.address;
}
return result;
}