Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
What’s this all about
This past November I participated several times in the #30DaysMapChallenge, a daily mapping visualization challenge. While I was satisfied with what I came up with, my main outcome was that I have no idea how to work with maps. Well, to say it differently, I was able to fiddle around and hit home eventually, but my knowledge of Coordinate Referencing Systems (CRS) and other important features was limited. For that purpose, I knew I’ll be back to explore some additional geographic data, leading to the following blog post.
In this blog post I’ll explore Golda Ice-cream locations throughout Israel. My wife and I came across Golda one day and were happily surprised when first tasting it: the ice-cream was fantastic, especially compared to other ice-cream shops we previously visited. Golda has a plethora of ice-cream flavors including fantastic Sorbets, chocolate pretzel, salted cashew, vanilla chocolate pistachio and more. Also, they have this really amazing pretzel sauce you can drizzle on top, what a treat!
To follow through with my motivation to learn more about maps and as a tribute to their great ice-cream I decided to explore Golda’s ice-cream locations. More specifically, we’ll be looking at distances from various points in the country to the nearest ice-cream location. Following a short overview of the data collection and ice-cream locations throughout Israel I will address the utmost important question pertaining distances and Golda locations:
Where do you not want to be if you want an ice-cream now?
I will say that I have no affiliation nor received any compensation for this post. I’m genuinely writing this as a means to learn about maps by exploring ice-cream locations. Stick with me if you want to learn about the nearest ice-cream shop to you.
Before we begin, a big thank you goes to Dominic Roye’s blog post on distances from the shore in Iceland for inspiration and practical help with the code. On a more theoretical level, I enjoyed and learned from Michael Dorman’s ‘Using R for Spatial Data Analysis’ materials from a workshop he gave. I highly recommend exploring both.
In contrast to previous blog posts of mine this one will not display code or raw data. However, I invite you to explore the full source code to learn what’s done under the hood.
Golda location information
I web-scraped Golda’s website to extract relevant information such as street addresses in Hebrew. To get exact locations I geocoded those street addresses using the {ggmap} R package, extracting corresponding latitude & longitude coordinates for each scraped address. I then queried the received longitude and latitude for Golda’s addresses in English to validate the information initially extracted.
Essentially, I geocoded the data twice. Any addresses in English that didn’t match the Hebrew ones I manually checked for the correct location. I identified about 2-3 locations that the extracted information was 250 meters off. Some locations might still not be exact, but overall I think we’re good to go. You can explore the scraping script and complete dataset with English locations for further info.
Our Golda ice-cream dataset looks as follows:
We have a total of 79 locations throughout Israel. Each row represents a store, the city and street it’s located in, their phone number and a corresponding longitude and latitude point. A few stores on the website were noted as currently closed, but for the sake of this post will treat them like all others.
Let’s see where they are across Israel:
A quick overview of our map shows that majority of Golda locations are in the Tel-Aviv metropolitan area (center of Israel). Other locations are scattered across the country with several in the north and a few down south. You can also hover over a location to learn where exactly is it located.
Measuring distances
We (well, I) want to calculate the distances to each Ice-cream location from various points on the map. Doing so requires us to project our map to a different coordinate reference system than the current. That is, our current CRS uses geographic coordinates that account for earth’s curvature, when we want a flat surface to measure distances. Think of it as cutting open a basketball, flattening it and then measuring a distance from two points.
Furthermore, We want to measure distances from various locations on the map to the nearest Golda ice-cream location. However, distance from ‘various locations’ is somewhat abstract; we want the distance from something a little more specific. To tackle that query, we’ll divide the geographical area of Israel into grids.
Grids… What? Let’s have a look:
On the left we have our map of Israel, and on the right the same map after we ‘cut’ it horizontally and vertically. Each grid represents a square polygon (except those on the borders) with specific geographic references to plot it, creating a 2 squared kilometers area (2km\(^2\)). We can measure the distance from the center of each 2km\(^2\) grid to the nearest ice-cream location.
In practical terms, here’s an example of distances from one random grid to a few random Golda locations:
Our small square represents a 2km\(^2\) area somewhere in southern Israel. The lines and corresponding values indicate the distance to each ice-cream location from our example grid. To be specific, I measure the air distance from the center of a grid cell to the various locations. In the above graph our sampled grid is connected with a red line to the nearest ice-cream location located in Eilat, 57km away.
Where’s Our Ice-cream
Once we understand how each grid’s nearest ice-cream location distance is found we can measure accordingly for all grids. Following that we can create a map of our grid cells filled with color indicating the distance to the nearest Golda ice-cream location:
Beautiful! Well, mainly for those living near ice-cream locations.
The main conclusion is you don’t want to be stuck somewhere in southern Israel or south of the dead sea with a sudden craving for ice-cream. We also see that similar to before, those living in the Tel-Aviv metropolitan area are safe with a Golda location near by.
I’ve added a twist to the map in which you can click on a grid cell to see how far is the nearest ice cream location. The distance is measured from the center of a grid so we’ll treat the grid cell as one unit.
We could also level up the map and have the distance measured from a specific location the user clicks on, not a whole grid cell. For that we’ll need something reactable such as an interactive web app (e.g. R Shiny) providing exact distances from a point clicked on the map. It seems pretty straight forward but for the purpose of the post I found the above appropriate and a great learning experience.
Static maps
Version 1
We can visualize it on a static map enabling us to easily share it as an image:
Version 2
Considering the geo-political issues in Israel, which I won’t elaborate here, I’ll share another map with different borders. These borders better reflect the feasibility of individuals to access these locations since people living in, say, Gaza (plotted in previous maps), cannot access any of the locations.
Conclusion
All this talk about ice-cream sure got me craving for some. I hope I didn’t instill too much ice-cream craving in you following this post, but at least now you know where to find the nearest location. I also invite you to explore the full source code or play with the data yourself.
Although a short post, I’ve learned a plenty on using maps, distances and reference systems. From a technical level it required learning about transitioning from various reference systems, calculating distances, understanding what occurs under the hood and learning how to interactively display the final result. While I definitely still don’t know enough about working with spatial data in R, I feel like I don’t know a little less after completing this blog post.
I hope you enjoyed this post and it helps you find the nearest ice-cream location next time you’re subject to an ice-cream frenzy!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.