-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmaps_final.py
269 lines (232 loc) · 9.92 KB
/
gmaps_final.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import math
import sys
import psycopg2
from googlemaps import GoogleMaps
import string
import random
def color_gen():
ABCDEF=""
for str in string.ascii_uppercase:
ABCDEF=ABCDEF+str
if(str=='F'):
break;
chars=ABCDEF + string.digits
return ''.join(random.choice(chars) for x in range(6))
###########################################################
## Google map python wrapper V0.1
##
############################################################
map_location = []
map_location.append("Red Fort, New Delhi")
map_location.append("Indore, India")
map_location.append("Ajanta and Ellora Caves")
map_location.append("Harappa")
class maps:
def __init__(self, centerLat, centerLng, zoom ):
self.center = (float(centerLat),float(centerLng))
self.zoom = int(zoom)
self.grids = None
self.paths = []
self.points = []
self.radpoints = []
self.gridsetting = None
self.coloricon = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|XXXXXX'
def setgrids(self,slat,elat,latin,slng,elng,lngin):
self.gridsetting = [slat,elat,latin,slng,elng,lngin]
def addpoint(self, lat, lng, address, color = '#FF0000'):
self.points.append((lat,lng,address,color[1:]))
#def addpointcoord(self, coord):
# self.points.append((coord[0],coord[1]))
def addradpoint(self, lat,lng,rad,color = '#0000FF'):
self.radpoints.append((lat,lng,rad,color))
def addpath(self,path,color = '#FF0000'):
path.append(color)
self.paths.append(path)
#create the html file which inlcude one google map and all points and paths
def draw(self, htmlfile):
f = open(htmlfile,'w')
f.write('<html>\n')
f.write('<head>\n')
f.write('<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />\n')
f.write('<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>\n')
f.write('<title>Google Maps - pygmaps </title>\n')
f.write('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>\n')
f.write('<script type="text/javascript">\n')
f.write('\tfunction initialize() {\n')
self.drawmap(f)
self.drawgrids(f)
self.drawpoints(f)
self.drawradpoints(f)
self.drawpaths(f,self.paths)
f.write('\t}\n')
f.write('</script>\n')
f.write('</head>\n')
f.write('<body style="margin:0px; padding:0px;" onload="initialize()">\n')
f.write('\t<div id="map_canvas" style="width: 100%; height: 100%;"></div>\n')
f.write('</body>\n')
f.write('</html>\n')
f.close()
def drawgrids(self, f):
if self.gridsetting == None:
return
slat = self.gridsetting[0]
elat = self.gridsetting[1]
latin = self.gridsetting[2]
slng = self.gridsetting[3]
elng = self.gridsetting[4]
lngin = self.gridsetting[5]
self.grids = []
r = [slat+float(x)*latin for x in range(0, int((elat-slat)/latin))]
for lat in r:
self.grids.append([(lat+latin/2.0,slng+lngin/2.0),(lat+latin/2.0,elng+lngin/2.0)])
r = [slng+float(x)*lngin for x in range(0, int((elng-slng)/lngin))]
for lng in r:
self.grids.append([(slat+latin/2.0,lng+lngin/2.0),(elat+latin/2.0,lng+lngin/2.0)])
for line in self.grids:
self.drawPolyline(f,line,strokeColor = "#000000")
def drawpoints(self,f):
cnt=0
for point in self.points:
self.drawpoint(f,point[0],point[1],point[2],point[3],cnt)
cnt=cnt+1
def drawradpoints(self, f):
for rpoint in self.radpoints:
path = self.getcycle(rpoint[0:3])
self.drawPolygon(f,path,strokeColor = rpoint[3])
def getcycle(self,rpoint):
cycle = []
lat = rpoint[0]
lng = rpoint[1]
rad = rpoint[2] #unit: meter
d = (rad/1000.0)/6378.8;
lat1 = (math.pi/180.0)* lat
lng1 = (math.pi/180.0)* lng
r = [x*30 for x in range(12)]
for a in r:
tc = (math.pi/180.0)*a;
y = math.asin(math.sin(lat1)*math.cos(d)+math.cos(lat1)*math.sin(d)*math.cos(tc))
dlng = math.atan2(math.sin(tc)*math.sin(d)*math.cos(lat1),math.cos(d)-math.sin(lat1)*math.sin(y))
x = ((lng1-dlng+math.pi) % (2.0*math.pi)) - math.pi
cycle.append( ( float(y*(180.0/math.pi)),float(x*(180.0/math.pi)) ) )
return cycle
def drawpaths(self, f, paths):
for path in paths:
#print path
self.drawPolyline(f,path[:-1], strokeColor = path[-1])
#############################################
# # # # # # Low level Map Drawing # # # # # #
#############################################
def drawmap(self, f):
f.write('\t\tvar centerlatlng = new google.maps.LatLng(%f, %f);\n' % (self.center[0],self.center[1]))
f.write('\t\tvar myOptions = {\n')
f.write('\t\t\tzoom: %d,\n' % (self.zoom))
f.write('\t\t\tcenter: centerlatlng,\n')
f.write('\t\t\tmapTypeId: google.maps.MapTypeId.ROADMAP\n')
f.write('\t\t};\n')
f.write('\t\tvar map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);\n')
f.write('\n')
def drawpoint(self,f,lat,lon,addr,color,cnt):
f.write('\t\tvar latlng = new google.maps.LatLng(%f, %f);\n'%(lat,lon))
f.write('\t\tvar img = new google.maps.MarkerImage(\'%s\',null,null,null,new google.maps.Size(42, 68));\n' % (self.coloricon.replace('XXXXXX',color)))
f.write('\t\tvar marker = new google.maps.Marker({\n')
f.write('\t\ttitle: "%s",\n' % addr)
f.write('\t\ticon: img,\n')
f.write('\t\tposition: latlng\n')
f.write('\t\t});\n')
f.write('\t\tmarker.setMap(map);\n')
f.write('\n')
def drawPolyline(self,f,path,\
clickable = False, \
geodesic = True,\
strokeColor = "#FF0000",\
strokeOpacity = 1.0,\
strokeWeight = 2
):
f.write('var PolylineCoordinates = [\n')
for coordinate in path:
f.write('new google.maps.LatLng(%f, %f),\n' % (coordinate[0],coordinate[1]))
f.write('];\n')
f.write('\n')
f.write('var Path = new google.maps.Polyline({\n')
f.write('clickable: %s,\n' % (str(clickable).lower()))
f.write('geodesic: %s,\n' % (str(geodesic).lower()))
f.write('path: PolylineCoordinates,\n')
f.write('strokeColor: "%s",\n' %(strokeColor))
f.write('strokeOpacity: %f,\n' % (strokeOpacity))
f.write('strokeWeight: %d\n' % (strokeWeight))
f.write('});\n')
f.write('\n')
f.write('Path.setMap(map);\n')
f.write('\n\n')
def drawPolygon(self,f,path,\
clickable = False, \
geodesic = True,\
fillColor = "#000000",\
fillOpacity = 0.0,\
strokeColor = "#FF0000",\
strokeOpacity = 1.0,\
strokeWeight = 1
):
f.write('var coords = [\n')
for coordinate in path:
f.write('new google.maps.LatLng(%f, %f),\n' % (coordinate[0],coordinate[1]))
f.write('];\n')
f.write('\n')
f.write('var polygon = new google.maps.Polygon({\n')
f.write('clickable: %s,\n' % (str(clickable).lower()))
f.write('geodesic: %s,\n' % (str(geodesic).lower()))
f.write('fillColor: "%s",\n' %(fillColor))
f.write('fillOpacity: %f,\n' % (fillOpacity))
f.write('paths: coords,\n')
f.write('strokeColor: "%s",\n' %(strokeColor))
f.write('strokeOpacity: %f,\n' % (strokeOpacity))
f.write('strokeWeight: %d\n' % (strokeWeight))
f.write('});\n')
f.write('\n')
f.write('polygon.setMap(map);\n')
f.write('\n\n')
if __name__ == "__main__":
try:
conn = psycopg2.connect("dbname=PUSTACK user=postgres host=localhost password=tiger port=5433");
print "Connection successful"
except:
print "I am unable to connect to the database"
cur = conn.cursor()
table='keyword_identification_extraction'
statement= "SELECT distinct page_no FROM " + table + " where keyword_type = '#2'"
cur.execute(statement)
page_number = cur.fetchall()
gmaps = GoogleMaps('AIzaSyAfeD4ORUK_63ldCoIP13zQ7VW6zYXPdcM')
address = 'India'
lat_cent, lng_cent = gmaps.address_to_latlng(address)
mymap = maps(lat_cent, lng_cent, 5)
for pno in page_number:
lat=[]
lng=[]
statement= "SELECT keyword FROM " + table + " where keyword_type = '#2' and page_no = " + str(pno[0])
cur.execute(statement)
rows = cur.fetchall()
for row in rows:
print row[0]
address = row[0]
try:
lat, lng = gmaps.address_to_latlng(address)
except:
print "Unknown Geo Address of "+address
mymap.addpoint(lat, lng,address.upper(),"#"+str(color_gen()))
mymap.draw('C:/Python27/temp/'+str(pno[0])+'.html')
"""
try:
statement= "INSERT INTO gmaps_data VALUES("+str(pno[0])+ ",lo_import('C:/Python27/temp/"+str(pno[0])+".html'))"
print statement
cur.execute(statement)
except psycopg2.IntegrityError:
conn.rollback()
print "*******************************************************"
print "\tDuplicate Entry : Skipping"
print "*******************************************************"
else:
conn.commit()
"""
cur.close()
conn.close()