#!C:\Perl\bin\perl.exe 

use Image::Magick;
use Image::ExifTool;
use Data::Dumper;
use Geo::Coordinates::DecimalDegrees;

$|=1; #!!!!!!!!!!!!!!
sub out_file(){
	open D , $pathdoc;
	binmode D;
	while (read(D, $buf, 1024) ){
		print $buf;
		sleep 1;
	}
}



#$dirname = "./test";
$dirname = "./";
#mkdir("bak");
#exec("move *.jpg bak");

$HEADER =<<HEADER;
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
	<name>phooto_kml</name>
	<Style id="style15">
		<LineStyle>
			<color>73ff0000</color>
			<width>5</width>
		</LineStyle>
	</Style>
	<Placemark>
		<name>photo kml		</name>
		<styleUrl>#style15</styleUrl>
		<ExtendedData>
			<Data name="_SnapToRoads">
				<value>true</value>
			</Data>
		</ExtendedData>
		<LineString>
			<tessellate>1</tessellate>
			<coordinates>
HEADER

$FOOTER=<<FOOTER;
			</coordinates>
		</LineString>
	</Placemark>
</Document>
</kml>
FOOTER


far_dir($dirname);


sub far_dir($dirname){
	my ($dirname) = @_;
	$i = 1;

	open(KML,">photo.kml");
	print KML $HEADER;

	opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
	while (defined($file = readdir(DIR))) {
#		$full_file  = "$dirname\\$file";
		$full_file  = "$file";
		$full_file =~s/\\\\/\\/g;	
	
		my $gps="";
		my $uri_gps="";
		if ($file=~/\.jpg/i ){
		
			view_gps( $full_file, \$gps, \$uri_gps);
			print $gps ."\n";
			if($gps){
				$gps_string = convert_gps($gps);
#				print"GPS String $gps_string " ."\n";
	        		print KML $gps_string  , ",0 \n";
			}
			$i++;		
		}

        }

	print KML $FOOTER;
	close(KML);

}


sub view_gps($in_file, \$gps, \$uri_gps){
	my ($in_file, $gps, $uri_gps) = @_;
	$lgps= $$gps = "";


	my $exifTool = new Image::ExifTool;
	$exifTool->Options('IgnoreMinorErrors'=>1 , 'Verbose'=>0);
	 $success = $exifTool->ExtractInfo($in_file);
	 $info = $exifTool->GetInfo();
	print Dumper ($info);

	$lat = $info->{ 'GPSLatitude' };
	$long = $info->{ 'GPSLongitude' };
	$NS = substr($lat,length($lat)-1,1);
	$EW = substr($long,length($long)-1,1);


	if ( !($lat  && $long && $EW && $NS )) {return};



	#6 deg 17' 52.0798" 46 deg 26' 44.3039" East North

	# 149 deg 49' 4.59" E 37 deg 20' 7.21" S East North 

#	$EW = substr($EW,0,1);
#	$NS = substr($NS,0,1);
	$lat =~s/[NS|deg]//g;
	$long =~s/[EW|deg]//g;
	$lat =~s/\s+/ /g;
	$long =~s/\s+/ /g;
	$lat=$NS.$lat;
	$long=$EW.$long;

#	print " $long|$lat\n";

	$lgps = $lat . " " . $long;
	$$gps = $lgps;	

        $lgps =~s/ /\+/g;
	$lgps =~s/"/\%22/g;	
	$lgps =~s/'/\%27/g;
	
	$$uri_gps = $lgps;
}




sub convert_gps{
	my ($gps_string) = @_;
	#$lgps= $$gps = "";

	if ($gps_string=~m/(.*[S|N])\s*[E|W](.*)/ ){
		$lat = $1;	
		$long = $2;
	}

#	print "$lat $long\n";
	#if ( !($lat  && $long && $EW && $NS )) {return};


	#$lat =~s/[NS|deg]//g;
	#$long =~s/[EW|deg]//g;
	$lat =~s/\s+/ /g;
	$long =~s/\s+/ /g;

	$lat =~s/°/ /g;
	$long =~s/°/ /g;

	$lat=$NS.$lat;
	$long=$EW.$long;

#	print " $long|$lat\n";

    	my $latd, my $longd;
    	($latd, $longd) = convertGPS2DEC($lat, $long);

	my $gps_decimal;	
	$gps_decimal = "$longd,$latd";
	return $gps_decimal;
	
}





sub convertGPS2DEC(){
        my($lat, $long) = @_;

	my $lats = 1, my $longs = 1;
	my $degrees = 0, my $minutes = 0, my $seconds = 0;

	if ( $lat =~/N/g ){
		$lats = 1;
	}elsif ( $lat =~/S/g ){
		$lats = -1;
	}

	if ( $long =~/E/g ){
		$longs = 1;
	}elsif ( $long =~/W/g ){
		$longs = -1;
	}

	$lat =~s/N//g;$lat =~s/S//g;$long =~s/E//g;$long =~s/W//g;$lat =~s/\'//g;$long =~s/\'//g;$lat =~s/\"//g;$long =~s/\"//g;

	my @arr1 = split /\s+/, $lat;
	$degrees = $arr1[0];$minutes = $arr1[1];$seconds = $arr1[2];

#	print "\n$lat ---$degrees---$minutes-----$seconds--\n";

	$lat = $lats * dms2decimal($degrees, $minutes, $seconds);
	my @arr2 = split /\s+/, $long;
	$degrees = $arr2[0];$minutes = $arr2[1];$seconds = $arr2[2];

#	print "\nlat2---$degrees---$minutes-----$seconds--\n";

	$long = $longs * dms2decimal($degrees, $minutes, $seconds);


return ($lat,$long);
}
