-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoCoder_api.py
More file actions
71 lines (64 loc) · 2.76 KB
/
GeoCoder_api.py
File metadata and controls
71 lines (64 loc) · 2.76 KB
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
import flask
import time
import pandas as pd
import numpy as np
from flask import request,Response
app = flask.Flask(__name__)
#app.config["DEBUG"] = True
#this function maps the address with the corrosponding GeoCodes
def create_new_file(address_to_Check):
#address_to_Check = pd.read_csv(source_filepath)
splitted_Address = address_to_Check['Address'].str.split(',')
df = pd.read_csv(r'Townlands__OSi_National_Placenames_Gazetteer.csv', usecols=(0,1,3,12))
df = df.drop_duplicates(subset=['County','English_Name'])
time_start = time.time()
for i in range(0,len(splitted_Address)):
#print (splitted_Address[i])
for j in range(0,len(splitted_Address[i])-1):
#print (splitted_Address[i][j].upper(),str(splitted_Address[i][-1]).upper())
x = df[(df.English_Name == str(splitted_Address[i][j]).upper().strip()) & (df.County == str(splitted_Address[i][-1]).upper().strip())]
#print (x)
if len(x):
splitted_Address[i].append(x.iloc[0]['X'])
splitted_Address[i].append(x.iloc[0]['Y'])
break
time_end = time.time()
print("Time taken for "+str(len(splitted_Address))+" address is:",time_end - time_start)
#splitted_Address.to_csv(dest_filepath,)
return splitted_Address
#this a basic form which will facilitate user to upload a file of addresses and get the file with GeoCodes
@app.route('/')
def form():
return """
<html>
<body>
<h1>Geocoder</h1>
<p>Please select the file with addresses to check (csv format only)</p>
<form action="/submit" method="post" enctype="multipart/form-data">
<input type="file" name="data_file" />
<input type="submit" />
</form>
</body>
</html>
"""
# This is the app which takes data from the input CSV file, calls the mapping function and sends the results back to user in the form of a CSV
@app.route('/submit', methods=["POST"])
def transform_view():
request_file = request.files['data_file']
if not request_file:
return "No file"
file_contents = request_file.stream.read().decode()
file_contents = file_contents.replace('"','')
#print (type(file_contents.splitlines()))
#print(file_contents)
df = pd.DataFrame(np.array(file_contents.splitlines()[1:]).reshape(-1,1),columns=['Address'])
#print (df)
result = create_new_file(df)
result = result.to_csv()
#print(result)
return Response(
result,
mimetype="text/csv",
headers={"Content-disposition":
"attachment; filename=GeoCodes.csv"})
app.run()