#!/usr/bin/perl -w
# Plugin: MTMapoint
#
# 1.0 Apr 25, 2004 1st release
# 1.01 Apr 26, 2004 remove unnecessary "use".
# 1.02 Apr 27, 2004 add "MapointThumbnail"
# auto recognize charset
# 1.1 May 2, 2004 resolve "address" only once
# 1.1.2 May 2, 2004 fixed some bugs which was in 1.1
# 1.1.2.1 May 5, 2004 avoid &log
# 1.2 May 5, 2004 Exif value optimize
# 1.3 May 7, 2004 Map resolution changed; sc=0 -> sc=2 1/10,000
# Fix some value of EXIF
# 2.0 Jun 2, 2004 Map selection
# 2.0.1 Jun 3, 2004 Map selection in string: @navi,mapfan,mapion
# 2.0.2 Jun 12, 2004 fixed bug when GPSDatum is "tokyo97"
# 2.1 Jul 17, 2004 added MapointPoint argv 'tokyo' to get tokyo point
# 2.2 Aug 3, 2004 changed where to get address from at-navi to mapion
# 2.21 Aug 25, 2004 added livedoor map for map(same as mapfan)
# 3.0 Aug 31, 2004 added MapointGet to get Exif values
#
# Copyright (C) 2004 Nobuhiro Miyatake; http://japo.net/miya/
package MT::Plugin::Mapoint;
use strict;
use MT::Template::Context;
use MT::ConfigMgr;
use Jcode;
use URI::URL;
require LWP::Protocol::http;
require LWP::UserAgent;
use vars qw($VERSION);
$VERSION = 3.0;
MT::Template::Context->add_container_tag(Mapoint => \&Mapoint);
MT::Template::Context->add_tag(MapointPoint => \&MapointPoint);
MT::Template::Context->add_tag(MapointMap => \&MapointMap);
MT::Template::Context->add_tag(MapointLocation => \&MapointLocation);
MT::Template::Context->add_tag(MapointDate => \&MapointDate);
MT::Template::Context->add_tag(MapointDateTime => \&MapointDateTime);
MT::Template::Context->add_tag(MapointAbstract => \&MapointAbstract);
MT::Template::Context->add_tag(MapointExif => \&MapointExif);
MT::Template::Context->add_tag(MapointThumbnail => \&MapointThumbnail);
MT::Template::Context->add_tag(MapointOrigin => \&MapointOrigin);
MT::Template::Context->add_conditional_tag(MapointIfMap => \&MapointIfMap);
MT::Template::Context->add_conditional_tag(MapointIfDate => \&MapointIfDate);
MT::Template::Context->add_conditional_tag(MapointIfExif => \&MapointIfExif);
MT::Template::Context->add_global_filter(getexif => \&MapointGet);
my $LOGDIR = "/home/miya/logs";
my $DEBUG = (-d $LOGDIR) ? 1 : 0;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$year += 1900;
my $today = sprintf("%04d%02d%02d",$year,$mon,$mday);
my %exifkeys = (
"FNumber"=>1,
"FocalLength"=>2,
"FocalLengthIn35mmFilm"=>2,
"ExposureTime"=>3
);
$|++;
if (MT->can('add_plugin')) {
require MT::Plugin;
my $plugin = new MT::Plugin();
$plugin->name( "Mapoint Plugin" );
$plugin->description( "handles image Exif and GPS information" );
$plugin->doc_link( "http://japo.net/miya/archives/catlist_mailog.html" );
MT->add_plugin( $plugin );
}
# container tag.
sub Mapoint
{
require MT::Entry;
my($ctx, $args) = @_;
my $src;
my $builder = $ctx->stash('builder');
my $tokens = $ctx->stash('tokens');
&log("Mapoint");
defined(my $entry = $ctx->stash('entry')) or return '';
if (defined($entry->keywords) and $src = $entry->keywords) {
if ($DEBUG) {
my $data = $src;
Jcode::convert(\$data, "euc", "utf8");
# &log("src: $data");
}
if (($src =~ /WGS84[\=:]\s*(\d+).(\d+).([\d\.]+)\/(\d+).(\d+).([\d\.]+)\/?(\d*)\s?/) ||
($src =~ /^(\d+).(\d+).([\d\.]+)\/(\d+).(\d+).([\d\.]+)\/?(\d*)$/) ||
($src =~ /lat\=(\d+).(\d+).([\d\.]+).?lon\=(\d+).(\d+).([\d\.]+)\/?(\d*)/)) {
&log("Mapoint: Registered Map") if ($DEBUG);
$ctx->stash('mapoint_Da', 'WGS84');
$ctx->stash('mapoint_N1', $1);
$ctx->stash('mapoint_N2', $2);
$ctx->stash('mapoint_N3', $3);
$ctx->stash('mapoint_E1', $4);
$ctx->stash('mapoint_E2', $5);
$ctx->stash('mapoint_E3', $6);
$ctx->stash('mapoint_L1', $7);
$ctx->stash('mapoint_repeat', 0);
} elsif (($src =~ /TKY97[\=:]\s*(\d+).(\d+).([\d\.]+)\/(\d+).(\d+).([\d\.]+)\/?(\d*)\s?/)) {
&log("Mapoint: Registered Map TKY") if ($DEBUG);
$ctx->stash('mapoint_Da', 'TKY97');
$ctx->stash('mapoint_N1', $1);
$ctx->stash('mapoint_N2', $2);
$ctx->stash('mapoint_N3', $3);
$ctx->stash('mapoint_E1', $4);
$ctx->stash('mapoint_E2', $5);
$ctx->stash('mapoint_E3', $6);
$ctx->stash('mapoint_L1', $7);
$ctx->stash('mapoint_repeat', 0);
} else {
$ctx->stash('mapoint_N1', '');
$ctx->stash('mapoint_repeat', 0);
}
if ($src =~ /loc[\=:]\s*[\{\(]([^\}\)]*)[\}\)]/i) {
$ctx->stash('mapoint_Lc', $1);
&log("Mapoint: Registered Location") if ($DEBUG);
} else {
$ctx->stash('mapoint_Lc', '');
}
if ($src =~ /XY[\=:]\s*(\d)\s*/) {
$ctx->stash('mapoint_Xy', $1);
&log("Mapoint: Registered XY") if ($DEBUG);
} else {
$ctx->stash('mapoint_Xy', '');
}
if ($src =~ /GPSRef[\=:]\s*([^\s]*)\s*/) {
$ctx->stash('mapoint_gpsref', $1);
&log("Mapoint: Registered gpsref") if ($DEBUG);
} else {
$ctx->stash('mapoint_gpsref', '');
}
if ($src =~ /Date[\=:]\s*(\d+\/\d+\/\d+)\s?/i) {
$ctx->stash('mapoint_dt', $1);
&log("Mapoint: Registered Date") if ($DEBUG);
} else {
$ctx->stash('mapoint_dt', '');
}
if ($src =~ /Time[\=:]\s*(\d+\:\d+\:\d+)\s?/i) {
$ctx->stash('mapoint_tm', $1);
&log("Mapoint: Registered Time") if ($DEBUG);
} else {
$ctx->stash('mapoint_tm', '');
}
if ($src =~ /THUMB[\=:]\s*(\
]+\>)\s?/i) {
$ctx->stash('mapoint_th', $1);
&log("Mapoint: Registered Thumb") if ($DEBUG);
} elsif ($src =~ /(\
]+\>)\s?/i) {
$ctx->stash('mapoint_th', $1);
&log("Mapoint: Registered Thumb") if ($DEBUG);
} else {
$ctx->stash('mapoint_th', '');
}
if ($src =~ /origin[\=:]\s*([^\s]*)\s*/) {
$ctx->stash('mapoint_or', $1);
&log("Mapoint: Registered origin") if ($DEBUG);
} else {
$ctx->stash('mapoint_or', '');
}
# from here $src may be destoried
if ($src =~ /EXIF\=\{([^\}]+)\}\s?/i) {
$ctx->stash('mapoint_Ex', $1);
&log("Mapoint: Registered Exif") if ($DEBUG);
} elsif (defined($entry->excerpt) and $src = $entry->excerpt) {
if ($src =~ /^Resolution:/) {
$ctx->stash('mapoint_Ex', $src);
&log("Mapoint: Registered Exif") if ($DEBUG);
} else {
$ctx->stash('mapoint_Ex', '');
}
} else {
$ctx->stash('mapoint_Ex', '');
}
} else {
$ctx->stash('mapoint_N1', '');
$ctx->stash('mapoint_Ex', '');
$ctx->stash('mapoint_Lc', '');
$ctx->stash('mapoint_dt', '');
$ctx->stash('mapoint_tm', '');
$ctx->stash('mapoint_Xy', '');
$ctx->stash('mapoint_gpsref', '');
$ctx->stash('mapoint_Da', '');
}
defined(my $html = $builder->build($ctx, $tokens))
or return $ctx->error($builder->errstr);
$html;
}
# container tag.
sub MapointMaplist
{
require MT::Entry;
my($ctx, $args) = @_;
my $src;
my $builder = $ctx->stash('builder');
my $tokens = $ctx->stash('tokens');
&log("MapointMaplist");
defined(my $entry = $ctx->stash('entry')) or return '';
if (defined($entry->keywords) and $src = $entry->keywords) {
$ctx->stash('mapoint_repeat', 0);
}
defined(my $html = $builder->build($ctx, $tokens))
or return $ctx->error($builder->errstr);
$html;
}
#############################################
sub MapointPoint
{
my ($ctx, $argv) = @_;
my $desc = $argv->{'format'};
defined($desc) or return;
my $type = $argv->{'type'};
$type = lc($type);
defined($type) or return;
defined(my $lat_do = $ctx->stash('mapoint_N1')) or return;
return '' unless $lat_do;
if ($DEBUG) {
my $tdesc = $desc;
Jcode::convert(\$tdesc, "euc", "utf8");
# &log("MapointPoint: ".$tdesc);
}
my $lat_fn = $ctx->stash('mapoint_N2');
my $lat_by = $ctx->stash('mapoint_N3');
my $lon_do = $ctx->stash('mapoint_E1');
my $lon_fn = $ctx->stash('mapoint_E2');
my $lon_by = $ctx->stash('mapoint_E3');
my $alt = $ctx->stash('mapoint_L1');
$alt = 0 unless($alt);
my $lat = $lat_do.".".$lat_fn.".".$lat_by;
my $lon = $lon_do.".".$lon_fn.".".$lon_by;
if (defined $argv && defined $argv->{'tokyo'}) {
# &log("MapointPoint: TOKYO") if ($DEBUG);
my (@tlat, @tlon, $talt) = &wgs2tky($lat_do,$lat_fn,$lat_by,$lon_do,$lon_fn,$lon_by, $alt, 'TKY');
$lon_do = $tlat[3];
$lon_fn = $tlat[4];
$lon_by = $tlat[5];
$lat_do = $tlat[0];
$lat_fn = $tlat[1];
$lat_by = $tlat[2];
}
if ($type eq 'n') {
$desc =~ s/\%1/$lat_do/;
$desc =~ s/\%2/$lat_fn/;
$desc =~ s/\%3/$lat_by/;
} elsif ($type eq 'e') {
$desc =~ s/\%1/$lon_do/;
$desc =~ s/\%2/$lon_fn/;
$desc =~ s/\%3/$lon_by/;
} elsif ($type eq 'a') {
if ($alt) {
unless ($desc =~ s/\((.*)\%1(.*)\)/$1$alt$2/) {
$desc =~ s/\%1/$alt/;
}
} else {
$desc =~ s/\((.*)\%1(.*)\)//;
}
} else {
return;
}
$desc;
}
#############################################
sub MapointDate
{
my ($ctx, $argv) = @_;
defined(my $dt = $ctx->stash('mapoint_dt')) or return;
return '' unless $dt;
&log("date: ".$dt) if ($DEBUG);
$dt;
}
#############################################
sub MapointDateTime
{
my ($ctx, $argv) = @_;
defined(my $dt = $ctx->stash('mapoint_dt')) or return;
return '' unless $dt;
defined(my $tm = $ctx->stash('mapoint_tm')) or return;
return '' unless $tm;
&log("datetime: ".$dt." ".$tm) if ($DEBUG);
$dt." ".$tm;
}
#############################################
sub MapointAbstract
{
my ($ctx, $argv) = @_;
defined(my $dt = $ctx->stash('mapoint_dt')) or return;
return '' unless $dt;
defined(my $tm = $ctx->stash('mapoint_tm')) or return;
return '' unless $tm;
&log("abstract: ".$dt." ".$tm) if ($DEBUG);
my $exif = '';
my $desc = "";
if (defined $argv && defined $argv->{'showall'} && $argv->{'showall'} eq "1") {
my $entry = $ctx->stash('entry');
my $keywords = $entry->keywords;
while ($keywords =~ s/exif=\{([^\}]*)\}//is) {
$exif = $1;
my $odt = $dt." ".$tm;
if ($exif =~ /datetimeoriginal:\s*(.*)\n/i) {
$odt = $1;
}
$desc .= "
DATE: $odt ".&_abs_exif($exif)."
";
}
} else {
$exif = &MapointExif;
# $desc .= "DATE: ".$dt." ".$tm." ".&_abs_exif($exif)."
";
$desc .= &_abs_exif($exif);
}
return $desc;
}
sub _abs_exif
{
my ($exif) = @_;
my $desc = '';
$exif = &_exif_norm($exif);
if ($exif =~ /FNumber:\s*([^\n]+)/si) {
$desc .= "$1 ";
}
if ($exif =~ /FocalLength:\s*([^\n]+)/si) {
$desc .= "$1";
if ($exif =~ /FocalLengthIn35mmFilm:\s*([^\n]+)/si) {
$desc .= "($1)";
}
$desc .= " ";
}
if ($exif =~ /ExposureTime:\s*([^\n]+)/si) {
$desc .= "$1 ";
}
if ($exif =~ /Model:\s*([^\n]+)/si) {
$desc = "$1; ".$desc;
}
if ($exif =~ /Make:\s*([^\n]+)/si) {
$desc = "CAMERA: $1 ".$desc;
}
return $desc;
}
#############################################
sub MapointIfMap
{
my $ctx = shift;
defined(my $lat_do = $ctx->stash('mapoint_N1')) or return;
$lat_do ne '';
}
#############################################
sub MapointIfDate
{
my $ctx = shift;
defined(my $dt = $ctx->stash('mapoint_dt')) or return;
$dt ne '';
}
#############################################
sub MapointIfExif
{
my $ctx = shift;
defined(my $exif = $ctx->stash('mapoint_Ex')) or return;
$exif ne '';
}
#############################################
sub MapointExif
{
my $ctx = shift;
my $argv = shift;
defined(my $exif = $ctx->stash('mapoint_Ex')) or return;
return '' unless $exif;
$exif = &_exif_norm($exif);
if (defined $argv && defined $argv->{'convert_breaks'}) {
if ($argv->{'convert_breaks'} > 0) {
$exif =~ s/\r?\n/
/gs;
}
}
$exif;
}
sub _exif_norm
{
my ($exif) = @_;
my $ct;
while (my ($key, $val) = each %exifkeys) {
if ($exif =~ /$key:\s*([^\n]+)/si) {
$ct = $1;
&log("key=$key ct=$ct") if ($DEBUG);
if ($val == 3 && $ct =~ /(\d+)\/(\d+)/) {
if ($1 > 0) {
$ct = eval($2 / $1);
$ct = sprintf("%d", $ct);
$ct = "1/$ct";
}
} elsif ($ct =~ /f?\/?([\d\.\/]+)/) {
&log("norm($val): $1") if ($DEBUG);
$ct = eval($1);
$ct = 0 unless defined $ct;
$ct = sprintf("%0.1f", $ct);
}
if ($val == 1) {$ct = "f/$ct";}
elsif ($val == 2) {$ct = $ct."mm";}
elsif ($val == 3) {$ct = $ct."s";}
$exif =~ s/$key:([^\n]+)/$key: $ct/si;
}
}
return $exif;
}
#############################################
sub MapointThumbnail
{
my $ctx = shift;
defined(my $thumb = $ctx->stash('mapoint_th')) or return;
return '' unless $thumb;
$thumb;
}
#############################################
sub MapointOrigin
{
my $ctx = shift;
defined(my $origin = $ctx->stash('mapoint_or')) or return;
return '' unless $origin;
$origin;
}
#############################################
my $maporigin = "http://www.at-navi.com";
my $mapfanorigin = "http://www.mapfan.com/index.cgi?ZM=11&";
my $livedoororigin = "http://www.mapion.co.jp/c/f?uc=1&grp=all&icon=home,,,,,&scl=25000&";
my $mapionorigin = "http://www.mapion.co.jp/c/f?scl=25000&uc=1&grp=Air&size=600,550&";
sub MapointMap
{
my $ctx = shift;
my $argv = shift;
my $internalcall = shift;
defined(my $lat_do = $ctx->stash('mapoint_N1')) or return;
return '' unless $lat_do;
my $lat_fn = $ctx->stash('mapoint_N2');
my $lat_by = $ctx->stash('mapoint_N3');
my $lon_do = $ctx->stash('mapoint_E1');
my $lon_fn = $ctx->stash('mapoint_E2');
my $lon_by = $ctx->stash('mapoint_E3');
my $datum = $ctx->stash('mapoint_Da');
my $alt = $ctx->stash('mapoint_L1');
my $lat = $lat_do.".".$lat_fn.".".$lat_by;
my $lon = $lon_do.".".$lon_fn.".".$lon_by;
#
my $map = '', my $mapstyle = 0;
if (defined $argv && defined $argv->{'map'}) {
$mapstyle = $argv->{'map'};
} elsif ($ctx->stash('mapoint_Xy') eq '') {
$mapstyle = 0;
} else {
$mapstyle = $ctx->stash('mapoint_Xy');
}
$mapstyle = 2 if (defined $internalcall && defined $internalcall->{'internal'});
my $ns = $ctx->stash('mapoint_gpsref');
$ns = "N/E" unless $ns;
$alt = 0 unless $alt;
if (lc($mapstyle) eq '@navi') {$mapstyle = 0;}
if (lc($mapstyle) eq 'mapfan') {$mapstyle = 1;}
if (lc($mapstyle) eq 'mapion') {$mapstyle = 2;}
if (lc($mapstyle) eq 'livedoor') {$mapstyle = 3;}
if (lc($mapstyle) eq 'alpslab') {$mapstyle = 4;}
if (lc($mapstyle) eq 'google') {$mapstyle = 5;}
if ($mapstyle == 1 || $mapstyle == 2 || $mapstyle == 3 || $mapstyle == 4 || $mapstyle == 5) { # mapfan/mapion/livedoor/alpslab
my (@tlat, @tlon, $talt) = &wgs2tky($lat_do,$lat_fn,$lat_by,$lon_do,$lon_fn,$lon_by, $alt, 'TKY');
if ($mapstyle == 1) {
$map = "$mapfanorigin"."MAP=E".$tlat[3].".".$tlat[4].".".$tlat[5]
."N".$tlat[0].".".$tlat[1].".".$tlat[2];
} elsif ($mapstyle == 3) {
$map = "$livedoororigin"."el=".$tlat[3]."/".$tlat[4]."/".$tlat[5]
."&nl=".$tlat[0]."/".$tlat[1]."/".$tlat[2]."&coco=".$tlat[0]."/".$tlat[1]."/".$tlat[2]
.",".$tlat[3]."/".$tlat[4]."/".$tlat[5];
} elsif ($mapstyle == 4) {
$map = $tlat[0]."/".$tlat[1]."/".$tlat[2]
.",".$tlat[3]."/".$tlat[4]."/".$tlat[5];
} elsif ($mapstyle == 5) {
$map = "[GoogleMap:".$tlat[0].".".$tlat[1].".".$tlat[2]
.",".$tlat[3].".".$tlat[4].".".$tlat[5]."]";
} else {
$map = "$mapionorigin"."el=".$tlat[3].".".$tlat[4].".".$tlat[5]
."&nl=".$tlat[0].".".$tlat[1].".".$tlat[2];
}
} else { # at-navi
$map = "$maporigin/map/checkMap.jsp?datum=0&unit=0&sc=2&fm=1&"."lat=$lat&lon=$lon";
}
&log("MapointMap: $map internal=".defined($internalcall->{internal})) if ($DEBUG);
$map;
}
#############################################
sub MapointLocation
{
my $ctx = shift;
my $loc = $ctx->stash('mapoint_Lc');
unless ($loc ne '') {
# get PublishCharset
my $cfg = MT::ConfigMgr->instance;
my $cgipath = $cfg->CGIPath;
$cfg->read_config($cgipath."mt.cfg");
my $charset = lc($cfg->get('PublishCharset'));
$charset = ($charset =~ /euc/i) ? "euc" : "utf8";
&log("CGIPATH: $cgipath, CHARSET: $charset") if ($DEBUG);
# get address
my $map = &MapointMap($ctx, undef, {'internal'=>1});
&log("Location URL: $map") if ($DEBUG);
$loc = &wgetlocation_mapion($map);
&log("Got Location: $loc") if ($DEBUG);
Jcode::convert(\$loc, "utf8", "euc") if ($charset eq 'utf8');
# store location into "keywords"
if (defined(my $entry = $ctx->stash('entry'))) {
&pushIntoKeywords($ctx, $loc);
$ctx->stash('mapoint_Lc', $loc);
&log("Location Saved") if ($DEBUG);
}
}
# &log("Location: $loc") if ($DEBUG);
$loc;
}
#############################################
my %exif = (
"01"=>"DateTimeOriginal",
"02"=>"DateTime",
"10"=>"Make",
"11"=>"Model",
"12"=>"Software",
"13"=>"JPEGProc",
"13"=>"JPEG_Type",
"20"=>"ExposureTime",
"21"=>"FNumber",
"22"=>"FocalLength",
"23"=>"FocalLengthIn35mmFilm",
"24"=>"BrightnessValue",
"25"=>"ShutterSpeedValue",
"26"=>"SubjectDistance",
"27"=>"SubjectDistanceRange",
"28"=>"WhiteBalance",
"29"=>"SceneType",
"30"=>"Sharpness",
"31"=>"SceneCaptureType",
"33"=>"ExposureProgram",
"34"=>"ExposureMode",
"35"=>"ExposureBiasValue",
"36"=>"ISOSpeedRatings",
"37"=>"Flash",
"38"=>"LightSource",
"39"=>"Contrast",
"40"=>"Saturation",
"41"=>"MaxApertureValue",
"42"=>"SensingMethod",
"80"=>"MeteringMode",
"81"=>"OECF",
"82"=>"color_type",
"99"=>"ExifVersion"
);
# "32"=>"ComponentsConfiguration",
sub MapointGet {
my ($text, $argv, $ctx) = @_;
use Image::Info qw(image_info dim);
my $blog = $ctx->stash('blog');
my $cfg = MT::ConfigMgr->instance;
my $site_path = $blog->site_path;
$site_path .= '/' unless ($site_path =~ /\/$/);
my $site_url = $blog->site_url;
my $entry = $ctx->stash('entry');
my $lacktext = $text;
my $mtkeyword = $entry->keywords;
my $rebuild_flag = 0;
my $dirty = 0;
if (defined $argv) {
$rebuild_flag = 1 if ($argv eq "rebuild");
}
if ($rebuild_flag) { # rebuild all?
$mtkeyword = &_clear_keyword_old($mtkeyword);
}
# check old format and then clear
if (defined $mtkeyword && $mtkeyword =~ /date=/is) {
$mtkeyword = &_clear_keyword_old($mtkeyword);
}
# build exif info
while ($lacktext =~ s/
]*src\s*=\s*"([^\"]+)"[^>]*>//is) {
my $imgfile = $1;
$imgfile =~ s/^$site_url//;
my $imgpath = $site_path.$imgfile;
&log("MapointGet(1): $imgfile") if ($DEBUG);
next unless (-e $imgpath);
# local file
my $info = image_info($imgpath);
next unless (ref $info);
# EXIF exists
next if ($mtkeyword =~ /File\: $imgfile/i);
# not same
my $exifinfo = "EXIF={ File: $imgfile\n";
$exifinfo .= "Resolution: ".dim($info)."\n";
foreach my $index (sort keys %exif) {
my $key = $exif{$index};
my $eachinfo = $info->{$key};
if ($eachinfo) {
if ($key eq 'DateTimeOriginal') {
($eachinfo =~ s/^(\d+)\:(\d+)\:(\d+) ([\d\: ]+)$/$1\/$2\/$3 $4/);
$exifinfo .= "Date: $1/$2/$3\nTime: $4\n";
} elsif ($key eq 'DateTime') {
$eachinfo =~ s/^(\d+)\:(\d+)\:(\d+) ([\d\: ]+)$/$1\/$2\/$3 $4/;
}
$exifinfo .= $key.": ".$eachinfo."\n";
}
}
# GPS Position Information
my $latref = $info->{GPSLatitudeRef};
my $lonref = $info->{GPSLongitudeRef};
my @lats = $info->{GPSLatitude};
my @lons = $info->{GPSLongitude};
my @alts = $info->{GPSAltitude};
my $datum = $info->{GPSMapDatum};
if ((scalar $lats[0])&&(scalar $lons[0])&&($lats[0][1]*$lats[0][3]*$lats[0][5]*$lons[0][1]*$lons[0][3]*$lons[0][5])) {
# GPS info exists
my $lat = $lats[0][0]/$lats[0][1].'.'.$lats[0][2]/$lats[0][3].'.'.$lats[0][4]/$lats[0][5];
my $lon = $lons[0][0]/$lons[0][1].'.'.$lons[0][2]/$lons[0][3].'.'.$lons[0][4]/$lons[0][5];
my $lat_do = $lats[0][0]/$lats[0][1];
my $lat_fn = $lats[0][2]/$lats[0][3];
my $lat_by = $lats[0][4]/$lats[0][5];
my $lon_do = $lons[0][0]/$lons[0][1];
my $lon_fn = $lons[0][2]/$lons[0][3];
my $lon_by = $lons[0][4]/$lons[0][5];
my $alt = $alts[0][0];
if ($datum eq 'TOKYO') {
my (@tlat, @tlon, $talt) = &wgs2tky($lat_do,$lat_fn,$lat_by,$lon_do,$lon_fn,$lon_by, $alt, 'WGS');
$lon_do = $tlat[3];
$lon_fn = $tlat[4];
$lon_by = $tlat[5];
$lat_do = $tlat[0];
$lat_fn = $tlat[1];
$lat_by = $tlat[2];
$alt = $talt;
}
$exifinfo .= "WGS84: ".$lat_do.".".$lat_fn.".".$lat_by."/".$lon_do.".".$lon_fn.".".$lon_by;
$exifinfo .= "/".$alt if ($alt);
$exifinfo .= "\n";
$exifinfo .= "GPSRef: ".$latref."/".$lonref."\n";
}
$exifinfo .= "\}\n";
$mtkeyword .= $exifinfo;
$dirty = 1;
}
if ($dirty) {
$entry->keywords("$mtkeyword");
$entry->save;
}
# &log("MapointGet(2): ".$entry->keywords) if ($DEBUG);
return $text;
}
sub _clear_keyword_old
{
my ($mtkeyword) = @_;
$mtkeyword =~ s/exif=\{[^\}]*\}//gis;
$mtkeyword =~ s/date=.* //gis;
$mtkeyword =~ s/time=.* //gis;
$mtkeyword =~ s/wgs84=.* //gis;
$mtkeyword =~ s/gpsref=.* //gis;
$mtkeyword =~ s/xy=.* //gis;
$mtkeyword =~ s/loc=\{[^\}]*\}//gis;
chomp($mtkeyword);
return $mtkeyword;
}
#------------------------------
# push keyword into entry
#------------------------------
sub pushIntoKeywords
{
my ($ctx, $loc) = @_;
my $entry = $ctx->stash('entry') or return;
my $keywords = $entry->keywords;
chomp($keywords);
$keywords =~ s/(exif=\s*\{[^\}]*)\}/$1LOC: \($loc\)\n\}/is;
$entry->keywords($keywords);
$entry->save;
}
#------------------------------
# web get
#------------------------------
sub wgetlocation_atnavi
{
my $url = shift;
# get first page
my $ua = new LWP::UserAgent; # create a useragent to test
my $uri = new URI::URL("$url");
my $form = "";
&log("1st url=$url ") if ($DEBUG);
my $request = new HTTP::Request('GET', $url, undef, undef);
$request->header('Content-Type', 'application/x-www-form-urlencoded');
my $response = $ua->request($request, undef, undef);
my $ht = $response->as_string;
# &log("1st src=$ht") if ($DEBUG);
# get frame
return 'Map' unless ($ht =~ /name="rstr"\s+action="(.+)"\s+method/i);
$url = $maporigin.$1;
return 'Map' unless ($ht =~ /name="lat"\s+value="(.*)"/i);
my $lat = $1;
return 'Map' unless ($ht =~ /name="lon"\s+value="(.*)"/i);
my $lon = $1;
$ua = new LWP::UserAgent; # create a useragent to test
$uri = new URI::URL("$url");
$form = "lat=$lat&lon=$lon&unit=0&datum=0&sc=0";
&log("2nd url=$url form=$form") if ($DEBUG);
$request = new HTTP::Request('GET', $url, undef, $form);
$request->header('Content-Type', 'application/x-www-form-urlencoded');
$response = $ua->request($request, undef, undef);
$ht = $response->as_string;
# &log("2nd src=$ht") if ($DEBUG);
# get frame
return 'Map' unless ($ht =~ /frame\s+src="(.+)"\s+name/i);
$url = $maporigin.$1;
$ua = new LWP::UserAgent; # create a useragent to test
$uri = new URI::URL("$url");
&log("3rd url=$url") if ($DEBUG);
$request = new HTTP::Request('GET', $url, undef, undef);
$request->header('Content-Type', 'application/x-www-form-urlencoded');
$response = $ua->request($request, undef, undef);
$ht = $response->as_string;
# &log("3rd src=$ht") if ($DEBUG);
# get location
if ($ht =~ /\\ (.*):/) {
return $1;
}
return 'pMap';
}
sub wgetlocation_mapion
{
my $url = shift;
# get first page
my $ua = new LWP::UserAgent; # create a useragent to test
my $uri = new URI::URL("$url");
my $form = "";
&log("get address url=$url ") if ($DEBUG);
my $request = new HTTP::Request('GET', $url, undef, undef);
$request->header('Content-Type', 'application/x-www-form-urlencoded');
my $response = $ua->request($request, undef, undef);
my $ht = $response->as_string;
# &log("1st src=$ht") if ($DEBUG);
# get location
# if ($ht =~ /\.*「(.*)」/) {
if ($ht =~ /\(.*)付/) {
return $1."付近";
}
return '住所取得できませんでした';
}
#------------------------------
# logging
#------------------------------
sub log
{
return unless ($DEBUG);
# for debug
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$year += 1900;
my $dt = sprintf("%04d%02d%02d%02d%02d%02d",$year,$mon,$mday,$hour,$min,$sec);
my $alog = "$LOGDIR/mp-$today.log";
my $um = umask(0);
open(LOGFILE, ">> $alog");
# chomp;
print LOGFILE $dt . " ($$) @_\n";
close(LOGFILE);
umask($um);
}
# eof
# The copyright of the code below is claimed by Nowral, the author written in the code
# http://member.nifty.ne.jp/Nowral/02_DATUM/02_DATUM.html for more info
#
# modified by Nob Seki on July 21, 2003
sub wgs2tky {
my (@lat,@long,$alt);
$lat[0] = shift; $lat[1] = shift, $lat[2] = shift;
$long[0] = shift; $long[1] = shift, $long[2] = shift;
$alt = shift;
my $datum = shift;
#
#!/usr/bin/perl -w
#
# Standard Molodensky Datum Transformation
# Nowral PXI07463@nifty.ne.jp
# 99/11/24
#
# Original Data in Tokyo97
my $b = $lat[0] + $lat[1]/60 + $lat[2]/3600;
my $l = $long[0] + $long[1]/60 + $long[2]/3600;
my $h = $alt;
#--- easy method by Nowral
my $nb;
my $nl;
if ($datum eq 'WGS') { # TOKYO -> WGS-84
$nb = $b - 0.00010695 * $b + 0.000017464 * $l + 0.0046017;
$nl = $l - 0.000046038 * $b - 0.000083043 * $l + 0.010040;
} else { # WGS-84 -> TOKYO
$nb = $b + 0.00010696 * $b - 0.000017467 * $l - 0.0046020;
$nl = $l + 0.000046047 * $b + 0.000083049 * $l - 0.010041;
}
return (°2dms($nb),°2dms($nl),$h);
#---
# Datum of Tokyo97
my $a = 6377397.155;
my $f = 1 / 299.152813;
# Datum of WGS 84
my $a_ = 6378137; # 赤道半径
my $f_ = 1 / 298.257223; # 扁平率
($b, $l, $h) = &molodensky($b, $l, $h, $a_, $f_, $a, $f);
return (°2dms($b),°2dms($l),$h);
}
sub molodensky {
# Constants
my $pi = 4 * atan2(1,1); # Pi
my $rd = $pi / 180; # Radian
# Change in meter
# e.g. $x_ = $x + $dx etc.
my $dx = +148;
my $dy = -507;
my $dz = -681;
my($b, $l, $h, $a, $f, $a_, $f_, $datum) = @_;
my($bda, $e2, $da, $df, $db, $dl, $dh);
my($sb, $cb, $sl, $cl, $rn, $rm);
$b *= $rd;
$l *= $rd;
$e2 = 2*$f - $f*$f; # 離心率 e^2
$bda = 1- $f; # 極半径 / 赤道半径 b/a
($da, $df) = ($a_-$a, $f_-$f);
($sb, $cb, $sl, $cl) = (sin($b), cos($b), sin($l), cos($l));
$rn = 1 / sqrt(1 - $e2*$sb*$sb);
$rm = $a * (1 - $e2) * $rn * $rn * $rn;
$rn *= $a;
#ずれの計算
$db = -$dx*$sb*$cl - $dy*$sb*$sl + $dz*$cb
+ $da*$rn*$e2*$sb*$cb/$a + $df*($rm/$bda+$rn*$bda)*$sb*$cb;
$db /= $rm + $h;
$dl = -$dx*$sl + $dy*$cl;
$dl /= ($rn+$h) * $cb;
$dh = $dx*$cb*$cl + $dy*$cb*$sl + $dz*$sb
- $da*$a/$rn + $df*$bda*$rn*$sb*$sb;
(($b+$db)/$rd, ($l+$dl)/$rd, $h+$dh);
}
sub deg2dms {
my($d) = @_;
my($m, $s, $sf);
$sf = int($d*360000 + 0.5);
$s = $sf / 100 % 60;
$m = $sf / 6000 % 60;
$d = int($sf/360000);
$sf %= 100;
# modified by Nob, returning an array, not formatted string
return ($d, $m, sprintf("%02d\.%02d", $s, $sf));
}
1;