Click here for the story about the gun registry data with the map.
The data from the federal gun registry, obtained and published by La Presse, is a mess. There is no consistency in how gun makes, models and calibres are recorded. Tens of thousands of guns are marked as unknown or left blank altogether. To give an idea, here are just a few ways in which .22 calibre rifle calibres that include the number “22” are categorized:
22 LR
22
223 REM
0.22
22 LR
22 MAG
22 CAL
22LR
22-250 REM
22 SHORT
22 LONG RIFLE
22 CALIBRE
22 LR/410 GA X 3″
22 PELLET
.22 LR
CALIBRE 22
22 LR/22 MAG
22 LONG
CAL 22
22 HORNET
22/410
222 REM
22 RF
22 MAGNUM
22 SLLR
22 SL LR
.22 LONG RIFLE
It’s the same with model an manufacturer names. Holy data cleanup, Batman! Even with the powerful OpenRefine tool, making all records uniform is a fool’s errand, unless you have many days to spare (I don’t).
I cleaned up as much as I could. The semi-refined data for Montreal and Laval is here for those who want it.
However, you can tell that there was some data validation when adding new records. Gun class, type, action and owner were perfectly uniform, although there are cases of miscategorization (e.g. handguns classified as rifles).
So if we’re going to see an acceptably accurate portrait of guns in Montreal, we have to go by those fields.
Preparing the data
I was interested in seeing how many guns per capita were in the different geographies. The gun registry data is anonymized to only show the first two digits of the owner’s postal code. Those digits do not correspond to established geographies. You’d need one more code to get the Forward Sortation Areas (FSA), whose shapefile is available from Statistics Canada.
But we can munge it. We can get the FSA geometries and merge polygons by the first two digits.
But first, the hard part: reshaping the data to give me counts per postal code for things like total guns, gun types ( rifles, handguns, shotguns), gun class (restricted, prohibited), and owner type (individual, business). This took some mix of SQL queries and Excel PivtotTables, depending on my needs. The result was a small CSV with only eight rows: one for each postal code.
Then I got the population data from the 2011 census by FSA and joined that to the FSA shapefiles in ArcMap.
Making the map in ArcMap
Then I isolated just the H postal codes using a definition query in the SQL language. This filters all polygons in the data that begin with H:
(click on images to expand them)
I then created a new field (first_two) in the attribute table and pulled in the first two digits from the CFSAUID column using the Left() function in the field calculator.
Then I ran a dissolve function to merge polygons on a common attribute. I chose the first_two column, and made sure it added up the population counts on the merged polygons.
And here is the result, complete with population:
The rest was easy: joining the gun data that I prepared to the postal codes, and creating a new calculated field of (total guns / population) x 1000.
ArcMap makes it easy to export the map to an ArcGIS Online account and make an interactive web map with it. However, a similar product could be made with the open-source QGIS and exported for web publication with Google Maps, Leaflet, or CartoDB. The result is here.
