Creating GeoJSON data from CSV and Shapefiles
I got a lot of CSV data from US Census for a recent spatial data project I've been working on. In order to visualize the data using Leaflet or other equivalent tools, we may have to create a GeoJSON data file that we can import into the Javascript code.
I have been exploring zip code level data for this project, so I was able to obtain the shapefile of zip codes from US Census website. The next step is to merge all the other CSV files I got to this shapefile using zip code as the common id.
QGIS has an easy method to perform this. Once we bring in the required shapefile in QGIS, we can add another layer, but this time, we are adding the CSV file as an attribute table only layer:
After we locate the CSV file, another window pops up to provide options for this addition. Here, we have to make sure to select the "No Geometry (attribute table only)" option, so that it will be added as a table to the map.
Now, when we right click on the shapefile and click on 'Properties', we should be able to see the "Joins" function. We can add a new join using the "+" sign at the bottom of this window. Here, we need to select the appropriate column names that QGIS can use to join the table to the shapefile. In this case, I am giving the names of the zip code column data in both data sources.
Once we finish adding that, we can right click and open the attribute table of the shapefile and can see that the CSV file values are merged with the shapefile accordingly.
The next step is to export this new data source to a GeoJSON file.
When we right-click on this shapefile, choose 'save as', it takes us to a new window, where we can choose the format of how we want to save it:
Choose 'GeoJSON' and save the file!