2

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>&lt;h4&gt;namehere&lt;/h4&gt;
&lt;ul class="textattributes"&gt;
  &lt;li&gt;&lt;strong&gt;&lt;span class="atr-name"&gt;id&lt;/span&gt;:&lt;/strong&gt; &lt;span class="atr-value"&gt;15L&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;span class="atr-name"&gt;issuetime&lt;/span&gt;:&lt;/strong&gt; &lt;span class="atr-value"&gt;Oct 14, 2016 8:39:56 AM&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;span class="atr-name"&gt;stormname&lt;/span&gt;:&lt;/strong&gt; &lt;span class="atr-value"&gt;NICOLE&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;span class="atr-name"&gt;validtime&lt;/span&gt;:&lt;/strong&gt; &lt;span class="atr-value"&gt;Oct 14, 2016 6:00:00 AM&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;span class="atr-name"&gt;windspeed&lt;/span&gt;:&lt;/strong&gt; &lt;span class="atr-value"&gt;64&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
</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.

6
  • If you're looking for a tool, maybe try FME. I've had good results with both KML and JSON transformations. Should work on Linux. Oct 17, 2016 at 19:40
  • I hadn't heard of FME before. Interesting. Looks a bit too heavy for my use, but it's good to know what's out there. Thanks!
    – Brian Pipa
    Oct 18, 2016 at 16:45
  • Well, I do not know how the final geojson format is supposed to look. But here is how to convert the encoded html from the description to a json stream in one command: xidel feed.kml -e '//description/parse-html(.)//ul/li/{substring-before(strong, ":"): span}'
    – BeniBela
    Nov 10, 2016 at 19:06
  • @BeniBela - that looks like a great solution, but it doesn't work. I get this error: Error: err:XPST0003: Invalid NCName: 'strong‌​' in: //description/parse-html(.)//ul/li/{substring-before(strong‌​ [<- error occurs before here] , ":"): span}
    – Brian Pipa
    Nov 14, 2016 at 15:20
  • So weird. You need to write g, yourself, instead copying it, there is an invisible space U+200b in my comment
    – BeniBela
    Nov 14, 2016 at 22:52

1 Answer 1

0

I ended up just writing a small Java class to read in the json file and fix things and that works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.