annotate globalneighbors/grid.py @ 25:991bce6b6881 default tip

[knn] placeholder for planning session
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Sep 2017 14:35:50 -0700
parents 22c384fe954d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
1 #!/usr/bin/env python
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
2
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
3 """
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
4 grid locations, for speed and fidelity
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
5 """
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
6
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import math
16
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
8 import sys
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
9 from .cli import CitiesParser
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
10 from .locations import locations
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 class LatLonGrid(object):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 lat_range = (-90., 90)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 lon_range = (-180., 180) # however, this wraps around
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def __init__(self, nlat, nlon):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 self.n = (nlat, nlon)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 self.range = (self.lat_range, self.lon_range)
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 self.d = ((self.lat_range[-1] - self.lat_range[0])/self.n[0],
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 (self.lon_range[-1] - self.lon_range[0])/self.n[1])
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 self.create_grid()
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 def create_grid(self):
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
25 self.grid = []
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
26 for _ in xrange(self.n[0]):
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
27 self.grid.append([set() for _ in xrange(self.n[-1])])
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 def add(self, geoid, lat, lon):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 latlon = (lat, lon)
21
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
31 index = self.index(lat, lon)
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
32 self[index].add(geoid)
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
33 return index
0
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 def __getitem__(self, index):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 """
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 index -- 2-tuple or list of i and j indices
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 """
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 return self.grid[index[0]][index[1]]
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 def index(self, lat, lon):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 return [int(math.floor((val-self.range[i][0])/self.d[i]))
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 for i, val in enumerate((lat, lon))]
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 def neighbors(self, i, j):
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 """
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 return neighbors of points i, j
5dba84370182 initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 """
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
49 imat = [i]
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
50 jmat = [j]
5
7e27e874655b test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
51
7e27e874655b test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
52 # latitude
1
1b94f3bf97e5 * limit distance function
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
53 if i:
5
7e27e874655b test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
54 imat.append(i-1)
7
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
55 if i < self.n[0]-1:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
56 imat.append(i+1)
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
57
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
58 # longitude: wraps around
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
59 if j:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
60 jmat.append(j-1)
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
61 else:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
62 jmat.append(self.n[-1] - 1)
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
63 if j == self.n[-1] - 1:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
64 jmat.append(0)
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
65 else:
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
66 jmat.append(j+1)
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
67
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
68 return [(_i, _j) for _i in imat for _j in jmat
254195d0bac2 partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents: 5
diff changeset
69 if (_i,_j) != (i,j)]
9
638fad06e556 use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
70
638fad06e556 use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
71 def neighbor_points(self, i, j):
638fad06e556 use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents: 7
diff changeset
72 """return all points in a lat-lon box"""
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
73 geoids = set()
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
74 geoids.update(self[(i,j)])
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
75 for ii, jj in self.neighbors(i,j):
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
76 geoids.update(self[ii,jj])
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
77 return geoids
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
78
11
d1b99c695511 remove obselete data
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
79
10
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
80 class GriddedLocations(object):
21ed15391e8a add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
81
21
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
82 def __init__(self, _locations, nlat=90, nlon=90):
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
83 self.locations = _locations
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
84 self.grid = LatLonGrid(nlat, nlon)
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
85 self.gridloc = {}
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
86 for geoid, (lat, lon) in locations.items():
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
87 gridloc[geoid] = self.grid.add(geoid, lat, lon)
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
88
15
21095c9006e5 city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
89
16
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
90 def main(args=sys.argv[1:]):
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
91 """CLI"""
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
92
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
93 # parse command line
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
94 parser = CitiesParser(description=__doc__)
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
95 options = parser.parse_args(args)
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
96
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
97 # read locations
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
98 city_locations = locations(parser.read_cities())
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
99
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
100 # make a grid
21
22c384fe954d add locations to grid; not used or tested yet
Jeff Hammel <k0scist@gmail.com>
parents: 20
diff changeset
101 gridded_locations = GriddedLocations(city_locations)
20
2fef925fbf37 display country + population in autocomplete drop down
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
102
16
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
103 if __name__ == '__main__':
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
104 main()
4583d0d9331a stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
105