0

I have two different datasets with latitude and longitude, one for manufacture and other for storage:

lat <- c(47.3523235, 47.5673237,47.606209) long <- c(-121.9837251, -121.8887269, -122.332071) facture_name <- c('X','Y','Z') manufacture <- data.frame(facture_name, lat, long) manufacture facture_name lat long 1 X 47.35232 -121.9837 2 Y 47.56732 -121.8887 3 Z 47.60621 -122.3321

lat <- c(46.535335,46.3786111,46.914479) long <- c(2121.9572497, -121.6919444, -121.642959) storage <- c('A','B','C') storage_db <- data.frame(storage, lat, long) storage_db storage lat long 1 A 46.53534 2121.9572 2 B 46.37861 -121.6919 3 C 46.91448 -121.6430

I am searching for a package/solution to determine the closest storage for each manufacture and calculate the distance. So I could then cluster the manufacture data based on distance from the storage.

Any guidance?

4
  • Hi nice to have you in our community. Is it possible to you improve the core of your question a little bit. IMO you want to build clusters based of the distance between manufacturer locations and storage locations with a certain capacity in common. It is not clear if you want to find a existing solution or a package or solotion procedure. Can you give an example to get the right understanding?
    – huckfinn
    Nov 21, 2021 at 21:39
  • Hi huckfinn thank you for getting back to me. I am searching for a solution procedure to determine the closest points of group1(manufacture) in group2(storage). I want to answer this question: " what is the distance from each manufacture to their closest storage?" I've seen some packages but all the solutions relate to a distance among a unit set of data. Nov 22, 2021 at 12:11
  • Hi cool, I don't know how familiar you are with R. R has some capabilities to cluster groups under a certain aspect (statmethods.net/advstats/cluster.html). One common key for the cluster analysis is to formulate some kind of a distance. The distance between each manufacturer and storage could help here. BTW Instead of answering me in a comment, you could improve your question by editing the text and embed some R related data or source content. Go to your question, click on edit and enrich the question with a related data frame.
    – huckfinn
    Nov 22, 2021 at 13:05
  • I do not have my detailed notes so I can only offer a comment and not a full answer. But the nngeo package in R will give you what you need directly. The vignette is found here: cran.r-project.org/web/packages/nngeo/vignettes/intro.pdf You can get nearest neighbors directly and even distances that you can join back to your original dataframes if need be. Nov 23, 2021 at 20:42

1 Answer 1

1

I give here an example with the package sf and the function st_nearest_feature, I assume that you are looking for the closest storage by using the geodesic distance.

Note that some of the coordinates provided in your example are likely wrong (longitude of 2121 ?)

# using the provided data in example
lat <- c(47.3523235, 47.5673237,47.606209)
long <- c(-121.9837251, -121.8887269, -122.332071)
facture_name <- c('X','Y','Z')
manufacture <- data.frame(facture_name, lat, long)

lat <- c(46.535335,46.3786111,46.914479)
long <- c(2121.9572497, -121.6919444, -121.642959)
storage <- c('A','B','C')
storage_db <- data.frame(storage, lat, long)


# loading the sf library
library(sf)

# creating the two spatial points dataset
manufacture_sf <- st_as_sf(x = manufacture, 
         coords = c("long", "lat"),
         crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

storage_sf <- st_as_sf(x = storage_db, 
                           coords = c("long", "lat"),
                           crs = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")

# reproject to have the distances in meters (need a better CRS)
manufacture_sf <- st_transform(manufacture_sf, 3857)
storage_sf <- st_transform(storage_sf, 3857)

# finding for each manufacture the index of its closest storage (geodesic distance)
idx <- st_nearest_feature(manufacture_sf, storage_sf)

# creating a new column in the manufacture dataset with the name of the closest storage
manufacture_sf$closest_storage <- storage_sf$storage[idx]

# calculating the distance between them
manufacture_sf$distance_to_storage <- st_distance(manufacture_sf, storage_sf[idx,], by_element = TRUE)

I hope it will help.

4
  • Hi Jerem Gelb , thank you very much! I tried your solution and gives me the results I was looking for ... Just one question on the output of st_distance , is there a way to convert it to KM ? Nov 30, 2021 at 11:00
  • To have an answer in KM, you should probably convert your CRS (EPSG 4326 in the example) to a CRS using coordinates in meters. This choice depends on you study area. If you have a world-wide dataset, you could use the EPSG 3847 (pseudo-mercator). Otherwise, local CRS will provide more accurate results. You can do this by applying the function st_transform (st_transform(manufacture_sf, 3857) and st_transform(storage_sf , 3857)) before calculating the distances. Nov 30, 2021 at 13:03
  • I get an error saying cannot transform sfc object with missing crs even tho I have up there the crs WGS84 as per your example Nov 30, 2021 at 21:52
  • I just edited the question to show how to do the reprojection. It works on my computer Dec 1, 2021 at 13:26

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.