-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.py
141 lines (123 loc) · 4.46 KB
/
transformer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pandas as pd
import os
import codecs
import datetime
# 5x5 grid letters, missing I
alphabet = 'ABCDEFGHJKLMNOPQRSTUVWXYZ'
def grid2xy(false_easting, false_northing, gridsizes, grid_ref):
'''Convert grid reference to coordinates'''
# false easting and northing
easting = -false_easting
northing = -false_northing
# convert letter(s) to easting and northing offset
for n in range(0, len(gridsizes)):
letter = grid_ref[n]
idx = alphabet.index(letter)
col = (idx % 5)
row = 4 - ((idx) / 5)
easting += (col * gridsizes[n])
northing += (row * gridsizes[n])
# numeric components of grid reference
grid_ref = grid_ref[len(gridsizes):] # remove the letters
e = '{:0<5}'.format(grid_ref[0:len(grid_ref)/2])
e = '{}.{}'.format(e[0:5],e[5:])
n = '{:0<5}'.format(grid_ref[len(grid_ref)/2:])
n = '{}.{}'.format(n[0:5],n[5:])
easting += float(e)
northing += float(n)
return easting, northing
def british2xy(grid_ref):
false_easting = 1000000
false_northing = 500000
gridsizes = [500000, 100000]
return grid2xy(false_easting, false_northing, gridsizes, grid_ref)
def irish2xy(grid_ref):
false_easting = 0
false_northing = 0
gridsizes = [100000]
return grid2xy(false_easting, false_northing, gridsizes, grid_ref)
#grid_ref = 'NO19001640' #ABNTY
#easting, northing = british2xy(grid_ref)
#print(easting, northing)
#grid_ref = 'Q5975900960' #BABOR
#easting, northing = irish2xy(grid_ref)
#print(easting, northing)
# set paths
dir_path = os.path.dirname(os.path.realpath(__file__))
file_in = dir_path + "\\" + "data.csv"
file_out = dir_path + "\\" + "data2.csv"
# read csv file
data = pd.read_csv(
file_in, # relative python path to subdirectory
encoding='utf-8',
sep='\t', # deliminiter
quotechar="'", # single quote allowed as quote character
usecols=['site','grid','country'], # only load the columns specified
skiprows=0, # skip X rows of the file
na_values=['.', '??'] # take any '.' or '??' values as NA
)
# create triples from dataframe
lines = []
for index, row in data.iterrows():
if row['country'] == 'IR':
easting, northing = irish2xy(str(row['grid']))
lines.append(row['site'] + "|" + str(int(easting)) + "|" + str(int(northing)) + "|" + "29903")
elif row['country'] == 'GB' :
#easting, northing = british2xy(str(row['grid']))
#lines.append(row['site'] + "|" + str(int(easting)) + "|" + str(int(northing)) + "|" + "27700")
print("")
# write output file
file = codecs.open(file_out, "w", "utf-8")
for line in lines:
file.write(line)
file.write("\r\n")
file.close()
# PROJ.4 projection definitions
#import pyproj
#crs_british = pyproj.Proj(init='EPSG:27700')
#crs_irish = pyproj.Proj(init='EPSG:29903')
#crs_wgs84 = pyproj.Proj(init='EPSG:4326')
#lng, lat = pyproj.transform(crs_british, crs_wgs84, easting, northing)
#lng, lat = pyproj.transform(crs_irish, crs_wgs84, easting, northing)
'''
# prepare the output shapefile
import fiona
from collections import OrderedDict
crs = {'no_defs': True, 'ellps': 'WGS84', 'datum': 'WGS84', 'proj': 'longlat'}
properties = OrderedDict([
('surveyid', 'int'),
('siteid', 'int'),
('gridref', 'str'),
('date', 'str'),
('river', 'str'),
])
layer = fiona.open('rhs.shp', 'w', crs=crs, driver='ESRI Shapefile', schema={'geometry': 'Point', 'properties': properties})
# open the spreadsheet
from openpyxl import load_workbook
wb = load_workbook(filename='River_Habitat_Survey-Details_and_Scores.xlsx', use_iterators=True)
ws = wb.get_sheet_by_name('Sheet1')
rows_iter = ws.iter_rows()
header_row = rows_iter.next()
# iterate over the records
for row in rows_iter:
grid_ref = row[2].value
if grid_ref[1] in alphabet:
# grid reference is for the british national grid
easting, northing = british2xy(grid_ref)
lng, lat = pyproj.transform(crs_british, crs_wgs84, easting, northing)
else:
# grid reference is for the irish grid
easting, northing = irish2xy(grid_ref)
lng, lat = pyproj.transform(crs_irish, crs_wgs84, easting, northing)
# write output feature
properties = OrderedDict([
('surveyid', row[0].value),
('siteid', row[1].value),
('gridref', grid_ref),
('date', str(row[3].value)),
('river', row[4].value),
])
feature = {'geometry': {'type': 'Point', 'coordinates': (lng, lat)}, 'properties': properties}
layer.write(feature)
layer.close()
'''