-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
177 lines (152 loc) · 5.59 KB
/
utils.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import base64
import json
import csv
import StringIO
from datetime import datetime
from functools import wraps
import urllib
from django.contrib.auth import authenticate
from django.http import HttpResponse
import httplib2
import os.path
import os
from django.conf import settings
from django.template.base import Template
from django.template.context import Context
from geonode import datamanager
def testFormhubConnection(url, username, password):
auth = base64.encodestring(username+ ':' + password)
headers = {'Authorization' : 'Basic ' + auth}
http = httplib2.Http(disable_ssl_certificate_validation=True)
http.add_credentials(username, password)
print "making request"
resp, content = http.request(url, headers=headers)
print resp.status
if resp.status == 200:
return True, "success"
elif resp.status == 404:
return False, "Cannot find URL"
elif resp.status == 403:
return False, "User name or password incorrect"
else:
return False, "Unknown error"
def getFormhubColumns(dataconnection):
url = dataconnection.formhub_url.strip('/') + "/data.csv"
auth = base64.encodestring(dataconnection.formhub_username + ':' + dataconnection.formhub_password)
headers = {'Authorization' : 'Basic ' + auth}
http = httplib2.Http(disable_ssl_certificate_validation=True)
http.add_credentials(dataconnection.formhub_username, dataconnection.formhub_password)
resp, content = http.request(url, headers=headers)
try:
headers = [""] + content.split("\n")[0].split(",")
except:
return {}
return zip(headers, headers)
def getFormhubCSV(dataconnection):
url = dataconnection.formhub_url.strip('/') + "/data.csv"
auth = base64.encodestring(dataconnection.formhub_username + ':' + dataconnection.formhub_password)
headers = {'Authorization' : 'Basic ' + auth}
http = httplib2.Http(disable_ssl_certificate_validation=True)
http.add_credentials(dataconnection.formhub_username, dataconnection.formhub_password)
resp, content = http.request(url, headers=headers)
f = StringIO.StringIO(content)
reader = csv.reader(f, delimiter=',')
headers = reader.next()
data = []
for row in reader:
data.append(row)
return headers, data
from geopy import geocoders
def geocodeSet(opentext, addition):
if addition:
geocodestring = opentext + addition
else:
geocodestring = opentext
g = geocoders.GoogleV3()
try:
place, (lat, lon) = g.geocode(geocodestring)
except:
return False
if lat and lon:
return {'lat':lat, 'lon':lon}
else:
return False
#do the geocode
from shapely.geometry import Point, mapping
from fiona import collection
import random
import tempfile
import shutil
from django.template.defaultfilters import slugify
from geonode.layers.utils import file_upload
def fixShpNames(namesarray):
newheaders = []
finalheaders = []
prefix = {}
for headername in namesarray:
try:
writesection = headername.split("/")[-1]
except:
writesection = headername
newname = (writesection[:10]) if len(writesection) > 10 else writesection
if newname in prefix.keys():
prefix[newname] +=1
else :
prefix[newname] = 1
newheaders.append(newname)
for newname in newheaders:
if prefix[newname] == 1:
finalheaders.append(newname)
else:
if len(newname) < 8:
newname = newname + str(prefix[newname]).zfill(1)
else:
newname = newname[:8]+ str(prefix[newname]).zfill(1)
finalheaders.append(newname)
return finalheaders
def createLayerFromCSV(dataconnection):
rawheaders, data = getFormhubCSV(dataconnection)
headers = fixShpNames(rawheaders)
print headers
props = {}
for headstr in headers:
props[headstr] = 'str'
schema = { 'geometry': 'Point', 'properties': props}
temporaryfile = tempfile.gettempdir() + "/" + slugify(dataconnection.title)
with collection(temporaryfile + ".shp", "w", "ESRI Shapefile", schema) as output:
for row in data:
dataset = dict(zip(rawheaders, row))
atrributes = dict(zip(headers, row))
try:
point = Point(float(dataset[dataconnection.lon_column]), float(dataset[dataconnection.lat_column]))
except Exception,e:
print str(e)
point = None
if not point and dataconnection.geocode_column:
print "trying to geocode"
pointset = geocodeSet(dataconnection.geocode_column, dataconnection.geocode_country)
if not pointset:
continue
else:
point = Point(float(pointset['lon']), float(pointset['lat']))
#attempt to geocode
if not point:
continue
output.write({
'properties': atrributes,
'geometry': mapping(point)
})
#add proj file to the mix
fromprojname = os.path.dirname(datamanager.__file__) + "/fixtures/wgs84.prj"
shutil.copy(fromprojname, temporaryfile + ".prj")
#upload file and done
newlayer = file_upload(temporaryfile + ".shp", user=dataconnection.owner, overwrite=True)
try:
os.remove(temporaryfile + ".shp")
os.remove(temporaryfile + ".cpg")
os.remove(temporaryfile + ".dbf")
os.remove(temporaryfile + ".shx")
os.remove(temporaryfile + ".prj")
except:
print "error removing file"
return newlayer