Merge pull request 'kml-pl' (#2) from kml-pl into main

Reviewed-on: #2
pull/4/head
silverwizard 4 years ago
commit 2edb0635b4
  1. 112
      kml.pl
  2. 8
      webroot/ours.kml

112
kml.pl

@ -5,21 +5,112 @@ use strict;
use warnings; use warnings;
use XML::LibXML; use XML::LibXML;
use Data::Dumper; use URI::Escape;
my $filename = 'ours.kml'; my $filename = 'webroot/ours.kml';
my $dom = XML::LibXML->load_xml(location => $filename, no_blanks => 1); my $dom = XML::LibXML->load_xml(location => $filename, no_blanks => 1);
my $xpc = XML::LibXML::XPathContext->new($dom); my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs('k', "http://earth.google.com/kml/2.2"); $xpc->registerNs('k', "http://earth.google.com/kml/2.2");
append_placemark("Fo'o", '-122,47', 'Fo%27o'); my $mode = shift;
append_placemark("Bar", '-122.5,47.5'); unless(defined($mode))
{
USAGE();
}
if ($mode eq 'add')
{
my ($name, $coordinates) = @ARGV;
unless (defined($name) && defined($coordinates))
{
say "Not enough args for add operation";
USAGE(1);
}
my $name_id = to_id($name);
my ($placemark) = $xpc->findnodes("//k:Placemark[\@id=\"$name_id\"]");
if(defined($placemark))
{
say "$name already exists in $filename";
exit(1);
}
append_placemark($name, $coordinates, uri_escape($name));
print $dom->toString(1);
}
# say $dom->toString(1); elsif ($mode eq 'remove')
open TEST_KML, ">test.kml"; {
print TEST_KML $dom->toString(1); my ($name) = @ARGV;
close TEST_KML; unless (defined($name))
{
say "No place name provided to delete operation";
USAGE(1);
}
$name = to_id($name);
my ($placemark) = $xpc->findnodes("//k:Placemark[\@id=\"$name\"]");
if(defined($placemark))
{
$placemark->unbindNode();
print $dom->toString(1);
}
else
{
say "$name was not found in $filename";
exit(1);
}
}
elsif ($mode eq 'set')
{
my ($name, $coordinates) = @ARGV;
unless (defined($name) && defined($coordinates))
{
say "Not enough args for set operation";
USAGE(1);
}
$name = to_id($name);
my ($placemark) = $xpc->findnodes("//k:Placemark[\@id=\"$name\"]");
if(defined($placemark))
{
my ($coordinates_node) = $xpc->findnodes("./k:Point/k:coordinates", $placemark);
my ($description_node) = $xpc->findnodes("./k:description", $placemark);
$coordinates_node->firstChild()->setData($coordinates);
$description_node->firstChild()->setData($coordinates);
print $dom->toString(1);
}
else
{
say "$name was not found in $filename";
exit(1);
}
}
else
{
say "Invalid operation";
USAGE(1);
}
sub USAGE
{
my $code = shift;
say "USAGE: ";
say "";
say "kml.pl add <name> <coordinates>";
say "";
say " Adds a new placemark to ours.kml for <name> at <coordinates>.";
say " The name will automatically link to <name> page on https://jackpoint.obscuritus.ca/";
say "";
say "";
say "kml.pl set <name> <coordinates>";
say "";
say " Sets the coordinates of the <name> placemark to <coordinates>.";
say "";
say "";
say "kml.pl remove <name>";
say "";
say " Removes the <name> placemark";
exit($code || 0);
}
sub add_empty_child sub add_empty_child
{ {
my $node = shift; my $node = shift;
@ -51,6 +142,11 @@ sub append_placemark
my $name_cdata = $dom->createCDATASection($name_content); my $name_cdata = $dom->createCDATASection($name_content);
$name_node->appendChild($name_cdata); $name_node->appendChild($name_cdata);
my $description_node = add_empty_child($placemark, 'description');
my $description_content = $coordinates;
my $description_cdata = $dom->createCDATASection($description_content);
$description_node->appendChild($description_cdata);
my $style_node = add_empty_child($placemark, 'styleUrl'); my $style_node = add_empty_child($placemark, 'styleUrl');
$style_node->appendText('#jackpoint-logo'); $style_node->appendText('#jackpoint-logo');

@ -12,14 +12,16 @@
</LabelStyle> </LabelStyle>
</Style> </Style>
<Placemark id="mctavishs_oldstyle_tavern"> <Placemark id="mctavishs_oldstyle_tavern">
<name><![CDATA[<a href="https://jackpoint.obscuritus.ca/index.php?title=McTavish%27s_Old-Style_Tavern">McTavish's Old-Style Tavern</a>]]></name> <name><![CDATA[<a href="https://jackpoint.obscuritus.ca/index.php?title=McTavish%27s%20Old-Style%20Tavern">McTavish's Old-Style Tavern</a>]]></name>
<description><![CDATA[-122.07850635051729,47.67988678685347]]></description>
<styleUrl>#jackpoint-logo</styleUrl> <styleUrl>#jackpoint-logo</styleUrl>
<Point> <Point>
<coordinates>-122.07850635051729,47.67988678685347,0</coordinates> <coordinates>-122.07850635051729,47.67988678685347</coordinates>
</Point> </Point>
</Placemark> </Placemark>
<Placemark id="jynxs_clinic"> <Placemark id="jynxs_clinic">
<name><![CDATA[<a href="https://jackpoint.obscuritus.ca/index.php?title=Jynx%27s_Clinic">Jynx's Clinic</a>]]></name> <name><![CDATA[<a href="https://jackpoint.obscuritus.ca/index.php?title=Jynx%27s%20Clinic">Jynx's Clinic</a>]]></name>
<description><![CDATA[-122.131861,47.704018]]></description>
<styleUrl>#jackpoint-logo</styleUrl> <styleUrl>#jackpoint-logo</styleUrl>
<Point> <Point>
<coordinates>-122.131861,47.704018</coordinates> <coordinates>-122.131861,47.704018</coordinates>

Loading…
Cancel
Save