title: Find a easier way to cluster 2-d scatter data into grid array data
body: <p>I have figured out a method to cluster disperse point data into structured 2-d array(like <strong>rasterize function</strong>). And I hope there are some better ways to achieve that target.    </p>
  
  <h2>My work</h2>
  
  <h3>1. Intro</h3>
  
  <ul>
  <li><strong>1000 point data</strong> has there dimensions of properties (lon, lat, emission) whicn represent one factory located at (x,y) emit certain amount of CO2 into atmosphere</li>
  <li><strong>grid network</strong>: predefine the 2-d array in the shape of 20x20</li>
  </ul>
  
  <p><a href="http://i4.tietuku.com/02fbaf32d2f09fff.png" rel="nofollow noreferrer">http://i4.tietuku.com/02fbaf32d2f09fff.png</a>  </p>
  
  <p>The code reproduced here:    </p>
  
  <pre><code>#### define the map area
  xc1,xc2,yc1,yc2 = 113.49805889531724,115.5030664238035,37.39995194888143,38.789235929357105       
  map = Basemap(llcrnrlon=xc1,llcrnrlat=yc1,urcrnrlon=xc2,urcrnrlat=yc2)     
  
  #### reading the point data and scatter plot by their position
  df = pd.read_csv("xxxxx.csv")
  px,py = map(df.lon, df.lat)       
  map.scatter(px, py, color = "red", s= 5,zorder =3)      
  
  #### predefine the grid networks      
  lon_grid,lat_grid = np.linspace(xc1,xc2,21), np.linspace(yc1,yc2,21)
  lon_x,lat_y = np.meshgrid(lon_grid,lat_grid)
  grids = np.zeros(20*20).reshape(20,20)
  plt.pcolormesh(lon_x,lat_y,grids,cmap =  'gray', facecolor = 'none',edgecolor = 'k',zorder=3)
  </code></pre>
  
  <h3>2. My target</h3>
  
  <blockquote>
    <ol>
    <li>Finding the nearest grid point for each factory </li>
    <li>Add the emission data into this grid number   </li>
    </ol>
  </blockquote>
  
  <h3>3. Algorithm realization</h3>
  
  3.1 Raster grid
  
  <p><strong>note</strong>: 20x20 grid points are distributed in this area represented by blue dot.    </p>
  
  <p><a href="http://i4.tietuku.com/8548554587b0cb3a.png" rel="nofollow noreferrer">http://i4.tietuku.com/8548554587b0cb3a.png</a>     </p>
  
  3.2 KD-tree
  
  <p><strong>Find the nearest blue dot of each red point</strong>     </p>
  
  <pre><code>sh = (20*20,2)
  grids = np.zeros(20*20*2).reshape(*sh)
  
  sh_emission = (20*20)
  grids_em = np.zeros(20*20).reshape(sh_emission)
  
  k = 0
  for j in range(0,yy.shape[0],1):
      for i in range(0,xx.shape[0],1):
          grids[k] = np.array([lon_grid[i],lat_grid[j]])
          k+=1
  
  T = KDTree(grids)
  
  x_delta = (lon_grid[2] - lon_grid[1])
  y_delta = (lat_grid[2] - lat_grid[1])
  R = np.sqrt(x_delta**2 + y_delta**2)
  
  for i in range(0,len(df.lon),1):
      idx = T.query_ball_point([df.lon.iloc[i],df.lat.iloc[i]], r=R)
      # there are more than one blue dot which are founded sometimes,      
      # So I'll calculate the distances between the factory(red point)       
      # and all blue dots which are listed 
      if (idx &gt; 1):
          distance = []
          for k in range(0,len(idx),1):
              distance.append(np.sqrt((df.lon.iloc[i] - grids[k][0])**2 + (df.lat.iloc[i] - grids[k][1])**2))
             pos_index = distance.index(min(distance))
             pos = idx[pos_index]
  
      # Only find 1 point
      else:
           pos = idx   
      grids_em[pos] += df.so2[i]      
  </code></pre>
  
  <h3>4. Result</h3>
  
  <pre><code>co2 = grids_em.reshape(20,20)
  plt.pcolormesh(lon_x,lat_y,co2,cmap =plt.cm.Spectral_r,zorder=3)
  </code></pre>
  
  <p><a href="http://i4.tietuku.com/6ded65c4ac301294.png" rel="nofollow noreferrer">http://i4.tietuku.com/6ded65c4ac301294.png</a></p>
  
  <h3>5. My question</h3>
  
  <ul>
  <li>Can someone point out some drawbacks or error of this method? </li>
  <li>Is there some algorithms more aligned with my target?     </li>
  </ul>
  
  <p>Thanks a lot!</p>