Abel Pérez
Abel Pérez

Reputation: 107

Add csv data containing coordinates to a map generated from a shp file and obtain information on each point

I am new to programming in SAS and I have been investigating to be able to join a shp file downloaded from the INE with a store characterization file that contains two fields with X and Y coordinates.

I would like to make a map for each autonomous community, so far fine, I have managed to make a join between the "base" map that I have downloaded from the INE with my characterization data by census section and this I have represented on a map, so you can see the map of the Basque Country region and in red, the polygons where there are shops.

This is almost what I want, but I don't want the entire polygon to be colored, I just want a point to appear in the polygon where the store is exactly (X and Y coordinates) and I would also like that at the point, at pass the cursor to me of the information contained in the store characterization csv or the file downloaded from the INE.

I don't know if I have explained myself well, I hope so, how could I do this? Thank you very much in advance, if you can help me in this I would be very happy.

You can download the INE file that contains all the polygons in Spain with all the census sections on this page: https://www.ine.es/ss/Satellite?L=es_ES&c=Page&cid=1259952026632&p=1259952026632&pagename=ProductosYServicios%2FPYSLayout select the year 2021 and then go ahead.

I can't share the stores file, but it would work with any excel / csv that contains 2 fields, one with X coordinates and the other with Y coordinates.

https://ibb.co/26yqS8G In this image you can see what I have done with the code below

I would like something similar but instead of coloring the polygons where there are stores, the exact points where the stores are located appear and that point gives me information (which comes from from csv)

library(ggplot2)
library(sf)
install.packages('rnaturalearth')
library(rnaturalearth)



ruta = "J:\\Data Quality & Process\\ABEL\\MAPAS\\CARACT_ESTANCOS_UV.csv"
datos.pos_ = read.csv2(ruta)
names(datos.pos_)


datos.INE = st_read('Espana_Seccionado2021/SECC_CE_20210101.shp')
names(datos.INE)

#Voy a intentar seccionar los datos del INE. País vasco (NCA)

PV = datos.INE[datos.INE$NCA == 'País Vasco' , ]

plot(st_geometry(PV), border="#aaaaaa", main="País Vasco")

#Join
datos_merged <- merge(PV, datos.pos_, by.x = "CUSEC", by.y = "SECCION_CENSAL")

plot(st_geometry(PV), border="#aaaaaa", main="País Vasco")
plot(st_geometry(datos.pos_), add=T, col="red")
plot(st_geometry(datos_merged_radio), add=T, lwd = 2) #Esto es para añadir un radio pero tengo que poner mas codigo antes que esta en el archivo de Pruebas20220111
´´´

Upvotes: 0

Views: 407

Answers (1)

defuneste
defuneste

Reputation: 408

You need some flavor of leaflet. You should learn a bit about mapview.

Here this is a small example on Llanes (hoping it is also in Pais Vasco!).

The use of osmdata is just to get some shops around Llanes.

It should produce what you want. You can start with that then twink it with your data. good luck!

library(sf)
library(osmdata)
library(mapview)

#this will just download node for key = "shop" in Llanes
some_shop_in_Llanes <- osmdata::opq(osmdata::getbb("Llanes spain"),
                                   nodes_only = TRUE) %>% 
    osmdata::add_osm_feature(key = "shop") %>% 
    osmdata::osmdata_sf() #keep them in sf format 
# some_shop_in_Llanes is a bit more complex object but our shopes are in 
# some_shop_in_Lanes$osm_points

# here I get their coords X/Y
just_the_coord <- sf::st_coordinates(some_shop_in_Llanes$osm_points) 

# I get plenty of data so I selected only the name and X?Y
shop_with_coords <- cbind(some_shop_in_Llanes$osm_points, just_the_coord) %>% 
                        dplyr::select(name, X, Y)
                
# then plot it! 
mapview(shop_with_coords)

Editing to provide just a data frame with X/Y

# 1. Some shops
shop <- data.frame(X = c(-4.758628, -4.758244, -4.756829, -4.759394, -4.753698,
                         -4.735330, -4.864548, -4.863816, -4.784694, -4.738924),
                   Y = c(43.42144, 43.42244, 43.42063, 43.42170, 43.41899,
                         43.41181, 43.42327, 43.42370, 43.42422, 43.40150),
                   name = LETTERS[1:10])

# 2. Convert into an sf object
shop_sf <- sf::st_as_sf(shop, coords = c("X", "Y"))

# 2.b editing to add CRS my bad!
shop_sf  <- sf::st_set_crs(shop_sf, 4326)

# Plot them thanks to mapview
mapview(shop_sf)

Upvotes: 1

Related Questions