Radreise-Wiki:MakeKML.pl: Unterschied zwischen den Versionen

Aus Radreise-Wiki
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 4: Zeile 4:


Das Skript fasst die Informationen aus dem Roadbook und dem Track zusammen und generiert eine GoogleEarth kompatible KML-Datei. Diese wird im '''''kml'''''-Verzeichnis gespeichert. Zusätzlich wird eine kmz-Datei im Output-Verzeichis (siehe [[Radreise-Wiki:ini.pl|ini.pl]]) gespeichert. Diese Datei kann manuell ins Wiki hochgeladen werden.
Das Skript fasst die Informationen aus dem Roadbook und dem Track zusammen und generiert eine GoogleEarth kompatible KML-Datei. Diese wird im '''''kml'''''-Verzeichnis gespeichert. Zusätzlich wird eine kmz-Datei im Output-Verzeichis (siehe [[Radreise-Wiki:ini.pl|ini.pl]]) gespeichert. Diese Datei kann manuell ins Wiki hochgeladen werden.
'''Quellcode des Skripts:'''


  <nowiki>
  <nowiki>
use strict;
use strict;
 
# Encoding Stuff
use Encode qw(encode decode);
use Encode qw(encode decode);
 
my $encoding = 'utf-8';
# Default Settings
my $encOut;
push @INC, '.';
$encOut  = 'cp1252'; # Windows Ansi
require "ini.pl";
$encOut   = 'cp850' ; # DOS Fenster
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
use URI::Escape qw( uri_escape_utf8 );
 
use CGI;
my $routeFileName = "";
my $cgi = new CGI;
my $routeURL      = "";
 
# Default Directories
if ($#ARGV != -1) {
require "ini.pl";
 
my ($baseDir, $outDir, $username, $password) = getIni();
$routeFileName = join " ", @ARGV;
 
my $routeFileName = "";
if ($routeFileName =~ / \($/) {  
my $routeURL      = "";
$routeFileName .= "retour)";
if ($#ARGV != -1) {
}
 
$routeFileName = join " ", @ARGV;
} else {
 
if ($routeFileName =~ / \($/) {  
my @content;
 
$routeFileName .= "retour)";
open FIN, "$baseDir/RouteList.txt" or die "\n$! : $baseDir/RouteList.txt";
}
while (<FIN>) { push @content, decode $encoding, $_; }
close (FIN);
my $urlstring  = uri_escape_utf8($routeFileName);
my @content_s = reverse sort @content;
$urlstring =~ s/%20/\_/g;
$urlstring =~ s/\(/%28/g;
my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
$urlstring =~ s/\)/%29/g;
 
$urlstring =~ s/\'/%27/g;
chomp $name;
 
$routeURL = $urlstring;
$routeFileName = $name;
}
} else {
 
my $routeFileName_e = encode $encAnsi, $routeFileName;
my @content;
 
$routeURL = encodeRouteNameURL($routeFileName);
open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt";
 
while (<FIN>) { push @content, decode $encoding, $_; }
print encode $encOut, "\n  Generating KML-File for $routeFileName\n";
close (FIN);
 
#===================
my @content_s = reverse sort @content;
# Reading Trackfile
 
my ($dateTXT, $dateKML, $type, $name, $url) = split " :: ", $content_s[0];
open FIN, "$baseDir/tracks/$routeFileName_e.txt" or die "\n$! : $baseDir/tracks/$routeFileName_e.txt";
my $track = <FIN>;
$routeFileName = $name;
close (FIN);
$url =~ s/\n//;
 
$routeURL = $url;
open FIN, "$baseDir/text/$routeFileName_e.txt" or die "\n$! : $baseDir/text/$routeFileName_e.txt";
}
 
my $ucontent = "";
my $routeFileName_e = encode 'cp1252', $routeFileName;
while (<FIN>) { $ucontent .= $_; }
close FIN;
print encode $encOut, "\n>$routeFileName<\n";
my $content = decode ($encoding, $ucontent);
print encode $encOut, ">$routeURL<\n";
 
if ($content !~ /
#=================
 
# Reading Trackfile
\{\{TOC_Radfernweg}}\n
.*
open FIN, "$baseDir/tracks/$routeFileName_e.txt" or die "$! : $baseDir/tracks/$routeFileName_e.txt";
\n==\ Roadbook\ ==\n
my $track = <FIN>;
.*
close (FIN);
\n==\ GPS-Tracks\ ==\n
.*
open FIN, "$baseDir/text/$routeFileName_e.txt" or die "$! : $baseDir/text/$routeFileName_e.txt";
\[\[Kategorie:\s*(
Fluss-Radfernweg|
my $ucontent = "";
Anderer\ Radfernweg|
while (<FIN>) { $ucontent .= $_; }
Querverbindung|
close FIN;
Direktverbindung|
my $content = decode ($encoding, $ucontent);
Radweit-Strecke|
Radreise|
if ($content !~ /
Fernroute|
Regionaler\ Radwanderweg|
{{TOC_Radfernweg}}\n
Routenplaner\ Testroute|
.*
Streckenvorschlag|
\n==\ Roadbook\ ==\n
Stadtrundfahrt
.*
)
\n==\ GPS-Tracks\ ==\n
 
.*
/xs) {
\[\[Kategorie:\s*(
 
Fluss-Radfernweg|
print encode $encOut, "Chapters in $routeFileName are not correct!\n";
Anderer\ Radfernweg|
exit(1);
Querverbindung|
}
Direktverbindung|
 
Fernroute|
my @content   = split "\n", $content;
Regionaler\ Radwanderweg|
 
Routenplaner\ Testroute|
my  @routeFile = ();
Streckenvorschlag|
push @routeFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
Stadtrundfahrt
push @routeFile, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n";
)\]\]
push @routeFile, "<Document>\n";
push @routeFile, " <name>$routeFileName</name>\n";
/xs) {
push @routeFile, " <Metadata>\n";
push @routeFile, " <![CDATA[<!--$routeURL-->]]>\n";
print encode $encOut, "Chapters in $routeFileName are not correct!\n";  
push @routeFile, " </Metadata>\n";
exit(1);
push @routeFile, " <open>0</open>\n";
}
push @routeFile, " <Placemark>\n";
push @routeFile, " <name>$routeFileName GPS-Track</name>\n";
my @content  = split "\n", $content;
push @routeFile, " <description>\n";
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL\">Wiki</a>]]> \n";
my  @routeFile = ();
push @routeFile, " </description>\n";
push @routeFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
push @routeFile, " <Style>\n";
push @routeFile, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n";
push @routeFile, " <LineStyle>\n";
push @routeFile, "<Document>\n";
push @routeFile, " <color>ffff00ff</color>\n";
push @routeFile, " <name>$routeFileName</name>\n";
push @routeFile, " <width>2</width>\n";
push @routeFile, " <Metadata>\n";
push @routeFile, " </LineStyle>\n";
push @routeFile, " <![CDATA[<!--$routeURL-->]]>\n";
push @routeFile, " </Style>\n";
push @routeFile, " </Metadata>\n";
push @routeFile, " <LineString>\n";
push @routeFile, " <open>0</open>\n";
push @routeFile, " <tessellate>1</tessellate>\n";
push @routeFile, " <Placemark>\n";
push @routeFile, " <coordinates>\n";
push @routeFile, " <name>$routeFileName GPS-Track</name>\n";
push @routeFile, " $track\n";
push @routeFile, " <description>\n";
push @routeFile, " </coordinates>\n";
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL\">Wiki</a>]]> \n";
push @routeFile, " </LineString>\n";
push @routeFile, " </description>\n";
push @routeFile, " </Placemark>\n";
push @routeFile, " <Style>\n";
push @routeFile, " <Folder>\n";
push @routeFile, " <LineStyle>\n";
push @routeFile, " <name>Orte</name>\n";
push @routeFile, " <color>ffff00ff</color>\n";
push @routeFile, " <open>0</open>\n";
push @routeFile, " <width>2</width>\n";
 
push @routeFile, " </LineStyle>\n";
my $roadBook  = 0;
push @routeFile, " </Style>\n";
my $place      = 0;
push @routeFile, " <LineString>\n";
my $touri      = 0;
push @routeFile, " <tessellate>1</tessellate>\n";
my $geo        = 0;
push @routeFile, " <coordinates>\n";
my $lon        = 0;
push @routeFile, " $track\n";
my $lat        = 0;
push @routeFile, " </coordinates>\n";
my $alt        = 0;
push @routeFile, " </LineString>\n";
my $road      = 0;
  push @routeFile, " </Placemark>\n";
my $kilo      = 0;
  push @routeFile, " <Folder>\n";
 
push @routeFile, " <name>Orte</name>\n";
my $placeName = "";
push @routeFile, " <open>0</open>\n";
my %placeName = ();
my $touriInfo  = "";
my $roadBook  = 0;
my $geoData    = "";
my $place      = 0;
my $roadInfo  = "";
my $touri      = 0;
my $kilometer  = "";
my $geo        = 0;
 
my $lon        = 0;
foreach my $line (@content) {
my $lat        = 0;
 
my $alt        = 0;
# Last Placemark is finished, Roadbook is finished
my $road      = 0;
if (($roadBook) and ($line =~ /^== .* ==$/)) {
my $kilo      = 0;
 
# Status-Übersicht
my $placeName  = "";
if ($touriInfo ne "") { $touri = 1; }
my %placeName  = ();
if ($roadInfo  ne "") { $road = 1; }
my $touriInfo = "";
 
my $geoData    = "";
my $placeNameURL = encodePlaceNameURL($placeName);
my $roadInfo  = "";
 
my $kilometer  = "";
if ($placeName{$placeName} > 1) {
$placeNameURL .= "_$placeName{$placeName}";
foreach my $line (@content) {
}
 
# Last Placemark is finished, Roadbook is finished
push @routeFile, " <Placemark>\n";
if (($roadBook) and ($line =~ /^== .* ==$/)) {
push @routeFile, " <name>$placeName</name>\n";
push @routeFile, " <visibility>0</visibility>\n";
# Status-Übersicht
push @routeFile, " <description>\n";
if ($touriInfo ne "") { $touri = 1; }
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
if ($roadInfo  ne "") { $road  = 1; }
push @routeFile, " </description>\n";
push @routeFile, " <Point>\n";
my $placeNameURL = $cgi->escape($placeName);
push @routeFile, " <coordinates>\n";
$placeNameURL    =~ s/\%20/\_/g;
push @routeFile, " $lon,$lat,$alt\n";
$placeNameURL    =~ s/\%/\./g;
push @routeFile, " </coordinates>\n";
if ($placeName{$placeName} > 1) {
push @routeFile, " </Point>\n";
$placeNameURL .= "_$placeName{$placeName}";
push @routeFile, " </Placemark>\n";
}
 
$roadBook  = 0;
push @routeFile, " <Placemark>\n";
$place    = 0;
push @routeFile, " <name>$placeName</name>\n";
$touri    = 0;
push @routeFile, " <visibility>0</visibility>\n";
$geo      = 0;
push @routeFile, " <description>\n";
$lon      = 0;
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
$lat      = 0;
push @routeFile, " </description>\n";
$alt      = 0;
push @routeFile, " <Point>\n";
$road      = 0;
push @routeFile, " <coordinates>\n";
$kilo      = 0;
push @routeFile, " $lon,$lat,$alt\n";
 
push @routeFile, " </coordinates>\n";
$placeName  = "";
push @routeFile, " </Point>\n";
%placeName  = ();
push @routeFile, " </Placemark>\n";
$touriInfo  = "";
$geoData    = "";
$roadBook  = 0;
$roadInfo  = "";
$place    = 0;
$kilometer  = "";
$touri    = 0;
 
$geo      = 0;
# Start of Roadbook
$lon      = 0;
} elsif ($line =~ /^== Roadbook ==$/) {
$lat      = 0;
 
$alt      = 0;
$roadBook = 1;
$road      = 0;
 
$kilo      = 0;
# A new Place within the Roadbook
} elsif ($roadBook and ($line =~ /^=== (.*) ===$/)) {
$placeName  = "";
 
%placeName  = ();
if ($place and not $kilo) {
$touriInfo  = "";
print "  No Kilometers: $routeFileName :: $placeName\n";
$geoData    = "";
#exit;
$roadInfo  = "";
}
$kilometer  = "";
 
# Save previous place
# Start of Roadbook
if ($place) {
} elsif ($line =~ /^== Roadbook ==$/) {
 
# Status-Übersicht
$roadBook = 1;
if ($touriInfo ne "") { $touri = 1; }
if ($roadInfo  ne "") { $road  = 1; }
# A new Place within the Roadbook
 
} elsif ($roadBook and ($line =~ /^=== (.*) ===$/)) {
my $placeNameURL = encodePlaceNameURL($placeName);
 
if ($place and not $kilo) {
if ($placeName{$placeName} > 1) {
print "  No Kilometers: $routeFileName :: $placeName\n";
$placeNameURL .= "_$placeName{$placeName}";
#exit;
}
}
 
push @routeFile, " <Placemark>\n";
# Save previous place
push @routeFile, " <name>$placeName</name>\n";
if ($place) {
push @routeFile, " <visibility>0</visibility>\n";
push @routeFile, " <description>\n";
# Status-Übersicht
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
if ($touriInfo ne "") { $touri = 1; }
push @routeFile, " </description>\n";
if ($roadInfo  ne "") { $road  = 1; }
push @routeFile, " <Point>\n";
push @routeFile, " <coordinates>\n";
my $placeNameURL = $cgi->escape($placeName);
push @routeFile, " $lon,$lat,$alt\n";
$placeNameURL    =~ s/\%20/\_/g;
push @routeFile, " </coordinates>\n";
$placeNameURL    =~ s/\%/\./g;
push @routeFile, " </Point>\n";
if ($placeName{$placeName} > 1) {
push @routeFile, " </Placemark>\n";
$placeNameURL .= "_$placeName{$placeName}";
}
}
 
$place    = 1;
push @routeFile, " <Placemark>\n";
$touri    = 0;
push @routeFile, " <name>$placeName</name>\n";
$geo      = 0;
push @routeFile, " <visibility>0</visibility>\n";
$lon      = 0;
push @routeFile, " <description>\n";
$lat      = 0;
push @routeFile, " <![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
$alt      = 0;
push @routeFile, " </description>\n";
$road      = 0;
push @routeFile, " <Point>\n";
$kilo      = 0;
push @routeFile, " <coordinates>\n";
 
push @routeFile, " $lon,$lat,$alt\n";
$placeName  = $1;
push @routeFile, " </coordinates>\n";
if (defined $placeName{$placeName}) {
push @routeFile, " </Point>\n";
$placeName{$placeName}++;
push @routeFile, " </Placemark>\n";
} else {
}
$placeName{$placeName} = 1;
}
$place    = 1;
 
$touri    = 0;
$touriInfo  = "";
$geo      = 0;
$geoData    = "";
$lon      = 0;
$roadInfo  = "";
$lat      = 0;
$kilometer  = "";
$alt      = 0;
 
$road      = 0;
#print "$routeFileName :: $placeName\n";
$kilo      = 0;
 
# Geodata within the Placemark
$placeName  = $1;
} elsif ($place and ($line =~ /^\{\{Geodaten\|(.*)}}$/)) {
if (defined $placeName{$placeName}) {
 
$placeName{$placeName}++;
$geo    = 1;
} else {
$geoData = $1;
$placeName{$placeName} = 1;
 
}
if ($geoData =~ /^([-]?\d+\.\d+)\|([-]?\d+\.\d+)\|([-]?\d+)\|([^\|]+)$/) {
 
$touriInfo  = "";
$lat  = $1;
$geoData    = "";
$lon  = $2;
$roadInfo  = "";
$alt  = $3;
$kilometer  = "";
 
} else {
#print "$routeFileName :: $placeName\n";
print "Error in Geodata: $geoData\n";
exit;
# Geodata within the Placemark
}
} elsif ($place and ($line =~ /^{{Geodaten\|(.*)}}$/)) {
 
#print "      $geoData\n";
$geo    = 1;
 
$geoData = $1;
# Kilometrierung within Placemark
} elsif ($place and ($line =~ /^\{\{Kilometrierung\|(.*)}}$/)) {
if ($geoData =~ /^([-]?\d+\.\d+)\|([-]?\d+\.\d+)\|([-]?\d+)\|([^\|]+)$/) {
 
if (not $geo) {
$lat  = $1;
print "No Coordinates: $routeFileName :: $placeName\n";
$lon  = $2;
exit;
$alt  = $3;
}
 
} else {
if ($kilo) {
print "Error in Geodata: $geoData\n";
print "  Multiple Kilometers: $routeFileName :: $placeName\n";
exit;
#exit;
}
}
 
#print "      $geoData\n";
$kilo      = 1;
$kilometer = $1;
# Kilometrierung within Placemark
 
} elsif ($place and ($line =~ /^{{Kilometrierung\|(.*)}}$/)) {
#print "      $kilometer\n";
 
if (not $geo) {
# KilometrierungHm within Placemark
print "No Coordinates: $routeFileName :: $placeName\n";
} elsif ($place and ($line =~ /^\{\{KilometrierungHm\|(.*)}}$/)) {
exit;
 
}
if (not $geo) {
print "No Coordinates: $routeFileName :: $placeName\n";
if ($kilo) {
exit;
print "  Multiple Kilometers: $routeFileName :: $placeName\n";
}
#exit;
 
}
if ($kilo) {
print "  Multiple Kilometers: $routeFileName :: $placeName\n";
$kilo      = 1;
#exit;
$kilometer = $1;
}
 
#print "      $kilometer\n";
$kilo      = 1;
$kilometer = $1;
# KilometrierungHm within Placemark
 
} elsif ($place and ($line =~ /^{{KilometrierungHm\|(.*)}}$/)) {
#print "      $kilometer\n";
 
if (not $geo) {
# KilometrierungHm6 within Placemark
print "No Coordinates: $routeFileName :: $placeName\n";
} elsif ($place and ($line =~ /^\{\{KilometrierungHm6\|(.*)}}$/)) {
exit;
 
}
if (not $geo) {
print "No Coordinates: $routeFileName :: $placeName\n";
if ($kilo) {
exit;
print "  Multiple Kilometers: $routeFileName :: $placeName\n";
}
#exit;
 
}
if ($kilo) {
print "   Multiple Kilometers: $routeFileName :: $placeName\n";
$kilo      = 1;
#exit;
$kilometer = $1;
}
 
#print "      $kilometer\n";
$kilo      = 1;
$kilometer = $1;
# Empty Line
 
} elsif ($line =~ /^\s*$/) {
#print "      $kilometer\n";
 
} elsif ($line =~ /<br style="clear:both" \/>/) {
# Empty Line
} elsif ($line =~ /^\s*$/) {
} elsif ($line =~ /\[\[Bild/) {
 
} elsif ($line =~ /<br style="clear:both" \/>/) {
} elsif ($place and not $geo and not $kilo) {
 
} elsif ($line =~ /\[\[Bild/) {
$touriInfo .= "$line\n";
 
} elsif ($place and not $geo and not $kilo) {
#print "      $line\n";
 
$touriInfo .= "$line\n";
} elsif ($place and $geo and not $kilo) {
 
#print "     $line\n";
$roadInfo .= "$line\n";
 
} elsif ($place and $geo and not $kilo) {
#print "      $line\n";
 
$roadInfo .= "$line\n";
}
 
}
#print "      $line\n";
 
push @routeFile, " </Folder>\n";
}
push @routeFile, "</Document>\n";
}
push @routeFile, "</kml>\n";
 
push @routeFile, " </Folder>\n";
my $content = "";
push @routeFile, "</Document>\n";
foreach my $line (@routeFile) {
push @routeFile, "</kml>\n";
$content .= $line;
 
}
my $content = "";
foreach my $line (@routeFile) {
my $exitCode =0;
$content .= $line;
}
&saveFileIfChangedOrNew ("$baseDir/kml/", "$routeFileName_e.kml", encode ($encoding, $content), 1);
 
my $exitCode =0;
print "\n";
$exitCode = saveFileIfChangedOrNew ("$baseDir/kml/", "$routeFileName_e.kml", encode ($encoding, $content), 1);
 
exit($exitCode);
exit($exitCode);
</nowiki>
 
#==========================================================
[[Kategorie: Verwaltungs-Software]]
#==========================================================
use Digest::MD5;
qw(md5_hex);
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
sub saveFileIfChangedOrNew {
(my $path, my $fileName, my $file, my $kmz) = @_;
if (open(FILE, "< $path$fileName")) {
my $md5 = Digest::MD5->new;
while (<FILE>) {
$md5->add($_);
}
close(FILE);
my $md5_old = $md5->hexdigest;
$md5 = Digest::MD5->new;
$md5->add($file);
my $md5_new = $md5->hexdigest;
unless ($md5_old eq $md5_new) {
print encode $encOut, "****************** Overwriting file $fileName ******************\n";
open  FT,"> $path$fileName";
print FT $file;
close FT;
$exitCode = 1;
if ($kmz) {
my $zip = Archive::Zip->new();
$zip->addFile("$path$fileName","doc.kml");
$fileName =~ s/\.kml/\.kmz/;
unless ( $zip->writeToFileNamed("$outDir/$fileName") == AZ_OK ) {
  die 'write error';
}
}
}
} else {
print encode $encOut, "****************** Creating new file $fileName ******************\n";
open  FT,"> $path$fileName";
print FT $file;
close FT;
$exitCode = 1;
if ($kmz) {
my $zip = Archive::Zip->new();
$zip->addFile("$path$fileName","doc.kml");
$fileName =~ s/\.kml/\.kmz/;
unless ( $zip->writeToFileNamed("$outDir/$fileName") == AZ_OK ) {
  die 'write error';
}
}
}
}
# =========================================================
  </nowiki>

Aktuelle Version vom 12. Januar 2018, 18:28 Uhr

Das Perl-Skript makeKML.pl wird mit dem Streckennamen als Parameter aufgerufen. Die zugehörige Streckendatei muss bereits im text- und die Trackdatei im track-Verzeichnis vorhanden sein. Beispiel für einen Aufruf:

perl makeKML.pl Donau

Das Skript fasst die Informationen aus dem Roadbook und dem Track zusammen und generiert eine GoogleEarth kompatible KML-Datei. Diese wird im kml-Verzeichnis gespeichert. Zusätzlich wird eine kmz-Datei im Output-Verzeichis (siehe ini.pl) gespeichert. Diese Datei kann manuell ins Wiki hochgeladen werden.

Quellcode des Skripts:

use strict;

use Encode qw(encode decode);

# Default Settings
push @INC, '.';
require "ini.pl";
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();

my $routeFileName = "";
my $routeURL      = "";

if ($#ARGV != -1) {

	$routeFileName = join " ", @ARGV;

	if ($routeFileName =~ / \($/) { 
	
		$routeFileName .= "retour)";
	}

} else {

	my @content;

	open FIN, "$baseDir/RouteList.txt" or die "\n$! : $baseDir/RouteList.txt";
	while (<FIN>) { push @content, decode $encoding, $_; }
	close (FIN);
	
	my @content_s = reverse sort @content;
	
	my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];

	chomp $name;

	$routeFileName = $name;
}

my $routeFileName_e = encode $encAnsi, $routeFileName;

$routeURL = encodeRouteNameURL($routeFileName);

print encode $encOut, "\n   Generating KML-File for $routeFileName\n";

#===================
# Reading Trackfile

open FIN, "$baseDir/tracks/$routeFileName_e.txt" or die "\n$! : $baseDir/tracks/$routeFileName_e.txt";
my $track = <FIN>;
close (FIN);

open FIN, "$baseDir/text/$routeFileName_e.txt" or die "\n$! : $baseDir/text/$routeFileName_e.txt";

my $ucontent = "";
while (<FIN>) { $ucontent .= $_; }
close FIN;
my $content = decode ($encoding, $ucontent);

if ($content !~ /

	\{\{TOC_Radfernweg}}\n
	.*
	\n==\ Roadbook\ ==\n
	.*
	\n==\ GPS-Tracks\ ==\n
	.*
	\[\[Kategorie:\s*(
		Fluss-Radfernweg|
		Anderer\ Radfernweg|
		Querverbindung|
		Direktverbindung|
		Radweit-Strecke|
		Radreise|
		Fernroute|
		Regionaler\ Radwanderweg|
		Routenplaner\ Testroute|
		Streckenvorschlag|
		Stadtrundfahrt
	)

	/xs) {

	print encode $encOut, "Chapters in $routeFileName are not correct!\n"; 
	exit(1);
}

my @content   = split "\n", $content;

my   @routeFile = ();
push @routeFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
push @routeFile, "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n";
push @routeFile, "<Document>\n";
push @routeFile, "	<name>$routeFileName</name>\n";
push @routeFile, "	<Metadata>\n";
push @routeFile, "		<![CDATA[<!--$routeURL-->]]>\n";
push @routeFile, "	</Metadata>\n";
push @routeFile, "	<open>0</open>\n";
push @routeFile, "	<Placemark>\n";
push @routeFile, "		<name>$routeFileName GPS-Track</name>\n";
push @routeFile, "		<description>\n";
push @routeFile, "			<![CDATA[<a href=\"http://radreise-wiki.de/$routeURL\">Wiki</a>]]> \n";
push @routeFile, "		</description>\n";
push @routeFile, "		<Style>\n";
push @routeFile, "			<LineStyle>\n";
push @routeFile, "				<color>ffff00ff</color>\n";
push @routeFile, "				<width>2</width>\n";
push @routeFile, "			</LineStyle>\n";
push @routeFile, "		</Style>\n";
push @routeFile, "		<LineString>\n";
push @routeFile, "			<tessellate>1</tessellate>\n";
push @routeFile, "			<coordinates>\n";
push @routeFile, "				$track\n";
push @routeFile, "			</coordinates>\n";
push @routeFile, "		</LineString>\n";
push @routeFile, "	</Placemark>\n";
push @routeFile, "	<Folder>\n";
push @routeFile, "		<name>Orte</name>\n";
push @routeFile, "		<open>0</open>\n";

my $roadBook   = 0;
my $place      = 0;
my $touri      = 0;
my $geo        = 0;
my $lon        = 0;
my $lat        = 0;
my $alt        = 0;
my $road       = 0;
my $kilo       = 0;

my $placeName  = "";
my %placeName  = ();
my $touriInfo  = "";
my $geoData    = "";
my $roadInfo   = "";
my $kilometer  = "";

foreach my $line (@content) {

	# Last Placemark is finished, Roadbook is finished
	if (($roadBook) and ($line =~ /^== .* ==$/)) {

		# Status-Übersicht
		if ($touriInfo ne "") { $touri = 1; }
		if ($roadInfo  ne "") { $road  = 1; }

		my $placeNameURL = encodePlaceNameURL($placeName);

		if ($placeName{$placeName} > 1) {
			$placeNameURL .= "_$placeName{$placeName}";
		}

		push @routeFile, "		<Placemark>\n";
		push @routeFile, "			<name>$placeName</name>\n";
		push @routeFile, "			<visibility>0</visibility>\n";
		push @routeFile, "			<description>\n";
		push @routeFile, "				<![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
		push @routeFile, "			</description>\n";
		push @routeFile, "			<Point>\n";
		push @routeFile, "				<coordinates>\n";
		push @routeFile, "					$lon,$lat,$alt\n";
		push @routeFile, "				</coordinates>\n";
		push @routeFile, "			</Point>\n";
		push @routeFile, "		</Placemark>\n";

		$roadBook  = 0;
		$place     = 0;
		$touri     = 0;
		$geo       = 0;
		$lon       = 0;
		$lat       = 0;
		$alt       = 0;
		$road      = 0;
		$kilo      = 0;

		$placeName  = "";
		%placeName  = ();
		$touriInfo  = "";
		$geoData    = "";
		$roadInfo   = "";
		$kilometer  = "";

	# Start of Roadbook
	} elsif ($line =~ /^== Roadbook ==$/) {

		$roadBook = 1;

	# A new Place within the Roadbook
	} elsif ($roadBook and ($line =~ /^=== (.*) ===$/)) {

		if ($place and not $kilo) {
			print "   No Kilometers: $routeFileName :: $placeName\n";
			#exit;
		}

		# Save previous place
		if ($place) {

			# Status-Übersicht
			if ($touriInfo ne "") { $touri = 1; }
			if ($roadInfo  ne "") { $road  = 1; }

			my $placeNameURL = encodePlaceNameURL($placeName);

			if ($placeName{$placeName} > 1) {
				$placeNameURL .= "_$placeName{$placeName}";
			}

			push @routeFile, "		<Placemark>\n";
			push @routeFile, "			<name>$placeName</name>\n";
			push @routeFile, "			<visibility>0</visibility>\n";
			push @routeFile, "			<description>\n";
			push @routeFile, "				<![CDATA[<a href=\"http://radreise-wiki.de/$routeURL#$placeNameURL\">$routeFileName</a>]]> \n";
			push @routeFile, "			</description>\n";
			push @routeFile, "			<Point>\n";
			push @routeFile, "				<coordinates>\n";
			push @routeFile, "					$lon,$lat,$alt\n";
			push @routeFile, "				</coordinates>\n";
			push @routeFile, "			</Point>\n";
			push @routeFile, "		</Placemark>\n";
		}

		$place     = 1;
		$touri     = 0;
		$geo       = 0;
		$lon       = 0;
		$lat       = 0;
		$alt       = 0;
		$road      = 0;
		$kilo      = 0;

		$placeName  = $1;
		if (defined $placeName{$placeName}) {
			$placeName{$placeName}++;
		} else {
			$placeName{$placeName} = 1;
		}

		$touriInfo  = "";
		$geoData    = "";
		$roadInfo   = "";
		$kilometer  = "";

		#print "$routeFileName :: $placeName\n";

	# Geodata within the Placemark
	} elsif ($place and ($line =~ /^\{\{Geodaten\|(.*)}}$/)) {

		$geo     = 1;
		$geoData = $1;

		if ($geoData =~ /^([-]?\d+\.\d+)\|([-]?\d+\.\d+)\|([-]?\d+)\|([^\|]+)$/) {

			$lat  = $1;
			$lon  = $2;
			$alt  = $3;

		} else {
			print "Error in Geodata: $geoData\n";
			exit;
		}

		#print "      $geoData\n";

	# Kilometrierung within Placemark
	} elsif ($place and ($line =~ /^\{\{Kilometrierung\|(.*)}}$/)) {

		if (not $geo) {
			print "No Coordinates: $routeFileName :: $placeName\n";
			exit;
		}

		if ($kilo) {
			print "   Multiple Kilometers: $routeFileName :: $placeName\n";
			#exit;
		}

		$kilo      = 1;
		$kilometer = $1;

		#print "      $kilometer\n";

	# KilometrierungHm within Placemark
	} elsif ($place and ($line =~ /^\{\{KilometrierungHm\|(.*)}}$/)) {

		if (not $geo) {
			print "No Coordinates: $routeFileName :: $placeName\n";
			exit;
		}

		if ($kilo) {
			print "   Multiple Kilometers: $routeFileName :: $placeName\n";
			#exit;
		}

		$kilo      = 1;
		$kilometer = $1;

		#print "      $kilometer\n";

	# KilometrierungHm6 within Placemark
	} elsif ($place and ($line =~ /^\{\{KilometrierungHm6\|(.*)}}$/)) {

		if (not $geo) {
			print "No Coordinates: $routeFileName :: $placeName\n";
			exit;
		}

		if ($kilo) {
			print "   Multiple Kilometers: $routeFileName :: $placeName\n";
			#exit;
		}

		$kilo      = 1;
		$kilometer = $1;

		#print "      $kilometer\n";

	# Empty Line
	} elsif ($line =~ /^\s*$/) {

	} elsif ($line =~ /<br style="clear:both" \/>/) {

	} elsif ($line =~ /\[\[Bild/) {

	} elsif ($place and not $geo and not $kilo) {

		$touriInfo .= "$line\n";

		#print "      $line\n";

	} elsif ($place and $geo and not $kilo) {

		$roadInfo .= "$line\n";

		#print "      $line\n";

	}
}

push @routeFile, "	</Folder>\n";
push @routeFile, "</Document>\n";
push @routeFile, "</kml>\n";

my $content = "";
foreach my $line (@routeFile) {
	$content .= $line;
}

my $exitCode =0;
$exitCode = saveFileIfChangedOrNew ("$baseDir/kml/", "$routeFileName_e.kml", encode ($encoding, $content), 1);

exit($exitCode);