Home > Blockchain >  I want to generate 100 random points with longitude and latitude?
I want to generate 100 random points with longitude and latitude?

Time:10-25

I am working on a school project, and need to generate 100 random points in Berlin or any other big city. I tried to do search for "pizza" in Berlin, but only got 17 points. It would be easier to just generate 100 random points.

How can I change this code to search for 100 random points with Google API?

library(osrm)
library(osrmr)
library(rjson)
library(cluster)
library(kmed)
library(googleway)
library(mapview)
library(tidyverse)
library(sf)
library(spData)


a <- google_places(search_string = 'pizza', location=c(52.516330935277914, 13.378463667974744),
              radius=8000, key='MY_API') #Henter data fra Google

b <- data.frame(a$results$geometry$location) #Subset av longitude and latitude

mapview(b, xcol="lng", ycol="lat", crs=4269, grid=FA

CodePudding user response:

What about something simple like defining a max and mix lat and long for Berlin and then sampling 100 points. If you want them to be spatial, you can use sf.

library(tidyverse)
library(sf)


set.seed(124)
points <- tibble(lat = runif(100, min = 13.350449, max = 13.419800),
                long = runif(100, min = 52.491840, max = 52.548662)) |>
  st_as_sf(coords = c("long", "lat"), crs = "EPSG:4326")

ggplot(points) 
  geom_sf()

PS, I'm not German and do not know the correct crs for appropriate maps in germany.

  • Related