简体   繁体   English

在python中转换坐标

[英]Transforming coordinates in python

I have one coordinate system 4.3, 50.8, 4.4, 50.9 and I want to convert points in that box to a box that is 4800X4800 pixels. 我有一个坐标系4.3、50.8、4.4、50.9,我想将该框中的点转换为4800X4800像素的框中。 Is there anything in the python language that can help do these conversions or do I have to write the math myself? python语言中有什么可以帮助进行这些转换的吗,还是我必须自己写数学吗?

You could write something like this: 您可以这样写:

def remap_coords(x, y, x_min=4.3, y_min=50.8,
                 x_max=4.4, y_max=50.9, width=4800, height=4800):
    new_x = (x - x_min) / (x_max - x_min) * width
    new_y = (y - y_min) / (y_max - y_min) * height
    return (int(round(new_x)), int(round(new_y)))

If I have understood what you require, then I would recommend numpy for this: http://www.numpy.org/ 如果我了解您的要求,那么我建议为此使用numpy: http ://www.numpy.org/

Here are the methods I would use: 这是我会使用的方法:

y = numpy.linspace(50.8,50.9,4800)
x = numpy.linspace(4.3,4.4,4800)
x,y = numpy.meshgrid(x,y)

linspace will generate, in this instance, 4800 values linearly interpolated between 50.8 and 50.9 for y and 4.3 and 4.4 for x. 在这种情况下,linspace将生成4800个值,这些值在y的50.8和50.9之间以及x的4.3和4.4之间线性内插。 meshgrid is then employed to form these 1D coordinate arrays into two 2D coordinate arrays (both of size 4800 by 4800 pixels) for both the x and y values. 然后使用meshgrid将这些1D坐标数组形成为x和y值的两个2D坐标数组(大小均为4800 x 4800像素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM