4

I have GPS coordinates of human mobility in Beijing Area. I want to divide geographical space in to rectangular grid of for example 2 sq. km (delta), and access the indexing position of any point inside the grid. The cells need not be exactly equal size, approximation can work in my case.

I have geographical region with the following coordinates (latitude, longitudes) of bounding box.

Bottom Left  (x1,y1) = 39.77750000, 116.17944444  
Top Left     (x1,y2) = 40.04722222, 116.58888889  
Bottom Right (x2,y1) = 39.77750000, 116.58888889  
Top Right    (x2,y2) = 40.04722222, 116.17944444  

This is rectangular region of 30 km x 34 kms. The solution in my mind is to take delta as 2km, increment latitude and longitude value by delta until upper bound reaches.
To access the index position of GPS point p, Let BL be the Bottom Left point of rectangular region

Row =    Distance [(p.lat,BL.long), (BL.lat, BL.long)] / delta 
Column = Distance [(BL.lat,p.long), (BL.lat, BL.long)] / delta 

Is there any easier way or supporting library to solve this problem? preferably in combination of row and columns (x,y), so that I can measure the closeness of grid cells by finding distance between two grid cells in Cartesian coordinates system. An example image and input dataset may give you clear idea of the description.

enter image description here

The input dataset in given in the link https://drive.google.com/file/d/1JjvS7igTmrtLA4E5Rs5D6tsdAXqzpYqX/view

Asif Khan
  • 1,228
  • 1
  • 11
  • 22
  • A Voronoi diagram should help you I guess. Look at [this](https://stackoverflow.com/questions/34968838/python-finite-boundary-voronoi-cells) for inspiration. – skrubber Jul 28 '18 at 17:10
  • In my case a grid cell may contains multiple points, while in voronoi diagram each cell containing only one point. – Asif Khan Jul 29 '18 at 04:24
  • 1
    I assumed you're looking for a bounded geometrical split-grid for your linear space of latitude and longitudes. Maybe, refer to the [spatial data structures](https://docs.scipy.org/doc/scipy/reference/tutorial/spatial.html) in SciPy for your use case. – skrubber Jul 29 '18 at 04:44
  • I attached the image as an example and link of data for better understanding of my problem. – Asif Khan Aug 09 '18 at 03:49

1 Answers1

3
bottomLeft = (39.77750000, 116.17944444)
bottomRight = (39.77750000, 116.58888889)
topLeft = (40.04722222, 116.58888889)
topRight = (40.04722222, 116.17944444)

cols = np.linspace(bottomLeft[1], bottomRight[1], num=18)
rows = np.linspace(bottomLeft[0], topLeft[0], num=15)
df['col'] = np.searchsorted(cols, df['long'])
df['row'] = np.searchsorted(rows, df['lat'])
Asif Khan
  • 1,228
  • 1
  • 11
  • 22
  • Can some explanation be added, please. – Shuchi Sethi Aug 31 '18 at 03:37
  • its very simple. bottomLeft, bottomRight, topLeft, topRight are coordinates of rectangular region. np.linspace divides a region beteen bottomLeft to bottomRight into 18 equal spaces and return the array of coordinates in cols. same for rows. then for each lat, long we can calculates its rows and columns – Asif Khan Sep 02 '18 at 10:49
  • read the file in df using pd.read_table('filename.csv', sep=','). filename.csv is the file at the link mentioned in question. – Asif Khan Sep 16 '18 at 17:11