From 42e533b53cbcce3442b51ceefa65fb526034ab92 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 18:58:11 -0600 Subject: [PATCH 1/8] kml.pl: take parameters, write to stdout --- kml.pl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kml.pl b/kml.pl index ac644e3..465eb60 100755 --- a/kml.pl +++ b/kml.pl @@ -7,18 +7,17 @@ use warnings; use XML::LibXML; use Data::Dumper; +my ($name, $coordinates, $article) = @ARGV; + my $filename = 'ours.kml'; my $dom = XML::LibXML->load_xml(location => $filename, no_blanks => 1); my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs('k', "http://earth.google.com/kml/2.2"); -append_placemark("Fo'o", '-122,47', 'Fo%27o'); -append_placemark("Bar", '-122.5,47.5'); +append_placemark($name, $coordinates, $article); # say $dom->toString(1); -open TEST_KML, ">test.kml"; -print TEST_KML $dom->toString(1); -close TEST_KML; +print $dom->toString(1); sub add_empty_child { From 29fd19132599c666b5a83b0bf997c6dc8ab2496a Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 19:36:04 -0600 Subject: [PATCH 2/8] kml.pl: Adding placemarks works --- kml.pl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kml.pl b/kml.pl index 465eb60..70a06b4 100755 --- a/kml.pl +++ b/kml.pl @@ -5,18 +5,17 @@ use strict; use warnings; use XML::LibXML; -use Data::Dumper; +use URI::Escape; -my ($name, $coordinates, $article) = @ARGV; +my ($name, $coordinates) = @ARGV; my $filename = 'ours.kml'; my $dom = XML::LibXML->load_xml(location => $filename, no_blanks => 1); my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs('k', "http://earth.google.com/kml/2.2"); -append_placemark($name, $coordinates, $article); +append_placemark($name, $coordinates, uri_escape($name)); -# say $dom->toString(1); print $dom->toString(1); sub add_empty_child @@ -50,6 +49,11 @@ sub append_placemark my $name_cdata = $dom->createCDATASection($name_content); $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'); $style_node->appendText('#jackpoint-logo'); From d0e398f974a7fd28f6b3889083b406d4bd8a817f Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 19:36:50 -0600 Subject: [PATCH 3/8] kml.pl: Stub remove, set and usage modes --- kml.pl | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/kml.pl b/kml.pl index 70a06b4..5551892 100755 --- a/kml.pl +++ b/kml.pl @@ -7,17 +7,42 @@ use warnings; use XML::LibXML; use URI::Escape; -my ($name, $coordinates) = @ARGV; - my $filename = 'ours.kml'; my $dom = XML::LibXML->load_xml(location => $filename, no_blanks => 1); my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs('k', "http://earth.google.com/kml/2.2"); -append_placemark($name, $coordinates, uri_escape($name)); - -print $dom->toString(1); +my $mode = shift; +if ($mode eq 'add') +{ + my ($name, $coordinates) = @ARGV; + unless (defined($name) && defined($coordinates)) + { + say "Not enough args for add operation"; + USAGE(1); + } + append_placemark($name, $coordinates, uri_escape($name)); + print $dom->toString(1); +} +elsif ($mode eq 'remove') +{ + say "Remove mode"; +} +elsif ($mode eq 'set') +{ + say "Set mode"; +} +else +{ + USAGE(1); +} +sub USAGE +{ + my $code = shift; + say "USAGE: "; + exit($code); +} sub add_empty_child { my $node = shift; From 966018aab231cbebdfa49b7a65cb7f455cc6a775 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 20:03:30 -0600 Subject: [PATCH 4/8] kml.pl: Can remove placemarks --- kml.pl | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/kml.pl b/kml.pl index 5551892..a64f0cd 100755 --- a/kml.pl +++ b/kml.pl @@ -13,6 +13,10 @@ my $xpc = XML::LibXML::XPathContext->new($dom); $xpc->registerNs('k', "http://earth.google.com/kml/2.2"); my $mode = shift; +unless(defined($mode)) +{ + USAGE(); +} if ($mode eq 'add') { my ($name, $coordinates) = @ARGV; @@ -26,7 +30,26 @@ if ($mode eq 'add') } elsif ($mode eq 'remove') { - say "Remove mode"; + # say "Remove mode"; + my ($name) = @ARGV; + 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); + } + # say $placemark->toString(1); } elsif ($mode eq 'set') { @@ -34,6 +57,7 @@ elsif ($mode eq 'set') } else { + say "Invalid operation"; USAGE(1); } @@ -41,7 +65,7 @@ sub USAGE { my $code = shift; say "USAGE: "; - exit($code); + exit($code || 0); } sub add_empty_child { From 8ef25466781e9e3bf932e12d29afdc85f9dfdeba Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 20:25:36 -0600 Subject: [PATCH 5/8] Add coords to Description in ours.kml --- ours.kml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ours.kml b/ours.kml index f3f9b70..2b16df1 100644 --- a/ours.kml +++ b/ours.kml @@ -12,14 +12,16 @@ - McTavish's Old-Style Tavern]]> + McTavish's Old-Style Tavern]]> + #jackpoint-logo - -122.07850635051729,47.67988678685347,0 + -122.07850635051729,47.67988678685347 - Jynx's Clinic]]> + Jynx's Clinic]]> + #jackpoint-logo -122.131861,47.704018 From d652656be6e9502eed0b5a807a42e666c2207031 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 20:27:04 -0600 Subject: [PATCH 6/8] kml.pl: don't add duplicate places --- kml.pl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/kml.pl b/kml.pl index a64f0cd..6454831 100755 --- a/kml.pl +++ b/kml.pl @@ -17,6 +17,7 @@ unless(defined($mode)) { USAGE(); } + if ($mode eq 'add') { my ($name, $coordinates) = @ARGV; @@ -25,12 +26,19 @@ if ($mode eq 'add') 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); } + elsif ($mode eq 'remove') { - # say "Remove mode"; my ($name) = @ARGV; unless (defined($name)) { @@ -49,12 +57,13 @@ elsif ($mode eq 'remove') say "$name was not found in $filename"; exit(1); } - # say $placemark->toString(1); } + elsif ($mode eq 'set') { say "Set mode"; } + else { say "Invalid operation"; From 53ed9141de1567ddc83f85dc4ca3096d1094d7af Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 21:07:40 -0600 Subject: [PATCH 7/8] kml.pl: Implement set operation --- kml.pl | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/kml.pl b/kml.pl index 6454831..32c8180 100755 --- a/kml.pl +++ b/kml.pl @@ -61,7 +61,27 @@ elsif ($mode eq 'remove') elsif ($mode eq 'set') { - say "Set mode"; + 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 From 7de5c409fea2759c4f7d4b7641c74acaf01b8859 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Wed, 25 Nov 2020 21:28:52 -0600 Subject: [PATCH 8/8] kml.pl: add usage --- kml.pl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kml.pl b/kml.pl index b06ba99..510e716 100755 --- a/kml.pl +++ b/kml.pl @@ -94,6 +94,21 @@ sub USAGE { my $code = shift; say "USAGE: "; + say ""; + say "kml.pl add "; + say ""; + say " Adds a new placemark to ours.kml for at ."; + say " The name will automatically link to page on https://jackpoint.obscuritus.ca/"; + say ""; + say ""; + say "kml.pl set "; + say ""; + say " Sets the coordinates of the placemark to ."; + say ""; + say ""; + say "kml.pl remove "; + say ""; + say " Removes the placemark"; exit($code || 0); } sub add_empty_child