Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. from matplotlib.path import Path as mPath
  2. import mysql.connector
  3. from mysql.connector import Error
  4.  
  5. #geofence
  6. geofence = '/PATH/TO/GEOFENCE/FILE' #CHANGEME
  7.  
  8. def is_point_in_polygon_matplotlib(point, polygon):
  9. pointTuple = (point['lat'], point['lon'])
  10. polygonTupleList = []
  11. for c in polygon:
  12. coordinateTuple = (c['lat'], c['lon'])
  13. polygonTupleList.append(coordinateTuple)
  14.  
  15. polygonTupleList.append(polygonTupleList[0])
  16. p = mPath(polygonTupleList)
  17. return p.contains_point(pointTuple)
  18.  
  19. def in_area(lat, lon, area):
  20. point = {'lat': lat, 'lon': lon}
  21. polygon = area['polygon']
  22. return is_point_in_polygon_matplotlib(point, polygon)
  23.  
  24. def parse_geofences_file(geofence_file):
  25. geofences = []
  26. # Read coordinates of areas from file.
  27. if geofence_file:
  28. with open(geofence_file) as f:
  29. for line in f:
  30. line = line.strip()
  31. if len(line) == 0: # Empty line.
  32. continue
  33. elif line.startswith("["): # Name line.
  34. name = line.replace("[", "").replace("]", "")
  35. geofences.append({
  36. 'name': name,
  37. 'polygon': []
  38. })
  39. else: # Coordinate line.
  40. lat, lon = line.split(",")
  41. LatLon = {'lat': float(lat), 'lon': float(lon)}
  42. geofences[-1]['polygon'].append(LatLon)
  43. return geofences
  44.  
  45. try:
  46. mySQLconnection = mysql.connector.connect(
  47. host = 'CHANGEME',
  48. user = 'CHANGEME',
  49. passwd = 'CHANGEME',
  50. database = 'CHANGEME')
  51. sql_select_Query = "select latitude, longitude from trs_spawn;" # CHANGEME. This is the place where you can set the type of objects you want to count. F.e.: "SELECT latitude, longitude from pokestop;" would count pokestops. Make sure that the query always just returns latitude and longitude values!
  52. cursor = mySQLconnection .cursor()
  53. cursor.execute(sql_select_Query)
  54. records = cursor.fetchall()
  55. cursor.close()
  56.  
  57. except Error as e :
  58. print ("Error while connecting to MySQL", e)
  59. finally:
  60. #closing database connection.
  61. if(mySQLconnection .is_connected()):
  62. mySQLconnection.close()
  63.  
  64. count = sum([in_area(float(row[0]),float(row[1]), parse_geofences_file(geofence)[0]) for row in records])
  65. print(count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement