ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: StarGeek on November 13, 2017, 01:06:26 PM

Title: State Abbreviations User Defined tag
Post by: StarGeek on November 13, 2017, 01:06:26 PM
Here's a simple user defined tag that will return the US Postal Code based upon XMP:State or IPTC:Province-State metadata in the file.

Example output:
C:\>exiftool -config USPostalCode.config -USPostalCode -State y:\!temp\Test3.jpg
US Postal Code                  : ME
State                           : Maine


edit:  Updated and renamed
edit 2:  Updated again
Title: Re: US Postal Code User Defined tag
Post by: Phil Harvey on November 13, 2017, 03:55:17 PM
Hi StarGeek,

Interesting, but I was expecting something else when I opened the file.  A postal code in canada is what you call a zip code in the US.

If you want, you could add Canada postal abbreviations to the list:

'alberta' => 'AB',
'british columbia' => 'BC',
'manitoba' => 'MB',
'new brunswick' => 'NB',
'newfoundland' => 'NL',
'nova scotia' => 'NS',
'northwest territories' => 'NT',
'nunavut' => 'NU',
'ontario' => 'ON',
'prince edward island' => 'PE',
'quebec' => 'QC',
'saskatchewan' => 'SK',
'yukon' => 'YT',

- Phil
Title: Re: US Postal Code User Defined tag
Post by: StarGeek on November 13, 2017, 04:11:21 PM
Yeah, looking around now there is some discrepancies with what they're called.  But Abbreviations seems to be the official term for the USPS, so I'll change it.  I did think about adding Canada and Mexico as well, but got lazy.  I found the USPS page with the US possessions such as Puerto Rico and Guam and I'll probably add those as well. 

Hmm... Maybe not Mexico.  I'm not sure I want to deal with accent marks.

Title: Re: State Abbreviations User Defined tag
Post by: StarGeek on December 01, 2017, 09:42:36 PM
Updated to add Canada and Mexico to the list.  Renamed to reflect that it was no longer just US state abbreviations.

But there's also a problem, probably a Windows only one.

I added a subroutine to deal with accented characters (based upon this StackOverflow answer (https://stackoverflow.com/a/18123985/3525475)).  The problem is that it doesn't work one Windows unless the -L option is added.  For example, if the State is Québec, it appears that internally that the MyNormalize routine is getting a value of QuA©bec, same as if it was printed on the Windows command line.

Any way to to do this properly?
Title: Re: State Abbreviations User Defined tag
Post by: Phil Harvey on December 02, 2017, 08:19:12 AM
Ah.  MyNormalize is assuming wide characters, but ExifTool treats all strings as byte streams internally.  You may have to convert the byte stream by calling utf8::decode.  I don't have time to try this right now, but try adding this as the second line of MyNormalize:

    utf8::decode($_);

You may have to add "use utf8;" to your config file.

Also, I would have to think about how to deal with non-utf8 encodings... later when I have more time.

- Phil   
Title: Re: State Abbreviations User Defined tag
Post by: StarGeek on December 02, 2017, 11:03:47 AM
That worked.

As an additional bonus, my main reason for following through on this, is that MyNormalize() can be used with the -api Filter to work on any tag if desired.

C:\Windows\System32>exiftool -config C:\Programs\UnixUtils\ExifTool_Configs\StateAbbrev.config -a -s -G1 -state -state# -Location -Location# -api "filter=MyNormalize($_)" y:\!temp\Test3.jpg
[XMP-photoshop] State                           : Quebec
[XMP-photoshop] State                           : Québec
[XMP-iptcCore]  Location                        : Yucatan
[XMP-iptcCore]  Location                        : Yucatán

Title: Re: State Abbreviations User Defined tag
Post by: Phil Harvey on December 02, 2017, 11:08:15 AM
Cool feature!  Glad that worked.

- Phil
Title: Re: State Abbreviations User Defined tag
Post by: ekkidee on December 05, 2017, 09:55:04 PM
That's pretty slick. Could this be done for timezone names too? (Or does exiftool already produce names when given a GMT offset?)
Title: Re: State Abbreviations User Defined tag
Post by: StarGeek on December 05, 2017, 11:16:27 PM
Depends upon what you mean by timezone names, but I think not without some extra work.  If you mean something that EST, then there's other things to take into account.  For example, given an offset of -5, is that EST or CDT?  So then you have to take into account location and the timestamp.