|
| 1 | +import pyproj |
| 2 | + |
| 3 | +import getcoords |
| 4 | +import locate |
| 5 | + |
| 6 | + |
| 7 | +class Grid: |
| 8 | + |
| 9 | + def __init__(self, data, col1=None, col2=None, epsg=4326): |
| 10 | + |
| 11 | + self.data = data |
| 12 | + self.col1 = col1 |
| 13 | + self.col2 = col2 |
| 14 | + self.epsg = epsg |
| 15 | + self.xs = None |
| 16 | + self.ys = None |
| 17 | + self.error = None |
| 18 | + self.grid_references = [] |
| 19 | + |
| 20 | + self.set_coords() |
| 21 | + if self.epsg != 4326: |
| 22 | + self.transform_coords() |
| 23 | + |
| 24 | + def set_coords(self): |
| 25 | + |
| 26 | + if isinstance(self.data, (tuple, list)) and (len(self.data) > 1): |
| 27 | + self.xs, self.ys, self.error = getcoords.from_list(self.data, self.col1) |
| 28 | + else: |
| 29 | + try: |
| 30 | + if self.data.endswith('.csv'): |
| 31 | + self.xs, self.ys, self.error = getcoords.from_csv(self.data, self.col1, self.col2) |
| 32 | + elif self.data.endswith('.shp'): |
| 33 | + self.xs, self.ys, self.error = getcoords.from_shapefile(self.data) |
| 34 | + else: |
| 35 | + raise AttributeError |
| 36 | + except AttributeError: |
| 37 | + self.set_error('Invalid parameters: Grid(data={}, col1={}, col2={}, epsg={})'.format( |
| 38 | + repr(self.data), self.col1, self.col2, self.epsg)) |
| 39 | + |
| 40 | + def transform_coords(self): |
| 41 | + |
| 42 | + try: |
| 43 | + p = pyproj.Proj(init='epsg:{}'.format(self.epsg)) |
| 44 | + self.xs, self.ys = p(self.xs, self.ys, inverse=True) |
| 45 | + except RuntimeError: |
| 46 | + self.error('EPSG:{} not found'.format(self.epsg)) |
| 47 | + |
| 48 | + def get_grid_refs(self): |
| 49 | + |
| 50 | + for x, y in zip(self.xs, self.ys): |
| 51 | + self.grid_references.append(locate.Point(x, y).get_grid_reference()) |
| 52 | + |
| 53 | + def write_references(self, fname=None, col='GRID_REFS'): |
| 54 | + |
| 55 | + if fname is None: |
| 56 | + fname = self.data |
| 57 | + self.get_grid_refs() |
| 58 | + |
| 59 | + def set_error(self, message): |
| 60 | + |
| 61 | + preamble = 'Error creating Grid object:' |
| 62 | + self.error = ('{} {}'.format(preamble, message)) |
| 63 | + print(self.error) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + |
| 68 | + lonlats = [(-34.907587535813704, 50.58441270574641), |
| 69 | + (108.93083026662671, 32.38153601114477), |
| 70 | + (-36.69218329018642, -45.06991972863084), |
| 71 | + (43.97154480746007, -46.140677181254475)] |
| 72 | + |
| 73 | + lonlats2 = [[(-34.907587535813704, 50.58441270574641), True, [1, 2], 1, 'a'], |
| 74 | + [(108.93083026662671, 32.38153601114477), False, [3, 4], 2, 'b'], |
| 75 | + [(-36.69218329018642, -45.06991972863084), False, [5, 6], 3, 'c'], |
| 76 | + [(43.97154480746007, -46.140677181254475), True, [7, 8], 4, 'd']] |
| 77 | + |
| 78 | + lonlats3 = [((-34.907587535813704, 50.58441270574641), 1), ((108.93083026662671, 32.38153601114477), 2), |
| 79 | + ((-36.69218329018642, -45.06991972863084), 3), ((43.97154480746007, -46.140677181254475), 4)] |
| 80 | + |
| 81 | + g = Grid((-34.907587535813704, 50.58441270574641)) |
| 82 | + g.write_references() |
| 83 | + # g = Grid(lonlats) |
| 84 | + # g = Grid(lonlats2, 0, epsg=3086) |
| 85 | + # g = Grid(lonlats3, 0) |
| 86 | + g = Grid('.tests/data/points.csv', 'POINT_X', 'POINT_Y') |
| 87 | + print(g.get_grid_refs()) |
| 88 | + # g = Grid('points.shp') |
| 89 | + # g = Grid('points.shp', epsg=3086) |
| 90 | + |
| 91 | + # g = Grid('good_crimes.csv', 'Longitude', 'Latitude', epsg=3086) |
| 92 | + |
| 93 | + # g = Grid('chicago_crimes_2016.csv', 'Longitude', 'Latitude') |
| 94 | + |
| 95 | + # print(g.grid_refs) |
| 96 | + |
| 97 | + # These are the desired method calls |
| 98 | + # g = Grid('myassets.csv', 'Lon', 'Lat', epsg=3086) |
| 99 | + # g.write_refs('myassets.shp', 'MY_OFFICIAL_GRID_REFERENCES') |
| 100 | + # g.write_assets('myassets.shp', 'MY_UNIQUE_ASSET_NAMES', |
| 101 | + # prefix='H', col=None, gzd=False, k100=False, delimiter='-') |
| 102 | + |
| 103 | + |
| 104 | +# These possibilities are done |
| 105 | +# print('Grid(True)') |
| 106 | +# g = Grid(True) |
| 107 | +# print('Grid(0)') |
| 108 | +# g = Grid(0) |
| 109 | +# print('Grid(1)') |
| 110 | +# g = Grid(1) |
| 111 | +# print('Grid(2)') |
| 112 | +# g = Grid(2) |
| 113 | +# print('Grid("i")') |
| 114 | +# g = Grid('i') |
| 115 | +# print('Grid("ijeg")') |
| 116 | +# g = Grid('ijeg') |
| 117 | +# print('Grid(0.123456)') |
| 118 | +# g = Grid(0.123456) |
| 119 | +# print('Grid([])') |
| 120 | +# g = Grid([]) |
| 121 | +# print('Grid(())') |
| 122 | +# g = Grid(()) |
| 123 | +# print('Grid([1])') |
| 124 | +# g = Grid([1]) |
| 125 | +# print('Grid((1,))') |
| 126 | +# g = Grid((1,)) |
| 127 | +# print('Grid((1))') |
| 128 | +# g = Grid((1)) |
| 129 | +# g = Grid('points1.shp') |
| 130 | +# g = Grid('points.shp', epsg=45673086) |
| 131 | +# g = Grid('points.csv', 'POINT_X', 'POINT') |
| 132 | +# g = Grid('points.csv', 'POINT_X') |
| 133 | +# g = Grid('points1.csv', 'POINT_X', 'B') |
| 134 | +# print('Grid([1, 2])') |
| 135 | +# g = Grid([1, 2]) |
| 136 | +# print('Grid((1, 2))') |
| 137 | +# g = Grid((1, 2)) |
| 138 | +# print('Grid(lonlats2, 1)') |
| 139 | +# g = Grid(lonlats2, 1) |
| 140 | +# print('Grid(lonlats2, 2)') |
| 141 | +# g = Grid(lonlats2, 2) |
| 142 | +# print('Grid(lonlats2, 3)') |
| 143 | +# g = Grid(lonlats2, 3) |
| 144 | +# print('Grid(lonlats2, 4)') |
| 145 | +# g = Grid(lonlats2, 4) |
| 146 | +# print('Grid(lonlats2, 5)') |
| 147 | +# g = Grid(lonlats2, 5) |
| 148 | +# g = Grid(lonlats3) |
0 commit comments