0
votes

I've been trying to map with leaflet to add addCircles() but my shapefile doesn't seem to have the latitude lat and longitude lng parameters so I got the centroids for each city as is the code below but it doesn't seem to show me the centroid points so I put in a df these values but I can't automate to get the points for n cities.

The data is here.

library(stringr)
library(leaflet)
library(sf)
library(dplyr)
quito = st_read("C:/Users/crist/Downloads/Administraciones Zonales/Administración_Zonal.shp") %>% 
  st_simplify(dTolerance = 1000) %>% 
  sf::st_transform('+init=epsg:4326')

sectores = read.csv("C:/Users/crist/Downloads/sector.csv", header = T,sep = ";", dec = ",", row.names = 1)
sectores

full_data = inner_join(quito, sectores, by = 'NOMBRE') %>%
  mutate(label_map = sprintf("<strong>%s </strong><br/>Valor: %g<br/>",NOMBRE, TARIFA_PROMEDIO_POR_HAB_DISPONIBLE_...) %>% lapply(htmltools::HTML))

bins = c(0, 10, 50, 100, 150,250)
pal_quito <- colorBin("Oranges", domain = full_data$TARIFA_PROMEDIO_POR_HAB_OCUPADA_..., 
                      bins = bins)

#I tried to do this because my shp didn't have the longitude and latitude variables 

full_data$centroids <- st_transform(full_data, 29101) %>% 
  st_centroid() %>% 
  st_geometry()

lngt_q = c(-78.41782, -78.67333, -78.4823, -78.60407, -78.36822, -78.50851, -78.56278, -78.3023)
lat_q = c(-0.08668143, -0.2179538, -0.1585809, 0.09029626, -0.4124271, -0.2112893, -0.311081, -0.2025039)

full_data$lngt_q =lngt_q
full_data$lat_q =lat_q

leaflet(data = full_data) %>%
  addTiles() %>% 
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(data = full_data,
              color = "#444444", 
              weight = 1,
              smoothFactor = 0.5, 
              fillOpacity = 0.2,
              label = full_data$label_map, 
              fillColor = pal_quito(full_data$TARIFA_PROMEDIO_POR_HAB_OCUPADA_...),
              stroke = T,
              highlightOptions = highlightOptions(
                weight = 5,
                color = "#666666",
                fillOpacity = 0.7)) %>% 
  addCircles(full_data$centroids, lng = lngt_q, lat = lat_q, color = "#045a8d", weight = 1, radius =1500,
             fillOpacity = 0.2)

[updated] There's a way to get the longitude and latitude of my shapefile automatically.

enter image description here

1

1 Answers

1
votes

I think you can do this by adding the centroid geometry to your polygon layer with st_centroid.

library(stringr)
library(leaflet)
library(sf)
library(dplyr)
library(mapview)  # I believe this is needed to make code above function

quito = st_read("C:/Users/Brian/Downloads/Administraciones Zonales/Administración_Zonal.shp") %>% 
      st_simplify(dTolerance = 1000) %>%
      #logintud y latitud   # this produced an error, thin it is intended as comment
      sf::st_transform('+init=epsg:4326')

## Adding the centroid of each polygon as a separate geometry column. This will not be active but can be accessed as needed
quito$geom2 = st_centroid(quito)

You will get a warning indicating that the centroids won't be accurate, because you are in a geographic rather than projected coordinate system and st_centroid assumes projected geometry. I would guess this isn't going to cause problems with reasonably small sized polygons at lower or middle latitudes, but you should be aware of the potential for distortion. If you need more accuracy, you could calculate your centroids before you warp into EPSG:4326 (WGS84). If you are going this route, you probably want to create the centroids as separate points, warp them separately and either join or use the separate points as your dataset to add the circles to your map.

At this point you can continue along the script as written until the last line, when you need to specify the geom2 column we created earlier as the data source

      addCircles(data = full_data$geom2, fill = TRUE, stroke = TRUE, color = "#000", fillColor = "blue", radius = 800, weight = 1)   ## I increased the radius to get it to display 

enter image description here

I don't know best practices to share the leaflet here, but static image shown