globalforest/forestmapview.py

56 lines
2 KiB
Python
Raw Normal View History

2020-09-28 23:18:26 -04:00
from kivy.app import App
from kivy.clock import Clock
from kivy_garden.mapview import MapView, MapSource
2020-10-14 00:19:43 -04:00
from treemarker import TreeMarker
class ForestMapView(MapView):
2020-09-28 23:18:26 -04:00
get_trees_timer = None
2020-10-14 00:19:43 -04:00
tree_names = []
map_source = MapSource(url="http://forest.deflax.net/tile/{z}/{x}/{y}.png",
cache_key="forest-map", tile_size=256,
image_ext="png", attribution="@ afx")
2020-09-28 23:18:26 -04:00
2020-10-14 11:54:37 -04:00
def print_current_zoom(self, *args):
print("] Zoom level: " + str(self.zoom))
2020-10-25 16:05:46 -04:00
#pqueue.put("] Zoom level: " + str(self.zoom))
2020-10-14 08:29:53 -04:00
2020-09-28 23:18:26 -04:00
def start_get_fov_trees(self):
# After one second get the trees in field of view
try:
self.get_trees_timer.cancel()
except:
pass
self.get_trees_timer = Clock.schedule_once(self.get_fov_trees, 1)
def get_fov_trees(self, *args):
# Get reference to main app and the db cursor
app = App.get_running_app()
2020-10-14 00:19:43 -04:00
# Gebug gps position
2020-10-18 19:16:32 -04:00
print("] Current map position lat: {} lon: {} ".format(self.lat, self.lon))
2020-09-28 23:18:26 -04:00
min_lat, min_lon, max_lat, max_lon = self.get_bbox()
sql_statement = "SELECT * FROM locations WHERE x > %s AND x < %s AND y > %s AND y < %s" % (min_lat, max_lat, min_lon, max_lon) #sql_statement = "SELECT * FROM locations"
2020-09-28 23:18:26 -04:00
app.cursor.execute(sql_statement)
trees = app.cursor.fetchall()
for tree in trees:
2020-10-14 00:19:43 -04:00
name = tree[0]
2020-10-25 16:05:46 -04:00
#print("] Tree detected: " + str(tree))
2020-10-14 00:19:43 -04:00
if name in self.tree_names:
continue
else:
self.add_tree(tree)
2020-09-28 23:18:26 -04:00
def add_tree(self, tree):
2020-10-14 00:19:43 -04:00
# Create TreeMarker
2020-10-14 18:13:22 -04:00
lat, lon = tree[20], tree[21]
treemarker = TreeMarker(lat=lat, lon=lon)
2020-10-15 06:52:18 -04:00
treemarker.tree_data = tree
2020-10-14 00:19:43 -04:00
# Add TreeMarker to the map
self.add_widget(treemarker)
# Keep track of the TreeMarker's name
2020-10-14 18:13:22 -04:00
name = tree[1]
2020-10-14 12:41:26 -04:00
self.tree_names.append(name)