Convert KML to GeoJSON

Maps. The most basic of all data visualizations. Fun apps like CartoDB and Google Fusion Tables have made map building accessible to even the most primitive computer user. But how do you get a TON of data into a map? Well Google caps the size of the KML file you can layer into your map to keep congestion on their KML rendering service down (KML layers call back to google for rendering and then google delivers the rendered result back to your map). Third-party plugins for rendering KML on your own service can bog your server down if you haven’t properly cached results, have a ton of traffic or are feeding very large files to the service. But GeoJSON solves these issues by being light and client-side (beautiful, I know).

But, shoot, now I have a ton of legacy data in KML, how can easily convert it to GeoJSON?

  1. Download some software
    1. Open source titles like QGis allow you to open most GIS formats and export them as GeoJSON. But these titles often require you install software dependencies like python packages and OS updates
  2. Find an online tool which converts the format for you
    1. There are a ton out there. Ogre is one of them, but it’s a little bit clunky and can get bogged down
  3. Use JSONify by TC McCarthy 

Demand in my professional life called for a node script I developed to be made publicly available with a front end for other staff members to use. I figured, why stop there, and made the whole thing open source. Currently JSONify allows you to convert KML or CSV files (must have a latitude/longitude column) into GeoJSON. The tool automatically figures out the geospatial data and then puts any extra columns in a “properties” subobject, which is pretty standard. To test the results I put my CSV or KML through the tool and then pass the resulting download into Google’s drag and drop tool and if it all checks out, bam, my GeoJSON is ready.

Nerding out

So, while this tool is quite useful that’s not really what I feel makes it so valuable. The application is written entirely in NodeJS, with Express and the Twitter Bootstrap. It took about 10 hours total to develop as NodeJS lends itself to rapid development. It’s backed by Mapbox’s toGeoJSON and csv2geojson npm packages and some logic from yours truly. So what’s so exciting about all of that? It’s lightening fast because… not a single byte is written to disk. EVERYTHING happens in memory. Your file upload, the conversion process and the returned download are all streams and buffers ¯\_(ツ)_/¯

Did you say this open source?

In fact, I did. Not every use-case is going to be exactly the same, plus you may have need to bring the application in house as my hosted version could get busy at some point (dare I say, it may need a queue on day). But you can run it yourself by cloning it from Github into the directory you want it to run from. Then just CD into the directory, run sudo npm install and once it’s done you have all of your dependencies (as long as you had node and npm installed to begin with). From there, copy config-sample.js to config.js, modify it if you need to, and then fire the app up by running node app.js. If you want it to run indefinitely I like to use forever (sudo npm install forever -g). Just simple run forever start app.js and the app is service. If you want to make is public facing or web accessible you should use nginx as your web server (all the cool kids do).

Sample nginx config

server {
    listen 80;

    server_name jsonify.*.com; ##this can be any domain/subdomain. The app won't care

    location / {
        proxy_pass; ##you should change 3000 to be whatever port you set in your config.js if you changed it.
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;