Mapping medical cannabis dispensaries: from PDF table to Google Map with R

Datetime:2016-08-23 01:44:46          Topic: R Program           Share

by Sheri Gilley, Microsoft Senior Software Engineer

In 2014, Illinois passed into law the creation of a medical cannabis pilot program. As my son has cancer and marijuana could greatly help with some of his symptoms, we eagerly applied for a card when registration was available early in 2015. The first dispensaries were not available until November 2015. At that time there were 9 dispensaries; the PDF file with a table of dispensary names and locations provided by the Illinois Department of Health was an adequate way to find a dispensary.

In the time that dispensaries have been available, my son has been in various hospitals and facilities in and around the city of Chicago.  First we were in Park Ridge, then Hyde Park, then Hinsdale, then downtown Chicago and now finally back home in Oak Park.  As we moved around the city, I would use that same PDF file to locate the dispensary closest to me. The list has grown from 9 names and addresses to 40 today. With 40 entries, the PDF table format is not at all useful for showing where the dispensaries are located.  The entries are listed in the order of the license issue date, making it all the more difficult to see which dispensaries might be easiest for me to visit.

So one weekend I decided to create a map of all the current locations.  Keeping in mind that more dispensaries will be available in the future, I wanted to create code that would read the official list of registered dispensaries , so that updates would be easy as more entries were added. 

I knew I could read the text of the file in R using pdftools , and could put the locations onto a google map using googleVis .  The hardest part of the code was trying to filter out the noise included in the text and reliably get the name, address, and phone number of each dispensary into a data frame. A few handy gsub statements worked their magic and I was left with data ready for mapping.  

I added in some geocoding to get the longitude and latitude, thanks to this tip .

Finally, after the data manipulation, the code to produce the map itself is rather straightforward:

# create id  and LatLong for googleVis map
all$id <- paste(all$name, all$address, all$phone, sep='; ')
all$LatLong = paste(result$lat, result$long, sep=":")

# Now plot with googleVis
require(googleVis)

g1 <- gvisMap(all, "LatLong" , "id",
options=list(showTip=TRUE,
showLine=TRUE,
enableScrollWheel=TRUE,
mapType='normal',
width=400, height=400
))

# this opens a browser with the plot
plot(g1)

# write the code that will be used on my website
cat(g1$html$chart, file="dispensariesIL.html")

I thought the map might be useful for others in Illinois also interested in finding a dispensary, so I created a small website for the map, and include a few other tips and tricks I have learned along the way by being the procurer of medicinal cannabis in my household.

Here is the resulting map, generated on August 12, 2016:

You can create this map yourself with this R code





About List