rmmoul
4/23/2018 - 12:23 AM

Lat Long conversion for import

Lat Long conversion for import into WP Google Map Pro plugin

<?php
	
	// quick and dirty csv long lat conversion script
	// converts lat / long in this format:
	// N34 58.840 W85 55.147
	// to decimal format for import into WP Google Map Pro plugin

	$csv = array_map('str_getcsv', file('caves.csv'));

	$new_csv = array();

	foreach($csv as $data){
		
		$long_lat_pair = $data[1];

		// break into long and lat
		$long_lat_pair_array = explode('W', $long_lat_pair);

		$long_lat_pair_array[0] = ltrim($long_lat_pair_array[0], 'N');

		$long_lat_pair_array[0] = trim($long_lat_pair_array[0]);
		$long_lat_pair_array[1] = trim($long_lat_pair_array[1]);

		$lat_array = explode(' ', $long_lat_pair_array[0]);
		$long_array = explode(' ', $long_lat_pair_array[1]);

		$lat = $lat_array[0];
		$lat_dec = $lat_array[1] / 60;
		$lat .= '.'.intval($lat_dec * 100000);

		$long = '-'.$long_array[0];
		$long_dec = $long_array[1] / 60;
		$long .= '.'.intval($long_dec * 100000);

		$new_data = array();
		$new_data['name'] = $data[0];
		$new_data['address'] = ' '; // needed, even if blank, for plugin import
		$new_data['lat'] = $lat;
		$new_data['long'] = $long;

		$description = '';

		if(!empty($data[3])){
			$description .= 'Cave Altitude: '.$data[3].'ft<br>';
		}

		$description .= '<a href="'.$data[2].'" target="blank">View in Google Maps</a>';

		$new_data['description'] = $description;

		$new_csv[] = $new_data;


	}// foreach

	$new_csv_file = fopen("new_caves.csv",'w') or die("Can't open php://output");

	fputcsv($new_csv_file, array('name','lat','long', 'desc'));
	
	foreach($new_csv as $value) {
		fputcsv($new_csv_file, $value);
	}

	fclose($new_csv_file) or die("Can't close php://output");

	echo 'done';