0

The program is intended to display a map with pins showing the locations of institutions that make use of one of our facilities. The program takes a csv file, reads the postcodes, geocodes them and places the pins on the map. The size of the pins is relative to the number of times they have used the facility.

However, when the csv file is uploaded the program generates a map with all the pins over Nigeria. Looking through the output from the program, it seems to be geocoding correctly so I am not sure what is going on. The program geocodes using an offline database as python's urllib is not compatible with the proxy setup at my office.

The program is split into two separate modules, the map generation module and the geocoding module. Here is the map generation part:

import folium
from bottle import route, run, template, static_file, request
import urllib.request
import urllib.parse
import json
import os
os.system('start geocoder.bat')
institutionList = []
map_osm = folium.Map(location=[55, -2], zoom_start=5)
@route('/spreadsheet.html')
def send_static():
    return static_file('spreadsheet.html',root='')
@route('/upload', method='POST')
def do_upload():
    category   = request.forms.get('category')
    upload     = request.files.get('upload')
    name, ext = os.path.splitext(upload.filename)
    if ext not in ('.csv'):
        return 'File extension not allowed.'
    upload.save('uploads/' + upload.filename)
    fileList = []
    with open('spreadsheetList','r') as f:
        while True:
            line = f.readline()
            if not line: break
            print(line.strip())
            print("line should have just printed")
            fileList.append(line.strip())
    f.close()
    lengthFileList = len(fileList)
    x = 0
    while x < lengthFileList:
        with open(('uploads/' + fileList[x]),'r') as spread:
            while True:
                line = spread.readline()
                if not line: break
                institutionDetails = line.split(',')
                institutionList.append(institutionDetails)
        spread.close()
        x = x + 1
    spreadsheetName = upload.filename
    f = open('spreadsheetList','a')
    f.write(spreadsheetName + '\n')
    f.close()
    with open('uploads/' + spreadsheetName, 'r') as f:
        while True:
            line = f.readline()
            if not line: break
            institutionDetails = line.split(',')
            institutionList.append(institutionDetails)
        print(institutionList)
    f.close()

    lengthOfList = len(institutionList)
    x = 0
    coords = []
    while x < lengthOfList:
        address = urllib.parse.quote_plus(institutionList[x][1])
        response = urllib.request.urlopen('http://localhost:80/geoCodeRequest/' + address).read().decode('utf-8')
        cleanResponse = str(response).replace('"','')
        coords = cleanResponse
        print(cleanResponse)
        institutionList[x].append(coords)
        x = x + 1
    print("http sources successfully accessed")    

    print(institutionList)
    x = 0
    while x < lengthOfList:
        try:
            map_osm.circle_marker(location=institutionList[x][3], radius=(int(institutionList[x][2]) * 10),popup=institutionList[x][0], line_color='#3186cc',fill_color='#3186cc', fill_opacity=0.2)
            print("marker added")
        except:
            print("marker could not be added")
        x = x + 1
    map_osm.create_map(path='osm.html')
    return '<meta http-equiv="refresh" content="0; url=osm.html">'
@route('/osm.html')
def send_static():
    return static_file('osm.html',root='')
run(host='localhost', port=8080)

A batch file is used to start the second module:

@echo off
python geocodeProxyBypass.py

Here is the second module of code, the geocoding module:

from bottle import route, run, template
import string
location = []
x = 0
@route('/geoCodeRequest/<name>')
def redir(name):
    x = 0
    print(name)
    print(name[:4])
    print(name[:3])
    with open('ukPostcode.csv','r') as f:
        while True:
            line = f.readline()
            print(line)
            if not line: break
            locationDetails = line.split(',')
            location.append(locationDetails)
            print(location[x][0])
            if location[x][0] == ('"' + name[:4] + '"'):
                coords = location[x][3] + ", " + location[x][4]
                return coords
            elif location[x][0] == ('"' + name[:3] + '"'):
                coords = location[x][3] + ", " + location[x][4]
                return ((coords.replace('"','')))
            else:
                print("no match found for " + name)
                x = x + 1
    f.close()   
run(host='localhost', port=80)                    

Here is what an example pin generated by the program should look like:

 var circle_1 = L.circle([51.74, -1.25
    ], 7460, {
     color: '#3186cc',
     fillColor: '#3186cc',
     fillOpacity: 0.2
     });
     circle_1.bindPopup("University of Oxford");
     circle_1._popup.options.maxWidth = 300;
     map.addLayer(circle_1)

Here is what is actually being output:

var circle_1 = L.circle([5, 1
    ], 7460, {
     color: '#3186cc',
     fillColor: '#3186cc',
     fillOpacity: 0.2
     });
     circle_1.bindPopup("University of Oxford");
     circle_1._popup.options.maxWidth = 300;
     map.addLayer(circle_1)

Apologies for the very long question, please help!

0 Answers0