{"id":25,"date":"2016-11-01T12:04:03","date_gmt":"2016-11-01T19:04:03","guid":{"rendered":"https:\/\/blogs.ubc.ca\/rzgeography\/?p=25"},"modified":"2016-11-01T12:04:03","modified_gmt":"2016-11-01T19:04:03","slug":"assignment4","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/rzgeography\/2016\/11\/01\/assignment4\/","title":{"rendered":"Assignment4"},"content":{"rendered":"<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># ------------------------------------------------------------------ #\r\n# ---------------------- Install Libraries ------------------------- #\r\n# ------------------------------------------------------------------ #\r\ninstall.packages(\"GISTools\")\r\ninstall.packages(\"RJSONIO\")\r\ninstall.packages(\"rgdal\")\r\ninstall.packages(\"RCurl\")\r\ninstall.packages(\"curl\")\r\n\r\n# Unused Libraries:\r\n# install.packages(\"ggmap\")\r\n# library(ggmap)\r\n# ------------------------------------------------------------------ #\r\n# ----------------------- Load Libararies -------------------------- #\r\n# ------------------------------------------------------------------ #\r\nlibrary(GISTools)\r\nlibrary(RJSONIO)\r\nlibrary(rgdal)\r\nlibrary(RCurl)\r\nlibrary(curl)<\/pre>\n<p><strong>Step1<\/strong>: This step is the beginning, and to make sure the R studio has installed and loaded the tools for my project.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># ------------------------------------------------------------------ #\r\n# ---------------------------- Acquire ----------------------------- #\r\n# ------------------------------------------------------------------ #\r\n# access csv data from desktop\r\nfname = ('\/User\/Borui\/Desktop\/crime_2004.csv')\r\n# Read data as csv\r\ndata = crime_2004.csv(fname, header=T)\r\n# inspect your data\r\nprint(head(crime_2004))<\/pre>\n<p><strong>Step2<\/strong>: When the R studio is ready, then I import the data about Vancouver crime statistics in 2004 into R, and let it read from the local.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># ------------------------------------------------- #\r\n# -------------- Parse: Geocoder ------------------- #\r\n# ------------------------------------------------- #\r\n# change intersection to 00's\r\ncrime_2004$HUNDRED_BLOCK = gsub(\"X\",\"0\",crime_2004$HUNDRED_BLOCK)\r\nprint(head(crime_2004$HUNDRED_BLOCK))\r\n\r\n# Join the strings from each column together &amp; add \"Vancouver, BC\":\r\ncrime_2004$full_address = paste(crime_2004$HUNDRED_BLOCK, \r\n paste(crime_2004$NEIGHBOURHOOD,\r\n \"Vancouver, BC\",\r\n sep=\", \"),\r\n sep=\" \")\r\n\r\n# removing \"Intersection \" from the full_address entries\r\ncrime_2004$full_address = gsub(\"Intersection \", \"\", crime_2004$full_address)\r\nprint(head(crime_2004$full_address))<\/pre>\n<p><strong>Step3<\/strong>: Then I notice some number &#8216;0&#8217; show the symbol &#8216;X&#8217; in the data. I change intersection to 00&#8217;s, and let &#8216;X&#8217; to become &#8216;0&#8217; under the HUNDRED_BLOCK column. Next, I created a new column called &#8216;full address&#8217; to combine the HUNDRED_BLOCK with NEIGHBOURHOOD. Also I remove the intersection in order to make my data clearer and easier.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># BC geocoading\r\nbc_geocode = function(search){\r\n if(!is.character(search)){stop(\"'search' must be a character string\")}\r\n search = RCurl::curlEscape(search)\r\n base_url = \"http:\/\/apps.gov.bc.ca\/pub\/geocoder\/addresses.json?addressString=\"\r\n url_tail = \"&amp;locationDescriptor=any&amp;maxResults=1&amp;interpolation=adaptive&amp;echo=true&amp;setBack=0&amp;outputSRS=4326&amp;minScore=1&amp;provinceCode=BC\"\r\n final_url = paste0(base_url, search, url_tail)\r\n response = RCurl::getURL(final_url)\r\n response_parsed = RJSONIO::fromJSON(response)\r\n if(length(response_parsed$features[[1]]$geometry[[3]]) &gt; 0){\r\n geocoords = list(lon = response_parsed$features[[1]]$geometry[[3]][1],\r\n lat = response_parsed$features[[1]]$geometry[[3]][2])\r\n }else{\r\n geocoords = NA\r\n }\r\n return(geocoords)\r\n}\r\n\r\n# Create an empty vector for latitude and longtitude coordinates\r\nlat = c() \r\nlon = c()\r\n# loop through the addresses\r\nfor(i in 1:length(crime_2004$full_address)){\r\n # store the address at index \"i\" as a character\r\n address = crime_2004$full_address[i]\r\n # append the latitude of the geocoded address to the lat vector\r\n lat = c(lat, bc_geocode(address)$lat)\r\n # append the longitude of the geocoded address to the lon vector\r\n lon = c(lon, bc_geocode(address)$lon)\r\n # at each iteration through the loop, print the coordinates\r\n print(paste(\"#\", i, \", \", lat[i], lon[i], sep = \",\"))\r\n}\r\n\r\n# add the lat lon coordinates to the dataframe\r\ncrime_2004$lat = lat\r\ncrime_2004$lon = lon\r\n# export csv file\r\nofile = \"\/Users\/Borui\/Desktop\/crime.csv\" \r\nwrite.csv(crime_2004, ofile)<\/pre>\n<p><strong>Step4<\/strong>: This step is a huge and time-consuming task. I firstly define the bc geocoding function in order to run the bc geocoding API to get the latitude and longitude coordinates for each full address. This task costs me 4 hours. After a major computational task, my data now has specific latitude and longitude coordinates related to the crime scene. Then I output this data and named crime_csv to keep a copy.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># ------------------------------------------------- #\r\n# --------------------- Mine ---------------------- #\r\n# ------------------------------------------------- #\r\n# --- Examine the unique cases --- #\r\nunique(crime_2004$TYPE)\r\nfor (i in 1:length(unique(crime_2004$TYPE))){\r\n print(unique(crime_2004$TYPE)[i], max.levels=0)\r\n}\r\n\r\n#Divide classes to group type\r\n#-----------------Mischief--------------#\r\nMischief = c('Mischief')\r\n#-----------------Theft---------------#\r\nTheft = c('Theft from Vehicle',\r\n 'Theft of Vehicle',\r\n 'Other Theft')\r\n#----------------Break and Enter----------#\r\nBreak_and_Enter = c('Break and Enter Commercial',\r\n 'Break and Enter Residential\/Other')\r\n#----------------Offence Against a Person------------#\r\nOffence_Against_a_Person = c('Offence Against a Person')\r\n#---------------Homicide--------------#\r\nHomicide = c('Homicide')\r\n\r\n# give class id numbers:\r\ncrime_2004$cid = 9999\r\nfor(i in 1:length(crime_2004$TYPE)){\r\n if(crime_2004$TYPE[i] %in% Mischief){\r\n crime_2004$cid[i] = 1 \r\n }else if(crime_2004$TYPE[i] %in% Theft){\r\n crime_2004$cid[i] = 2 \r\n }else if(crime_2004$TYPE[i] %in% Break_and_Enter){\r\n crime_2004$cid[i] = 3 \r\n }else if(crime_2004$TYPE[i] %in% Offence_Against_a_Person){\r\n crime_2004$cid[i] = 4 \r\n }else if(crime_2004$TYPE[i] %in% Homicide){\r\n crime_2004$cid[i] = 5 \r\n }else{\r\n crime_2004$cid[i] = 0 \r\n }\r\n}<\/pre>\n<p><strong>Step5:<\/strong> To find every crime types, I type a unique code. Then I integrate my data and divide to 5 different types crime. They are the mischief, the theft, the break and enter crime, offence against a person and the homicide. After the integration, I give the class ID number for each type crime.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># --- handle overlapping points --- #\r\n# Set offset for points in same location:\r\ncrime_2004$lat_offset = crime_2004$lat\r\ncrime_2004$lon_offset = crime_2004$lon\r\n\r\n# Run loop - if value overlaps, offset it by a random number\r\nfor(i in 1:length(crime_2004$lat)){\r\n if ( (crime_2004$lat_offset[i] %in% crime_2004$lat_offset) &amp;&amp; (crime_2004$lon_offset[i] %in% crime_2004$lon_offset)){\r\n crime_2004$lat_offset[i] = crime_2004$lat_offset[i] + runif(1, 0.0001, 0.0005)\r\n crime_2004$lon_offset[i] = crime_2004$lon_offset[i] + runif(1, 0.0001, 0.0005)\r\n } \r\n}\r\n# get a frequency distribution of the calls:\r\ntop_calls = data.frame(table(crime_2004$TYPE))\r\ntop_calls = top_calls[order(top_calls$Freq), ]\r\nprint(top_calls)<\/pre>\n<p><strong>Step6:<\/strong>\u00a0To avoid the same coordinates for the crime, I set the offset on the latitude and longitude by adding 0.0001 to 0.0005. Then I get two new columns for the lat_offset and lon_offset.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\"># ------------------------------------------------------------------ #\r\n# ---------------------------- Filter ------------------------------ #\r\n# ------------------------------------------------------------------ #\r\ndata_filter = subset(crime_2004, (lat &lt;= 49.313162) &amp; (lat &gt;= 49.199554) &amp; \r\n (lon &lt;= -123.019028) &amp; (lon &gt;= -123.271371) &amp; is.na(lon) == FALSE )\r\n# plot the data\r\nplot(data_filter$lon, data_filter$lat)\r\nofile_filtered = '\/Users\/Borui\/Desktop\/crime_filtered.csv' \r\nwrite.csv(data_filter, ofile_filtered)<\/pre>\n<p><strong>Step7:\u00a0<\/strong>After the offset the latitude and longitude, I need to filter some data outside Vancouver&#8217;s boundary. Then I give a limit to the boundary coordinate to only focus on Vancouver. I plot each data to this limited area and export as crime_filtered.csv file.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#-------------Saving Dataset------------#\r\n# --- Convert Data to Shapefile --- #\r\n# store coordinates in dataframe\r\ncoords_crime_2004 = data.frame(data_filter$lon_offset, data_filter$lat_offset)\r\n# create spatialPointsDataFrame\r\ndata_shp = SpatialPointsDataFrame(coords = coords_crime_2004, data = data_filter)\r\n# set the projection to wgs84\r\nprojection_wgs84 = CRS(\"+proj=longlat +datum=WGS84\")\r\nproj4string(data_shp) = projection_wgs84\r\n# set an output folder for our geojson files:\r\ngeofolder = \"\/Users\/Borui\/Desktop\/GEOB_472\/\"\r\n# Join the folderpath to each file name:\r\nopoints_shp = paste(geofolder, \"calls_\", \"1401\", \".shp\", sep = \"\")\r\nprint(opoints_shp)# print this out to see where your file will go &amp; what it will be called\r\nopoints_geojson = paste(geofolder, \"calls_\", \"1401\", \".geojson\", sep = \"\") \r\nprint(opoints_geojson) # print this out to see where your file will go &amp; what it will be called\r\n# write the file to a shp\r\nwriteOGR(data_shp, opoints_shp, layer = \"data_shp\", driver = \"ESRI Shapefile\",\r\n check_exists = FALSE)\r\n# write file to geojson\r\nwriteOGR(data_shp, opoints_geojson, layer = \"data_shp\", driver = \"GeoJSON\",\r\n check_exists = FALSE)<\/pre>\n<p><strong>Step8:<\/strong> On this step, with a filtered dataset, I can write my file out to a shapefile and a geojson. I create latitude and longitute coordinates of the crime in WGS84 projection shape file by using filtered data.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#---------------------Creating Hexagon Grid-------------------#\r\n# --- aggregate to a grid --- #\r\n# ref: http:\/\/www.inside-r.org\/packages\/cran\/GISTools\/docs\/poly.counts\r\n# set the file name - combine the shpfolder with the name of the grid\r\ngrid_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m.geojson'\r\n\r\n# read in the hex grid setting the projection to utm10n\r\nhexgrid = readOGR(grid_fn, 'OGRGeoJSON')\r\n\r\n# transform the projection to wgs84 to match the point file and store\r\n# it to a new variable (see variable: projection_wgs84)\r\nhexgrid_wgs84 = spTransform(hexgrid, projection_wgs84)\r\n\r\n#-----------------Assigning Values to Hexagons-----------------#\r\n# Use the poly.counts() function to count the number of occurrences of calls per grid cell\r\ngrid_cnt = poly.counts(data_shp, hexgrid_wgs84)\r\n\r\n# define the output names:\r\nohex_shp = paste(geofolder, \"hexgrid_250m_\", \"1401\", \"_cnts\",\r\n \".shp\", sep = \"\")\r\nprint(ohex_shp)\r\n\r\nohex_geojson = paste(geofolder, \"hexgrid_250m_\",\"1401\",\"_cnts\",\".geojson\") \r\nprint(ohex_geojson)\r\n\r\n# write the file to a shp\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_shp, \r\n layer = \"hexgrid_wgs84\", driver = \"ESRI Shapefile\")\r\n\r\n# write file to geojson\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_geojson, \r\n layer = \"hexgrid_wgs84\", driver = \"GeoJSON\")<\/pre>\n<p><strong>Step9:\u00a0<\/strong>I download a hexgonal grid at a resolution of 250m and reproject it from UTM zone 10 to WGS84. Then using the polu.counts function to count the number of points in each grid cell. \u00a0Also output them to shapefile and geojson.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#---------------------Version1---------------------------#\r\n#--------------------------------------------------------#\r\n# add the leaflet library to your script\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# subset out your data_filter:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\n# initiate leaflet\r\nm = leaflet()\r\n# add openstreetmap tiles (default)\r\nm = addTiles(m)\r\n# you can now see that our maptiles are rendered\r\nm\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'),\r\n domain = data_filter$cid)\r\nm = addCircleMarkers(m, \r\n lng = df_Mischief$lon_offset, # we feed the longitude coordinates \r\n lat = df_Mischief$lat_offset,\r\n popup = df_Mischief$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, \r\n fillOpacity = 0.75, \r\n color = colorFactors(df_Mischief$cid),\r\n group = \"1 - Mischief\")\r\nm = addCircleMarkers(m, \r\n lng = df_Theft$lon_offset, lat=df_Theft$lat_offset, \r\n popup = df_Theft$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Theft$cid),\r\n group = \"2 - Theft\")\r\nm = addCircleMarkers(m, \r\n lng = df_Break_and_Enter$lon_offset, lat=df_Break_and_Enter$lat_offset, \r\n popup = df_Break_and_Enter$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Break_and_Enter$cid),\r\n group = \"3 - Break_and_Enter\")\r\nm = addCircleMarkers(m, \r\n lng = df_Offence_Against_a_Person$lon_offset, lat=df_Offence_Against_a_Person$lat_offset, \r\n popup = df_Offence_Against_a_Person$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Offence_Against_a_Person$cid),\r\n group = \"4 - Offence_Against_a_Person\")\r\nm = addCircleMarkers(m, \r\n lng = df_Homicide$lon_offset, lat=df_Homicide$lat_offset, \r\n popup = df_Homicide$Case_Type, \r\n radius = 2,\r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Homicide$cid),\r\n group = \"5 - Homicide\")\r\n\r\nm = addTiles(m, group = \"OSM (default)\") \r\nm = addProviderTiles(m,\"Stamen.Toner\", group = \"Toner\")\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n\r\nm = addLayersControl(m,\r\n baseGroups = c(\"Toner Lite\",\"Toner\"),\r\n overlayGroups = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\")\r\n)\r\n# make the map\r\nm<\/pre>\n<p><strong>Step10<\/strong>: This is the version1 code. \u00a0It is working with point data. Firstly, I use the subset function to get store my data into 5 different classes. Then I initiate the leaflet map and insure different colors for the different classes. Next I type addCircleMarker function to add points on the map.Finally, I use addProviderTiles function to change the map&#8217;s background and use addLayerControl function to create a control bar to choose the different layers.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-26\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM-300x208.png\" alt=\"screen-shot-2016-11-01-at-1-49-09-am\" width=\"300\" height=\"208\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM-300x208.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM-768x533.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM-1024x711.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM-500x347.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.09-AM.png 1962w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-27\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM-300x208.png\" alt=\"screen-shot-2016-11-01-at-1-49-47-am\" width=\"300\" height=\"208\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM-300x208.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM-768x533.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM-1024x711.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM-500x347.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-1.49.47-AM.png 1962w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#-------------------------Version2----------------------------#\r\n#-------------------------------------------------------------#\r\n# add the leaflet library to your script\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# subset out my crime_filtered:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\ndata_filterlist = list(df_Mischief = subset(data_filter, cid == 1),\r\n df_Theft = subset(data_filter, cid == 2),\r\n df_Break_and_Enter = subset(data_filter, cid == 3),\r\n df_Offence_Against_a_Person = subset(data_filter, cid == 4),\r\n df_Homicide = subset(data_filter, cid == 5))\r\n\r\n# Remember we also had these groups associated with each variable? Let's put them in a list too:\r\nlayerlist = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\",\r\n \"0 - other\")\r\n\r\n# We can keep that same color variable:\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\n\r\n# Now we have our loop - each time through the loop, it is adding our markers to the map object:\r\nfor (i in 1:length(data_filterlist)){\r\n m = addCircleMarkers(m, \r\n lng=data_filterlist[[i]]$lon_offset,\r\n lat=data_filterlist[[i]]$lat_offset, \r\n popup=data_filterlist[[i]]$Case_Type, \r\n radius=2,\r\n stroke = FALSE, \r\n fillOpacity = 0.75,\r\n color = colorFactors(data_filterlist[[i]]$cid),\r\n group = layerlist[i]\r\n )\r\n}\r\nm = addTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\") \r\nm = addLayersControl(m,\r\n overlayGroups = c(\"Toner Lite\"),\r\n baseGroups = layerlist\r\n)\r\nm<\/pre>\n<p><strong>Step11:<\/strong>\u00a0This is the version2 code. It is optimizing version. The same process like the version1, I also use subset function to divide my data. Then I create a list to include these variables. Other fundamental color and map code keep the same like the version1.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-28\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM-300x208.png\" alt=\"screen-shot-2016-11-01-at-2-00-53-am\" width=\"300\" height=\"208\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM-300x208.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM-768x533.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM-1024x710.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM-500x347.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.00.53-AM.png 1964w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-29\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM-300x207.png\" alt=\"screen-shot-2016-11-01-at-2-09-17-am\" width=\"300\" height=\"207\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM-300x207.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM-768x531.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM-1024x708.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM-500x346.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.09.17-AM.png 1958w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#----------------------Version3-1--------------------------#\r\n#----------------------------------------------------------#\r\nlibrary(rgdal)\r\nlibrary(GISTools)\r\n# define the filepath\r\nhex_1401_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m_1401_counts.geojson'\r\n# read in the geojson\r\nhex_1401 = readOGR(hex_1401_fn, \"OGRGeoJSON\")\r\n\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\n palette = \"Greens\",\r\n domain = hex_1401$data\r\n)\r\n\r\n# add the polygons to the map\r\nm = addPolygons(m, \r\n data = hex_1401,\r\n stroke = FALSE, \r\n smoothFactor = 0.2, \r\n fillOpacity = 1,\r\n color = ~pal(hex_1401$data),\r\n popup = paste(\"Number of calls: \", hex_1401$data, sep=\"\")\r\n)\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_1401$data,\r\n title = \"Vancouver Crime Distribution in 2004\",\r\n labFormat = labelFormat(prefix = \" \"),\r\n opacity = 0.75\r\n)\r\nm<\/pre>\n<p><strong>Step12:<\/strong>\u00a0This is the version3-1 code. It is working with polygons. To do this map, I need to read my hegrid with my data. Then I use colorNumeric function to choose green to show on the map. Finally, I add my polygon on the map.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-30\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM-300x208.png\" alt=\"screen-shot-2016-11-01-at-2-11-17-am\" width=\"300\" height=\"208\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM-300x208.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM-768x533.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM-1024x710.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM-500x347.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.11.17-AM.png 1964w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">#---------------------------Version3-2----------------------------#\r\n#-----------------------------------------------------------------#\r\nlibrary(rgdal)\r\nlibrary(GISTools)\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# initiate leaflet map layer\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\") \r\n\r\n# --- hex grid --- #\r\n# store the file name of the geojson hex grid\r\nhex_1401_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m_1401_counts.geojson'\r\n\r\n# read in the data\r\nhex_1401 = readOGR(hex_1401_fn, \"OGRGeoJSON\")\r\n\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\n palette = \"Greens\",\r\n domain = hex_1401$data\r\n)\r\n\r\n# add hex grid\r\nm = addPolygons(m, \r\n data = hex_1401,\r\n stroke = FALSE, \r\n smoothFactor = 0.2, \r\n fillOpacity = 1,\r\n color = ~pal(hex_1401$data),\r\n popup= paste(\"Number of calls: \",hex_1401$data, sep=\"\"),\r\n group = \"hex\"\r\n)\r\n\r\n# add legend\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_1401$data,\r\n title = \"Vancouver Crime Distribution in 2004\",\r\n labFormat = labelFormat(prefix = \" \"),\r\n opacity = 0.75\r\n)\r\n\r\n# --- points data --- #\r\n# subset out my crime_filtered:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\ndata_filterlist = list(df_Mischief = subset(data_filter, cid == 1),\r\n df_Theft = subset(data_filter, cid == 2),\r\n df_Break_and_Enter = subset(data_filter, cid == 3),\r\n df_Offence_Against_a_Person = subset(data_filter, cid == 4),\r\n df_Homicide = subset(data_filter, cid == 5))\r\n\r\n\r\nlayerlist = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\",\r\n \"0 - other\")\r\n\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\nfor (i in 1:length(data_filterlist)){\r\n m = addCircleMarkers(m, \r\n lng=data_filterlist[[i]]$lon_offset, lat=data_filterlist[[i]]$lat_offset, \r\n popup=data_filterlist[[i]]$Case_Type, \r\n radius=2,\r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(data_filterlist[[i]]$cid),\r\n group = layerlist[i]\r\n )\r\n \r\n}\r\n\r\nm = addLayersControl(m,\r\n overlayGroups = c(\"Toner Lite\", \"hex\"),\r\n baseGroups = layerlist\r\n)\r\n# show map\r\nm<\/pre>\n<p><strong>Step13:<\/strong>\u00a0This is another vesion3-2 code, but it is synthesis. It includes both points and polygons on the map. For the point part, I copy the code from version2, then I also keep the code from version3 in order to combine point and polygon maps.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-31\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM-300x208.png\" alt=\"screen-shot-2016-11-01-at-2-19-39-am\" width=\"300\" height=\"208\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM-300x208.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM-768x532.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM-1024x709.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM-500x346.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.39-AM.png 1964w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-32\" src=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM-300x209.png\" alt=\"screen-shot-2016-11-01-at-2-19-56-am\" width=\"300\" height=\"209\" srcset=\"https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM-300x209.png 300w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM-768x535.png 768w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM-1024x713.png 1024w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM-500x348.png 500w, https:\/\/blogs.ubc.ca\/rzgeography\/files\/2016\/11\/Screen-Shot-2016-11-01-at-2.19.56-AM.png 1958w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><strong>Comments:<\/strong> At the beginning, I have around 56228 records. After running the data through passing and geocoding and filtering, I have 44918 records left. I think these data are reliable without lost of error. When seeing the final map, it is clear that the map only focus on Vancouver with different types of crime. A shpfile is a vector data storage format for storing the location, shape and attributes of geographic features. Csv means common separated values, and it is a plain text format. It holds plain text as a series of values. Excel or xls file holds information about all the worksheets in a workbook, comprising both content and formatting. Geojson is a standard for representing the core geographic features that we are interested in. It does not contain everything that a shapfile can contain and it does not contain any styling information. In this project, I use csv file (crime_2004.csv) for the data source, and use shpfile (calls_1401.shp) and geojson (hexgrid_250m.geojson) to store the location in order to show the the distribution on the final map.<\/p>\n<pre class=\"a-b-r-La a-b-ja-Eb-Vb\">---------------Full Script---------------\r\n<code>#####################################################################\r\n# Vancouver Crime Distribution in 2004: Data Processing Script\r\n# Date:2016.10.31\r\n# By: Rui Zhang (Jerry)\r\n#####################################################################\r\n# ------------------------------------------------------------------ #\r\n# ---------------------- Install Libraries ------------------------- #\r\n# ------------------------------------------------------------------ #\r\ninstall.packages(\"GISTools\")\r\ninstall.packages(\"RJSONIO\")\r\ninstall.packages(\"rgdal\")\r\ninstall.packages(\"RCurl\")\r\ninstall.packages(\"curl\")\r\n\r\n# Unused Libraries:\r\n# install.packages(\"ggmap\")\r\n# library(ggmap)\r\n# ------------------------------------------------------------------ #\r\n# ----------------------- Load Libararies -------------------------- #\r\n# ------------------------------------------------------------------ #\r\nlibrary(GISTools)\r\nlibrary(RJSONIO)\r\nlibrary(rgdal)\r\nlibrary(RCurl)\r\nlibrary(curl)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------------- Acquire ----------------------------- #\r\n# ------------------------------------------------------------------ #\r\n# access csv data from desktop\r\nfname = ('\/User\/Borui\/Desktop\/crime_2004.csv')\r\n# Read data as csv\r\ndata = crime_2004.csv(fname, header=T)\r\n# inspect your data\r\nprint(head(crime_2004))\r\n\r\n# ------------------------------------------------- #\r\n# -------------- Parse: Geocoder ------------------- #\r\n# ------------------------------------------------- #\r\n# change intersection to 00's\r\ncrime_2004$HUNDRED_BLOCK = gsub(\"X\",\"0\",crime_2004$HUNDRED_BLOCK)\r\nprint(head(crime_2004$HUNDRED_BLOCK))\r\n\r\n# Join the strings from each column together &amp; add \"Vancouver, BC\":\r\ncrime_2004$full_address = paste(crime_2004$HUNDRED_BLOCK, \r\n paste(crime_2004$NEIGHBOURHOOD,\r\n \"Vancouver, BC\",\r\n sep=\", \"),\r\n sep=\" \")\r\n\r\n# removing \"Intersection \" from the full_address entries\r\ncrime_2004$full_address = gsub(\"Intersection \", \"\", crime_2004$full_address)\r\nprint(head(crime_2004$full_address))\r\n\r\n# BC geocoading\r\nbc_geocode = function(search){\r\n if(!is.character(search)){stop(\"'search' must be a character string\")}\r\n search = RCurl::curlEscape(search)\r\n base_url = \"http:\/\/apps.gov.bc.ca\/pub\/geocoder\/addresses.json?addressString=\"\r\n url_tail = \"&amp;locationDescriptor=any&amp;maxResults=1&amp;interpolation=adaptive&amp;echo=true&amp;setBack=0&amp;outputSRS=4326&amp;minScore=1&amp;provinceCode=BC\"\r\n final_url = paste0(base_url, search, url_tail)\r\n response = RCurl::getURL(final_url)\r\n response_parsed = RJSONIO::fromJSON(response)\r\n if(length(response_parsed$features[[1]]$geometry[[3]]) &gt; 0){\r\n geocoords = list(lon = response_parsed$features[[1]]$geometry[[3]][1],\r\n lat = response_parsed$features[[1]]$geometry[[3]][2])\r\n }else{\r\n geocoords = NA\r\n }\r\n return(geocoords)\r\n}\r\n\r\n# Create an empty vector for latitude and longtitude coordinates\r\nlat = c() \r\nlon = c()\r\n# loop through the addresses\r\nfor(i in 1:length(crime_2004$full_address)){\r\n # store the address at index \"i\" as a character\r\n address = crime_2004$full_address[i]\r\n # append the latitude of the geocoded address to the lat vector\r\n lat = c(lat, bc_geocode(address)$lat)\r\n # append the longitude of the geocoded address to the lon vector\r\n lon = c(lon, bc_geocode(address)$lon)\r\n # at each iteration through the loop, print the coordinates\r\n print(paste(\"#\", i, \", \", lat[i], lon[i], sep = \",\"))\r\n}\r\n\r\n\r\n# add the lat lon coordinates to the dataframe\r\ncrime_2004$lat = lat\r\ncrime_2004$lon = lon\r\n# export csv file\r\nofile = \"\/Users\/Borui\/Desktop\/crime.csv\" \r\nwrite.csv(crime_2004, ofile)\r\n\r\n# ------------------------------------------------- #\r\n# --------------------- Mine ---------------------- #\r\n# ------------------------------------------------- #\r\n# --- Examine the unique cases --- #\r\nunique(crime_2004$TYPE)\r\nfor (i in 1:length(unique(crime_2004$TYPE))){\r\n print(unique(crime_2004$TYPE)[i], max.levels=0)\r\n}\r\n\r\n#Divide classes to group type\r\n#-----------------Mischief--------------#\r\nMischief = c('Mischief')\r\n#-----------------Theft---------------#\r\nTheft = c('Theft from Vehicle',\r\n 'Theft of Vehicle',\r\n 'Other Theft')\r\n#----------------Break and Enter----------#\r\nBreak_and_Enter = c('Break and Enter Commercial',\r\n 'Break and Enter Residential\/Other')\r\n#----------------Offence Against a Person------------#\r\nOffence_Against_a_Person = c('Offence Against a Person')\r\n#---------------Homicide--------------#\r\nHomicide = c('Homicide')\r\n\r\n# give class id numbers:\r\ncrime_2004$cid = 9999\r\nfor(i in 1:length(crime_2004$TYPE)){\r\n if(crime_2004$TYPE[i] %in% Mischief){\r\n crime_2004$cid[i] = 1 \r\n }else if(crime_2004$TYPE[i] %in% Theft){\r\n crime_2004$cid[i] = 2 \r\n }else if(crime_2004$TYPE[i] %in% Break_and_Enter){\r\n crime_2004$cid[i] = 3 \r\n }else if(crime_2004$TYPE[i] %in% Offence_Against_a_Person){\r\n crime_2004$cid[i] = 4 \r\n }else if(crime_2004$TYPE[i] %in% Homicide){\r\n crime_2004$cid[i] = 5 \r\n }else{\r\n crime_2004$cid[i] = 0 \r\n }\r\n}\r\n\r\n# --- handle overlapping points --- #\r\n# Set offset for points in same location:\r\ncrime_2004$lat_offset = crime_2004$lat\r\ncrime_2004$lon_offset = crime_2004$lon\r\n\r\n# Run loop - if value overlaps, offset it by a random number\r\nfor(i in 1:length(crime_2004$lat)){\r\n if ( (crime_2004$lat_offset[i] %in% crime_2004$lat_offset) &amp;&amp; (crime_2004$lon_offset[i] %in% crime_2004$lon_offset)){\r\n crime_2004$lat_offset[i] = crime_2004$lat_offset[i] + runif(1, 0.0001, 0.0005)\r\n crime_2004$lon_offset[i] = crime_2004$lon_offset[i] + runif(1, 0.0001, 0.0005)\r\n } \r\n}\r\n# get a frequency distribution of the calls:\r\ntop_calls = data.frame(table(crime_2004$TYPE))\r\ntop_calls = top_calls[order(top_calls$Freq), ]\r\nprint(top_calls)\r\n\r\n# ------------------------------------------------------------------ #\r\n# ---------------------------- Filter ------------------------------ #\r\n# ------------------------------------------------------------------ #\r\ndata_filter = subset(crime_2004, (lat &lt;= 49.313162) &amp; (lat &gt;= 49.199554) &amp; \r\n (lon &lt;= -123.019028) &amp; (lon &gt;= -123.271371) &amp; is.na(lon) == FALSE )\r\n# plot the data\r\nplot(data_filter$lon, data_filter$lat)\r\nofile_filtered = '\/Users\/Borui\/Desktop\/crime_filtered.csv' \r\nwrite.csv(data_filter, ofile_filtered)\r\n\r\n#-------------Saving Dataset------------#\r\n# --- Convert Data to Shapefile --- #\r\n# store coordinates in dataframe\r\ncoords_crime_2004 = data.frame(data_filter$lon_offset, data_filter$lat_offset)\r\n\r\n# create spatialPointsDataFrame\r\ndata_shp = SpatialPointsDataFrame(coords = coords_crime_2004, data = data_filter)\r\n\r\n# set the projection to wgs84\r\nprojection_wgs84 = CRS(\"+proj=longlat +datum=WGS84\")\r\nproj4string(data_shp) = projection_wgs84\r\n\r\n# set an output folder for our geojson files:\r\ngeofolder = \"\/Users\/Borui\/Desktop\/GEOB_472\/\"\r\n\r\n# Join the folderpath to each file name:\r\nopoints_shp = paste(geofolder, \"calls_\", \"1401\", \".shp\", sep = \"\")\r\nprint(opoints_shp)# print this out to see where your file will go &amp; what it will be called\r\n\r\nopoints_geojson = paste(geofolder, \"calls_\", \"1401\", \".geojson\", sep = \"\") \r\nprint(opoints_geojson) # print this out to see where your file will go &amp; what it will be called\r\n\r\n# write the file to a shp\r\nwriteOGR(data_shp, opoints_shp, layer = \"data_shp\", driver = \"ESRI Shapefile\",\r\n check_exists = FALSE)\r\n\r\n# write file to geojson\r\nwriteOGR(data_shp, opoints_geojson, layer = \"data_shp\", driver = \"GeoJSON\",\r\n check_exists = FALSE)\r\n\r\n#---------------------Creating Hexagon Grid-------------------#\r\n# --- aggregate to a grid --- #\r\n# ref: http:\/\/www.inside-r.org\/packages\/cran\/GISTools\/docs\/poly.counts\r\n# set the file name - combine the shpfolder with the name of the grid\r\ngrid_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m.geojson'\r\n\r\n# read in the hex grid setting the projection to utm10n\r\nhexgrid = readOGR(grid_fn, 'OGRGeoJSON')\r\n\r\n# transform the projection to wgs84 to match the point file and store\r\n# it to a new variable (see variable: projection_wgs84)\r\nhexgrid_wgs84 = spTransform(hexgrid, projection_wgs84)\r\n\r\n#-----------------Assigning Values to Hexagons-----------------#\r\n# Use the poly.counts() function to count the number of occurrences of calls per grid cell\r\ngrid_cnt = poly.counts(data_shp, hexgrid_wgs84)\r\n\r\n# define the output names:\r\nohex_shp = paste(geofolder, \"hexgrid_250m_\", \"1401\", \"_cnts\",\r\n \".shp\", sep = \"\")\r\nprint(ohex_shp)\r\n\r\nohex_geojson = paste(geofolder, \"hexgrid_250m_\",\"1401\",\"_cnts\",\".geojson\") \r\nprint(ohex_geojson)\r\n\r\n# write the file to a shp\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_shp, \r\n layer = \"hexgrid_wgs84\", driver = \"ESRI Shapefile\")\r\n\r\n# write file to geojson\r\nwriteOGR(check_exists = FALSE, hexgrid_wgs84, ohex_geojson, \r\n layer = \"hexgrid_wgs84\", driver = \"GeoJSON\")\r\n\r\n#---------------------Version1---------------------------#\r\n#--------------------------------------------------------#\r\n# add the leaflet library to your script\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# subset out your data_filter:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\n# initiate leaflet\r\nm = leaflet()\r\n# add openstreetmap tiles (default)\r\nm = addTiles(m)\r\n# you can now see that our maptiles are rendered\r\nm\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'),\r\n domain = data_filter$cid)\r\nm = addCircleMarkers(m, \r\n lng = df_Mischief$lon_offset, # we feed the longitude coordinates \r\n lat = df_Mischief$lat_offset,\r\n popup = df_Mischief$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, \r\n fillOpacity = 0.75, \r\n color = colorFactors(df_Mischief$cid),\r\n group = \"1 - Mischief\")\r\nm = addCircleMarkers(m, \r\n lng = df_Theft$lon_offset, lat=df_Theft$lat_offset, \r\n popup = df_Theft$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Theft$cid),\r\n group = \"2 - Theft\")\r\nm = addCircleMarkers(m, \r\n lng = df_Break_and_Enter$lon_offset, lat=df_Break_and_Enter$lat_offset, \r\n popup = df_Break_and_Enter$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Break_and_Enter$cid),\r\n group = \"3 - Break_and_Enter\")\r\nm = addCircleMarkers(m, \r\n lng = df_Offence_Against_a_Person$lon_offset, lat=df_Offence_Against_a_Person$lat_offset, \r\n popup = df_Offence_Against_a_Person$Case_Type, \r\n radius = 2, \r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Offence_Against_a_Person$cid),\r\n group = \"4 - Offence_Against_a_Person\")\r\nm = addCircleMarkers(m, \r\n lng = df_Homicide$lon_offset, lat=df_Homicide$lat_offset, \r\n popup = df_Homicide$Case_Type, \r\n radius = 2,\r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(df_Homicide$cid),\r\n group = \"5 - Homicide\")\r\n\r\nm = addTiles(m, group = \"OSM (default)\") \r\nm = addProviderTiles(m,\"Stamen.Toner\", group = \"Toner\")\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n\r\nm = addLayersControl(m,\r\n baseGroups = c(\"Toner Lite\",\"Toner\"),\r\n overlayGroups = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\")\r\n)\r\n\r\n# make the map\r\nm\r\n\r\n#-------------------------Version2----------------------------#\r\n#-------------------------------------------------------------#\r\n# add the leaflet library to your script\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# subset out my crime_filtered:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\ndata_filterlist = list(df_Mischief = subset(data_filter, cid == 1),\r\n df_Theft = subset(data_filter, cid == 2),\r\n df_Break_and_Enter = subset(data_filter, cid == 3),\r\n df_Offence_Against_a_Person = subset(data_filter, cid == 4),\r\n df_Homicide = subset(data_filter, cid == 5))\r\n\r\n# Remember we also had these groups associated with each variable? Let's put them in a list too:\r\nlayerlist = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\",\r\n \"0 - other\")\r\n\r\n# We can keep that same color variable:\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\n\r\n# Now we have our loop - each time through the loop, it is adding our markers to the map object:\r\nfor (i in 1:length(data_filterlist)){\r\n m = addCircleMarkers(m, \r\n lng=data_filterlist[[i]]$lon_offset,\r\n lat=data_filterlist[[i]]$lat_offset, \r\n popup=data_filterlist[[i]]$Case_Type, \r\n radius=2,\r\n stroke = FALSE, \r\n fillOpacity = 0.75,\r\n color = colorFactors(data_filterlist[[i]]$cid),\r\n group = layerlist[i]\r\n )\r\n}\r\nm = addTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\") \r\nm = addLayersControl(m,\r\n overlayGroups = c(\"Toner Lite\"),\r\n baseGroups = layerlist\r\n)\r\nm\r\n\r\n#----------------------Version3-1--------------------------#\r\n#----------------------------------------------------------#\r\nlibrary(rgdal)\r\nlibrary(GISTools)\r\n# define the filepath\r\nhex_1401_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m_1401_counts.geojson'\r\n# read in the geojson\r\nhex_1401 = readOGR(hex_1401_fn, \"OGRGeoJSON\")\r\n\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\")\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\n palette = \"Greens\",\r\n domain = hex_1401$data\r\n)\r\n\r\n# add the polygons to the map\r\nm = addPolygons(m, \r\n data = hex_1401,\r\n stroke = FALSE, \r\n smoothFactor = 0.2, \r\n fillOpacity = 1,\r\n color = ~pal(hex_1401$data),\r\n popup = paste(\"Number of calls: \", hex_1401$data, sep=\"\")\r\n)\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_1401$data,\r\n title = \"Vancouver Crime Distribution in 2004\",\r\n labFormat = labelFormat(prefix = \" \"),\r\n opacity = 0.75\r\n)\r\nm\r\n\r\n#---------------------------Version3-2----------------------------#\r\n#-----------------------------------------------------------------#\r\nlibrary(rgdal)\r\nlibrary(GISTools)\r\nlibrary(leaflet)\r\nfilename = \"C:\/Users\/Administrator\/Desktop\/crime_filtered.csv\"\r\ndata_filter = read.csv(curl('https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/201401CaseLocationsDetails-geo-filtered.csv'), header=TRUE)\r\n# initiate leaflet map layer\r\nm = leaflet()\r\nm = addProviderTiles(m, \"Stamen.TonerLite\", group = \"Toner Lite\") \r\n\r\n# --- hex grid --- #\r\n# store the file name of the geojson hex grid\r\nhex_1401_fn = 'https:\/\/raw.githubusercontent.com\/joeyklee\/aloha-r\/master\/data\/calls_2014\/geo\/hgrid_250m_1401_counts.geojson'\r\n\r\n# read in the data\r\nhex_1401 = readOGR(hex_1401_fn, \"OGRGeoJSON\")\r\n\r\n# Create a continuous palette function\r\npal = colorNumeric(\r\n palette = \"Greens\",\r\n domain = hex_1401$data\r\n)\r\n\r\n# add hex grid\r\nm = addPolygons(m, \r\n data = hex_1401,\r\n stroke = FALSE, \r\n smoothFactor = 0.2, \r\n fillOpacity = 1,\r\n color = ~pal(hex_1401$data),\r\n popup= paste(\"Number of calls: \",hex_1401$data, sep=\"\"),\r\n group = \"hex\"\r\n)\r\n\r\n# add legend\r\nm = addLegend(m, \"bottomright\", pal = pal, values = hex_1401$data,\r\n title = \"Vancouver Crime Distribution in 2004\",\r\n labFormat = labelFormat(prefix = \" \"),\r\n opacity = 0.75\r\n)\r\n\r\n# --- points data --- #\r\n# subset out my crime_filtered:\r\ndf_Mischief = subset(data_filter, cid == 1)\r\ndf_Theft = subset(data_filter, cid == 2)\r\ndf_Break_and_Enter = subset(data_filter, cid == 3)\r\ndf_Offence_Against_a_Person = subset(data_filter, cid == 4)\r\ndf_Homicide = subset(data_filter, cid == 5)\r\n\r\ndata_filterlist = list(df_Mischief = subset(data_filter, cid == 1),\r\n df_Theft = subset(data_filter, cid == 2),\r\n df_Break_and_Enter = subset(data_filter, cid == 3),\r\n df_Offence_Against_a_Person = subset(data_filter, cid == 4),\r\n df_Homicide = subset(data_filter, cid == 5))\r\n\r\nlayerlist = c(\"1 - Mischief\", \"2 - Theft\",\r\n \"3 - Break_and_Enter\",\"4 - Offence_Against_a_Person\", \"5 - Homicide\",\r\n \"0 - other\")\r\n\r\ncolorFactors = colorFactor(c('red', 'orange', 'purple', 'blue', 'pink', 'brown'), domain=data_filter$cid)\r\nfor (i in 1:length(data_filterlist)){\r\n m = addCircleMarkers(m, \r\n lng=data_filterlist[[i]]$lon_offset, lat=data_filterlist[[i]]$lat_offset, \r\n popup=data_filterlist[[i]]$Case_Type, \r\n radius=2,\r\n stroke = FALSE, fillOpacity = 0.75,\r\n color = colorFactors(data_filterlist[[i]]$cid),\r\n group = layerlist[i]\r\n )\r\n }\r\nm = addLayersControl(m,\r\n overlayGroups = c(\"Toner Lite\", \"hex\"),\r\n baseGroups = layerlist\r\n)\r\n# show map\r\nm\r\n<\/code><\/pre>\n<p><strong>Visualization:\u00a0<\/strong><\/p>\n<p>When making this map, I think the cartographic constraint is the final map, which combines the point with the polygon. When seeing this final map, there are so many points with deep colour, and the green polygons are covered by these points. As a result, it is unclear for people to see the frequency distribution on the map through the green polygons.<\/p>\n<p><strong>Insight:<\/strong><\/p>\n<p>According to the map, I can see downtown has the highest crime frequency no matter which types of crime. In addition, the theft and break and enter crime both are the most common rascality in Vancouver. \u00a0Downtown has the largest population density and lots of homeless people. It is make sense that these floating population bring more criminal activities. Furthermore, when focusing on downtown, I also notice that the east side has more criminal activities than the west side. Related to the real world, the east side is actually bad than other places, especially in the main street of downtown. Here is filled with lots of drug activities, erotic trades and drunken riots. There is no doubt that these bad activities will bring more criminal activities.<\/p>\n<p>In addition to downtown, the distribution of theft and breaking crime in Vancouver area . When I see these criminal activities, they are more common on the busy street like the boardway and kingsway. Generally, the east side of Vancouver has more criminal activities than the west side of Vancouver. This is maybe caused by the population class. Due to the low rent and house price, there are more low educated and jobless people in the east side. These people maybe steal valuable things to sell. As a result, more theft and breaking crime happen here.<\/p>\n<p><strong>Reflection:<\/strong><\/p>\n<p>Now, I have experienced ArcGIS, Adobe Illustrator and R studio for the map making. My favourite software is the AI. It is good to design the infographic with a nice picture, but it is hard to deal with the enormous data. To process the huge data, R studio is better to operate. However, when comparing it to other softwares, it requires more base knowledge for programming. Thus, it has a high level difficulty for most people to design. The ArcGIS is good at data processing and map display. However, when comparing its map to the AI, it is not good-looking; when comparing its data processing to the R studio, it is not fast and accurate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; # # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Install Libraries &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- # # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; # install.packages(&#8220;GISTools&#8221;) install.packages(&#8220;RJSONIO&#8221;) install.packages(&#8220;rgdal&#8221;) install.packages(&#8220;RCurl&#8221;) install.packages(&#8220;curl&#8221;) # Unused Libraries: # install.packages(&#8220;ggmap&#8221;) # library(ggmap) # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; # # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; Load Libararies &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; # # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; # library(GISTools) library(RJSONIO) library(rgdal) library(RCurl) library(curl) Step1: This step is the beginning, and to make sure the R studio has [&hellip;]<\/p>\n","protected":false},"author":34603,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/users\/34603"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":8,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/posts\/25\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/rzgeography\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}