source: CLARIN_KML/exec/generate_CLARIN_KMZ.sh @ 2044

Last change on this file since 2044 was 2044, checked in by sanmai, 12 years ago
  • Fix a bug that was not noticed before because it happened not to disrupt the script ($KML/Z_file_relpath).
  • Copy final KMZ file to a location for publication on the public DoBeS website.
  • Property svn:executable set to *
File size: 5.7 KB
Line 
1#!/bin/bash
2
3###
4###                     Central script to convert CLARIN centers CSV file to Google Earth KMZ file.
5###
6
7bold=$(tput bold);
8normal=$(tput sgr0);
9
10# As published on Stackoverflow
11function robust_directory_of_origin()
12{
13        SOURCE="${BASH_SOURCE[0]}";
14        DIR="$( dirname "$SOURCE" )";
15        while [ -h "$SOURCE" ]
16        do 
17          SOURCE="$(readlink "$SOURCE")";
18          [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
19          DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )";
20        done
21        DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )";
22
23        echo "${DIR}";
24}
25
26CSV_file_URL='http://www.clarin.eu/export_organisations_coordinates';
27original_KMZ_file_URL='http://www.mpi.nl/DOBES/dobesmap/language_sites.kmz';
28
29ROOT_directory_path="$(robust_directory_of_origin)/../"; # "$(realpath $(dirname ..) )/"; # X- $0
30INPUT_directory_path="${ROOT_directory_path}/.input/";
31EXEC_directory_path="${ROOT_directory_path}/exec/";
32OUTPUT_directory_path="${ROOT_directory_path}/.output/";
33SAXON_directory_path="${EXEC_directory_path}/"; # X- SaxonHE9-4-0-4J/
34
35_SAXON_JAR_file_path="${SAXON_directory_path}/saxon9he.jar";
36SAXON_JAR_file_path="$(realpath $_SAXON_JAR_file_path)";
37
38XSLT_1_file_path="${EXEC_directory_path}/XMLdb_to_KML.xsl";
39XSLT_2_file_path="${EXEC_directory_path}/KML_original_to_final.xsl";
40XSLT_3_file_path="${EXEC_directory_path}/replicate.xsl";
41CSV2XML_file_path="${EXEC_directory_path}/csv2xml";
42
43KMZ_original_file_path="${INPUT_directory_path}/language_sites.kmz";
44CSV_file_path="${INPUT_directory_path}/export_organisations_coordinates";
45
46KML_original_file_path="${INPUT_directory_path}/doc.kml";
47XML_of_CSV_file_path="${OUTPUT_directory_path}/organisations_coordinates.xml";
48KML_chunk_file_path="${OUTPUT_directory_path}/KML_chunk.xml";
49
50KML_final_file_path="${OUTPUT_directory_path}/doc.kml";
51KMZ_final_file_path="${OUTPUT_directory_path}/language_sites.kmz";
52KMZ_publication_directory_path='/var/www/drupal/external/kml/';
53
54timestamp=$( date +%H:%M:%S );
55
56echo -e "\n\n${bold}Central script to convert CLARIN centers CSV file to Google Earth KMZ file. ($timestamp)${normal}";
57echo "ROOT: ${ROOT_directory_path}";
58echo "Saxon JAR: ${SAXON_JAR_file_path}";
59
60mkdir "${INPUT_directory_path}" 2> '/dev/null';
61mkdir "${OUTPUT_directory_path}" 2> '/dev/null';
62
63echo -e "\n\n${bold}Downloading CSV file from "${CSV_file_URL}" to "${CSV_file_path}" ...${normal}";
64wget -O - "${CSV_file_URL}" > "${CSV_file_path}";
65
66if [ $? -ne 0 ]
67then
68        echo "ERROR: the download of the CSV file failed!";
69        exit 1;
70fi
71
72echo -e "\n\n${bold}Downloading original KMZ file from "${original_KMZ_file_URL}" to "${KMZ_original_file_path}" ...${normal}";
73wget -O - "${original_KMZ_file_URL}" > "${KMZ_original_file_path}";
74
75if [ $? -ne 0 ]
76then
77        echo "ERROR: the download of the original KMZ file failed!";
78        exit 1;
79fi
80
81if test -r "${OUTPUT_directory_path}" -a -r "${SAXON_JAR_file_path}" -a -x "${CSV2XML_file_path}" -a -r "${XSLT_1_file_path}" -a -r "${XSLT_1_file_path}" -a -r "${XSLT_2_file_path}" -a -r "${XSLT_3_file_path}" -a -r "${KMZ_publication_directory_path}"
82then
83        if test -r "${KMZ_original_file_path}" -a -r "${CSV_file_path}"
84        then
85                echo -e "\n\n${bold}Converting CSV file to an XML representation ...${normal}";
86
87                # X- Remove this <table> root element hack.
88                echo -e "<table>\n"                                                     > "${XML_of_CSV_file_path}";
89                < "${CSV_file_path}" "${CSV2XML_file_path}"     >> "${XML_of_CSV_file_path}";
90                echo -e "\n</table>\n"                                                  >> "${XML_of_CSV_file_path}";
91
92                if [ $? -ne 0 ]
93                then
94                        echo "ERROR: csv2xml command exited with an error!";
95                        exit 1;
96                fi
97
98                echo -e "\n\n${bold}Transforming XML representation of CSV in ${XML_of_CSV_file_path} to a KML folder XML chunk ${KML_chunk_file_path} ...${normal}";
99                java -jar "${SAXON_JAR_file_path}" -a:off -t -xi:off -s:"${XML_of_CSV_file_path}" -xsl:"${XSLT_1_file_path}" -o:"${KML_chunk_file_path}";
100
101
102                echo -e "\n\n${bold}Unzipping doc.kml from KMZ file in ${KMZ_original_file_path} ...${normal}";
103                unzip "${KMZ_original_file_path}" -d "${INPUT_directory_path}";
104
105                echo -e "\n\n${bold}Including generated XML chunk (a KML folder) in the main KML file ...${normal}";
106                java -jar "${SAXON_JAR_file_path}" -warnings:fatal -a:off -t -xi:off -s:"${KML_original_file_path}" -xsl:"${XSLT_2_file_path}" -o:"${KML_final_file_path}" KML_chunk_file_path="${KML_chunk_file_path}";
107                # Another run, just to resolve the XInclude.
108                java -jar "${SAXON_JAR_file_path}" -warnings:fatal -a:off -t -xi:on -xsl:"${XSLT_3_file_path}" -s:"${KML_final_file_path}" -o:"${KML_final_file_path}";
109                #<"${KML_final_file_path}" xsltproc --xinclude -o "${KML_final_file_path}"
110
111
112                if [[ $( grep '<xi:include' -F -l "${KML_final_file_path}" ) = "${KML_final_file_path}" ]]
113                then
114                        echo "${bold}ERROR: Found one or more string occurrences suggestive of residual unresolved XInclude element in ${KML_final_file_path}!${normal}";
115                        exit 3;
116                fi
117
118                echo -e "\n\n${bold}Zipping ${KML_final_file_path} to KMZ file ${KMZ_final_file_path} and moving it to "${KMZ_publication_directory_path}" ...${normal}";
119                cd $(dirname "${KML_final_file_path}");
120                KML_final_file_relpath=$(basename "${KML_final_file_path}");
121
122                zip -9 -D "${KMZ_final_file_path}" "${KML_final_file_relpath}";
123
124                rm "${KML_final_file_relpath}";
125                rm "${KML_original_file_path}";
126
127                cp -v --remove-destination "${KMZ_final_file_path}" -t "${KMZ_publication_directory_path}/";
128        else
129                echo -e "ERROR: The input data file(s) ${KMZ_original_file_path}\nand/or\n${CSV_file_path} is/are not existent as well as readable/executable!";
130                exit 2;
131        fi
132else
133        echo -e "ERROR: One or more of the following script files is not existent as well as readable: \n${OUTPUT_directory_path}\n${SAXON_JAR_file_path}\n${CSV2XML_file_path}\n${XSLT_1_file_path}\n${XSLT_1_file_path}\n${XSLT_2_file_path}\n${XSLT_3_file_path}\n${KMZ_publication_directory_path}";
134        exit 2;
135fi
136
137exit 0;
Note: See TracBrowser for help on using the repository browser.