Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import psycopg2
  2. import turtle
  3. import math
  4.  
  5. def parse_point(point):
  6. aux = point[6:-1].split()
  7. aux[0] = float(aux[0])
  8. aux[1] = float(aux[1])
  9. return aux
  10.  
  11.  
  12. db = psycopg2.connect(user = 'postgres', password = 'postgres' , host = 'localhost', database = 'taxis')
  13. turtle.speed(0)
  14. turtle.tracer(100)
  15.  
  16. def draw_square(i,j,ratio):
  17. turtle.penup()
  18. turtle.goto(i,j)
  19. turtle.color(ratio , 0, 1 - ratio)
  20. turtle.begin_fill()
  21. turtle.pendown()
  22. turtle.forward(1)
  23. turtle.right(90)
  24. turtle.forward(1)
  25. turtle.right(90)
  26. turtle.forward(1)
  27. turtle.right(90)
  28. turtle.forward(1)
  29. turtle.right(90)
  30. turtle.end_fill()
  31. turtle.penup()
  32.  
  33. def heat_map(db):
  34. query = 'SELECT st_xmin(st_envelope(st_collect(st_transform(geom,3763)))),st_ymin(st_envelope(st_collect(st_transform(geom,3763)))),st_xmax(st_envelope(st_collect(st_transform(geom,3763)))),st_ymax(st_envelope(st_collect(st_transform(geom,3763)))), st_astext(st_envelope(st_collect(st_transform(geom,3763)))) FROM cont_aad_caop2017 WHERE concelho = \'PORTO\';'
  35. cursor = db.cursor()
  36. cursor.execute(query)
  37. results = cursor.fetchall()
  38. results = results[0]
  39. xmin = float(results[0])
  40. ymin = float(results[1])
  41. xmax = float(results[2])
  42. ymax = float(results[3])
  43. poligono = results[4]
  44. #print(poligono)
  45. xrects = math.ceil((xmax - xmin) / 600)
  46. yrects = math.ceil((ymax - ymin) / 600)
  47. turtle.setworldcoordinates(0,0,int(xrects),int(yrects))
  48. print(xrects)
  49. print(yrects)
  50. turtle.left(90)
  51. query = 'SELECT count(*) FROM taxi_services;'
  52. cursor.execute(query)
  53. results = cursor.fetchall()
  54. total = int(results[0][0])
  55. print(total)
  56. maximo = 0
  57. resultados_finais = list()
  58. for i in range(int(yrects)):
  59. resultados_finais.append(list())
  60. for j in range(int(xrects)):
  61.  
  62. currx = xmin + j * 600
  63. curry = ymin + i * 600
  64. currx2 = xmin + (j+1) * 600
  65. curry2 = ymin + (i+1) * 600
  66. query = 'SELECT count(*) FROM taxi_services WHERE st_x(st_transform(initial_point,3763)) >= ' + str(currx) + ' AND st_x(st_transform(initial_point,3763)) <= ' + str(currx2) + ' AND st_y(st_transform(initial_point,3763)) >= ' + str(curry) + ' AND st_y(st_transform(initial_point,3763)) <= ' + str(curry2) + ';'
  67. cursor.execute(query)
  68. results = cursor.fetchall()
  69. nservicos = int(results[0][0])
  70. resultados_finais[i].append(nservicos)
  71. print(nservicos)
  72. if nservicos > maximo:
  73. maximo = nservicos
  74. for i in range(int(yrects)):
  75. for j in range(int(xrects)):
  76. draw_square(j,i,(resultados_finais[i][j] * 1.0) / (maximo * 1.0))
  77. turtle.exitonclick()
  78.  
  79. heat_map(db)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement