将坐标转换为没有丢失点的像素

时间:2017-07-24 18:34:03

标签: python numpy coordinates gis coordinate-transformation

我正在尝试将坐标从纬度/经度转换为像素,但在此过程中我失去了分数。

我正在使用的代码如下:

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

df=pd.read_csv('cords.csv')
cords=df.as_matrix(columns=['x','y'])
gt=[7.6445503225, 5.4065168747250134e-06,  0.0,  45.07436634583334,  0.0, -5.406516856707135e-06]

index=np.zeros(cords.shape)
index[:,1]=((cords[:,1] - gt[3]) / gt[5]).round()
index[:,0]=((cords[:,0] - gt[0]) / gt[1]).round()
index=index.astype(int)
index[:,0]=index[:,0]-min(index[:,0])+1
index[:,1]=index[:,1]-min(index[:,1])+1
row=max(index[:,1])
col=max(index[:,0])
image=np.zeros([row+1,col+1])
for i in range(0,len(index)):
    image[index[i,1],index[i,0]]=255

如果我绘制坐标或索引点,我得到这个: enter image description here

如果我绘制图像,我得到这个: enter image description here

正如您所看到的,将lat / lon转换为像素数时缺少一些要点。黄色是255值,紫色是0值。 怎么解决这个问题?

在这里,您可以找到我正在使用的坐标cords.csv

在这里,您可以找到需要为每个像素设置的值的坐标。 cords_valus.csv

2 个答案:

答案 0 :(得分:0)

当您的小数点坐标被舍入到图像的整数索引时,舍入过程似乎有些奇怪......可能是底层坐标中的某些周期性。无论如何,我将提供以下解决方案,使您的图像看起来不错,即使它没有找到问题的根源。在scipy.ndimage模块中使用image moropholgy。

import scipy.ndimage as spim
from skimage.morphology import square

im = spim.binary_closing(input=image, structure=square(3))

答案 1 :(得分:0)

我对此并不感到非常自豪,因为以这种方式使用字典键感觉有些麻烦,但似乎有用:

import scipy as sp
import pandas as pd
import matplotlib.pyplot as plt

a = pd.DataFrame.from_csv('cords_value.csv')

x = sp.array(a['x'])
y = sp.array(a['y'])
v = sp.array(a['value'])

xdict = {x_: i for x_, i in zip(sp.unique(x), range(len(sp.unique(x))))}
ydict = {y_: i for y_, i in zip(sp.unique(y), range(len(sp.unique(y))))}

im = sp.zeros([list(xdict.values())[-1]+1, list(ydict.values())[-1]+1])
for i in range(len(v)):
    im[xdict[x[i]], ydict[y[i]]] = v[i]

plt.imshow(im, cmap=plt.cm.spectral)

我想不出另一种方法可以使用浮点数(x和y)作为整数列表的索引(用于将值映射到图像上)。我认为结果看起来不错:

enter image description here