Merge pull request #105 from elman22/master

Updated KML/KMZ export script
This commit is contained in:
supertobi
2018-01-20 14:27:04 +01:00
committed by GitHub

View File

@@ -1,20 +1,20 @@
--[[ --[[
This file is part of darktable, This file is part of darktable,
Copyright 2016 by Tobias Jakobs. Copyright 2018 by Tobias Jakobs.
Copyright 2016 by Erik Augustin. Copyright 2018 by Erik Augustin.
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
]] ]]
--[[ --[[
darktable KML export script darktable KML export script
@@ -47,92 +47,143 @@ dt.configuration.check_version(...,{3,0,0},{4,0,0},{5,0,0})
gettext.bindtextdomain("kml_export",dt.configuration.config_dir.."/lua/locale/") gettext.bindtextdomain("kml_export",dt.configuration.config_dir.."/lua/locale/")
local function _(msgid) local function _(msgid)
return gettext.dgettext("kml_export", msgid) return gettext.dgettext("kml_export", msgid)
end end
-- Sort a table -- Sort a table
local function spairs(_table, order) -- Code copied from http://stackoverflow.com/questions/15706270/sort-a-table-in-lua local function spairs(_table, order) -- Code copied from http://stackoverflow.com/questions/15706270/sort-a-table-in-lua
-- collect the keys -- collect the keys
local keys = {} local keys = {}
for _key in pairs(_table) do keys[#keys + 1] = _key end 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, -- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys -- otherwise just sort the keys
if order then if order then
table.sort(keys, function(a,b) return order(_table, a, b) end) table.sort(keys, function(a,b) return order(_table, a, b) end)
else else
table.sort(keys) table.sort(keys)
end end
-- return the iterator function -- return the iterator function
local i = 0 local i = 0
return function() return function()
i = i + 1 i = i + 1
if keys[i] then if keys[i] then
return keys[i], _table[keys[i]] return keys[i], _table[keys[i]]
end
end end
end
end end
local function show_status(storage, image, format, filename, number, total, high_quality, extra_data) 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 end
-- Strip accents from a string -- Strip accents from a string
-- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/ -- Copied from https://forums.coronalabs.com/topic/43048-remove-special-characters-from-string/
function string.stripAccents( str ) function string.stripAccents( str )
local tableAccents = {} local tableAccents = {}
tableAccents["à"] = "a" -- A
tableAccents["á"] = "a" tableAccents["à"] = "a"
tableAccents["â"] = "a" tableAccents["À"] = "A"
tableAccents["ã"] = "a" tableAccents["á"] = "a"
tableAccents["ä"] = "a" tableAccents["Á"] = "A"
tableAccents["ç"] = "c" tableAccents["â"] = "a"
tableAccents["è"] = "e" tableAccents["Â"] = "A"
tableAccents["é"] = "e" tableAccents["ã"] = "a"
tableAccents["ê"] = "e" tableAccents["Ã"] = "A"
tableAccents["ë"] = "e" tableAccents["ä"] = "a"
tableAccents["ì"] = "i" tableAccents["Ä"] = "A"
tableAccents["í"] = "i" -- B
tableAccents["î"] = "i" -- C
tableAccents["ï"] = "i" tableAccents["ç"] = "c"
tableAccents["ñ"] = "n" tableAccents["Ç"] = "C"
tableAccents["ò"] = "o" tableAccents["č"] = "c"
tableAccents["ó"] = "o" tableAccents["Č"] = "C"
tableAccents["ô"] = "o" -- D
tableAccents["õ"] = "o" tableAccents["ď"] = "d"
tableAccents["ö"] = "o" tableAccents["Ď"] = "d"
tableAccents["ù"] = "u" -- E
tableAccents["ú"] = "u" tableAccents["è"] = "e"
tableAccents["û"] = "u" tableAccents["È"] = "E"
tableAccents["ü"] = "u" tableAccents["é"] = "e"
tableAccents["ý"] = "y" tableAccents["É"] = "E"
tableAccents["ÿ"] = "y" tableAccents["ê"] = "e"
tableAccents["À"] = "A" tableAccents["Ê"] = "E"
tableAccents["Á"] = "A" tableAccents["ë"] = "e"
tableAccents["Â"] = "A" tableAccents["Ë"] = "E"
tableAccents["Ã"] = "A" tableAccents["ě"] = "e"
tableAccents["Ä"] = "A" tableAccents["Ě"] = "E"
tableAccents["Ç"] = "C" -- F
tableAccents["È"] = "E" -- G
tableAccents["É"] = "E" -- H
tableAccents["Ê"] = "E" -- I
tableAccents["Ë"] = "E" tableAccents["ì"] = "i"
tableAccents["Ì"] = "I" tableAccents["Ì"] = "I"
tableAccents["Í"] = "I" tableAccents["í"] = "i"
tableAccents["Î"] = "I" tableAccents["Í"] = "I"
tableAccents["Ï"] = "I" tableAccents["î"] = "i"
tableAccents["Ñ"] = "N" tableAccents["Î"] = "I"
tableAccents["Ò"] = "O" tableAccents["ï"] = "i"
tableAccents["Ó"] = "O" tableAccents["Ï"] = "I"
tableAccents["Ô"] = "O" -- J
tableAccents["Õ"] = "O" -- K
tableAccents["Ö"] = "O" -- L
tableAccents["Ù"] = "U" tableAccents["ĺ"] = "l"
tableAccents["Ú"] = "U" tableAccents["Ĺ"] = "L"
tableAccents["Û"] = "U" tableAccents["ľ"] = "l"
tableAccents["Ü"] = "U" tableAccents["Ľ"] = "L"
tableAccents["Ý"] = "Y" -- 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 = ""
@@ -145,7 +196,6 @@ function string.stripAccents( str )
end end
return normalizedString return normalizedString
end end
-- Escape XML characters -- Escape XML characters
@@ -153,255 +203,278 @@ end
-- https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents -- https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents
function string.escapeXmlCharacters( str ) function string.escapeXmlCharacters( str )
str = string.gsub(str,"&", "&amp;") str = string.gsub(str,"&", "&amp;")
str = string.gsub(str,"\"", "&quot;") str = string.gsub(str,"\"", "&quot;")
str = string.gsub(str,"'", "&apos;") str = string.gsub(str,"'", "&apos;")
str = string.gsub(str,"<", "&lt;") str = string.gsub(str,"<", "&lt;")
str = string.gsub(str,">", "&gt;") str = string.gsub(str,">", "&gt;")
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
end
return filename
end end
local function create_kml_file(storage, image_table, extra_data) local function create_kml_file(storage, image_table, extra_data)
if not df.check_if_bin_exists("mkdir") then if not df.check_if_bin_exists("mkdir") then
dt.print_error(_("mkdir not found")) dt.print_error(_("mkdir not found"))
return return
end end
if not df.check_if_bin_exists("convert") then if not df.check_if_bin_exists("convert") then
dt.print_error(_("convert not found")) dt.print_error(_("convert not found"))
return return
end end
if not df.check_if_bin_exists("xdg-open") then if not df.check_if_bin_exists("xdg-open") then
dt.print_error(_("xdg-open not found")) dt.print_error(_("xdg-open not found"))
return return
end end
if not df.check_if_bin_exists("xdg-user-dir") then if not df.check_if_bin_exists("xdg-user-dir") then
dt.print_error(_("xdg-user-dir not found")) dt.print_error(_("xdg-user-dir not found"))
return 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 end
dt.print_error("Will try to export KML file now") exportDirectory = dt.configuration.tmp_dir
imageFoldername = ""
else
exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string")
local imageFoldername -- Creates dir if not exsists
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then imageFoldername = "files/"
if not df.check_if_bin_exists("zip") then local mkdirCommand = "mkdir -p "..exportDirectory.."/"..imageFoldername
dt.print_error(_("zip not found")) dt.control.execute(mkdirCommand)
return end
end
exportDirectory = dt.configuration.tmp_dir -- Create the thumbnails
imageFoldername = "" 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 else
exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") -- Remove exported image if it has no GPS data
os.remove(exported_image)
-- Creates dir if not exsists
imageFoldername = "files/"
local mkdirCommand = "mkdir -p "..exportDirectory.."/"..imageFoldername
dt.control.execute(mkdirCommand)
end end
local pattern = "[/]?([^/]+)$"
filmName = string.match(image.film.path, pattern)
-- Create the thumbnails -- Strip accents from the filename, because GoogleEarth can't open them
for image,exported_image in pairs(image_table) do -- https://github.com/darktable-org/lua-scripts/issues/54
if ((image.longitude and image.latitude) and filmName = string.stripAccents(filmName)
(image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data end
) 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 exportKMLFilename = filmName..".kml"
-- In this example, '-size 120x120' gives a hint to the JPEG decoder that the image is going to be downscaled to exportKMZFilename = filmName..".kmz"
-- 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" -- Create the KML file
dt.control.execute(convertToThumbCommand) local kml_file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
else kml_file = kml_file.."<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
-- Remove exported image if it has no GPS data kml_file = kml_file.."<Document>\n"
os.remove(exported_image)
end
local pattern = "[/]?([^/]+)$" --image_table = dt.gui.selection();
filmName = string.match(image.film.path, pattern) kml_file = kml_file.."<name>"..filmName.."</name>\n"
kml_file = kml_file.." <description>Exported from darktable</description>\n"
-- Strip accents from the filename, because GoogleEarth can't open them for image,exported_image in pairs(image_table) do
-- https://github.com/darktable-org/lua-scripts/issues/54
filmName = string.stripAccents(filmName)
end
exportKMLFilename = filmName..".kml"
exportKMZFilename = filmName..".kmz"
-- Create the KML file
local kml_file = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
kml_file = kml_file.."<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
kml_file = kml_file.."<Document>\n"
--image_table = dt.gui.selection();
kml_file = kml_file.."<name>"..filmName.."</name>\n"
kml_file = kml_file.." <description>Exported from darktable</description>\n"
for image,exported_image in pairs(image_table) do
-- Extract filename, e.g DSC9784.ARW -> DSC9784 -- Extract filename, e.g DSC9784.ARW -> DSC9784
filename = string.upper(string.gsub(image.filename,"%.%w*", "")) 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 -- Extract extension from exported image (user can choose JPG or PNG), e.g DSC9784.JPG -> .JPG
extension = string.match(exported_image,"%.%w*$") 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 (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data
) then ) then
kml_file = kml_file.." <Placemark>\n"
local image_title, image_description
if (image.title and image.title ~= "") then
image_title = string.escapeXmlCharacters(image.title)
else
image_title = image.filename
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.." <name>"..image_title.."</name>\n"
kml_file = kml_file.." <description>"..image_description.."</description>\n"
kml_file = kml_file.." <Style>\n"
kml_file = kml_file.." <IconStyle>\n"
kml_file = kml_file.." <Icon>\n"
kml_file = kml_file.." <href>"..imageFoldername.."thumb_"..filename..".jpg</href>\n"
kml_file = kml_file.." </Icon>\n"
kml_file = kml_file.." </IconStyle>\n"
kml_file = kml_file.." <BalloonStyle>\n"
kml_file = kml_file.." <text><![CDATA[<p><b>"..image_title.."</b></p><img src=\""..imageFoldername..filename..extension.."\"><p>"..image_description.."</p>]]></text>\n"
kml_file = kml_file.." <textColor>ff000000</textColor>\n"
kml_file = kml_file.." <displayMode>default</displayMode>\n"
kml_file = kml_file.." </BalloonStyle>\n"
kml_file = kml_file.." </Style>\n"
kml_file = kml_file.." <Point>\n"
kml_file = kml_file.." <extrude>1</extrude>\n"
kml_file = kml_file.." <coordinates>"..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",0</coordinates>\n"
kml_file = kml_file.." <TimeStamp>\n"
kml_file = kml_file.." <when>"..string.gsub(image.exif_datetime_taken," ", "T").."Z".."</when>\n"
kml_file = kml_file.." </TimeStamp>\n"
kml_file = kml_file.." </Point>\n"
kml_file = kml_file.." </Placemark>\n"
end
end
-- Connects all images with an path
if ( dt.preferences.read("kml_export","CreatePath","bool") == true ) then
kml_file = kml_file.." <Placemark>\n" kml_file = kml_file.." <Placemark>\n"
kml_file = kml_file.." <name>Path</name>\n" -- ToDo: I think a better name would be nice
--kml_file = kml_file.." <description></description>\n"
kml_file = kml_file.." <Style>\n" local image_title, image_description
kml_file = kml_file.." <LineStyle>\n" if (image.title and image.title ~= "") then
kml_file = kml_file.." <color>ff0000ff</color>\n" image_title = string.escapeXmlCharacters(image.title)
kml_file = kml_file.." <width>5</width>\n" else
kml_file = kml_file.." </LineStyle>\n" image_title = filename..extension
kml_file = kml_file.." </Style>\n"
kml_file = kml_file.." <LineString>\n"
kml_file = kml_file.." <coordinates>\n"
for image,exported_image in spairs(image_table, function(t,a,b) return t[b] < t[a] 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 end
kml_file = kml_file.." </coordinates>\n" -- Characters should not be escaped in CDATA, but we are using HTML fragment, so we must escape them
kml_file = kml_file.." </LineString>\n" image_description = string.escapeXmlCharacters(image.description)
kml_file = kml_file.." <name>"..image_title.."</name>\n"
kml_file = kml_file.." <description>"..image_description.."</description>\n"
kml_file = kml_file.." <Style>\n"
kml_file = kml_file.." <IconStyle>\n"
kml_file = kml_file.." <Icon>\n"
kml_file = kml_file.." <href>"..imageFoldername.."thumb_"..filename..".jpg</href>\n"
kml_file = kml_file.." </Icon>\n"
kml_file = kml_file.." </IconStyle>\n"
kml_file = kml_file.." <BalloonStyle>\n"
kml_file = kml_file.." <text><![CDATA[<p><b>"..image_title.."</b></p><img src=\""..imageFoldername..filename..extension.."\"><p>"..image_description.."</p>]]></text>\n"
kml_file = kml_file.." <textColor>ff000000</textColor>\n"
kml_file = kml_file.." <displayMode>default</displayMode>\n"
kml_file = kml_file.." </BalloonStyle>\n"
kml_file = kml_file.." </Style>\n"
kml_file = kml_file.." <Point>\n"
kml_file = kml_file.." <extrude>1</extrude>\n"
kml_file = kml_file.." <coordinates>"..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",0</coordinates>\n"
kml_file = kml_file.." <TimeStamp>\n"
kml_file = kml_file.." <when>"..string.gsub(image.exif_datetime_taken," ", "T").."Z".."</when>\n"
kml_file = kml_file.." </TimeStamp>\n"
kml_file = kml_file.." </Point>\n"
kml_file = kml_file.." </Placemark>\n" kml_file = kml_file.." </Placemark>\n"
end end
end
kml_file = kml_file.."</Document>\n" -- Connects all images with an path
kml_file = kml_file.."</kml>" if ( dt.preferences.read("kml_export","CreatePath","bool") == true ) then
kml_file = kml_file.." <Placemark>\n"
kml_file = kml_file.." <name>Path</name>\n" -- ToDo: I think a better name would be nice
--kml_file = kml_file.." <description></description>\n"
local file = io.open(exportDirectory.."/"..exportKMLFilename, "w") kml_file = kml_file.." <Style>\n"
file:write(kml_file) kml_file = kml_file.." <LineStyle>\n"
file:close() kml_file = kml_file.." <color>ff0000ff</color>\n"
kml_file = kml_file.." <width>5</width>\n"
kml_file = kml_file.." </LineStyle>\n"
kml_file = kml_file.." </Style>\n"
dt.print("KML file created in "..exportDirectory) kml_file = kml_file.." <LineString>\n"
kml_file = kml_file.." <coordinates>\n"
-- Compress the files to create a KMZ file for image,exported_image in spairs(image_table, function(t,a,b) return b.exif_datetime_taken > a.exif_datetime_taken end) do
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then if ((image.longitude and image.latitude) and
exportDirectory = dt.preferences.read("kml_export","ExportDirectory","string") (image.longitude ~= 0 and image.latitude ~= 90) -- Sometimes the north-pole but most likely just wrong data
) then
local createKMZCommand = "zip --test --move --junk-paths " local altitude = 0;
createKMZCommand = createKMZCommand .."\""..exportDirectory.."/"..exportKMZFilename.."\" " -- KMZ filename if (image.elevation) then
createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..exportKMLFilename.."\" " -- KML file altitude = image.elevation;
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*", ""))
createKMZCommand = createKMZCommand .."\""..dt.configuration.tmp_dir.."/"..imageFoldername.."thumb_"..filename..".jpg\" " -- thumbnails
createKMZCommand = createKMZCommand .."\""..exported_image.."\" " -- images
end
end end
dt.control.execute(createKMZCommand) kml_file = kml_file.." "..string.gsub(tostring(image.longitude),",", ".")..","..string.gsub(tostring(image.latitude),",", ".")..",altitude\n"
end
end end
-- Open the file with the standard programm kml_file = kml_file.." </coordinates>\n"
if ( dt.preferences.read("kml_export","OpenKmlFile","bool") == true ) then kml_file = kml_file.." </LineString>\n"
local kmlFileOpenCommand
if ( dt.preferences.read("kml_export","CreateKMZ","bool") == true ) then kml_file = kml_file.." </Placemark>\n"
kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMZFilename.."\"" end
else
kmlFileOpenCommand = "xdg-open "..exportDirectory.."/\""..exportKMLFilename.."\"" kml_file = kml_file.."</Document>\n"
end kml_file = kml_file.."</kml>"
dt.control.execute(kmlFileOpenCommand)
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 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)
end
end end
-- Preferences -- Preferences
dt.preferences.register("kml_export", dt.preferences.register("kml_export",
"OpenKmlFile", "OpenKmlFile",
"bool", "bool",
_("KML export: Open KML/KMZ file after export"), _("KML export: Open KML/KMZ file after export"),
_("Opens the KML file after the export with the standard program for KML files"), _("Opens the KML file after the export with the standard program for KML files"),
false ) false )
local handle = io.popen("xdg-user-dir DESKTOP") local handle = io.popen("xdg-user-dir DESKTOP")
local result = handle:read() local result = handle:read()
if (result == nil) then if (result == nil) then
result = "" result = ""
end end
handle:close() 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", dt.preferences.register("kml_export",
"CreatePath", "ExportDirectory",
"bool", "directory",
_("KML export: Connect images with path"), _("KML export: Export directory"),
_("connect all images with a path"), _("A directory that will be used to export the KML/KMZ files"),
false ) result )
dt.preferences.register("kml_export", dt.preferences.register("kml_export",
"CreateKMZ", "CreatePath",
"bool", "bool",
_("KML export: Create KMZ file"), _("KML export: Connect images with path"),
_("Compress all imeges to one KMZ file"), _("connect all images with a path"),
true ) false )
dt.preferences.register("kml_export",
"CreateKMZ",
"bool",
_("KML export: Create KMZ file"),
_("Compress all imeges to one KMZ file"),
true )
-- Register -- Register
dt.register_storage("kml_export", _("KML/KMZ Export"), nil, create_kml_file) dt.register_storage("kml_export", _("KML/KMZ Export"), nil, create_kml_file)
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
-- kate: hl Lua;