- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有没有办法检查一个简单的特征几何在 R 中是否连续?我正在用 ggplot2 创建一些 map ,它适用于单多边形连续单位,换句话说,没有岛屿或遥远领土的国家。例如,拉脱维亚工作正常:
但葡萄牙变得一团糟:
而拥有遥远领土的国家是一场灾难(这是荷兰):
我猜这是过滤非连续部分的几何图形然后分别填充它们的问题。某处是否有执行此操作的功能?
以葡萄牙为例进行编辑
(抱歉,图片需要单独下载,不能100%重现)
library(rnaturalearth)
library(rnaturalearthhires)
library(dplyr)
library(purrr)
library(tidyr)
library(scales)
library(magrittr)
library(png)
# > packageVersion("ggplot2")
# [1] ‘2.2.1.9000’
#devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
library(sf)
library(sp)
############ flag_fill function #############
flag_fill <- function(df){
# establish boundaries; rescale to boundaries; filter into polygon
# df must have columns 'geometry' and 'flag_image'
df <- as_data_frame(df) %>% st_as_sf()
# establish bounding boxes
xmin <- map(df$geometry, st_bbox) %>% map_dbl("xmin")
xmax <- map(df$geometry, st_bbox) %>% map_dbl("xmax")
ymin <- map(df$geometry, st_bbox) %>% map_dbl("ymin")
ymax <- map(df$geometry, st_bbox) %>% map_dbl("ymax")
# check for alpha value
alpha_check <- function(flag_image){
if(dim(flag_image)[3] > 3) hasalpha <- TRUE else hasalpha <- FALSE
}
alph <- map_lgl(df$flag_image, alpha_check)
# matrix of colours
NumRow <- map_dbl(df$flag_image, function(x) dim(x)[1])
NumCol <- map_dbl(df$flag_image, function(x) dim(x)[2])
matrixList <- vector("list", nrow(df))
matrixList <- mapply(matrix, matrixList, data = "#00000000",
nrow = NumRow, ncol = NumCol, byrow = FALSE)
matrixList <- map2(df$flag_image, alph, function(x, y) {
rgb(x[,,1], x[,,2], x[,,3],
ifelse(y, x[,,4], 1)
) %>%
matrix(ncol = dim(x)[2], nrow = dim(x)[1])
})
df_func <- function(DF){
suppressWarnings(
DF <- DF %>%
set_colnames(value = 1:ncol(.)) %>%
mutate(Y = nrow(.):1) %>%
gather(X, color, -Y) %>%
select(X, Y, color) %>%
mutate(X = as.integer(X))
)
return(DF)
}
matrixList <- map(matrixList, as.data.frame)
matrixList <- map(matrixList, df_func)
# resize
for(m in 1:length(matrixList)){
matrixList[[m]]$X <- rescale(matrixList[[m]]$X,
to = c(xmin[[m]], xmax[[m]]))
matrixList[[m]]$Y <- rescale(matrixList[[m]]$Y,
to = c(ymin[[m]], ymax[[m]]))
}
# filter into polygon
latlonList <- map(df$geometry, st_coordinates)
for(ll in 1:length(latlonList)){
latlonList[[ll]] <- latlonList[[ll]][, 1:2]
}
poly_check <- function(x, y){
x <- x[point.in.polygon(x$X, x$Y,
y[, 1],
y[, 2]
) %>%
as.logical, ]
return(x)
}
matrixList <- Map(poly_check, matrixList, latlonList)
# put back in dataframe:
df <- df %>%
mutate(latlon = latlonList, plot_image = matrixList)
return(df)
}
######## flag_plot function #############
flag_plot <- function(df){
# takes a dataframe with column 'state' for country or state,
# and plot_image, the result of flag_fill(), as well as 'color',
# also the result of flag_fill()
p <- ggplot()
df_list <- unique(df$state)
for (i in seq_along(df_list)){
DF <- df$plot_image[[i]]
p <- p + geom_tile(data = DF,
aes(x = X, y = Y),
fill = DF$color)
}
p + xlab(NULL) + ylab(NULL) +
geom_sf(data = df, size = .2, alpha = 0.01) +
theme(panel.background = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
panel.grid.major = element_line(colour = "white"), # hack from
#https://github.com/tidyverse/ggplot2/issues/2071
axis.ticks = element_blank(),
axis.line = element_blank())
}
######## data #########
globe <- countries10 %>% st_as_sf() %>%
filter(!is.na(ISO_A2)) %>%
select(state = SUBUNIT, iso = ISO_A2, continent = CONTINENT,
region = SUBREGION, geometry) %>%
mutate(iso = tolower(iso))
####### images #########
# pngs can be downloaded from here: https://github.com/hjnilsson/country-flags
# using png image 250px as working directory
country_list <- dir() %>% gsub("\\.png", '', .) %>%
.[which(!. %in% globe$iso)] %>% as_data_frame() %>% rename(iso = value)
globe <- left_join(globe, country_list) %>%
mutate(flag_image = list(array(NA, c(1, 1, 3))))
flags <- paste0(globe$iso, ".png")
for(i in 1:nrow(globe)){
globe$flag_image[[i]] <- readPNG(source = flags[[i]])
}
######## plot:
globe %>% filter(state %in% c("Portugal")) %>%
flag_fill() %>%
flag_plot()
最佳答案
问题不在sf
,但在 geom_tile()
.当我们有岛屿时,我们有许多多边形,但这段代码将它们视为一个多边形。
您可以修复将组列存储在 latlonList
中的问题。
for(ll in 1:length(latlonList)){
latlonList[[ll]] <- latlonList[[ll]][, c(1, 2, 4)]
}
poly_check()
计算组内多边形点的函数
poly_check <- function(x, y) {
island_list <- y %>%
as_tibble() %>%
group_by(L2) %>%
nest() %>%
pull(data)
lists <- map(island_list, ~{
y <- as.matrix(.x)
x[point.in.polygon(x$X, x$Y, y[, 1], y[, 2]) %>% as.logical, ]
})
bind_rows(lists, .id = ".id")
}
do()
简化此功能。
group = .id
内
geom_tile()
功能。
for (i in seq_along(df_list)){
DF <- df$plot_image[[i]]
p <- p + geom_tile(data = DF, aes(x = X, y = Y, group = .id),
fill = DF$color)
}
library(rnaturalearth)
library(rnaturalearthhires)
library(dplyr)
library(purrr)
library(tidyr)
library(scales)
library(magrittr)
library(png)
# > packageVersion("ggplot2")
# [1] ‘2.2.1.9000’
#devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
library(sf)
library(sp)
############ flag_fill function #############
flag_fill <- function(df){
# establish boundaries; rescale to boundaries; filter into polygon
# df must have columns 'geometry' and 'flag_image'
df <- as_data_frame(df) %>% st_as_sf()
# establish bounding boxes
xmin <- map(df$geometry, st_bbox) %>% map_dbl("xmin")
xmax <- map(df$geometry, st_bbox) %>% map_dbl("xmax")
ymin <- map(df$geometry, st_bbox) %>% map_dbl("ymin")
ymax <- map(df$geometry, st_bbox) %>% map_dbl("ymax")
# check for alpha value
alpha_check <- function(flag_image){
if(dim(flag_image)[3] > 3) hasalpha <- TRUE else hasalpha <- FALSE
}
alph <- map_lgl(df$flag_image, alpha_check)
# matrix of colours
NumRow <- map_dbl(df$flag_image, function(x) dim(x)[1])
NumCol <- map_dbl(df$flag_image, function(x) dim(x)[2])
matrixList <- vector("list", nrow(df))
matrixList <- mapply(matrix, matrixList, data = "#00000000",
nrow = NumRow, ncol = NumCol, byrow = FALSE)
matrixList <- map2(df$flag_image, alph, function(x, y) {
rgb(x[,,1], x[,,2], x[,,3], ifelse(y, x[,,4], 1)) %>%
matrix(ncol = dim(x)[2], nrow = dim(x)[1])
})
df_func <- function(DF){
suppressWarnings(
DF <- DF %>%
set_colnames(value = 1:ncol(.)) %>%
mutate(Y = nrow(.):1) %>%
gather(X, color, -Y) %>%
select(X, Y, color) %>%
mutate(X = as.integer(X))
)
return(DF)
}
matrixList <- map(matrixList, as.data.frame)
matrixList <- map(matrixList, df_func)
# resize
for(m in 1:length(matrixList)){
matrixList[[m]]$X <- rescale(matrixList[[m]]$X, to = c(xmin[[m]], xmax[[m]]))
matrixList[[m]]$Y <- rescale(matrixList[[m]]$Y, to = c(ymin[[m]], ymax[[m]]))
}
# filter into polygon
latlonList <- map(df$geometry, st_coordinates)
for(ll in 1:length(latlonList)){
latlonList[[ll]] <- latlonList[[ll]][, c(1, 2, 4)]
}
poly_check <- function(x, y) {
island_list <- y %>%
as_tibble() %>%
group_by(L2) %>%
nest() %>%
pull(data)
lists <- map(island_list, ~{
y <- as.matrix(.x)
x[point.in.polygon(x$X, x$Y, y[, 1], y[, 2]) %>% as.logical, ]
})
bind_rows(lists, .id = ".id")
}
matrixList <- Map(poly_check, matrixList, latlonList)
# put back in dataframe:
df <- df %>%
mutate(latlon = latlonList, plot_image = matrixList)
return(df)
}
######## flag_plot function #############
flag_plot <- function(df){
# takes a dataframe with column 'state' for country or state,
# and plot_image, the result of flag_fill(), as well as 'color',
# also the result of flag_fill()
p <- ggplot()
df_list <- unique(df$state)
for (i in seq_along(df_list)){
DF <- df$plot_image[[i]]
p <- p + geom_tile(data = DF, aes(x = X, y = Y, group = .id),
fill = DF$color)
}
p + xlab(NULL) + ylab(NULL) +
geom_sf(data = df, size = .2, alpha = 0.01) +
theme(panel.background = element_blank(),
panel.border = element_blank(),
axis.text = element_blank(),
panel.grid.major = element_line(colour = "white"), # hack from
#https://github.com/tidyverse/ggplot2/issues/2071
axis.ticks = element_blank(),
axis.line = element_blank())
}
######## data #########
globe <- countries10 %>%
st_as_sf() %>%
filter(!is.na(ISO_A2)) %>%
select(state = SUBUNIT, iso = ISO_A2, continent = CONTINENT,
region = SUBREGION, geometry) %>%
mutate(iso = tolower(iso))
####### images #########
# pngs can be downloaded from here: https://github.com/hjnilsson/country-flags
# using png image 250px as working directory
country_list <- dir() %>%
gsub("\\.png", '', .) %>%
.[which(!. %in% globe$iso)] %>%
as_data_frame() %>%
rename(iso = value)
globe <- left_join(globe, country_list) %>%
mutate(flag_image = list(array(NA, c(1, 1, 3))))
flags <- paste0(globe$iso, ".png")
for(i in 1:nrow(globe)) {
globe$flag_image[[i]] <- readPNG(source = flags[[i]])
}
######## plot:
globe %>% filter(state %in% c("Portugal")) %>%
flag_fill() %>%
flag_plot()
关于r - 检查 SF 几何在 R 中是否连续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48366952/
我是 C++ 的新手,所以请放轻松。 我正在尝试使用 sfml 创建 RenderWindow。然后,在创建播放器时,该播放器关联的“窗口”对象被设置为先前创建的 RenderWindow。我的目的是
我有一个 sf 的列表我想行绑定(bind)以创建单个 sf 的对象目的。我正在寻找类似于 data.table::rbindlist 的函数,这将以有效的方式堆叠各个对象。 可重现示例的数据: my
我正在尝试在 R 中使用 sf 创建一个 95% 的最小凸多边形。只要我只将数据分组到 1 个变量上,我的代码就可以正常工作,但是当我分组到两个变量上时,输出将失去其 sf 类并且改为 grouped
我试图使用内连接或左连接连接两个 sf 数据帧。这些数据框内部都有几何列。我不断收到错误: check_join(x, y) 中的错误: y 应该是一个 data.frame;对于空间连接,使用 st
我对使用 SFML 图形库中的 sf::Shape 有疑问。在我的游戏中,我使用 sf::RectangleShapes。例如用户界面或播放器。这是一个和平的代码: std::unique_ptr r
我正在学习 C++ 中的 SFML 库。我一直在尝试通过制作一个包含两个独立的 std::map 的音乐类 (sf::Music) 和声音 (sf::Sound) 来实现一种在我的游戏中组织音频的有效
有没有一种简单的方法可以使 sf::Text 对象在 sf::RectangleShape 对象中居中? 文本具有可变长度,但在创建后不会改变。 我正在使用 SFML 2.4。 最佳答案 将一个对象置
在我的 SwiftUI 应用程序中,我的字符串名称是 SF 符号图像的名称,或存储在 Assets 目录中的图像。 我想创建一个 View ,首先尝试将图像显示为 SF 符号图像,如果不存在具有该名称
我一直在使用EhCache在我的项目中实现一些缓存。我已经将以下依赖项添加到我的pom.xml中 org.springframework spring-context 4.
我想创建一个数组,其中包含将绘制到窗口上的所有 Sprite 、文本和形状,我的问题是如何使这个数组同时具有 sf::Drawable 和 sf::Transformable? 最佳答案 您需要创建一
我得到了一个派生自 sf::Packet 的类,它在其构造函数中传递了一个引用 iots 类型的 Integer。现在在构造函数中,我尝试将 Integer 添加到 sf::Packet 的数据中,如
当我尝试编译以下代码时: SFMLSet.cpp: #include "SFMLSet.h" SFMLSet::SFMLSet(string texturePath) { if(!textur
我正在使用 sf::Clock 来控制循环。 我想知道是否允许我使用超过 1 个 sf::Clock,如果允许,是否所有“时钟”都将正常运行并按预期工作在所有操作系统上。 例如: sf::Clock
我将C++图形中的SFML libraby用于我的国际象棋游戏。 当您在游戏中移动棋子时,会发生鼠标左键事件。所以这是我最初的跟踪方式。 sf::Event e; if (e.type == sf::
我正在尝试使用 SFML 库制作简单的按钮。当我将鼠标放在按钮上时,该按钮应该会更改其文本颜色。 void Button::updateColor(sf::Vector2i MousePos) {
void update(bool moright, bool moleft) { Clock Clock(); if (moright == true){
我的 JSON 有问题。我的代码中的这一行抛出异常 String jsontxt = IOUtils.toString(new FileInputStream(Filename), "UTF-8");
代码如下: 引擎.h #include #include #include #include #include #include #include class Engine { publ
我意识到这可能是重复的,但我搜索了许多论坛和问题,知道是什么原因导致了问题,但无法在此处找到它。我正在使用 SFML 2.0,我已将错误追踪到: void GameObjectManager::Dra
我在 sf 类型的简单特征( POINT )中保存了多个轨迹.我想计算后续位置(即行)之间的欧几里得距离。到目前为止,我已经使用 Pythagorean formula for calculating
我是一名优秀的程序员,十分优秀!