#!/usr/bin/perl # Let's try adding gx:labelVisibility to all the KML (LineStrings). # It makes line labels visible on SOME versions of "Google Earth" on SOME devices. # (I'm not trying any OGR proper method today, if any.) # Input: a KML; Output: same KML with gx:labelVisibility added to each LineString. # Copyright: https://www.gnu.org/licenses/gpl.html # Author: Dan Jacobson https://www.jidanni.org/ # Created: 2026-05-24T23:18:00+0000 # Last-Updated: 2026-05-25T09:09:04+0000 # Update #: 13 use strict; use warnings 'FATAL'; use XML::LibXML; my $I; my %checks; while (<>) { s/(]*/$1/; #I don't know how to deal with namespaces, sorry. $I .= $_; } my $doc = XML::LibXML->load_xml( string => $I ); for my $n ( $doc->findnodes("//Placemark") ) { next unless $n->findnodes("LineString"); for ( $n->findnodes("name") ) { # sure hope it has one my $styleUrl = $doc->createElement('styleUrl'); $styleUrl->appendText("#0"); $n->insertAfter( $styleUrl, $_ ); $checks{styleUrl}++; } } for my $n ( $doc->findnodes("//Placemark/..") ) { for ( $n->findnodes("name") ) { # sure hope it has one my $Style = $doc->createElement('Style'); $Style->setAttribute( 'id', 0 ); my $LineStyle = $doc->createElement('LineStyle'); $Style->appendChild($LineStyle); my $color = $doc->createElement('color'); $color->appendText('ff0000ff'); $LineStyle->appendChild($color); my $glv = $doc->createElement('gx:labelVisibility'); $glv->appendText(1); $LineStyle->appendChild($glv); $n->insertAfter( $Style, $_ ); $checks{Style}++; } } die "Only did " . keys %checks unless keys %checks == 2; for ( $doc->findnodes("/kml") ) { $_->setAttribute( "xmlns", "http://www.opengis.net/kml/2.2" ); $_->setAttribute( "xmlns:gx", "http://www.google.com/kml/ext/2.2" ); } print $doc->toString(1);