- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有现成的工具,或者是否有人开发了一种方法来确定空间形状的人轴地理方向。一般来说,我希望能够确定一个形状是东西方向还是南北方向,但理想情况下,每个形状都会有一个角度或度数测量值。
ArcGIS 提供了“计算主角工具”,但它是为正交形状设计的,我正在处理类似 Blob 或至少不是很正交的野火周界。乍一看,Arc 工具提供的测量结果非常粗略。
我想使用 sf 对象来执行此操作,因此例如可以使用 sf 包中的北卡罗来纳州数据。北卡罗来纳州 100 个县各自的地理方位是什么?
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
感谢您的帮助!
最佳答案
flightplanning-R
包有一个计算最小边界矩形、方位角、高度和宽度的函数。 ( https://github.com/caiohamamura/flightplanning-R )
我稍微调整了一下,并在下面的另一个函数中使用它来返回一个带有方向角和 POLYGON 几何列的 sf 对象。角度从 0(东西)到 180(也是东西),其中 90 是南北。
# Copied function getMinBBox()
# from https://github.com/caiohamamura/flightplanning-R/blob/master/R/utils.R
# credit there given to: Daniel Wollschlaeger <https://github.com/ramnathv>
library(tidyverse)
library(sf)
library(sfheaders)
nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) %>%
st_geometry() %>% st_as_sf()
getMinBBox <- function(x) {
stopifnot(is.matrix(x), is.numeric(x), nrow(x) >= 2, ncol(x) == 2)
## rotating calipers algorithm using the convex hull
H <- grDevices::chull(x) ## hull indices, vertices ordered clockwise
n <- length(H) ## number of hull vertices
hull <- x[H, ] ## hull vertices
## unit basis vectors for all subspaces spanned by the hull edges
hDir <- diff(rbind(hull, hull[1, ])) ## hull vertices are circular
hLens <- sqrt(rowSums(hDir^2)) ## length of basis vectors
huDir <- diag(1/hLens) %*% hDir ## scaled to unit length
## unit basis vectors for the orthogonal subspaces
## rotation by 90 deg -> y' = x, x' = -y
ouDir <- cbind(-huDir[ , 2], huDir[ , 1])
## project hull vertices on the subspaces spanned by the hull edges, and on
## the subspaces spanned by their orthogonal complements - in subspace coords
projMat <- rbind(huDir, ouDir) %*% t(hull)
## range of projections and corresponding width/height of bounding rectangle
rangeH <- matrix(numeric(n*2), ncol=2) ## hull edge
rangeO <- matrix(numeric(n*2), ncol=2) ## orthogonal subspace
widths <- numeric(n)
heights <- numeric(n)
for(i in seq(along=numeric(n))) {
rangeH[i, ] <- range(projMat[ i, ])
## the orthogonal subspace is in the 2nd half of the matrix
rangeO[i, ] <- range(projMat[n+i, ])
widths[i] <- abs(diff(rangeH[i, ]))
heights[i] <- abs(diff(rangeO[i, ]))
}
## extreme projections for min-area rect in subspace coordinates
## hull edge leading to minimum-area
eMin <- which.min(widths*heights)
hProj <- rbind( rangeH[eMin, ], 0)
oProj <- rbind(0, rangeO[eMin, ])
## move projections to rectangle corners
hPts <- sweep(hProj, 1, oProj[ , 1], "+")
oPts <- sweep(hProj, 1, oProj[ , 2], "+")
## corners in standard coordinates, rows = x,y, columns = corners
## in combined (4x2)-matrix: reverse point order to be usable in polygon()
## basis formed by hull edge and orthogonal subspace
basis <- cbind(huDir[eMin, ], ouDir[eMin, ])
hCorn <- basis %*% hPts
oCorn <- basis %*% oPts
pts <- t(cbind(hCorn, oCorn[ , c(2, 1)]))
## angle of longer edge pointing up
dPts <- diff(pts)
e <- dPts[which.max(rowSums(dPts^2)), ] ## one of the longer edges
eUp <- e * sign(e[2]) ## rotate upwards 180 deg if necessary
deg <- atan2(eUp[2], eUp[1])*180 / pi ## angle in degrees
return(list(pts=pts, width=heights[eMin], height=widths[eMin], angle=deg))
}
##############
## Use getMinBBox in a custom function to return an sf object
##############
min_box_sf <- function(x){
crs <- st_crs(x)
x_as_matrix <- st_coordinates(x)[,1:2]
min_box <- getMinBBox(x_as_matrix)
box <- sfheaders::sf_polygon(min_box$pts) %>%
st_set_crs(crs)
box$angle <- min_box$angle
box
}
# Testing on a county in the nc dataset with an unusual shape and orientation:
min_box_sf(nc[56,])
#> Simple feature collection with 1 feature and 2 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -76.19819 ymin: 35.11926 xmax: -75.31058 ymax: 36.23016
#> Geodetic CRS: NAD27
#> id geometry angle
#> 1 1 POLYGON ((-76.19819 36.0092... 117.4866
#Plotting county 56 & the associated minimum bounding box
ggplot() +
geom_sf(data = nc[56,],
fill = 'red',
alpha = .2) +
geom_sf(data = min_box_sf(nc[56,]),
fill = NA)
异常形状的戴尔县,北卡罗来纳州有一个“长”方向约为 117 度的最小边界框,或从北-西北到南-东南。
# Using the function on each row of an sf object.
# note the crs is not retained.
pmap_dfr(nc, min_box_sf)
#> Simple feature collection with 100 features and 2 fields
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.86573 xmax: -75.31058 ymax: 36.87134
#> CRS: NA
#> First 10 features:
#> id angle geometry
#> 1 1 177.0408464 POLYGON ((-81.74847 36.2486...
#> 2 1 179.0078231 POLYGON ((-81.3505 36.36728...
#> 3 1 178.4492784 POLYGON ((-80.97202 36.2365...
#> 4 1 136.8896308 POLYGON ((-75.59489 36.2906...
#> 5 1 149.5889916 POLYGON ((-77.71197 36.8713...
#> 6 1 179.5157854 POLYGON ((-77.21774 36.2322...
#> 7 1 147.1227419 POLYGON ((-75.90195 36.2792...
#> 8 1 0.1751954 POLYGON ((-76.95329 36.2937...
#> 9 1 0.1759289 POLYGON ((-78.32017 36.1949...
#> 10 1 179.0809855 POLYGON ((-80.02092 36.5467...
将所有县的最小边界框绘制在一起:
pmap_dfr(nc, min_box_sf) %>%
ggplot() +
geom_sf(alpha = .2)
由 reprex package 创建于 2021-08-20 (v2.0.1)
关于spatial - 有没有办法确定 sf 中多边形的主要方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67171573/
我是 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
我是一名优秀的程序员,十分优秀!