1 | #!/bin/bash |
---|
2 | |
---|
3 | ### |
---|
4 | ### Central script to convert CLARIN centers CSV file to Google Earth KMZ file. |
---|
5 | ### |
---|
6 | |
---|
7 | bold=$(tput bold); |
---|
8 | normal=$(tput sgr0); |
---|
9 | |
---|
10 | # As published on Stackoverflow |
---|
11 | function 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 | |
---|
26 | CSV_file_URL='http://www.clarin.eu/export_organisations_coordinates'; |
---|
27 | original_KMZ_file_URL='http://www.mpi.nl/DOBES/dobesmap/language_sites.kmz'; |
---|
28 | |
---|
29 | ROOT_directory_path="$(robust_directory_of_origin)/../"; # "$(realpath $(dirname ..) )/"; # X- $0 |
---|
30 | INPUT_directory_path="${ROOT_directory_path}/.input/"; |
---|
31 | EXEC_directory_path="${ROOT_directory_path}/exec/"; |
---|
32 | OUTPUT_directory_path="${ROOT_directory_path}/.output/"; |
---|
33 | SAXON_directory_path="${EXEC_directory_path}/"; # X- SaxonHE9-4-0-4J/ |
---|
34 | |
---|
35 | _SAXON_JAR_file_path="${SAXON_directory_path}/saxon9he.jar"; |
---|
36 | SAXON_JAR_file_path="$(realpath $_SAXON_JAR_file_path)"; |
---|
37 | |
---|
38 | XSLT_1_file_path="${EXEC_directory_path}/XMLdb_to_KML.xsl"; |
---|
39 | XSLT_2_file_path="${EXEC_directory_path}/KML_original_to_final.xsl"; |
---|
40 | XSLT_3_file_path="${EXEC_directory_path}/replicate.xsl"; |
---|
41 | CSV2XML_file_path="${EXEC_directory_path}/csv2xml"; |
---|
42 | |
---|
43 | KMZ_original_file_path="${INPUT_directory_path}/language_sites.kmz"; |
---|
44 | CSV_file_path="${INPUT_directory_path}/export_organisations_coordinates"; |
---|
45 | |
---|
46 | KML_original_file_path="${INPUT_directory_path}/doc.kml"; |
---|
47 | XML_of_CSV_file_path="${OUTPUT_directory_path}/organisations_coordinates.xml"; |
---|
48 | KML_chunk_file_path="${OUTPUT_directory_path}/KML_chunk.xml"; |
---|
49 | |
---|
50 | KML_final_file_path="${OUTPUT_directory_path}/doc.kml"; |
---|
51 | KMZ_final_file_path="${OUTPUT_directory_path}/language_sites.kmz"; |
---|
52 | KMZ_publication_directory_path='/var/www/drupal/external/kml/'; |
---|
53 | |
---|
54 | timestamp=$( date +%H:%M:%S ); |
---|
55 | |
---|
56 | echo -e "\n\n${bold}Central script to convert CLARIN centers CSV file to Google Earth KMZ file. ($timestamp)${normal}"; |
---|
57 | echo "ROOT: ${ROOT_directory_path}"; |
---|
58 | echo "Saxon JAR: ${SAXON_JAR_file_path}"; |
---|
59 | |
---|
60 | mkdir "${INPUT_directory_path}" 2> '/dev/null'; |
---|
61 | mkdir "${OUTPUT_directory_path}" 2> '/dev/null'; |
---|
62 | |
---|
63 | echo -e "\n\n${bold}Downloading CSV file from "${CSV_file_URL}" to "${CSV_file_path}" ...${normal}"; |
---|
64 | wget -O - "${CSV_file_URL}" > "${CSV_file_path}"; |
---|
65 | |
---|
66 | if [ $? -ne 0 ] |
---|
67 | then |
---|
68 | echo "ERROR: the download of the CSV file failed!"; |
---|
69 | exit 1; |
---|
70 | fi |
---|
71 | |
---|
72 | echo -e "\n\n${bold}Downloading original KMZ file from "${original_KMZ_file_URL}" to "${KMZ_original_file_path}" ...${normal}"; |
---|
73 | wget -O - "${original_KMZ_file_URL}" > "${KMZ_original_file_path}"; |
---|
74 | |
---|
75 | if [ $? -ne 0 ] |
---|
76 | then |
---|
77 | echo "ERROR: the download of the original KMZ file failed!"; |
---|
78 | exit 1; |
---|
79 | fi |
---|
80 | |
---|
81 | if 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}" |
---|
82 | then |
---|
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 |
---|
132 | else |
---|
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; |
---|
135 | fi |
---|
136 | |
---|
137 | exit 0; |
---|