Setting the data
# Get palestinian cities, convert it to sf dataframe, assign projection to the coordinates and save it
data(world.cities)
palestine_cities <- world.cities %>% filter(country.etc == "Palestine")
class(palestine_cities)
## [1] "data.frame"
palestine_cities <- as_tibble(palestine_cities)
write_excel_csv(palestine_cities,
file = "D:/R_Gaza/IRachailovich.github.io/data/palestinians_cities.csv")
names(palestine_cities) #get the names for the coordinate variables
## [1] "name" "country.etc" "pop" "lat" "long"
## [6] "capital"
palestine_cities_sf <- st_as_sf(palestine_cities, coords = c("lat", "long")) #convert to sf df tibble
class(palestine_cities_sf) #check if class has changed to sf dataframe tibble
## [1] "sf" "tbl_df" "tbl" "data.frame"
st_crs(palestine_cities_sf) #check for geographic projections
## Coordinate Reference System: NA
st_crs(palestine_cities_sf) <- 4326 #assign geographic projection (4326, i.e., longlat WGS84)
st_crs(palestine_cities_sf)$proj4string #check if the geographic projection was assigned
## [1] "+proj=longlat +datum=WGS84 +no_defs"
names(palestine_cities_sf) #check if "lat" and "long" variables were changed to "geometry" variable.
## [1] "name" "country.etc" "pop" "capital" "geometry"
st_write(palestine_cities_sf, "D:/R_Gaza/IRachailovich.github.io/data/palestinian_cities",
driver = "ESRI Shapefile", delete_layer = TRUE) # save the sf data frame as shapefile
## Writing layer `palestinian_cities' to data source
## `D:/R_Gaza/IRachailovich.github.io/data/palestinian_cities' using driver `ESRI Shapefile'
## Writing 244 features with 4 fields and geometry type Point.
unzip(zipfile = "D:/R_Gaza/gaza/data/localities_palestine_sf.zip" , exdir ="data/localities_palestine")
localities_palestine_sf <- st_read("data/localities_palestine")
## Reading layer `%D8%AA%D8%AC%D9%85%D8%B9%D8%A7%D8%AA_%D9%81%D9%84%D8%B3%D8%B7%D9%8A%D9%86___Localities_Palestine' from data source `D:\R_Gaza\IRachailovich.github.io\data\localities_palestine'
## using driver `ESRI Shapefile'
## Simple feature collection with 619 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 3809204 ymin: 3661924 xmax: 3960023 ymax: 3836009
## Projected CRS: WGS 84 / Pseudo-Mercator
localities_palestine_sf %>% head()
## Simple feature collection with 6 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 3822272 ymin: 3669405 xmax: 3844607 ymax: 3707141
## Projected CRS: WGS 84 / Pseudo-Mercator
## OBJECTID REGIONCODE DISTCODE GOVCODE LOCCODE NAMEAR
## 1 1 2 24 2470 24703430 عبسان الجديدة
## 2 2 2 24 2470 24703470 خزاعة
## 3 3 2 24 2465 24653200 مخيم دير البلح
## 4 4 2 24 2465 24653275 وادي السلقا
## 5 5 2 24 2455 24552681 أم النصر (القرية البدوية)
## 6 6 2 24 2465 24653065 مخيم النصيرات
## NAMEEN NOTES Shape__Are Shape__Len
## 1 'Abasan al Jadida <NA> 4919436.7 11010.191
## 2 Khuza'a <NA> 9193057.5 14360.481
## 3 Deir al Balah Camp <NA> 337604.1 3122.714
## 4 Wadi as Salqa <NA> 8752155.1 12611.773
## 5 Um Al-Nnaser (Al Qaraya al Badawiya) <NA> 4306257.9 9547.706
## 6 An Nuseirat Camp <NA> 1099596.7 10312.221
## geometry
## 1 MULTIPOLYGON (((3824072 367...
## 2 MULTIPOLYGON (((3826180 367...
## 3 MULTIPOLYGON (((3823061 368...
## 4 MULTIPOLYGON (((3827289 368...
## 5 MULTIPOLYGON (((3844490 370...
## 6 MULTIPOLYGON (((3827522 369...
class(localities_palestine_sf)
## [1] "sf" "data.frame"
st_crs(localities_palestine_sf)$proj4string
## [1] "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"
localities_palestine_WGS84 <- st_transform(localities_palestine_sf, crs = 4326)
st_crs(localities_palestine_WGS84)$proj4string
## [1] "+proj=longlat +datum=WGS84 +no_defs"
st_write(localities_palestine_WGS84, "D:/R_Gaza/IRachailovich.github.io/data/localities_palestine",
driver = "ESRI Shapefile", delete_layer = TRUE)
## Deleting layer `localities_palestine' failed
## Writing layer `localities_palestine' to data source
## `D:/R_Gaza/IRachailovich.github.io/data/localities_palestine' using driver `ESRI Shapefile'
## Writing 619 features with 10 fields and geometry type Multi Polygon.
st_crs(localities_palestine_WGS84)$proj4string
## [1] "+proj=longlat +datum=WGS84 +no_defs"
st_crs(palestine_cities_sf)$proj4string
## [1] "+proj=longlat +datum=WGS84 +no_defs"
Creating maps
simple_polygons <- plotTag({
ggplot(localities_palestine_WGS84)+
geom_sf(colour = "black")
}, "simple_polygons", width = 700, height = 400)
simple_polygons
save_html(html = simple_polygons, file = "simple_polygons.html")
col_fun <- colorFactor(c("#337bd7", "#c8143b"), domain = NULL)
itamar <- st_point(c(34.82375, 31.89530))
itamarpopup <- c("Where I.J.R was born")
pal_popup <- paste0("<strong>City Name:</strong>", palestine_cities$name)
palestine_map <- leaflet(localities_palestine_WGS84) %>%
addPolygons(stroke = TRUE, weight = 0.7, color = "black",
fillColor = ~col_fun(REGIONCODE),
group = "Regions", fillOpacity = 0.8) %>%
addCircleMarkers(lng = 34.82375 , lat = 31.89530, group = "Where I Live",
popup = itamarpopup) %>%
addCircleMarkers(data = palestine_cities, group = "Cities", popup = pal_popup,
radius = 0.01, color = "#2c120c") %>%
addTiles(group = "OSM") %>%
addProviderTiles(provider = "CartoDB", group = "CartoDB") %>%
addProviderTiles(provider = "CartoDB.Voyager", group = "CartoDB.Voyager") %>%
addProviderTiles(provider = "Esri", group = "Esri") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "CartoDB.Voyager", "Esri"),
overlayGroups = c("Regions", "Where I Live", "Cities"))
palestine_map