Skip to main content

CSV to GeoJSON

In recent months I've been doing a lot of online mapping. From Common Ground to Data Mapper, the maps I've been making have made heavy use of the GeoJSON file format because it is designed for this very purpose. However, local councils mostly publish CSV files. That gives me a conversion problem.

Our Data Mapper tool has already encouraged both Bradford and Calderdale councils to start publishing more GeoJSON files on Data Mill North and Calderdale Data Works but, in general, councils may not have the people or skills to create GeoJSON files themselves. Often it can be hard enough to output as CSV. It was time to help them out a bit.

Today I've created a simple web-based tool that attempts to convert a CSV file into GeoJSON with minimal user input. You select the CSV file and the tool looks for columns that might contain coordinates. It checks the column headings and looks for anything named "Latitude" and "Longitude". If they don't exist it looks for "lat" and "lon" or "easting" and "northing". It'll even look for the "GeoX", "GeoY", and "CoordinateReferenceSystem" columns used by the national standard defined for the brownfield land registers. At the moment it can cope with WGS84 (Latitude/Longitude) and OSGB36 (Eastings/Northings) coordinate systems but there's no reason that couldn't be expanded to more in the future. For now, those two systems cover many of the council datasets I've seen.

The tool displays the first few rows of the CSV file and lets you rename columns if you need to tidy them up. Renaming columns is also useful if the tool failed to spot the geographic columns; name them latitude/longitude and the tool will then find them. You can also choose which fields you want to keep the field in the GeoJSON output. That lets you remove empty or irrelevant columns. Below the table is an interactive map showing all the points that have been found. Clicking on a point brings up a popup displaying its properties. The map is quite helpful because it can quickly show if the coordinates are inverted, offset, or just plain wrong in the CSV file.

Litter bins locations in Leeds. When the file was created the Eastings and Northings columns were accidentally made identical and therefore all the litter bins sit on a diagonal line
Credit: ODI Leeds/Leeds City Council

The GeoJSON output is displayed at the bottom. It is automatically minified and has the coordinate precision reduced to six decimal places to minimise the file size without really losing any information. Then it is merely a matter of saving the file.

Converting a CSV file of Calderdale cycle storage locations into GeoJSONCredit: ODI Leeds/Calderdale Council

My hope is that individuals who have responsibility for publishing CSV files will be able to create GeoJSON files to sit alongside them. No GIS background required.

If you want to contribute, or spot any bugs, the code can be found on Github.