From b406b36cda216a1927e14f9d89bfeb9ea5a78a4c Mon Sep 17 00:00:00 2001 From: Elman Date: Mon, 15 Jan 2018 17:31:19 +0100 Subject: [PATCH 1/5] Added all Slovak and Czech accents --- contrib/kml_export.lua | 101 +++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/contrib/kml_export.lua b/contrib/kml_export.lua index ab1f8c0..1ab8f78 100644 --- a/contrib/kml_export.lua +++ b/contrib/kml_export.lua @@ -82,57 +82,108 @@ end -- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/ function string.stripAccents( str ) local tableAccents = {} + -- A tableAccents["à"] = "a" - tableAccents["á"] = "a" - tableAccents["â"] = "a" - tableAccents["ã"] = "a" - tableAccents["ä"] = "a" - tableAccents["ç"] = "c" - tableAccents["è"] = "e" - tableAccents["é"] = "e" - tableAccents["ê"] = "e" - tableAccents["ë"] = "e" - tableAccents["ì"] = "i" - tableAccents["í"] = "i" - tableAccents["î"] = "i" - tableAccents["ï"] = "i" - tableAccents["ñ"] = "n" - tableAccents["ò"] = "o" - tableAccents["ó"] = "o" - tableAccents["ô"] = "o" - tableAccents["õ"] = "o" - tableAccents["ö"] = "o" - tableAccents["ù"] = "u" - tableAccents["ú"] = "u" - tableAccents["û"] = "u" - tableAccents["ü"] = "u" - tableAccents["ý"] = "y" - tableAccents["ÿ"] = "y" tableAccents["À"] = "A" + tableAccents["á"] = "a" tableAccents["Á"] = "A" + tableAccents["â"] = "a" tableAccents["Â"] = "A" + tableAccents["ã"] = "a" tableAccents["Ã"] = "A" + tableAccents["ä"] = "a" tableAccents["Ä"] = "A" + -- B + -- C + tableAccents["ç"] = "c" tableAccents["Ç"] = "C" + tableAccents["č"] = "c" + tableAccents["Č"] = "C" + -- D + tableAccents["ď"] = "d" + tableAccents["Ď"] = "d" + -- E + tableAccents["è"] = "e" tableAccents["È"] = "E" + tableAccents["é"] = "e" tableAccents["É"] = "E" + tableAccents["ê"] = "e" tableAccents["Ê"] = "E" + tableAccents["ë"] = "e" tableAccents["Ë"] = "E" + tableAccents["ě"] = "e" + tableAccents["Ě"] = "E" + -- F + -- G + -- H + -- I + tableAccents["ì"] = "i" tableAccents["Ì"] = "I" + tableAccents["í"] = "i" tableAccents["Í"] = "I" + tableAccents["î"] = "i" tableAccents["Î"] = "I" + tableAccents["ï"] = "i" tableAccents["Ï"] = "I" + -- J + -- K + -- L + tableAccents["ĺ"] = "l" + tableAccents["Ĺ"] = "L" + tableAccents["ľ"] = "l" + tableAccents["Ľ"] = "L" + -- M + -- N + tableAccents["ñ"] = "n" tableAccents["Ñ"] = "N" + tableAccents["ň"] = "n" + tableAccents["Ň"] = "N" + -- O + tableAccents["ò"] = "o" tableAccents["Ò"] = "O" + tableAccents["ó"] = "o" tableAccents["Ó"] = "O" + tableAccents["ô"] = "o" tableAccents["Ô"] = "O" + tableAccents["õ"] = "o" tableAccents["Õ"] = "O" + tableAccents["ö"] = "o" tableAccents["Ö"] = "O" + -- P + -- Q + -- R + tableAccents["ŕ"] = "r" + tableAccents["Ŕ"] = "R" + tableAccents["ř"] = "r" + tableAccents["Ř"] = "R" + -- S + tableAccents["š"] = "s" + tableAccents["Š"] = "S" + -- T + tableAccents["ť"] = "t" + tableAccents["Ť"] = "T" + -- U + tableAccents["ù"] = "u" tableAccents["Ù"] = "U" + tableAccents["ú"] = "u" tableAccents["Ú"] = "U" + tableAccents["û"] = "u" tableAccents["Û"] = "U" + tableAccents["ü"] = "u" tableAccents["Ü"] = "U" + tableAccents["ů"] = "u" + tableAccents["Ů"] = "U" + -- V + -- W + -- X + -- Y + tableAccents["ý"] = "y" tableAccents["Ý"] = "Y" + tableAccents["ÿ"] = "y" + tableAccents["Ÿ"] = "Y" + -- Z + tableAccents["ž"] = "z" + tableAccents["Ž"] = "Z" local normalizedString = "" From d2977183582f6738eb01edd9a8bb7f57601c228b Mon Sep 17 00:00:00 2001 From: Elman Date: Mon, 15 Jan 2018 17:40:50 +0100 Subject: [PATCH 2/5] Path is now sorted by exif_datetime_taken --- contrib/kml_export.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/kml_export.lua b/contrib/kml_export.lua index 1ab8f78..f2e5666 100644 --- a/contrib/kml_export.lua +++ b/contrib/kml_export.lua @@ -357,7 +357,7 @@ local function create_kml_file(storage, image_table, extra_data) kml_file = kml_file.." \n" kml_file = kml_file.." \n" - for image,exported_image in spairs(image_table, function(t,a,b) return t[b] < t[a] end) do + for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do if ((image.longitude and image.latitude) and (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data ) then From e9d0f706edc96cd31d493f8edfda4015fc58441c Mon Sep 17 00:00:00 2001 From: Elman Date: Mon, 15 Jan 2018 17:41:26 +0100 Subject: [PATCH 3/5] Correct handling of duplicates --- contrib/kml_export.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/contrib/kml_export.lua b/contrib/kml_export.lua index f2e5666..b240445 100644 --- a/contrib/kml_export.lua +++ b/contrib/kml_export.lua @@ -213,6 +213,20 @@ function string.escapeXmlCharacters( str ) return str end +-- Add duplicate index to filename +-- image.filename does not have index, exported_image has index +function addDuplicateIndex( index, filename ) + if index > 0 then + filename = filename.."_" + if index < 10 then + filename = filename.."0" + end + filename = filename..index + end + + return filename +end + local function create_kml_file(storage, image_table, extra_data) if not df.check_if_bin_exists("mkdir") then dt.print_error(_("mkdir not found")) @@ -297,6 +311,8 @@ local function create_kml_file(storage, image_table, extra_data) for image,exported_image in pairs(image_table) do -- Extract filename, e.g DSC9784.ARW -> DSC9784 filename = string.upper(string.gsub(image.filename,"%.%w*", "")) + -- Handle duplicates + filename = addDuplicateIndex( image.duplicate_index, filename ) -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG extension = string.match(exported_image,"%.%w*$") @@ -309,7 +325,7 @@ local function create_kml_file(storage, image_table, extra_data) if (image.title and image.title ~= "") then image_title = string.escapeXmlCharacters(image.title) else - image_title = image.filename + image_title = filename..extension end -- Characters should not be escaped in CDATA, but we are using HTML fragment, so we must escape them image_description = string.escapeXmlCharacters(image.description) @@ -396,7 +412,8 @@ local function create_kml_file(storage, image_table, extra_data) (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data ) then local filename = string.upper(string.gsub(image.filename,"%.%w*", "")) - + -- Handle duplicates + filename = addDuplicateIndex( image.duplicate_index, filename ) createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images end From 0c1de20a1ac5c284da9f025fc88434cec80fa203 Mon Sep 17 00:00:00 2001 From: Elman Date: Mon, 15 Jan 2018 17:56:02 +0100 Subject: [PATCH 4/5] Code formatting --- contrib/kml_export.lua | 182 +++++++++++++++++++++-------------------- 1 file changed, 92 insertions(+), 90 deletions(-) diff --git a/contrib/kml_export.lua b/contrib/kml_export.lua index b240445..58e0a15 100644 --- a/contrib/kml_export.lua +++ b/contrib/kml_export.lua @@ -1,7 +1,7 @@ --[[ This file is part of darktable, - Copyright 2016 by Tobias Jakobs. - Copyright 2016 by Erik Augustin. + Copyright 2018 by Tobias Jakobs. + Copyright 2018 by Erik Augustin. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -81,7 +81,7 @@ end -- Strip accents from a string -- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/ function string.stripAccents( str ) - local tableAccents = {} + local tableAccents = {} -- A tableAccents["à"] = "a" tableAccents["À"] = "A" @@ -185,18 +185,17 @@ function string.stripAccents( str ) tableAccents["ž"] = "z" tableAccents["Ž"] = "Z" - local normalizedString = "" + local normalizedString = "" - for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do - if tableAccents[strChar] ~= nil then - normalizedString = normalizedString..tableAccents[strChar] - else - normalizedString = normalizedString..strChar + for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do + if tableAccents[strChar] ~= nil then + normalizedString = normalizedString..tableAccents[strChar] + else + normalizedString = normalizedString..strChar + end end - end - - return normalizedString + return normalizedString end -- Escape XML characters @@ -245,7 +244,7 @@ local function create_kml_file(storage, image_table, extra_data) return end - dt.print_error("Will try to export KML file now") + dt.print_log("Will try to export KML file now") local imageFoldername if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then @@ -265,7 +264,6 @@ local function create_kml_file(storage, image_table, extra_data) dt.control.execute(mkdirCommand) end - -- Create the thumbnails for image,exported_image in pairs(image_table) do if ((image.longitude and image.latitude) and @@ -275,11 +273,11 @@ local function create_kml_file(storage, image_table, extra_data) filename = string.upper(string.gsub(filename,"%.%w*", "")) -- convert -size 92x92 filename.jpg -resize 92x92 +profile "*" thumbnail.jpg - -- In this example, '-size 120x120' gives a hint to the JPEG decoder that the image is going to be downscaled to - -- 120x120, allowing it to run faster by avoiding returning full-resolution images to GraphicsMagick for the - -- subsequent resizing operation. The '-resize 120x120' specifies the desired dimensions of the output image. It - -- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other - -- profiles that might be present in the input and aren't needed in the thumbnail. + -- In this example, '-size 120x120' gives a hint to the JPEG decoder that the image is going to be downscaled to + -- 120x120, allowing it to run faster by avoiding returning full-resolution images to GraphicsMagick for the + -- subsequent resizing operation. The '-resize 120x120' specifies the desired dimensions of the output image. It + -- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other + -- profiles that might be present in the input and aren't needed in the thumbnail. local convertToThumbCommand = "convert -size 96x96 "..exported_image.." -resize 92x92 -mattecolor \"#FFFFFF\" -frame 2x2 +profile \"*\" "..exportDirectory.."/"..imageFoldername.."thumb_"..filename..".jpg" dt.control.execute(convertToThumbCommand) @@ -309,14 +307,14 @@ local function create_kml_file(storage, image_table, extra_data) kml_file = kml_file.." Exported from darktable\n" for image,exported_image in pairs(image_table) do - -- Extract filename, e.g DSC9784.ARW -> DSC9784 - filename = string.upper(string.gsub(image.filename,"%.%w*", "")) - -- Handle duplicates - filename = addDuplicateIndex( image.duplicate_index, filename ) - -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG - extension = string.match(exported_image,"%.%w*$") + -- Extract filename, e.g DSC9784.ARW -> DSC9784 + filename = string.upper(string.gsub(image.filename,"%.%w*", "")) + -- Handle duplicates + filename = addDuplicateIndex( image.duplicate_index, filename ) + -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG + extension = string.match(exported_image,"%.%w*$") - if ((image.longitude and image.latitude) and + if ((image.longitude and image.latitude) and (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data ) then kml_file = kml_file.." \n" @@ -357,37 +355,39 @@ local function create_kml_file(storage, image_table, extra_data) end end --- Connects all images with an path + -- Connects all images with an path if ( dt.preferences.read("kml_export","CreatePath","bool") == true ) then - kml_file = kml_file.." \n" - kml_file = kml_file.." Path\n" -- ToDo: I think a better name would be nice - --kml_file = kml_file.." \n" + kml_file = kml_file.." \n" + kml_file = kml_file.." Path\n" -- ToDo: I think a better name would be nice + --kml_file = kml_file.." \n" - kml_file = kml_file.." \n" + kml_file = kml_file.." \n" - kml_file = kml_file.." \n" - kml_file = kml_file.." \n" + kml_file = kml_file.." \n" + kml_file = kml_file.." \n" - for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - local altitude = 0; - if (image.elevation) then - altitude = image.elevation; - end - kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",altitude\n" - end - end - kml_file = kml_file.." \n" - kml_file = kml_file.." \n" + for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + local altitude = 0; + if (image.elevation) then + altitude = image.elevation; + end - kml_file = kml_file.." \n" + kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",altitude\n" + end + end + + kml_file = kml_file.." \n" + kml_file = kml_file.." \n" + + kml_file = kml_file.." \n" end kml_file = kml_file.."\n" @@ -399,77 +399,79 @@ local function create_kml_file(storage, image_table, extra_data) dt.print("KML file created in "..exportDirectory) --- Compress the files to create a KMZ file + -- Compress the files to create a KMZ file if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then - exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") + exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") local createKMZCommand = "zip --test --move --junk-paths " createKMZCommand = createKMZCommand .."\""..exportDirectory.."/"..exportKMZFilename.."\" " -- KMZ filename createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..exportKMLFilename.."\" " -- KML file for image,exported_image in pairs(image_table) do - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - local filename = string.upper(string.gsub(image.filename,"%.%w*", "")) - -- Handle duplicates - filename = addDuplicateIndex( image.duplicate_index, filename ) - createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails - createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images - end + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + local filename = string.upper(string.gsub(image.filename,"%.%w*", "")) + -- Handle duplicates + filename = addDuplicateIndex( image.duplicate_index, filename ) + + createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails + createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images + end end - dt.control.execute(createKMZCommand) + dt.control.execute(createKMZCommand) end --- Open the file with the standard programm + -- Open the file with the standard programm if ( dt.preferences.read("kml_export","OpenKmlFile","bool") == true ) then - local kmlFileOpenCommand + local kmlFileOpenCommand if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMZFilename.."\"" else kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMLFilename.."\"" - end + end dt.control.execute(kmlFileOpenCommand) end - end -- Preferences dt.preferences.register("kml_export", - "OpenKmlFile", - "bool", - _("KML export: Open KML/KMZ file after export"), - _("Opens the KML file after the export with the standard program for KML files"), - false ) + "OpenKmlFile", + "bool", + _("KML export: Open KML/KMZ file after export"), + _("Opens the KML file after the export with the standard program for KML files"), + false ) local handle = io.popen("xdg-user-dir DESKTOP") local result = handle:read() if (result == nil) then - result = "" + result = "" end handle:close() -dt.preferences.register("kml_export", - "ExportDirectory", - "directory", - _("KML export: Export directory"), - _("A directory that will be used to export the KML/KMZ files"), - result ) dt.preferences.register("kml_export", - "CreatePath", - "bool", - _("KML export: Connect images with path"), - _("connect all images with a path"), - false ) + "ExportDirectory", + "directory", + _("KML export: Export directory"), + _("A directory that will be used to export the KML/KMZ files"), + result ) + dt.preferences.register("kml_export", - "CreateKMZ", - "bool", - _("KML export: Create KMZ file"), - _("Compress all imeges to one KMZ file"), - true ) + "CreatePath", + "bool", + _("KML export: Connect images with path"), + _("connect all images with a path"), + false ) + +dt.preferences.register("kml_export", + "CreateKMZ", + "bool", + _("KML export: Create KMZ file"), + _("Compress all imeges to one KMZ file"), + true ) -- Register dt.register_storage("kml_export", _("KML/KMZ Export"), nil, create_kml_file) From f86b16735b3d4aa3d1a283dc0028f795832af994 Mon Sep 17 00:00:00 2001 From: Elman Date: Mon, 15 Jan 2018 22:54:22 +0100 Subject: [PATCH 5/5] Formatted code to follow project settings --- contrib/kml_export.lua | 719 +++++++++++++++++++++-------------------- 1 file changed, 361 insertions(+), 358 deletions(-) diff --git a/contrib/kml_export.lua b/contrib/kml_export.lua index 58e0a15..070a02f 100644 --- a/contrib/kml_export.lua +++ b/contrib/kml_export.lua @@ -1,20 +1,20 @@ --[[ - This file is part of darktable, - Copyright 2018 by Tobias Jakobs. - Copyright 2018 by Erik Augustin. + This file is part of darktable, + Copyright 2018 by Tobias Jakobs. + Copyright 2018 by Erik Augustin. - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . + You should have received a copy of the GNU General Public License + along with this program. If not, see . ]] --[[ darktable KML export script @@ -47,155 +47,155 @@ dt.configuration.check_version(...,{3,0,0},{4,0,0},{5,0,0}) gettext.bindtextdomain("kml_export",dt.configuration.config_dir.."/lua/locale/") local function _(msgid) - return gettext.dgettext("kml_export", msgid) + return gettext.dgettext("kml_export", msgid) end -- Sort a table local function spairs(_table, order) -- Code copied from http://stackoverflow.com/questions/15706270/sort-a-table-in-lua - -- collect the keys - local keys = {} - for _key in pairs(_table) do keys[#keys + 1] = _key end + -- collect the keys + local keys = {} + for _key in pairs(_table) do keys[#keys + 1] = _key end - -- if order function given, sort by it by passing the table and keys a, b, - -- otherwise just sort the keys - if order then - table.sort(keys, function(a,b) return order(_table, a, b) end) - else - table.sort(keys) - end + -- if order function given, sort by it by passing the table and keys a, b, + -- otherwise just sort the keys + if order then + table.sort(keys, function(a,b) return order(_table, a, b) end) + else + table.sort(keys) + end - -- return the iterator function - local i = 0 - return function() - i = i + 1 - if keys[i] then - return keys[i], _table[keys[i]] - end + -- return the iterator function + local i = 0 + return function() + i = i + 1 + if keys[i] then + return keys[i], _table[keys[i]] end + end end local function show_status(storage, image, format, filename, number, total, high_quality, extra_data) - dt.print(string.format(_("Export Image %i/%i"), number, total)) + dt.print(string.format(_("Export Image %i/%i"), number, total)) end -- Strip accents from a string -- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/ function string.stripAccents( str ) - local tableAccents = {} - -- A - tableAccents["à"] = "a" - tableAccents["À"] = "A" - tableAccents["á"] = "a" - tableAccents["Á"] = "A" - tableAccents["â"] = "a" - tableAccents["Â"] = "A" - tableAccents["ã"] = "a" - tableAccents["Ã"] = "A" - tableAccents["ä"] = "a" - tableAccents["Ä"] = "A" - -- B - -- C - tableAccents["ç"] = "c" - tableAccents["Ç"] = "C" - tableAccents["č"] = "c" - tableAccents["Č"] = "C" - -- D - tableAccents["ď"] = "d" - tableAccents["Ď"] = "d" - -- E - tableAccents["è"] = "e" - tableAccents["È"] = "E" - tableAccents["é"] = "e" - tableAccents["É"] = "E" - tableAccents["ê"] = "e" - tableAccents["Ê"] = "E" - tableAccents["ë"] = "e" - tableAccents["Ë"] = "E" - tableAccents["ě"] = "e" - tableAccents["Ě"] = "E" - -- F - -- G - -- H - -- I - tableAccents["ì"] = "i" - tableAccents["Ì"] = "I" - tableAccents["í"] = "i" - tableAccents["Í"] = "I" - tableAccents["î"] = "i" - tableAccents["Î"] = "I" - tableAccents["ï"] = "i" - tableAccents["Ï"] = "I" - -- J - -- K - -- L - tableAccents["ĺ"] = "l" - tableAccents["Ĺ"] = "L" - tableAccents["ľ"] = "l" - tableAccents["Ľ"] = "L" - -- M - -- N - tableAccents["ñ"] = "n" - tableAccents["Ñ"] = "N" - tableAccents["ň"] = "n" - tableAccents["Ň"] = "N" - -- O - tableAccents["ò"] = "o" - tableAccents["Ò"] = "O" - tableAccents["ó"] = "o" - tableAccents["Ó"] = "O" - tableAccents["ô"] = "o" - tableAccents["Ô"] = "O" - tableAccents["õ"] = "o" - tableAccents["Õ"] = "O" - tableAccents["ö"] = "o" - tableAccents["Ö"] = "O" - -- P - -- Q - -- R - tableAccents["ŕ"] = "r" - tableAccents["Ŕ"] = "R" - tableAccents["ř"] = "r" - tableAccents["Ř"] = "R" - -- S - tableAccents["š"] = "s" - tableAccents["Š"] = "S" - -- T - tableAccents["ť"] = "t" - tableAccents["Ť"] = "T" - -- U - tableAccents["ù"] = "u" - tableAccents["Ù"] = "U" - tableAccents["ú"] = "u" - tableAccents["Ú"] = "U" - tableAccents["û"] = "u" - tableAccents["Û"] = "U" - tableAccents["ü"] = "u" - tableAccents["Ü"] = "U" - tableAccents["ů"] = "u" - tableAccents["Ů"] = "U" - -- V - -- W - -- X - -- Y - tableAccents["ý"] = "y" - tableAccents["Ý"] = "Y" - tableAccents["ÿ"] = "y" - tableAccents["Ÿ"] = "Y" - -- Z - tableAccents["ž"] = "z" - tableAccents["Ž"] = "Z" + local tableAccents = {} + -- A + tableAccents["à"] = "a" + tableAccents["À"] = "A" + tableAccents["á"] = "a" + tableAccents["Á"] = "A" + tableAccents["â"] = "a" + tableAccents["Â"] = "A" + tableAccents["ã"] = "a" + tableAccents["Ã"] = "A" + tableAccents["ä"] = "a" + tableAccents["Ä"] = "A" + -- B + -- C + tableAccents["ç"] = "c" + tableAccents["Ç"] = "C" + tableAccents["č"] = "c" + tableAccents["Č"] = "C" + -- D + tableAccents["ď"] = "d" + tableAccents["Ď"] = "d" + -- E + tableAccents["è"] = "e" + tableAccents["È"] = "E" + tableAccents["é"] = "e" + tableAccents["É"] = "E" + tableAccents["ê"] = "e" + tableAccents["Ê"] = "E" + tableAccents["ë"] = "e" + tableAccents["Ë"] = "E" + tableAccents["ě"] = "e" + tableAccents["Ě"] = "E" + -- F + -- G + -- H + -- I + tableAccents["ì"] = "i" + tableAccents["Ì"] = "I" + tableAccents["í"] = "i" + tableAccents["Í"] = "I" + tableAccents["î"] = "i" + tableAccents["Î"] = "I" + tableAccents["ï"] = "i" + tableAccents["Ï"] = "I" + -- J + -- K + -- L + tableAccents["ĺ"] = "l" + tableAccents["Ĺ"] = "L" + tableAccents["ľ"] = "l" + tableAccents["Ľ"] = "L" + -- M + -- N + tableAccents["ñ"] = "n" + tableAccents["Ñ"] = "N" + tableAccents["ň"] = "n" + tableAccents["Ň"] = "N" + -- O + tableAccents["ò"] = "o" + tableAccents["Ò"] = "O" + tableAccents["ó"] = "o" + tableAccents["Ó"] = "O" + tableAccents["ô"] = "o" + tableAccents["Ô"] = "O" + tableAccents["õ"] = "o" + tableAccents["Õ"] = "O" + tableAccents["ö"] = "o" + tableAccents["Ö"] = "O" + -- P + -- Q + -- R + tableAccents["ŕ"] = "r" + tableAccents["Ŕ"] = "R" + tableAccents["ř"] = "r" + tableAccents["Ř"] = "R" + -- S + tableAccents["š"] = "s" + tableAccents["Š"] = "S" + -- T + tableAccents["ť"] = "t" + tableAccents["Ť"] = "T" + -- U + tableAccents["ù"] = "u" + tableAccents["Ù"] = "U" + tableAccents["ú"] = "u" + tableAccents["Ú"] = "U" + tableAccents["û"] = "u" + tableAccents["Û"] = "U" + tableAccents["ü"] = "u" + tableAccents["Ü"] = "U" + tableAccents["ů"] = "u" + tableAccents["Ů"] = "U" + -- V + -- W + -- X + -- Y + tableAccents["ý"] = "y" + tableAccents["Ý"] = "Y" + tableAccents["ÿ"] = "y" + tableAccents["Ÿ"] = "Y" + -- Z + tableAccents["ž"] = "z" + tableAccents["Ž"] = "Z" - local normalizedString = "" + local normalizedString = "" - for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do - if tableAccents[strChar] ~= nil then - normalizedString = normalizedString..tableAccents[strChar] - else - normalizedString = normalizedString..strChar - end + for strChar in string.gmatch(str, "([%z\1-\127\194-\244][\128-\191]*)") do + if tableAccents[strChar] ~= nil then + normalizedString = normalizedString..tableAccents[strChar] + else + normalizedString = normalizedString..strChar end + end - return normalizedString + return normalizedString end -- Escape XML characters @@ -203,275 +203,278 @@ end -- https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents function string.escapeXmlCharacters( str ) - str = string.gsub(str,"&", "&") - str = string.gsub(str,"\"", """) - str = string.gsub(str,"'", "'") - str = string.gsub(str,"<", "<") - str = string.gsub(str,">", ">") + str = string.gsub(str,"&", "&") + str = string.gsub(str,"\"", """) + str = string.gsub(str,"'", "'") + str = string.gsub(str,"<", "<") + str = string.gsub(str,">", ">") - return str + return str end -- Add duplicate index to filename -- image.filename does not have index, exported_image has index function addDuplicateIndex( index, filename ) - if index > 0 then - filename = filename.."_" - if index < 10 then - filename = filename.."0" - end - filename = filename..index + if index > 0 then + filename = filename.."_" + if index < 10 then + filename = filename.."0" end + filename = filename..index + end - return filename + return filename end local function create_kml_file(storage, image_table, extra_data) - if not df.check_if_bin_exists("mkdir") then - dt.print_error(_("mkdir not found")) - return - end - if not df.check_if_bin_exists("convert") then - dt.print_error(_("convert not found")) - return - end - if not df.check_if_bin_exists("xdg-open") then - dt.print_error(_("xdg-open not found")) - return - end - if not df.check_if_bin_exists("xdg-user-dir") then - dt.print_error(_("xdg-user-dir not found")) - return + if not df.check_if_bin_exists("mkdir") then + dt.print_error(_("mkdir not found")) + return + end + if not df.check_if_bin_exists("convert") then + dt.print_error(_("convert not found")) + return + end + if not df.check_if_bin_exists("xdg-open") then + dt.print_error(_("xdg-open not found")) + return + end + if not df.check_if_bin_exists("xdg-user-dir") then + dt.print_error(_("xdg-user-dir not found")) + return + end + + dt.print_error("Will try to export KML file now") + + local imageFoldername + if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then + if not df.check_if_bin_exists("zip") then + dt.print_error(_("zip not found")) + return end - dt.print_log("Will try to export KML file now") + exportDirectory = dt.configuration.tmp_dir + imageFoldername = "" + else + exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") - local imageFoldername - if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then - if not df.check_if_bin_exists("zip") then - dt.print_error(_("zip not found")) - return - end + -- Creates dir if not exsists + imageFoldername = "files/" + local mkdirCommand = "mkdir -p "..exportDirectory.."/"..imageFoldername + dt.control.execute(mkdirCommand) + end - exportDirectory = dt.configuration.tmp_dir - imageFoldername = "" + -- Create the thumbnails + for image,exported_image in pairs(image_table) do + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + local path, filename, filetype = string.match(exported_image, "(.-)([^\\/]-%.?([^%.\\/]*))$") + filename = string.upper(string.gsub(filename,"%.%w*", "")) + + -- convert -size 92x92 filename.jpg -resize 92x92 +profile "*" thumbnail.jpg + -- In this example, '-size 120x120' gives a hint to the JPEG decoder that the image is going to be downscaled to + -- 120x120, allowing it to run faster by avoiding returning full-resolution images to GraphicsMagick for the + -- subsequent resizing operation. The '-resize 120x120' specifies the desired dimensions of the output image. It + -- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other + -- profiles that might be present in the input and aren't needed in the thumbnail. + + local convertToThumbCommand = "convert -size 96x96 "..exported_image.." -resize 92x92 -mattecolor \"#FFFFFF\" -frame 2x2 +profile \"*\" "..exportDirectory.."/"..imageFoldername.."thumb_"..filename..".jpg" + dt.control.execute(convertToThumbCommand) else - exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") - - -- Creates dir if not exsists - imageFoldername = "files/" - local mkdirCommand = "mkdir -p "..exportDirectory.."/"..imageFoldername - dt.control.execute(mkdirCommand) + -- Remove exported image if it has no GPS data + os.remove(exported_image) end - -- Create the thumbnails - for image,exported_image in pairs(image_table) do - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - local path, filename, filetype = string.match(exported_image, "(.-)([^\\/]-%.?([^%.\\/]*))$") - filename = string.upper(string.gsub(filename,"%.%w*", "")) + local pattern = "[/]?([^/]+)$" + filmName = string.match(image.film.path, pattern) - -- convert -size 92x92 filename.jpg -resize 92x92 +profile "*" thumbnail.jpg - -- In this example, '-size 120x120' gives a hint to the JPEG decoder that the image is going to be downscaled to - -- 120x120, allowing it to run faster by avoiding returning full-resolution images to GraphicsMagick for the - -- subsequent resizing operation. The '-resize 120x120' specifies the desired dimensions of the output image. It - -- will be scaled so its largest dimension is 120 pixels. The '+profile "*"' removes any ICM, EXIF, IPTC, or other - -- profiles that might be present in the input and aren't needed in the thumbnail. + -- Strip accents from the filename, because GoogleEarth can't open them + -- https://github.com/darktable-org/lua-scripts/issues/54 + filmName = string.stripAccents(filmName) + end - local convertToThumbCommand = "convert -size 96x96 "..exported_image.." -resize 92x92 -mattecolor \"#FFFFFF\" -frame 2x2 +profile \"*\" "..exportDirectory.."/"..imageFoldername.."thumb_"..filename..".jpg" - dt.control.execute(convertToThumbCommand) - else - -- Remove exported image if it has no GPS data - os.remove(exported_image) + exportKMLFilename = filmName..".kml" + exportKMZFilename = filmName..".kmz" + + -- Create the KML file + local kml_file = "\n" + kml_file = kml_file.."\n" + kml_file = kml_file.."\n" + + --image_table = dt.gui.selection(); + kml_file = kml_file..""..filmName.."\n" + kml_file = kml_file.." Exported from darktable\n" + + for image,exported_image in pairs(image_table) do + -- Extract filename, e.g DSC9784.ARW -> DSC9784 + filename = string.upper(string.gsub(image.filename,"%.%w*", "")) + -- Handle duplicates + filename = addDuplicateIndex( image.duplicate_index, filename ) + -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG + extension = string.match(exported_image,"%.%w*$") + + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + kml_file = kml_file.." \n" + + local image_title, image_description + if (image.title and image.title ~= "") then + image_title = string.escapeXmlCharacters(image.title) + else + image_title = filename..extension + end + -- Characters should not be escaped in CDATA, but we are using HTML fragment, so we must escape them + image_description = string.escapeXmlCharacters(image.description) + + kml_file = kml_file.." "..image_title.."\n" + kml_file = kml_file.." "..image_description.."\n" + kml_file = kml_file.." \n" + + kml_file = kml_file.." \n" + kml_file = kml_file.." 1\n" + kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",0\n" + kml_file = kml_file.." \n" + kml_file = kml_file.." "..string.gsub(image.exif_datetime_taken," ", "T").."Z".."\n" + kml_file = kml_file.." \n" + kml_file = kml_file.." \n" + + kml_file = kml_file.." \n" + end + end + + -- Connects all images with an path + if ( dt.preferences.read("kml_export","CreatePath","bool") == true ) then + kml_file = kml_file.." \n" + kml_file = kml_file.." Path\n" -- ToDo: I think a better name would be nice + --kml_file = kml_file.." \n" + + kml_file = kml_file.." \n" + + kml_file = kml_file.." \n" + kml_file = kml_file.." \n" + + for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + local altitude = 0; + if (image.elevation) then + altitude = image.elevation; end - local pattern = "[/]?([^/]+)$" - filmName = string.match(image.film.path, pattern) - - -- Strip accents from the filename, because GoogleEarth can't open them - -- https://github.com/darktable-org/lua-scripts/issues/54 - filmName = string.stripAccents(filmName) + kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",altitude\n" + end end - exportKMLFilename = filmName..".kml" - exportKMZFilename = filmName..".kmz" + kml_file = kml_file.." \n" + kml_file = kml_file.." \n" - -- Create the KML file - local kml_file = "\n" - kml_file = kml_file.."\n" - kml_file = kml_file.."\n" + kml_file = kml_file.." \n" + end - --image_table = dt.gui.selection(); - kml_file = kml_file..""..filmName.."\n" - kml_file = kml_file.." Exported from darktable\n" + kml_file = kml_file.."\n" + kml_file = kml_file.."" + + local file = io.open(exportDirectory.."/"..exportKMLFilename, "w") + file:write(kml_file) + file:close() + + dt.print("KML file created in "..exportDirectory) + + -- Compress the files to create a KMZ file + if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then + exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") + + local createKMZCommand = "zip --test --move --junk-paths " + createKMZCommand = createKMZCommand .."\""..exportDirectory.."/"..exportKMZFilename.."\" " -- KMZ filename + createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..exportKMLFilename.."\" " -- KML file for image,exported_image in pairs(image_table) do - -- Extract filename, e.g DSC9784.ARW -> DSC9784 - filename = string.upper(string.gsub(image.filename,"%.%w*", "")) + if ((image.longitude and image.latitude) and + (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data + ) then + local filename = string.upper(string.gsub(image.filename,"%.%w*", "")) -- Handle duplicates filename = addDuplicateIndex( image.duplicate_index, filename ) - -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG - extension = string.match(exported_image,"%.%w*$") - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - kml_file = kml_file.." \n" - - local image_title, image_description - if (image.title and image.title ~= "") then - image_title = string.escapeXmlCharacters(image.title) - else - image_title = filename..extension - end - -- Characters should not be escaped in CDATA, but we are using HTML fragment, so we must escape them - image_description = string.escapeXmlCharacters(image.description) - - kml_file = kml_file.." "..image_title.."\n" - kml_file = kml_file.." "..image_description.."\n" - kml_file = kml_file.." \n" - - kml_file = kml_file.." \n" - kml_file = kml_file.." 1\n" - kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",0\n" - kml_file = kml_file.." \n" - kml_file = kml_file.." "..string.gsub(image.exif_datetime_taken," ", "T").."Z".."\n" - kml_file = kml_file.." \n" - kml_file = kml_file.." \n" - - kml_file = kml_file.." \n" - end + createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails + createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images + end end - -- Connects all images with an path - if ( dt.preferences.read("kml_export","CreatePath","bool") == true ) then - kml_file = kml_file.." \n" - kml_file = kml_file.." Path\n" -- ToDo: I think a better name would be nice - --kml_file = kml_file.." \n" + dt.control.execute(createKMZCommand) + end - kml_file = kml_file.." \n" + -- Open the file with the standard programm + if ( dt.preferences.read("kml_export","OpenKmlFile","bool") == true ) then + local kmlFileOpenCommand - kml_file = kml_file.." \n" - kml_file = kml_file.." \n" - - for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - local altitude = 0; - if (image.elevation) then - altitude = image.elevation; - end - - kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",altitude\n" - end - end - - kml_file = kml_file.." \n" - kml_file = kml_file.." \n" - - kml_file = kml_file.." \n" - end - - kml_file = kml_file.."\n" - kml_file = kml_file.."" - - local file = io.open(exportDirectory.."/"..exportKMLFilename, "w") - file:write(kml_file) - file:close() - - dt.print("KML file created in "..exportDirectory) - - -- Compress the files to create a KMZ file if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then - exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") - - local createKMZCommand = "zip --test --move --junk-paths " - createKMZCommand = createKMZCommand .."\""..exportDirectory.."/"..exportKMZFilename.."\" " -- KMZ filename - createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..exportKMLFilename.."\" " -- KML file - - for image,exported_image in pairs(image_table) do - if ((image.longitude and image.latitude) and - (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data - ) then - local filename = string.upper(string.gsub(image.filename,"%.%w*", "")) - -- Handle duplicates - filename = addDuplicateIndex( image.duplicate_index, filename ) - - createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails - createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images - end - end - - dt.control.execute(createKMZCommand) - end - - -- Open the file with the standard programm - if ( dt.preferences.read("kml_export","OpenKmlFile","bool") == true ) then - local kmlFileOpenCommand - - if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then - kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMZFilename.."\"" - else - kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMLFilename.."\"" - end - dt.control.execute(kmlFileOpenCommand) + kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMZFilename.."\"" + else + kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMLFilename.."\"" end + dt.control.execute(kmlFileOpenCommand) + end end -- Preferences dt.preferences.register("kml_export", - "OpenKmlFile", - "bool", - _("KML export: Open KML/KMZ file after export"), - _("Opens the KML file after the export with the standard program for KML files"), - false ) + "OpenKmlFile", + "bool", + _("KML export: Open KML/KMZ file after export"), + _("Opens the KML file after the export with the standard program for KML files"), + false ) local handle = io.popen("xdg-user-dir DESKTOP") local result = handle:read() if (result == nil) then - result = "" + result = "" end handle:close() dt.preferences.register("kml_export", - "ExportDirectory", - "directory", - _("KML export: Export directory"), - _("A directory that will be used to export the KML/KMZ files"), - result ) + "ExportDirectory", + "directory", + _("KML export: Export directory"), + _("A directory that will be used to export the KML/KMZ files"), + result ) dt.preferences.register("kml_export", - "CreatePath", - "bool", - _("KML export: Connect images with path"), - _("connect all images with a path"), - false ) + "CreatePath", + "bool", + _("KML export: Connect images with path"), + _("connect all images with a path"), + false ) dt.preferences.register("kml_export", - "CreateKMZ", - "bool", - _("KML export: Create KMZ file"), - _("Compress all imeges to one KMZ file"), - true ) + "CreateKMZ", + "bool", + _("KML export: Create KMZ file"), + _("Compress all imeges to one KMZ file"), + true ) -- Register dt.register_storage("kml_export", _("KML/KMZ Export"), nil, create_kml_file) + +-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua +-- kate: hl Lua;