Skip to content

Instantly share code, notes, and snippets.

@idshklein
Created November 12, 2024 19:28
Show Gist options
  • Save idshklein/b862fa32bcb319c61f874b799c2d8064 to your computer and use it in GitHub Desktop.
Save idshklein/b862fa32bcb319c61f874b799c2d8064 to your computer and use it in GitHub Desktop.
pacman::p_load(WikidataQueryServiceR,tidyverse,sf,qgisprocess,ggrepel)
qgis_configure()
data <- query_wikidata('
SELECT ?place ?placeLabel ?time ?location ?instanceLabel
WHERE
{
wd:Q2910987 wdt:P625 ?loc .
SERVICE wikibase:around {
?place wdt:P625 ?location .
bd:serviceParam wikibase:center ?loc .
bd:serviceParam wikibase:radius "10" .
}
?place wdt:P585 | wdt:P580 ?time.
OPTIONAL { ?place wdt:P31 ?instance }
SERVICE wikibase:label { bd:serviceParam wikibase:language "he" }
BIND(geof:distance(?loc, ?location) as ?dist)
} ORDER BY ?dist')
q <- data %>%
st_as_sf(wkt = "location",crs = 4326) %>%
mutate(time = as_date(time)) %>%
filter(time > "1900-01-01") %>%
summarise() %>%
qgis_run_algorithm_p("native:orientedminimumboundingbox") %>%
qgis_extract_output() %>%
st_read()
ggplot() +
geom_sf(data = q) +
geom_sf_label(data = data %>%
st_as_sf(wkt = "location",crs = 4326) %>%
mutate(time = as_date(time)) %>%
filter(time > "1900-01-01"),mapping = aes(label = placeLabel))
q_cent <- q %>% st_centroid() %>% st_coordinates()
data %>%
st_as_sf(wkt = "location",crs = 4326) %>%
mutate(time = as_date(time),
x = st_coordinates(location)[,1],
y= st_coordinates(location)[,2],
relative_x = (x-q_cent[1,"X"]) * cos(q$angle * pi/180) +
(y-q_cent[1,"Y"]) * sin(q$angle* pi/180),
relative_y = -(x-q_cent[1,"X"]) * sin(q$angle* pi/180) +
(y-q_cent[1,"Y"]) * cos(q$angle* pi/180)) %>%
filter(time > "1900-01-01") %>%
ggplot(aes(x = relative_x,y = time, label=placeLabel)) + geom_label()
data %>%
st_as_sf(wkt = "location",crs = 4326) %>%
mutate(time = as_date(time),
x = st_coordinates(location)[,1],
y= st_coordinates(location)[,2],
relative_x = (x-q_cent[1,"X"]) * cos(q$angle * pi/180) +
(y-q_cent[1,"Y"]) * sin(q$angle* pi/180),
relative_y = -(x-q_cent[1,"X"]) * sin(q$angle* pi/180) +
(y-q_cent[1,"Y"]) * cos(q$angle* pi/180)) %>%
filter(time > "1900-01-01") %>%
ggplot(aes(x = -relative_y,y = time, label=placeLabel)) + geom_label()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment