Skip to content

Commit 7677abd

Browse files
Yet another directory change.
1 parent 8cc41b9 commit 7677abd

17 files changed

+262
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Philip Griffith
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do this at some point.

pyutm/__init__.py

Whitespace-only changes.

pyutm/getcoords.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
def from_list(data, col):
2+
3+
coords = ()
4+
xs = None
5+
ys = None
6+
error = None
7+
try:
8+
# Grab the column containing coordinates if applicable
9+
if col is not None:
10+
coords = tuple(zip(*data))[col]
11+
else:
12+
if not isinstance(data[0], (tuple, list)) and not isinstance(data[1], (tuple, list)):
13+
coords = [(data[0], data[1])]
14+
else:
15+
coords = data
16+
xs = tuple(coord[0] for coord in coords)
17+
ys = tuple(coord[1] for coord in coords)
18+
except (TypeError, IndexError):
19+
error = 'Invalid coordinates: {}'.format(coords)
20+
finally:
21+
return xs, ys, error
22+
23+
24+
def from_csv(data, col1, col2):
25+
26+
import pandas
27+
xs = None
28+
ys = None
29+
error = None
30+
try:
31+
df = pandas.read_csv(data)
32+
xs = df[col1].values
33+
ys = df[col2].values
34+
except (FileNotFoundError, KeyError) as e:
35+
if type(e).__name__ == 'KeyError':
36+
error = 'Invalid column names: col1={}, col2={}'.format(col1, col2)
37+
else:
38+
error = e
39+
finally:
40+
return xs, ys, error
41+
42+
43+
def from_shapefile(data):
44+
45+
import shapefile
46+
xs = None
47+
ys = None
48+
error = None
49+
try:
50+
sf = shapefile.Reader(data)
51+
shapes = sf.shapes()
52+
# Only accept Point, PointZ and PointM geometries
53+
if shapes[0].shapeType in (1, 11, 21):
54+
xs = tuple(shape.points[0][0] for shape in shapes)
55+
ys = tuple(shape.points[0][1] for shape in shapes)
56+
except shapefile.ShapefileException as e:
57+
error = e
58+
finally:
59+
return xs, ys, error

pyutm/main.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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)

pyutm/setrefs.py

Whitespace-only changes.

setup.py

Whitespace-only changes.

tests/data/points.cpg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8

tests/data/points.csv

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
OID,Id,POINT_X,POINT_Y
2+
-1,0,-118.418681394000000,44.820703608400002
3+
-1,0,-103.188850642000010,35.480078531799997
4+
-1,0,-152.562683848000010,65.520768720500001
5+
-1,0,-156.057234486999990,19.742539480200001
6+
-1,0,-81.670585520600000,28.127543362400001
7+
-1,0,-72.914167112000001,43.498865047499997
8+
-1,0,-66.533303400500003,17.894144738000001
9+
-1,0,1.498165853390000,52.012089309200000
10+
-1,0,-36.692183290199999,-45.069919728599999
11+
-1,0,43.971544807500003,-46.140677181299999
12+
-1,0,59.675987445899999,4.898761393810000
13+
-1,0,108.930830267000000,32.381536011100003
14+
-1,0,74.309672631799998,63.076582986399998
15+
-1,0,153.902643277000010,40.947595632099997
16+
-1,0,120.709162245000000,-32.220830297100001
17+
-1,0,170.320924217000000,-42.571485672500003
18+
-1,0,-70.956421774099994,-28.294719637499998
19+
-1,0,-126.278890160000000,-40.786889918100002
20+
-1,0,-99.509953844099996,0.258812432436000
21+
-1,0,-21.344659802599999,6.683357148180000
22+
-1,0,-34.907587535799998,50.584412705699997
23+
-1,0,62.888259803799997,38.449161576000002
24+
-1,0,12.205740379600000,-24.011689827000001
25+
-1,0,86.444923761499993,-19.014821714800000
26+
-1,0,123.564515451999990,64.861178740699998
27+
-1,0,251.402070826000000,4.257190516060000

tests/data/points.dbf

1.27 KB
Binary file not shown.

0 commit comments

Comments
 (0)