0

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

0

Here! https://stackoverflow.com/a/61371842/11570343

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)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.