I have KML generated by a WMS (Web Map Service). I need this KML in geojson format and I'm doing this all on a linux box. My problem is that the description in the KML contains encoded HTML like so (some text changed):
<description><h4>namehere</h4>
<ul class="textattributes">
<li><strong><span class="atr-name">id</span>:</strong> <span class="atr-value">15L</span></li>
<li><strong><span class="atr-name">issuetime</span>:</strong> <span class="atr-value">Oct 14, 2016 8:39:56 AM</span></li>
<li><strong><span class="atr-name">stormname</span>:</strong> <span class="atr-value">NICOLE</span></li>
<li><strong><span class="atr-name">validtime</span>:</strong> <span class="atr-value">Oct 14, 2016 6:00:00 AM</span></li>
<li><strong><span class="atr-name">windspeed</span>:</strong> <span class="atr-value">64</span></li>
</ul>
</description>
Which looks like this after I run it through sed to fix the < and >:
<description><h4>namehere</h4>
<ul class="textattributes">
<li><strong><span class="atr-name">id</span>:</strong> <span class="atr-value">15L</span></li>
<li><strong><span class="atr-name">issuetime</span>:</strong> <span class="atr-value">Oct 14, 2016 8:39:56 AM</span></li>
<li><strong><span class="atr-name">stormname</span>:</strong> <span class="atr-value">NICOLE</span></li>
<li><strong><span class="atr-name">validtime</span>:</strong> <span class="atr-value">Oct 14, 2016 6:00:00 AM</span></li>
<li><strong><span class="atr-name">windspeed</span>:</strong> <span class="atr-value">64</span></li>
</ul>
</description>
So my question is - is there an easy way to get those as properties into the geojson file that gets created when I run ogr2ogr (with ogr2ogr -f "GeoJSON" feed.json feed.kml) on it? My current plan is:
- run it through sed to convert the < and the >
use xidel to grab each piece out of it with: "xidel feed.kml -e /kml/Document/Folder/Placemark/Description |sed '/^\s*$/d'" which will output multiple sections that look like this:
namehere id: 24W issuetime: Oct 14, 2016 8:39:56 AM stormname: NICOLE validtime: Oct 14, 2016 6:00:00 AM windspeed: 64
convert it to geojson with "togeojson feed.xml >feed.json"
- change the description with jq
- somehow loop and insert (using jq) the XML properties from the xidel output
This should work once I get all of these wired up but I can't help but think that there's an easier way. Is there some way to do all of this with a command line switch in ogr2ogr or via some other npm tool or something? My goal is to have the name/value pairs in each properties section, though honestly, if I could just get them somewhere accessible in the json I'd be happy.
UPDATE: I just realized I can use togeojson to convert the KML directly to json and that creates the json with the description containing valid HTML (with some stray \n strings). That doesn't require me to run it through sed. Maybe that would be a better path to pull out and change them into properties. Maybe. I may try that if nobody gives me any good ideas.
xidel feed.kml -e '//description/parse-html(.)//ul/li/{substring-before(strong, ":"): span}'
g,
yourself, instead copying it, there is an invisible space U+200b in my comment