0
votes

I'm trying to convert coordinates from latitude/longitude into pixel but I lose points in this process.

The code that I'm using is the following:

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

If I plot the coordinates or the index points I get this: enter image description here

If I plot the image I get this: enter image description here

As you can see, there are some points that are missing in translating lat/lon into pixel numbers. Yellow is 255 value and purple is 0 value. How can this be solved?

Here you find the coordinates that I'm using cords.csv

Here you find the coordinates with the values that need to be set to each pixel. cords_valus.csv

2
What is gt doing? - Willem Van Onsem
It the conversion vector from lat/lon to axis coordinates. Is obtained using GDAL. - Lorenzo Bottaccioli

2 Answers

0
votes

When your decimal coordinates are being rounded to integer indices for the image there seems to be an oddity in the rounding processes...probably some periodicity in the underlying coordinates. Anyway, I'll offer the following solution to make your image look nice, even it if does not get to the root of your problem. Use image moropholgy in the scipy.ndimage module.

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

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

I'm not immensely proud of this, because it feels a bit kludgey to use dictionary keys in this way, but it seems to work:

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)

I couldn't think of another way to use the floats (x and y) as indices into a list of ints (for mapping the values onto the image). The result looks nice I think:

enter image description here