obsidian-map-view icon indicating copy to clipboard operation
obsidian-map-view copied to clipboard

Allow for KML parsing

Open mofosyne opened this issue 2 years ago • 5 comments

I noticed that in google earth you can copy locations as KML, it typically looks like this

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Placemark id="1.2.1">
	<name>44 Martin Pl</name>
	<address>44 Martin Pl, Sydney NSW 2000</address>
	<snippet>44 Martin Pl, Sydney NSW 2000</snippet>
	<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-33.8673204&lng=151.20955569999998&fid=0x6b12ae402314345f:0xbbdef08fbbf2089&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
	<styleUrl>#geocode</styleUrl>
	<ExtendedData>
		<Data name="placepageUri">
			<value>https://www.google.com/earth/rpc/entity?lat=-33.8673204&amp;lng=151.20955569999998&amp;fid=0x6b12ae402314345f:0xbbdef08fbbf2089&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
		</Data>
	</ExtendedData>
	<MultiGeometry>
		<Point>
			<coordinates>151.2095557,-33.8673204,0</coordinates>
		</Point>
		<LinearRing>
			<coordinates>
				151.205922688165,-33.8700451588762,0 151.205922688165,-33.8645956411238,0 151.213188711835,-33.8645956411238,0 151.213188711835,-33.8700451588762,0 151.205922688165,-33.8700451588762,0 
			</coordinates>
		</LinearRing>
	</MultiGeometry>
</Placemark>
</kml>

Might be worth being able to parse this to generate a front matter or geolink

mofosyne avatar Dec 08 '23 07:12 mofosyne

For now, I created this quick converter script which generates a markdown table from the KML script. Might be worth

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KML to Markdown Converter</title>
</head>
<body>
    <h1>KML to Markdown Converter</h1>
    <p>Paste your KML XML content in the textbox below:</p>
    <textarea id="kmlInput" rows="10" cols="80"></textarea>
    <button onclick="convertToMarkdown()">Convert to Markdown</button>
    <div id="markdownOutput"></div>

    <!-- https://developers.google.com/kml/documentation/kmlreference#placemark
        <Placemark id="ID">
            <name>...</name>                             // string 
            <visibility>1</visibility>                   // boolean 
            <open>0</open>                               // boolean 
            <atom:author>...<atom:author>                // xmlns:atom 
            <atom:link href=" "/>                        // xmlns:atom 
            <address>...</address>                       // string 
            <xal:AddressDetails>...</xal:AddressDetails> // xmlns:xal 
            <phoneNumber>...</phoneNumber>               // string 
            <Snippet maxLines="2">...</Snippet>          // string 
            <description>...</description>               // string 
            <AbstractView>...</AbstractView>             // Camera or LookAt 
            <TimePrimitive>...</TimePrimitive>
            <styleUrl>...</styleUrl>                     // anyURI 
            <StyleSelector>...</StyleSelector>
            <Region>...</Region>
            <Metadata>...</Metadata>                     // deprecated in KML 2.2 
            <ExtendedData>...</ExtendedData>             // new in KML 2.2 
            <Geometry>...</Geometry>
        </Placemark>
    -->

    <script>
        function convertToMarkdown() {
            function formatCoordinates(coordinates) {
                const [longitude, latitude] = coordinates.split(',').map(coord => coord.trim());
                return `${latitude},${longitude}`;
            }

            const kmlInput = document.getElementById('kmlInput').value;
            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(kmlInput, 'application/xml');
            const title = xmlDoc.querySelector('Folder > name').textContent;

            let markdownOutput = `# ${title}\n`;
            markdownOutput += `\n`
            markdownOutput += `| Name | Phone Number | Address |\n`
            markdownOutput += `| ---  | ---          | ---     |\n`
            
            const placemarks = xmlDoc.querySelectorAll('Placemark');
            placemarks.forEach((placemark, index) => {
                let elementToText = (elem) => elem == null ? "" : elem.textContent;
                const nameElement        = placemark.querySelector('name');
                const addressElement     = placemark.querySelector('address');
                const phoneNumberElement = placemark.querySelector('phoneNumber');
                const coordinatesElement = placemark.querySelector('coordinates');

                const name        = elementToText(nameElement       );
                const address     = elementToText(addressElement    );
                const phoneNumber = elementToText(phoneNumberElement);
                const coordinates = coordinatesElement == null ? "" : formatCoordinates(coordinatesElement.textContent);

                markdownOutput += `| ${name} | ${phoneNumber} | [${address}](geo:${coordinates})\n`;
            });

            document.getElementById('markdownOutput').innerHTML = `<h2>Markdown Output:</h2><pre>${markdownOutput}</pre>`;
        }
    </script>
</body>
</html>

Which would read

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder id="results">
	<name>hub australia</name>
	<Placemark id="1.2.1">
		<name>Hub Collins Street</name>
		<address>Level 3/162 Collins St, Melbourne VIC 3000</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 3/162 Collins St, Melbourne VIC 3000</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad643d15fadaf3d:0xdf91aeb71603ce9f&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_A</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad643d15fadaf3d:0xdf91aeb71603ce9f&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9678749,-37.8144843,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.3.1">
		<name>Hub Flinders Street</name>
		<address>Level 7/180 Flinders St, Melbourne VIC 3000</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 7/180 Flinders St, Melbourne VIC 3000</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad643aa45cb53af:0xe1ac677ad764eadd&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_B</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad643aa45cb53af:0xe1ac677ad764eadd&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9686598,-37.8169355,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.4.1">
		<name>Hub Southern Cross</name>
		<address>Level 2/696 Bourke St, Melbourne VIC 3000</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 2/696 Bourke St, Melbourne VIC 3000</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad65d4e4189fb87:0xb8eff891005465a&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_C</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad65d4e4189fb87:0xb8eff891005465a&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9535917,-37.816691,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.5.1">
		<name>Hub Parliament Station</name>
		<address>Level 18/1 Nicholson St, East Melbourne VIC 3002</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 18/1 Nicholson St, East Melbourne VIC 3002</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad643609f5ab93f:0x7e9bbb15d43338df&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_D</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad643609f5ab93f:0x7e9bbb15d43338df&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9734425,-37.8089915,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.6.1">
		<name>Hub St Kilda Road</name>
		<address>Level 12/412 St Kilda Rd, Melbourne VIC 3004</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 12/412 St Kilda Rd, Melbourne VIC 3004</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad643f56dff9ae1:0xc60ae81d710ffb7&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_E</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad643f56dff9ae1:0xc60ae81d710ffb7&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9743061,-37.8350387,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.7.1">
		<name>The Hub at Docklands</name>
		<address>80 Harbour Esplanade, Docklands VIC 3008</address>
		<phoneNumber>(03) 8622 4822</phoneNumber>
		<snippet>80 Harbour Esplanade, Docklands VIC 3008</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad65d5bb24bf621:0x7233bc6937d462f9&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_F</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad65d5bb24bf621:0x7233bc6937d462f9&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9471592,-37.8195936,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.8.1">
		<name>Hub Church Street</name>
		<address>Level 4/459 Church St, Richmond VIC 3121</address>
		<phoneNumber>1300 482 611</phoneNumber>
		<snippet>Level 4/459 Church St, Richmond VIC 3121</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad64339b09f8a89:0x508e720eff0f08b6&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_G</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad64339b09f8a89:0x508e720eff0f08b6&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.997899,-37.8273332,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.9.1">
		<name>Community Hubs Australia</name>
		<address>31/367 Collins St, Melbourne VIC 3000</address>
		<phoneNumber>(03) 8614 3430</phoneNumber>
		<snippet>31/367 Collins St, Melbourne VIC 3000</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad642b35e52a7f7:0x94b97db7273bcf1&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_H</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad642b35e52a7f7:0x94b97db7273bcf1&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9624633,-37.8171697,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.10.1">
		<name>Hub@Exhibition</name>
		<address>113 Exhibition St, Melbourne VIC 3004</address>
		<snippet>113 Exhibition St, Melbourne VIC 3004</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad642c85fea5e43:0xa13b380e9ecc4719&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_I</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad642c85fea5e43:0xa13b380e9ecc4719&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9707397,-37.8128878,0</coordinates>
		</Point>
	</Placemark>
	<Placemark id="1.11.1">
		<name>The Hub (Building 863)</name>
		<address>Southbank VIC 3006</address>
		<snippet>Southbank VIC 3006</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.8189&lng=144.959&fid=0x6ad64342cfeedc2b:0x90da6341d0ab65bd&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_J</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.8189&amp;lng=144.959&amp;fid=0x6ad64342cfeedc2b:0x90da6341d0ab65bd&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<Point>
			<coordinates>144.9693026,-37.8243822,0</coordinates>
		</Point>
	</Placemark>
</Folder>
</kml>

and convert it to something like

| Name | Phone Number | Address |
| ---  | ---          | ---     |
| Hub Collins Street | 1300 482 611 | [Level 3/162 Collins St, Melbourne VIC 3000](geo:-37.8144843,144.9678749)
| Hub Flinders Street | 1300 482 611 | [Level 7/180 Flinders St, Melbourne VIC 3000](geo:-37.8169355,144.9686598)
| Hub Southern Cross | 1300 482 611 | [Level 2/696 Bourke St, Melbourne VIC 3000](geo:-37.816691,144.9535917)
| Hub Parliament Station | 1300 482 611 | [Level 18/1 Nicholson St, East Melbourne VIC 3002](geo:-37.8089915,144.9734425)
| Hub St Kilda Road | 1300 482 611 | [Level 12/412 St Kilda Rd, Melbourne VIC 3004](geo:-37.8350387,144.9743061)
| The Hub at Docklands | (03) 8622 4822 | [80 Harbour Esplanade, Docklands VIC 3008](geo:-37.8195936,144.9471592)
| Hub Church Street | 1300 482 611 | [Level 4/459 Church St, Richmond VIC 3121](geo:-37.8273332,144.997899)
| Community Hubs Australia | (03) 8614 3430 | [31/367 Collins St, Melbourne VIC 3000](geo:-37.8171697,144.9624633)
| Hub@Exhibition |  | [113 Exhibition St, Melbourne VIC 3004](geo:-37.8128878,144.9707397)
| The Hub (Building 863) |  | [Southbank VIC 3006](geo:-37.8243822,144.9693026)

e.g.

Name Phone Number Address
Hub Collins Street 1300 482 611 Level 3/162 Collins St, Melbourne VIC 3000
Hub Flinders Street 1300 482 611 Level 7/180 Flinders St, Melbourne VIC 3000
Hub Southern Cross 1300 482 611 Level 2/696 Bourke St, Melbourne VIC 3000
Hub Parliament Station 1300 482 611 Level 18/1 Nicholson St, East Melbourne VIC 3002
Hub St Kilda Road 1300 482 611 Level 12/412 St Kilda Rd, Melbourne VIC 3004
The Hub at Docklands (03) 8622 4822 80 Harbour Esplanade, Docklands VIC 3008
Hub Church Street 1300 482 611 Level 4/459 Church St, Richmond VIC 3121
Community Hubs Australia (03) 8614 3430 31/367 Collins St, Melbourne VIC 3000
Hub@Exhibition 113 Exhibition St, Melbourne VIC 3004
The Hub (Building 863) Southbank VIC 3006

Not sure what people would typically want, but this might be a good starting point.

mofosyne avatar Dec 08 '23 14:12 mofosyne

Noticed that mapview doesn't deal well with markdown tables... so here's an updated version with markdown list instead

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KML to Markdown Converter</title>
</head>
<body>
    <h1>KML to Markdown Converter</h1>
    <p>Paste your KML XML content in the textbox below:</p>
    <textarea id="kmlInput" rows="10" cols="80"></textarea>
    <button onclick="convertToMarkdown()">Convert to Markdown</button>
    <div id="markdownOutput"></div>

    <!-- https://developers.google.com/kml/documentation/kmlreference#placemark
        <Placemark id="ID">
            <name>...</name>                             // string 
            <visibility>1</visibility>                   // boolean 
            <open>0</open>                               // boolean 
            <atom:author>...<atom:author>                // xmlns:atom 
            <atom:link href=" "/>                        // xmlns:atom 
            <address>...</address>                       // string 
            <xal:AddressDetails>...</xal:AddressDetails> // xmlns:xal 
            <phoneNumber>...</phoneNumber>               // string 
            <Snippet maxLines="2">...</Snippet>          // string 
            <description>...</description>               // string 
            <AbstractView>...</AbstractView>             // Camera or LookAt 
            <TimePrimitive>...</TimePrimitive>
            <styleUrl>...</styleUrl>                     // anyURI 
            <StyleSelector>...</StyleSelector>
            <Region>...</Region>
            <Metadata>...</Metadata>                     // deprecated in KML 2.2 
            <ExtendedData>...</ExtendedData>             // new in KML 2.2 
            <Geometry>...</Geometry>
        </Placemark>
    -->

    <script>
        function convertToMarkdown() {
            function formatCoordinates(coordinates) {
                const [longitude, latitude] = coordinates.split(',').map(coord => coord.trim());
                return `${latitude},${longitude}`;
            }

            const kmlInput = document.getElementById('kmlInput').value;
            const parser = new DOMParser();
            const xmlDoc = parser.parseFromString(kmlInput, 'application/xml');
            const title = xmlDoc.querySelector('Folder > name').textContent;

            let markdownOutput = `# Converted KML Markdown Document\n\n`;
            markdownOutput += `## ${title}\n\n`;

            const placemarks = xmlDoc.querySelectorAll('Placemark');
            placemarks.forEach((placemark, index) => {
                let elementToText = (elem) => elem == null ? "" : elem.textContent;
                const nameElement        = placemark.querySelector('name');
                const addressElement     = placemark.querySelector('address');
                const phoneNumberElement = placemark.querySelector('phoneNumber');
                const coordinatesElement = placemark.querySelector('coordinates');

                const name        = elementToText(nameElement       );
                const address     = elementToText(addressElement    );
                const phoneNumber = elementToText(phoneNumberElement);
                const coordinates = coordinatesElement == null ? "" : formatCoordinates(coordinatesElement.textContent);

                markdownOutput += ` - [${name}](geo:${coordinates})\n`;
                if (addressElement != null)
                {
                    markdownOutput += `     - Address: ${address}\n`;
                }
                if (phoneNumberElement != null)
                {
                    markdownOutput += `     - Phone Number: ${phoneNumber}\n`;
                }
            });

            document.getElementById('markdownOutput').innerHTML = `<h2>Markdown Output:</h2><pre>${markdownOutput}</pre>`;
        }
    </script>
</body>
</html>

This approach would render below

# Converted KML Markdown Document

## hub australia

 - [Hub Collins Street](geo:-37.8144843,144.9678749)
     - Address: Level 3/162 Collins St, Melbourne VIC 3000
     - Phone Number: 1300 482 611
 - [Hub Flinders Street](geo:-37.8169355,144.9686598)
     - Address: Level 7/180 Flinders St, Melbourne VIC 3000
     - Phone Number: 1300 482 611
 - [Hub Southern Cross](geo:-37.816691,144.9535917)
     - Address: Level 2/696 Bourke St, Melbourne VIC 3000
     - Phone Number: 1300 482 611
 - [Hub Parliament Station](geo:-37.8089915,144.9734425)
     - Address: Level 18/1 Nicholson St, East Melbourne VIC 3002
     - Phone Number: 1300 482 611
 - [Hub St Kilda Road](geo:-37.8350387,144.9743061)
     - Address: Level 12/412 St Kilda Rd, Melbourne VIC 3004
     - Phone Number: 1300 482 611
 - [The Hub at Docklands](geo:-37.8195936,144.9471592)
     - Address: 80 Harbour Esplanade, Docklands VIC 3008
     - Phone Number: (03) 8622 4822
 - [Hub Church Street](geo:-37.8273332,144.997899)
     - Address: Level 4/459 Church St, Richmond VIC 3121
     - Phone Number: 1300 482 611
 - [Community Hubs Australia](geo:-37.8171697,144.9624633)
     - Address: 31/367 Collins St, Melbourne VIC 3000
     - Phone Number: (03) 8614 3430
 - [Hub@Exhibition](geo:-37.8128878,144.9707397)
     - Address: 113 Exhibition St, Melbourne VIC 3004
 - [The Hub (Building 863)](geo:-37.8243822,144.9693026)
     - Address: Southbank VIC 3006

Converted KML Markdown Document

hub australia

  • Hub Collins Street
    • Address: Level 3/162 Collins St, Melbourne VIC 3000
    • Phone Number: 1300 482 611
  • Hub Flinders Street
    • Address: Level 7/180 Flinders St, Melbourne VIC 3000
    • Phone Number: 1300 482 611
  • Hub Southern Cross
    • Address: Level 2/696 Bourke St, Melbourne VIC 3000
    • Phone Number: 1300 482 611
  • Hub Parliament Station
    • Address: Level 18/1 Nicholson St, East Melbourne VIC 3002
    • Phone Number: 1300 482 611
  • Hub St Kilda Road
    • Address: Level 12/412 St Kilda Rd, Melbourne VIC 3004
    • Phone Number: 1300 482 611
  • The Hub at Docklands
    • Address: 80 Harbour Esplanade, Docklands VIC 3008
    • Phone Number: (03) 8622 4822
  • Hub Church Street
    • Address: Level 4/459 Church St, Richmond VIC 3121
    • Phone Number: 1300 482 611
  • Community Hubs Australia
    • Address: 31/367 Collins St, Melbourne VIC 3000
    • Phone Number: (03) 8614 3430
  • Hub@Exhibition
    • Address: 113 Exhibition St, Melbourne VIC 3004
  • The Hub (Building 863)
    • Address: Southbank VIC 3006

mofosyne avatar Dec 08 '23 15:12 mofosyne

That's awesome, thanks for sharing!

ParkerRobb avatar Dec 08 '23 18:12 ParkerRobb

Additional note. There is a different KML representation when you select a single KML entry from the search result.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Placemark id="1.2.1">
	<name>Little cardboard cafe</name>
	<address>356 Pascoe Vale Rd, Essendon VIC 3040</address>
	<snippet>356 Pascoe Vale Rd, Essendon VIC 3040</snippet>
	<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.743784&lng=144.9282409&fid=0x6ad65bc05ea7c86b:0x69abc64e099aa320&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
	<styleUrl>#listing_A</styleUrl>
	<ExtendedData>
		<Data name="placepageUri">
			<value>https://www.google.com/earth/rpc/entity?lat=-37.743784&amp;lng=144.9282409&amp;fid=0x6ad65bc05ea7c86b:0x69abc64e099aa320&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
		</Data>
	</ExtendedData>
	<MultiGeometry>
		<Point>
			<coordinates>144.9282409,-37.743784,0</coordinates>
		</Point>
		<LinearRing>
			<coordinates>
				144.924781046958,-37.7463788897818,0 144.924781046958,-37.7411891102182,0 144.931700753042,-37.7411891102182,0 144.931700753042,-37.7463788897818,0 144.924781046958,-37.7463788897818,0 
			</coordinates>
		</LinearRing>
	</MultiGeometry>
</Placemark>
</kml>

vs when selecting the entire kml search result

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder id="results">
	<name>little cardboard cafe</name>
	<Placemark id="1.2.1">
		<name>Little cardboard cafe</name>
		<address>356 Pascoe Vale Rd, Essendon VIC 3040</address>
		<snippet>356 Pascoe Vale Rd, Essendon VIC 3040</snippet>
		<description><![CDATA[<!DOCTYPE html><html><head></head><body><script type="text/javascript">window.location.href="https://www.google.com/earth/rpc/entity?lat=-37.743784&lng=144.9282409&fid=0x6ad65bc05ea7c86b:0x69abc64e099aa320&hl=en&gl=au&client=earth-client&cv=7.3.6.9345&useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)";</script></body></html>]]></description>
		<styleUrl>#listing_A</styleUrl>
		<ExtendedData>
			<Data name="placepageUri">
				<value>https://www.google.com/earth/rpc/entity?lat=-37.743784&amp;lng=144.9282409&amp;fid=0x6ad65bc05ea7c86b:0x69abc64e099aa320&amp;hl=en&amp;gl=au&amp;client=earth-client&amp;cv=7.3.6.9345&amp;useragent=GoogleEarth/7.3.6.9345(X11;Linux (5.15.0.0);en;kml:2.2;client:Pro;type:default)</value>
			</Data>
		</ExtendedData>
		<MultiGeometry>
			<Point>
				<coordinates>144.9282409,-37.743784,0</coordinates>
			</Point>
			<LinearRing>
				<coordinates>
					144.924781046958,-37.7463788897818,0 144.924781046958,-37.7411891102182,0 144.931700753042,-37.7411891102182,0 144.931700753042,-37.7463788897818,0 144.924781046958,-37.7463788897818,0 
				</coordinates>
			</LinearRing>
		</MultiGeometry>
	</Placemark>
</Folder>
</kml>

main difference is the presence of these tags

<Folder id="results">
	<name>little cardboard cafe</name>

so your pasting would need to account for both

mofosyne avatar Dec 18 '23 08:12 mofosyne

In the meantime before this feature gets integrated into this mapview plugin, I've placed the tool to a permalinked portion of my site.

https://briankhuu.com/tools/kml-to-markdown/

mofosyne avatar Mar 22 '24 00:03 mofosyne

@mofosyne do I have your permission to integrate this into Map View as originally discussed? I'm finally getting around to it. If so, from where should I get the most up-to-date version of the script?

esm7 avatar Dec 26 '24 17:12 esm7

Yes feel free to copy it over. You may want to adapt it as I do think it's not the best approach, but see what you can do!

mofosyne avatar Dec 26 '24 18:12 mofosyne

No change to code so can just use the code here. But do consider adjusting it as needed

mofosyne avatar Dec 26 '24 18:12 mofosyne

Thank you!

esm7 avatar Dec 26 '24 19:12 esm7

Released in 5.5.0, thank you for the contribution here!

esm7 avatar Jan 22 '25 16:01 esm7