简体   繁体   English

将2D空间中的点分组为python中的预定义笛卡尔网格单元

[英]Group points in 2D space into predefined cartesian grid cells in python

I have huge no. 我没有。 of points (about million) in 2D plane. 2D平面中的点数(约百万)。 I want to group them into cells defined by a square grid of fixed length (each cell side is 3). 我想将它们分为固定长度的正方形网格定义的单元格(每个单元格边为3)。 I don't need no. 我不需要 of points in a given cell rather I need the information about the cell in which given point is. 给定单元中的点数,而我需要有关给定点所在单元格的信息。 What would be the best and fast way to do this? 什么是最快,最快的方法呢? Is there a python package which can be used? 是否有可以使用的python包?

You can use np.searchsorted to do this. 您可以使用np.searchsorted执行此操作。 For example: 例如:

import numpy as np

xgrid = np.arange(0,33,3)
ygrid = np.arange(0,33,3)

mypoints = np.array([[4,8],[13,19],[21,1]])

icells = np.searchsorted(xgrid,mypoints[:,0])-1
jcells = np.searchsorted(ygrid,mypoints[:,1])-1

print icells,jcells
# [1 4 6] [2 6 0]  

print xgrid[icells],ygrid[jcells]
# [ 3 12 18] [ 6 18  0]

For each point, you find the i and j index for the x and y grid, which you can then use to find "information" about the cell which that given point is in. 对于每个点,您都可以找到xy网格的ij索引,然后可以使用它们来查找有关该给定点所在的像元的“信息”。

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

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