Tuesday, July 30, 2024

Geospatial Tutorial 2 - Drive Time Analysis in Singapore

Geospatial Analysis Tutorial 2: Identifying locations for Opening Restaurents

Introduction

In the first tutorial, we looked at how we can download the geojson file from the internet and then use it to visualize maps of various countries(we looked at Indonesia). In this blog, we will take a step further and identify potential location for opening an Indian restaurant for Singapore.

Step 0: What will be the steps to achieve this

  • Get the geojson map for Singapore
  • Get the metric at a planning area level or unit of analysis
  • Shortlist the area of interest based on heat map
  • Compare key locations in terms of constant drive time distances

Step 1: Importing the libraries

package.name<-c("tidyverse","sf","raster","tmap",
                "leaflet","mapview","viridis","ggthemes",
                "htmltools","osrm","mapboxapi")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}
Loading required package: tidyverse
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Loading required package: sf

Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE

Loading required package: raster

Loading required package: sp


Attaching package: 'raster'


The following object is masked from 'package:dplyr':

    select


Loading required package: tmap

Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')

Loading required package: leaflet

Loading required package: mapview

Loading required package: viridis

Loading required package: viridisLite

Loading required package: ggthemes

Loading required package: htmltools

Loading required package: osrm

Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright

Routing: OSRM - http://project-osrm.org/

Loading required package: mapboxapi

Usage of the Mapbox APIs is governed by the Mapbox Terms of Service.
Please visit https://www.mapbox.com/legal/tos/ for more information.


Step 2:Downloading the geo-json map for Singapore

I found that the map shared in a Kaggle challenge seems to fulfil all the requirements. You can download it based on the below steps



go to the link and then download the file as shown below

Step 3: Reading the geojson file into R

Now we can use the downloaded file and read it using read_sf function

file_json<-"district_and_planning_area.geojson"
sg_map<-read_sf(file_json)


Plotting the Map to check its features


leaflet(sg_map)%>%
  addTiles()%>%
  addPolygons(fillColor = "white",
              color = "#000000",
              weight = 5,
              fillOpacity = 0.5,
              popup = ~glue::glue("<b>{planning_area}</b><br>{district}"))%>%
  addPolylines(color="black",
               weight=2,
               fillOpacity = 3)


We can note here is that the map has certain geographical features such as lat long details, some relief information,etc.We would need to have a vanilla version of the Map so that it acts as a whiteboard for our analysis.


Step 4: Cleaning the Map

Removing/Commenting addTiles converts the map into a plain format

leaflet(sg_map)%>%
  # addTiles()%>%
  addPolygons(fillColor = "white",
              color = "#000000",
              weight = 2,
              fillOpacity = 2,
              popup = ~glue::glue("<b>{planning_area}</b><br>{district}"))%>%
  addPolylines(color="black",
               weight=2,
               fillOpacity = 3)



For getting the white background, we will be using the htmltools features

backg<-htmltools::tags$style(".leaflet-container{ background:white;}")

leaflet(sg_map)%>%
  htmlwidgets::prependContent(backg)%>%
  # addTiles()%>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 2,
              fillOpacity = 2,
              popup = ~glue::glue("<b>{planning_area}</b><br>{district}"))%>%
  addPolylines(color="black",
               weight=2,
               fillOpacity = 3)