class: title-slide, center, bottom background-image: linear-gradient(to bottom, transparent 0%, black 120%), url('img/cover.png') # "Learning GIS with<br>ice-cream parlors" Open Source in GIS conference • June 9th, 2021 .small[ Amit Levinson • [<svg viewBox="0 0 576 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:white;" xmlns="http://www.w3.org/2000/svg"> <path d="M280.37 148.26L96 300.11V464a16 16 0 0 0 16 16l112.06-.29a16 16 0 0 0 15.92-16V368a16 16 0 0 1 16-16h64a16 16 0 0 1 16 16v95.64a16 16 0 0 0 16 16.05L464 480a16 16 0 0 0 16-16V300L295.67 148.26a12.19 12.19 0 0 0-15.3 0zM571.6 251.47L488 182.56V44.05a12 12 0 0 0-12-12h-56a12 12 0 0 0-12 12v72.61L318.47 43a48 48 0 0 0-61 0L4.34 251.47a12 12 0 0 0-1.6 16.9l25.5 31A12 12 0 0 0 45.15 301l235.22-193.74a12.19 12.19 0 0 1 15.3 0L530.9 301a12 12 0 0 0 16.9-1.6l25.5-31a12 12 0 0 0-1.7-16.93z"></path></svg>](https://amitlevinson.com) [<svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:white;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg>](https://github.com/AmitLevinson) [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:white;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg>](https://twitter.com/Amit_Levinson) [<svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:white;" xmlns="http://www.w3.org/2000/svg"> <path d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3zM135.4 416H69V202.2h66.5V416zm-33.2-243c-21.3 0-38.5-17.3-38.5-38.5S80.9 96 102.2 96c21.2 0 38.5 17.3 38.5 38.5 0 21.3-17.2 38.5-38.5 38.5zm282.1 243h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9V416z"></path></svg>](https://www.linkedin.com/in/amit-levinson/) ] --- class: bold-last-item # About me -- - Sociology MA Graduate, BGU -- - Been using R Programming language for < 2 years -- - Had no idea what is GIS prior to using R -- - Enjoy: -- - Open source tools (R, Linux, Git, MySQL) -- - Blogging -- - Learning new things -- - 🍪 & 🥛 --- ## "Regular" data How I first encountered and learn to work with data: ```r head(iris) ``` ``` ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ``` --- ## Spatial data<sup>*</sup> -- How I encountered spatial data (e.g. `.shp`): ```r head(shelters[,2]) ``` ``` *## Simple feature collection with 6 features and 0 fields *## Geometry type: POINT *## Dimension: XY *## Bounding box: xmin: 181680.1 ymin: 574027.4 xmax: 181972.9 ymax: 574304 *## Projected CRS: Israel 1993 / Israeli TM Grid ## # A tibble: 6 x 1 ## geometry *## <POINT [m]> *## 1 (181710.6 574217.8) ## 2 (181680.1 574304) ## 3 (181820.6 574243) ## 4 (181819.3 574027.4) ## 5 (181972.9 574046.2) ## 6 (181912.3 574151.1) ``` .footnote[ \* To reflect my learning process, I address spatial data here as in a vector format (and not, e.g., raster) ] ??? - data of shelters across beer-sheva shapefiles --- background-image: url(https://media.giphy.com/media/kfLPQJRuivFwqGBxp3/giphy.gif) background-size: contain ??? Gif credit: tenor.com --- ## Returning to my comfort zone Used a `.csv` file instead: ``` ## # A tibble: 6 x 2 *## lat lon ## <dbl> <dbl> ## 1 31.3 34.8 ## 2 31.3 34.8 ## 3 31.3 34.8 ## 4 31.3 34.8 ## 5 31.3 34.8 ## 6 31.3 34.8 ``` --
--- class: inverse, center, middle # Exploring distances to the nearest ice-cream parlors --- # Background -- - Fiddled around but never really knew what I was doing -- - Participated in several days of the [#30Daysmapchallenge](https://github.com/tjukanovt/30DayMapChallenge) -- - Realized it's time to learn some spatial analysis .footnote[The talk is based on an [earlier blog post on the topic.](https://amitlevinson.com/blog/exploring-ice-cream-locations/) ] -- .center[] .pull-left[ ### Inspiration Dominic Royé's blog post about [distances to the sea in Iceland](https://dominicroye.github.io/en/2019/calculating-the-distance-to-the-sea-in-r/) ] -- .pull-right[ ### Serendipitous resource Michael Dorman's [Spatial data analysis workshop](https://michaeldorman.github.io/R-Spatial-Workshop-at-CBS-2021/main.html) at Israel's CBS ] --- # Data Where do you find ice-cream locations data? -- <img src='https://github.com/AmitLevinson/talks/blob/main/open-source-in-gis/img/golda_css.PNG?raw=true'> --- class: bold-last-item # Data collection - [Scrape the data from the website](https://github.com/AmitLevinson/Datasets/blob/master/golda/golda-icecreams.R) ``` ## [1] "\nאור עקיבא\n\nכשר\nהשקמים 8\n04-646-0705\n\n" ``` -- - Clean it -- - Geocode the data to get lat & long from addresses -- - Reverse geocode the long & lat and verify each address. -- Final output looked like this (total of 79 addresses): |city |street |number |location | lon| lat|google_address | |:---------|:--------|:-----------|:-------------------|--------:|--------:|:---------------------------------| |אור עקיבא |השקמים 8 |04-646-0705 |השקמים 8, אור עקיבא | 34.91794| 32.50436|Ha-Shikmim St 8, Or Akiva, Israel | ??? Notice we have long and lat, it's not yet even recognized as anything spatial. -- Discrepancies were manually verified (though I probably verified everything anyway). --- background-image: url(img/worst_day_top.jpg) background-size: contain --- background-image: url(img/worst_day_full.jpg) background-size: contain --- ## How does our data look? .center[ <iframe src='data/widgets/simple_map.html' width = 650, height = 500></iframe> ] .footnote[If you can't see the iframe, please refresh the page.] ??? We haven't done any manipulation yet, notice the warning message I wanted to enable users to interact with the map so leaflet was important here (and not, for example, ggplot) --- class: inverse, center, middle # Some spatial manipulations --- ## Transforming We might want to be more explicit with our spatial data: -- **Geographic** -- ``` ## Simple feature collection with 1 feature and 0 fields ## Geometry type: POINT ## Dimension: XY ## Bounding box: xmin: 34.86002 ymin: 32.02051 xmax: 34.86002 ymax: 32.02051 *## Geodetic CRS: WGS 84 ## # A tibble: 1 x 1 ## geometry ## <POINT [°]> ## 1 (34.86002 32.02051) ``` -- **Projected** -- ``` ## Simple feature collection with 1 feature and 0 fields ## Geometry type: POINT ## Dimension: XY ## Bounding box: xmin: 186911.1 ymin: 658636.9 xmax: 186911.1 ymax: 658636.9 *## Projected CRS: Israel 1993 / Israeli TM Grid ## # A tibble: 1 x 1 ## geometry ## <POINT [m]> ## 1 (186911.1 658636.9) ``` ??? -- Essentially we might want to know what coordinate reference system were using -- CRS as in how the coordinates in our geometries relate to the surface of the Earth. - WGS World Geodetic System, מערכת גאודטית עולמית) --- ## Grids We want to calculate distances to Golda, but **from where?** -- .column[ .center[ #### Original map <img src='img/plots/map.png' height = 400px> ] ] -- .column[ .center[ #### Grid layout (2km<sup>2</sup>) <img src='img/plots/squaregrid.png' height = 400px> ] ] -- .column[ .center[ #### Israel as grid <img src='img/plots/grid_map.png' height = 400px> ] ] ??? The grids help overcoming the reactivity we might find in web based apps --- ## Distances We have our grid cells, how do we identify the **nearest ice-cream location?** -- .pull-left[ Example for 1 grid cell to several Goldas: ```r example_distances <- st_distance(x = both_distances$geometry, y = both_distances$geometry.1[1]) # Print 10: head(set_units(example_distances, "km"), 10) ``` ``` ## Units: [km] ## [,1] ## [1,] 218.88346 ## [2,] 272.69260 *## [3,] 57.20938 ## [4,] 194.03313 ## [5,] 182.41663 ## [6,] 209.48198 ## [7,] 131.61719 ## [8,] 256.71129 ## [9,] 218.63946 ## [10,] 225.91495 ``` ] -- -- .pull-right[ <img src='img/plots/grid_example.png' height = 450px> ] --- ## Distances -- <img src="index_files/figure-html/unnamed-chunk-23-1.png" width="100%" /> --- ## Cosmetics <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:#3f51b5;" xmlns="http://www.w3.org/2000/svg"> <path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"></path></svg> -- ``` ## Rows: 7,375 ## Columns: 7 ## $ geometry.x <POLYGON [°]> POLYGON ((34.90188 29.49693..., POLYGON ((34.91766~ ## $ distance_km <dbl> 7.871391, 7.332709, 6.613721, 7.363470, 6.918497, 5.722077~ ## $ location_id <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3~ ## $ city <chr> "אילת", "אילת", "אילת", "אילת", "אילת", "אילת", "אילת", "א~ ## $ street <chr> "מתחם הספירל", "מתחם הספירל", "מתחם הספירל", "מתחם הספירל"~ ## $ number <chr> "08-9428989", "08-9428989", "08-9428989", "08-9428989", "0~ ## $ geometry.y <POINT [°]> POINT (34.95968 29.54952), POINT (34.95968 29.54952)~ ``` -- ```r create_label_distances <- function(km, street, city){ glue(" <div style='text-align:left;'> You are <span style='font-size:13px;'><b>`{round(km, 1)}`</b></span> km from the nearest location at:</div> <div style='text-align:right;'> `{street}`, `{city}`</div>") %>% HTML()} ``` -- ``` ## [[1]] ## <div style='text-align:left;'> ## You are <span style='font-size:13px;'><b>7.9</b></span> km from the nearest location at:</div> ## <div style='text-align:right;'> ## מתחם הספירל, אילת</div> ``` --- ## Et Voila .center[ <iframe src='data/widgets/full_map.html' width = 650, height = 500></iframe> ] .footnote[If you can't see the iframe, please refresh the page.] --- class: inverse, center,middle # How does this relate to open source --- ## Feedback -- <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M466.27 225.31c4.674-22.647.864-44.538-8.99-62.99 2.958-23.868-4.021-48.565-17.34-66.99C438.986 39.423 404.117 0 327 0c-7 0-15 .01-22.22.01C201.195.01 168.997 40 128 40h-10.845c-5.64-4.975-13.042-8-21.155-8H32C14.327 32 0 46.327 0 64v240c0 17.673 14.327 32 32 32h64c11.842 0 22.175-6.438 27.708-16h7.052c19.146 16.953 46.013 60.653 68.76 83.4 13.667 13.667 10.153 108.6 71.76 108.6 57.58 0 95.27-31.936 95.27-104.73 0-18.41-3.93-33.73-8.85-46.54h36.48c48.602 0 85.82-41.565 85.82-85.58 0-19.15-4.96-34.99-13.73-49.84zM64 296c-13.255 0-24-10.745-24-24s10.745-24 24-24 24 10.745 24 24-10.745 24-24 24zm330.18 16.73H290.19c0 37.82 28.36 55.37 28.36 94.54 0 23.75 0 56.73-47.27 56.73-18.91-18.91-9.46-66.18-37.82-94.54C206.9 342.89 167.28 272 138.92 272H128V85.83c53.611 0 100.001-37.82 171.64-37.82h37.82c35.512 0 60.82 17.12 53.12 65.9 15.2 8.16 26.5 36.44 13.94 57.57 21.581 20.384 18.699 51.065 5.21 65.62 9.45 0 22.36 18.91 22.27 37.81-.09 18.91-16.71 37.82-37.82 37.82z"></path></svg> Don't I have anything better to blog about? -- > Someone will always have what to say about your work -- ### Positive -- <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M466.27 286.69C475.04 271.84 480 256 480 236.85c0-44.015-37.218-85.58-85.82-85.58H357.7c4.92-12.81 8.85-28.13 8.85-46.54C366.55 31.936 328.86 0 271.28 0c-61.607 0-58.093 94.933-71.76 108.6-22.747 22.747-49.615 66.447-68.76 83.4H32c-17.673 0-32 14.327-32 32v240c0 17.673 14.327 32 32 32h64c14.893 0 27.408-10.174 30.978-23.95 44.509 1.001 75.06 39.94 177.802 39.94 7.22 0 15.22.01 22.22.01 77.117 0 111.986-39.423 112.94-95.33 13.319-18.425 20.299-43.122 17.34-66.99 9.854-18.452 13.664-40.343 8.99-62.99zm-61.75 53.83c12.56 21.13 1.26 49.41-13.94 57.57 7.7 48.78-17.608 65.9-53.12 65.9h-37.82c-71.639 0-118.029-37.82-171.64-37.82V240h10.92c28.36 0 67.98-70.89 94.54-97.46 28.36-28.36 18.91-75.63 37.82-94.54 47.27 0 47.27 32.98 47.27 56.73 0 39.17-28.36 56.72-28.36 94.54h103.99c21.11 0 37.73 18.91 37.82 37.82.09 18.9-12.82 37.81-22.27 37.81 13.489 14.555 16.371 45.236-5.21 65.62zM88 432c0 13.255-10.745 24-24 24s-24-10.745-24-24 10.745-24 24-24 24 10.745 24 24z"></path></svg> Someone asking for technical assistance about using the code in another country. -- <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M466.27 286.69C475.04 271.84 480 256 480 236.85c0-44.015-37.218-85.58-85.82-85.58H357.7c4.92-12.81 8.85-28.13 8.85-46.54C366.55 31.936 328.86 0 271.28 0c-61.607 0-58.093 94.933-71.76 108.6-22.747 22.747-49.615 66.447-68.76 83.4H32c-17.673 0-32 14.327-32 32v240c0 17.673 14.327 32 32 32h64c14.893 0 27.408-10.174 30.978-23.95 44.509 1.001 75.06 39.94 177.802 39.94 7.22 0 15.22.01 22.22.01 77.117 0 111.986-39.423 112.94-95.33 13.319-18.425 20.299-43.122 17.34-66.99 9.854-18.452 13.664-40.343 8.99-62.99zm-61.75 53.83c12.56 21.13 1.26 49.41-13.94 57.57 7.7 48.78-17.608 65.9-53.12 65.9h-37.82c-71.639 0-118.029-37.82-171.64-37.82V240h10.92c28.36 0 67.98-70.89 94.54-97.46 28.36-28.36 18.91-75.63 37.82-94.54 47.27 0 47.27 32.98 47.27 56.73 0 39.17-28.36 56.72-28.36 94.54h103.99c21.11 0 37.73 18.91 37.82 37.82.09 18.9-12.82 37.81-22.27 37.81 13.489 14.555 16.371 45.236-5.21 65.62zM88 432c0 13.255-10.745 24-24 24s-24-10.745-24-24 10.745-24 24-24 24 10.745 24 24z"></path></svg> Micha Silver emailing me with complete code on how to do the same thing with rasters. -- ??? - The person trying using the 2039, but that's the one for Israel! - Micha provided the code for using a raster approach. I was and currently barley am familiar with raster, but this definitely opens up new avenues and glad to have received that feedback. --- ## Personal reasons -- - GIS was available in an already used OS platform (R) -- - Free -- - Can use/change others' code freely -- - Pushes to high standards knowing others see/read your code -- - Get to contribute back -- - **The community!** --- class: inverse, center, middle # Thank you! <img src='img/icecream.png' height =200px>