- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想弄清楚如何将坐标在空间引用 GDA94 (EPSG 4283) 中的多边形转换为 xy 坐标(逆仿射变换矩阵)。
以下代码有效:
import sys
import numpy as np
from osgeo import gdal
from osgeo import gdalconst
from shapely.geometry import Polygon
from shapely.geometry.polygon import LinearRing
# Bounding Box (via App) approximating part of QLD.
poly = Polygon(
LinearRing([
(137.8, -10.6),
(153.2, -10.6),
(153.2, -28.2),
(137.8, -28.2),
(137.8, -10.6)
])
)
# open raster data
ds = gdal.Open(sys.argv[1], gdalconst.GA_ReadOnly)
# get inverse transform matrix
(success, inv_geomatrix) = gdal.InvGeoTransform(ds.GetGeoTransform())
print inv_geomatrix
# build numpy rotation matrix
rot = np.matrix(([inv_geomatrix[1], inv_geomatrix[2]], [inv_geomatrix[4], inv_geomatrix[5]]))
print rot
# build numpy translation matrix
trans = np.matrix(([inv_geomatrix[0]], [inv_geomatrix[3]]))
print trans
# build affine transformation matrix
affm = np.matrix(([inv_geomatrix[1], inv_geomatrix[2], inv_geomatrix[0]],
[inv_geomatrix[4], inv_geomatrix[5], inv_geomatrix[3]],
[0, 0, 1]))
print affm
# poly is now a shapely geometry in gd94 coordinates -> convert to pixel
# - project poly onte raster data
xy = (rot * poly.exterior.xy + trans).T # need to transpose here to have a list of (x,y) pairs
print xy
这是打印矩阵的输出:
(-2239.4999999999995, 20.0, 0.0, -199.49999999999986, 0.0, -20.0)
[[ 20. 0.]
[ 0. -20.]]
[[-2239.5]
[ -199.5]]
[[ 2.00000000e+01 0.00000000e+00 -2.23950000e+03]
[ 0.00000000e+00 -2.00000000e+01 -1.99500000e+02]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00]]
[[ 516.5 12.5]
[ 824.5 12.5]
[ 824.5 364.5]
[ 516.5 364.5]
[ 516.5 12.5]]
有没有办法用 scipy.ndimage
的 affine_transform
函数做到这一点?
最佳答案
有几个选项。不是所有的空间变换都在线性空间,所以不能都用仿射变换,所以不要老是依赖它。如果您有两个 EPSG SRID,则可以使用 GDAL 的 OSR 模块进行通用空间变换。 I wrote an example a while back , 可以进行调整。
否则,仿射变换具有基本数学:
/ a b xoff \
[x' y' 1] = [x y 1] | d e yoff |
\ 0 0 1 /
or
x' = a * x + b * y + xoff
y' = d * x + e * y + yoff
可以在 Python 中通过点列表实现。
# original points
pts = [(137.8, -10.6),
(153.2, -10.6),
(153.2, -28.2),
(137.8, -28.2)]
# Interpret result from gdal.InvGeoTransform
# see http://www.gdal.org/classGDALDataset.html#af9593cc241e7d140f5f3c4798a43a668
xoff, a, b, yoff, d, e = inv_geomatrix
for x, y in pts:
xp = a * x + b * y + xoff
yp = d * x + e * y + yoff
print((xp, yp))
这与 Shapely 的 shapely.affinity.affine_transform
function 中使用的基本算法相同。 .
from shapely.geometry import Polygon
from shapely.affinity import affine_transform
poly = Polygon(pts)
# rearrange the coefficients in the order expected by affine_transform
matrix = (a, b, d, e, xoff, yoff)
polyp = affine_transform(poly, matrix)
print(polyp.wkt)
最后,值得一提的是 scipy.ndimage.interpolation.affine_transform
function适用于图像或栅格数据,而不是矢量数据。
关于python - 来自 gda94 的 affine_transform xy 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006237/
我有一个图像(保存为 numpy 数组),我想用变换矩阵对其进行变换。假设变换矩阵是: [[ 0.99729046 -0.07356456 22.57990962] [ 0.07356456
背景/背景 我试图重现 scipy 的 ndimage.affine_transform 输出的值函数,但与 scipy 实现相比,我似乎使用了不同的“三次”插值方案。 例子 让我们来看一个非常简单的
这段代码: from scipy.ndimage.interpolation import affine_transform import numpy as np ... nzoom = 1.2 ne
我的目标 是以这样一种方式转换图像,即三个源点映射到一个空数组中的三个目标点。我已经解决了找到正确仿射矩阵的问题,但是我无法对彩色图像应用仿射变换。 更具体地说,我正在努力正确使用 scipy.ndi
我正在尝试使用ndi.interpolation.affine_transform转换一个简单的矩阵,但我得到的结果是相反的。例如: import scipy.ndimage as ndi m = [
我想弄清楚如何将坐标在空间引用 GDA94 (EPSG 4283) 中的多边形转换为 xy 坐标(逆仿射变换矩阵)。 以下代码有效: import sys import numpy as np fro
我对 scipy.ndimage.interpolation.affine_transform 的 API 感到困惑.并根据 this issue 判断我不是唯一一个。我实际上想用 affine_tr
我正在尝试为时间序列中的数据创建热图(最终是散点图)。我想以一种表明它们处于线性时间轴上的方式来定位它们。 如何使用 matplotlib Affine2D 或 scipy.ndimage.affin
我是一名优秀的程序员,十分优秀!