how to get lat and long

hey i want to get the lat and long of only those having distance less than 5000m or 5kms
how i can write code for that

 
    library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    set.seed(19)
    store=data.frame(lat=runif(1,33.45,33.75),long=runif(1,72.83,73.17))
    onlineloc=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    
    p=runif(1, 0, 1)
    if(p>0.5){instore=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    Sys.sleep(10)
    print(instore)
    }else{Sys.sleep(10)
      print("NO INSTORE CUSTOMER APPEARED")
    }
#>         lat     long
#> 1  33.70783 73.10150
#> 2  33.66670 73.14288
#> 3  33.60976 73.09793
#> 4  33.57661 73.06561
#> 5  33.60510 73.06707
#> 6  33.67357 72.91224
#> 7  33.59360 72.90426
#> 8  33.56916 73.14372
#> 9  33.63713 72.94552
#> 10 33.53505 72.88051
    #instore=data.frame(lat=runif(10,33.45,33.75),long=runif(10,72.83,73.17))
    instore
#>         lat     long
#> 1  33.70783 73.10150
#> 2  33.66670 73.14288
#> 3  33.60976 73.09793
#> 4  33.57661 73.06561
#> 5  33.60510 73.06707
#> 6  33.67357 72.91224
#> 7  33.59360 72.90426
#> 8  33.56916 73.14372
#> 9  33.63713 72.94552
#> 10 33.53505 72.88051
    instorecustomer=round(instore,digits = 2)
    onlinecustomer=round(onlineloc,digits = 2)
    plot(onlineloc,main="Online and instore customer loc",col=" red")
    points(instore,pch=2)
    points(store,pch=15)
    legend("bottomright",legend=c("Instore Customer","Online Customer","Store"),pch=c(2,1,15),col=c("black","red","black"))

    
    onlinecustomer
#>      lat  long
#> 1  33.65 73.10
#> 2  33.47 73.14
#> 3  33.56 72.91
#> 4  33.52 73.14
#> 5  33.54 72.97
#> 6  33.62 73.02
#> 7  33.70 72.98
#> 8  33.67 73.12
#> 9  33.57 73.07
#> 10 33.58 73.16
    instorecustomer
#>      lat  long
#> 1  33.71 73.10
#> 2  33.67 73.14
#> 3  33.61 73.10
#> 4  33.58 73.07
#> 5  33.61 73.07
#> 6  33.67 72.91
#> 7  33.59 72.90
#> 8  33.57 73.14
#> 9  33.64 72.95
#> 10 33.54 72.88
    
    library(sf)
    p1=onlinecustomer%>%st_as_sf(coords = c("lat","long"), crs = 4326)
    p2=instorecustomer%>%st_as_sf(coords = c("lat","long"), crs = 4326)
    #nrow(st_join(onlinecluster, instorecustomer, join = st_equals, left = F)) == nrow(p1)
    i=nrow(st_join(p1,p2, join = st_equals, left = F)) 
    orders=nrow(onlinecustomer) 
    orders=orders-i
    print(paste("No of task to be delivered are",orders))
#> [1] "No of task to be delivered are 10"
    
    library(data.table)
    df<-structure(list(ID=c(1:10), lat = c(onlinecustomer$lat)
                       , lon = c(onlinecustomer$long))
                  , col.Names = c("ID","lat", "lon"), row.names = c(NA, -10L), class = c("data.table","data.frame"))
    df
#>     ID   lat   lon
#>  1:  1 33.65 73.10
#>  2:  2 33.47 73.14
#>  3:  3 33.56 72.91
#>  4:  4 33.52 73.14
#>  5:  5 33.54 72.97
#>  6:  6 33.62 73.02
#>  7:  7 33.70 72.98
#>  8:  8 33.67 73.12
#>  9:  9 33.57 73.07
#> 10: 10 33.58 73.16
    ref<-structure(list(ID=letters[1:10], lat = c(instorecustomer$lat), lon = c(instorecustomer$long))
                   , col.Names = c("ID","lat", "lon"),row.names = c(NA, -10L), class = c("data.table","data.frame"))
    ref
#>     ID   lat   lon
#>  1:  a 33.71 73.10
#>  2:  b 33.67 73.14
#>  3:  c 33.61 73.10
#>  4:  d 33.58 73.07
#>  5:  e 33.61 73.07
#>  6:  f 33.67 72.91
#>  7:  g 33.59 72.90
#>  8:  h 33.57 73.14
#>  9:  i 33.64 72.95
#> 10:  j 33.54 72.88
    #Setting to data.table format
    setDT(df)
    setDT(ref)
    #creating a table with cartesian join 
    df1<-setkey(df[,c(k=1,.SD)],k)[ref[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]
    df1
    #calculating the Euclidean distance and giving a rank in ascending order of distance
    df1[,EuDist:=sqrt((lat-i.lat)^2+(lon-i.lon)^2)][,distRank:=rank(EuDist,ties="random"),by=.(ID)]
    df1
#>      ID   lat   lon i.ID i.lat i.lon     EuDist distRank
#>   1:  1 33.65 73.10    a 33.71 73.10 0.06000000        4
#>   2:  2 33.47 73.14    a 33.71 73.10 0.24331050        6
#>   3:  3 33.56 72.91    a 33.71 73.10 0.24207437        9
#>   4:  4 33.52 73.14    a 33.71 73.10 0.19416488        6
#>   5:  5 33.54 72.97    a 33.71 73.10 0.21400935        9
#>   6:  6 33.62 73.02    a 33.71 73.10 0.12041595        5
#>   7:  7 33.70 72.98    a 33.71 73.10 0.12041595        3
#>   8:  8 33.67 73.12    a 33.71 73.10 0.04472136        2
#>   9:  9 33.57 73.07    a 33.71 73.10 0.14317821        7
#>  10: 10 33.58 73.16    a 33.71 73.10 0.14317821        6
#>  11:  1 33.65 73.10    b 33.67 73.14 0.04472136        2
#>  12:  2 33.47 73.14    b 33.67 73.14 0.20000000        5
#>  13:  3 33.56 72.91    b 33.67 73.14 0.25495098       10
#>  14:  4 33.52 73.14    b 33.67 73.14 0.15000000        5
#>  15:  5 33.54 72.97    b 33.67 73.14 0.21400935       10
#>  16:  6 33.62 73.02    b 33.67 73.14 0.13000000        9
#>  17:  7 33.70 72.98    b 33.67 73.14 0.16278821        8
#>  18:  8 33.67 73.12    b 33.67 73.14 0.02000000        1
#>  19:  9 33.57 73.07    b 33.67 73.14 0.12206556        5
#>  20: 10 33.58 73.16    b 33.67 73.14 0.09219544        4
#>  21:  1 33.65 73.10    c 33.61 73.10 0.04000000        1
#>  22:  2 33.47 73.14    c 33.61 73.10 0.14560220        3
#>  23:  3 33.56 72.91    c 33.61 73.10 0.19646883        7
#>  24:  4 33.52 73.14    c 33.61 73.10 0.09848858        3
#>  25:  5 33.54 72.97    c 33.61 73.10 0.14764823        7
#>  26:  6 33.62 73.02    c 33.61 73.10 0.08062258        4
#>  27:  7 33.70 72.98    c 33.61 73.10 0.15000000        6
#>  28:  8 33.67 73.12    c 33.61 73.10 0.06324555        3
#>  29:  9 33.57 73.07    c 33.61 73.10 0.05000000        3
#>  30: 10 33.58 73.16    c 33.61 73.10 0.06708204        2
#>  31:  1 33.65 73.10    d 33.58 73.07 0.07615773        5
#>  32:  2 33.47 73.14    d 33.58 73.07 0.13038405        2
#>  33:  3 33.56 72.91    d 33.58 73.07 0.16124515        5
#>  34:  4 33.52 73.14    d 33.58 73.07 0.09219544        2
#>  35:  5 33.54 72.97    d 33.58 73.07 0.10770330        4
#>  36:  6 33.62 73.02    d 33.58 73.07 0.06403124        2
#>  37:  7 33.70 72.98    d 33.58 73.07 0.15000000        7
#>  38:  8 33.67 73.12    d 33.58 73.07 0.10295630        6
#>  39:  9 33.57 73.07    d 33.58 73.07 0.01000000        1
#>  40: 10 33.58 73.16    d 33.58 73.07 0.09000000        3
#>  41:  1 33.65 73.10    e 33.61 73.07 0.05000000        3
#>  42:  2 33.47 73.14    e 33.61 73.07 0.15652476        4
#>  43:  3 33.56 72.91    e 33.61 73.07 0.16763055        6
#>  44:  4 33.52 73.14    e 33.61 73.07 0.11401754        4
#>  45:  5 33.54 72.97    e 33.61 73.07 0.12206556        5
#>  46:  6 33.62 73.02    e 33.61 73.07 0.05099020        1
#>  47:  7 33.70 72.98    e 33.61 73.07 0.12727922        4
#>  48:  8 33.67 73.12    e 33.61 73.07 0.07810250        4
#>  49:  9 33.57 73.07    e 33.61 73.07 0.04000000        2
#>  50: 10 33.58 73.16    e 33.61 73.07 0.09486833        5
#>  51:  1 33.65 73.10    f 33.67 72.91 0.19104973        8
#>  52:  2 33.47 73.14    f 33.67 72.91 0.30479501       10
#>  53:  3 33.56 72.91    f 33.67 72.91 0.11000000        4
#>  54:  4 33.52 73.14    f 33.67 72.91 0.27459060       10
#>  55:  5 33.54 72.97    f 33.67 72.91 0.14317821        6
#>  56:  6 33.62 73.02    f 33.67 72.91 0.12083046        6
#>  57:  7 33.70 72.98    f 33.67 72.91 0.07615773        2
#>  58:  8 33.67 73.12    f 33.67 72.91 0.21000000        8
#>  59:  9 33.57 73.07    f 33.67 72.91 0.18867962        9
#>  60: 10 33.58 73.16    f 33.67 72.91 0.26570661        9
#>  61:  1 33.65 73.10    g 33.59 72.90 0.20880613        9
#>  62:  2 33.47 73.14    g 33.59 72.90 0.26832816        8
#>  63:  3 33.56 72.91    g 33.59 72.90 0.03162278        1
#>  64:  4 33.52 73.14    g 33.59 72.90 0.25000000        8
#>  65:  5 33.54 72.97    g 33.59 72.90 0.08602325        1
#>  66:  6 33.62 73.02    g 33.59 72.90 0.12369317        7
#>  67:  7 33.70 72.98    g 33.59 72.90 0.13601471        5
#>  68:  8 33.67 73.12    g 33.59 72.90 0.23409400        9
#>  69:  9 33.57 73.07    g 33.59 72.90 0.17117243        8
#>  70: 10 33.58 73.16    g 33.59 72.90 0.26019224        8
#>  71:  1 33.65 73.10    h 33.57 73.14 0.08944272        6
#>  72:  2 33.47 73.14    h 33.57 73.14 0.10000000        1
#>  73:  3 33.56 72.91    h 33.57 73.14 0.23021729        8
#>  74:  4 33.52 73.14    h 33.57 73.14 0.05000000        1
#>  75:  5 33.54 72.97    h 33.57 73.14 0.17262677        8
#>  76:  6 33.62 73.02    h 33.57 73.14 0.13000000        8
#>  77:  7 33.70 72.98    h 33.57 73.14 0.20615528       10
#>  78:  8 33.67 73.12    h 33.57 73.14 0.10198039        5
#>  79:  9 33.57 73.07    h 33.57 73.14 0.07000000        4
#>  80: 10 33.58 73.16    h 33.57 73.14 0.02236068        1
#>  81:  1 33.65 73.10    i 33.64 72.95 0.15033296        7
#>  82:  2 33.47 73.14    i 33.64 72.95 0.25495098        7
#>  83:  3 33.56 72.91    i 33.64 72.95 0.08944272        3
#>  84:  4 33.52 73.14    i 33.64 72.95 0.22472205        7
#>  85:  5 33.54 72.97    i 33.64 72.95 0.10198039        3
#>  86:  6 33.62 73.02    i 33.64 72.95 0.07280110        3
#>  87:  7 33.70 72.98    i 33.64 72.95 0.06708204        1
#>  88:  8 33.67 73.12    i 33.64 72.95 0.17262677        7
#>  89:  9 33.57 73.07    i 33.64 72.95 0.13892444        6
#>  90: 10 33.58 73.16    i 33.64 72.95 0.21840330        7
#>  91:  1 33.65 73.10    j 33.54 72.88 0.24596748       10
#>  92:  2 33.47 73.14    j 33.54 72.88 0.26925824        9
#>  93:  3 33.56 72.91    j 33.54 72.88 0.03605551        2
#>  94:  4 33.52 73.14    j 33.54 72.88 0.26076810        9
#>  95:  5 33.54 72.97    j 33.54 72.88 0.09000000        2
#>  96:  6 33.62 73.02    j 33.54 72.88 0.16124515       10
#>  97:  7 33.70 72.98    j 33.54 72.88 0.18867962        9
#>  98:  8 33.67 73.12    j 33.54 72.88 0.27294688       10
#>  99:  9 33.57 73.07    j 33.54 72.88 0.19235384       10
#> 100: 10 33.58 73.16    j 33.54 72.88 0.28284271       10
#>      ID   lat   lon i.ID i.lat i.lon     EuDist distRank
    #selecting the shortest distance
    df1<-df1[distRank==1]
    
    df1$lat
#>  [1] 33.67 33.65 33.57 33.62 33.56 33.54 33.47 33.52 33.58 33.70
    df1$lon
#>  [1] 73.12 73.10 73.07 73.02 72.91 72.97 73.14 73.14 73.16 72.98
    df1$i.lat
#>  [1] 33.67 33.61 33.58 33.61 33.59 33.59 33.57 33.57 33.57 33.64
    df1$i.lon
#>  [1] 73.14 73.10 73.07 73.07 72.90 72.90 73.14 73.14 73.14 72.95
    
    library(mapsapi)
    library(gmapsdistance)
    library(ggmap)
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
    register_google(key="")
    finalloc=paste0(LAT=df1$lat,"+",LONG=df1$lon)
    startloc=paste0(LAT=df1$i.lat,"+",LONG=df1$i.lon)
    
    finalloc
#>  [1] "33.67+73.12" "33.65+73.1"  "33.57+73.07" "33.62+73.02" "33.56+72.91"
#>  [6] "33.54+72.97" "33.47+73.14" "33.52+73.14" "33.58+73.16" "33.7+72.98"
    startloc
#>  [1] "33.67+73.14" "33.61+73.1"  "33.58+73.07" "33.61+73.07" "33.59+72.9" 
#>  [6] "33.59+72.9"  "33.57+73.14" "33.57+73.14" "33.57+73.14" "33.64+72.95"
    addresses <- data.frame(from = c(finalloc),
                            to = c(startloc))
    addresses <- as.data.frame(lapply(addresses, function(x) as.character(x)), stringsAsFactors = F)
    addresses     
#>           from          to
#> 1  33.67+73.12 33.67+73.14
#> 2   33.65+73.1  33.61+73.1
#> 3  33.57+73.07 33.58+73.07
#> 4  33.62+73.02 33.61+73.07
#> 5  33.56+72.91  33.59+72.9
#> 6  33.54+72.97  33.59+72.9
#> 7  33.47+73.14 33.57+73.14
#> 8  33.52+73.14 33.57+73.14
#> 9  33.58+73.16 33.57+73.14
#> 10  33.7+72.98 33.64+72.95
    d <- mapdist(addresses$from, addresses$to, mode = "driving",key="")
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.47+73.14&destinations=33.57+73.14&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.52+73.14&destinations=33.57+73.14&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.54+72.97&destinations=33.59+72.9&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.56+72.91&destinations=33.59+72.9&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.57+73.07&destinations=33.58+73.07&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.58+73.16&destinations=33.57+73.14&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.62+73.02&destinations=33.61+73.07&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.65+73.1&destinations=33.61+73.1&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.67+73.12&destinations=33.67+73.14&key=xxx&mode=driving
#> Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=33.7+72.98&destinations=33.64+72.95&key=xxx&mode=driving
   d
#> # A tibble: 10 x 9
#>    from        to              m    km miles seconds minutes  hours mode   
#>    <chr>       <chr>       <int> <dbl> <dbl>   <int>   <dbl>  <dbl> <chr>  
#>  1 33.67+73.12 33.67+73.14 12706 12.7   7.90    1421    23.7 0.395  driving
#>  2 33.65+73.1  33.61+73.1   3613  3.61  2.25     794    13.2 0.221  driving
#>  3 33.57+73.07 33.58+73.07 12762 12.8   7.93    1225    20.4 0.340  driving
#>  4 33.62+73.02 33.61+73.07  7413  7.41  4.61    1556    25.9 0.432  driving
#>  5 33.56+72.91 33.59+72.9   4258  4.26  2.65     677    11.3 0.188  driving
#>  6 33.54+72.97 33.59+72.9   2915  2.92  1.81     354     5.9 0.0983 driving
#>  7 33.47+73.14 33.57+73.14  7021  7.02  4.36    1679    28.0 0.466  driving
#>  8 33.52+73.14 33.57+73.14 16318 16.3  10.1     2827    47.1 0.785  driving
#>  9 33.58+73.16 33.57+73.14 11471 11.5   7.13    1563    26.0 0.434  driving
#> 10 33.7+72.98  33.64+72.95 19406 19.4  12.1     2131    35.5 0.592  driving

Created on 2020-10-24 by the reprex package (v0.3.0)

  1. convert data frames to simple features with st_as_sf
  2. pick a appropriate projection in meters
  3. pick an appropriate crs with st_transform
  4. pick a reference point (store?)
  5. use st_buffer to find points within radius
  6. review this

how i can get the lat and long of those greater than 5000 again as i have to use lat and long further.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

This approach is correct, but for a reference I've created some code to assist.

set.seed(19)
store = data.frame(lat = runif(1, 33.45, 33.75),
                   long = runif(1, 72.83, 73.17))
onlineloc = data.frame(lat = runif(10, 33.45, 33.75),
                       long = runif(10, 72.83, 73.17))


library(sf)

# convert to spatial objects with crs 4326
online_sf <-
  st_as_sf(onlineloc, coords = c("long", "lat"), crs = 4326)
store_sf <- st_as_sf(store, coords = c("long", "lat"), crs = 4326)

# crs 4326 permits measuring distances in meters.
# otherwise this would need to be the same unit


# calculate distances
dist_from_store <- st_distance(online_sf, store_sf)

# bind the distances to the online_sf object
bind_cols(online_sf, dist = dist_from_store) %>%
  # convert meters to numeric for filtering
  filter(as.numeric(dist) > 5000)
2 Likes