Example #1
0
def overlapping_cities(cursor, verbose):
	city_dict = city_q.get_live_cities(cursor)
	
	checked = []
	overlap_dict = {}
	
	for id1, c1 in city_dict.items():
		c1.overlap = 0
	
	# Cache some stuff
	if verbose:
		it = cli_f.progressbar(city_dict.items(), "cities_check.overlapping_cities: ", 40, with_eta = True)
	else:
		it = city_dict.items()
	
	for id1, c1 in it:
		checked.append(id1)
		
		for id2, c2 in city_dict.items():
			if id2 in checked: continue
			
			amount = city_f.overlap(c1, c2)
			
			c1.overlap += city_f.overlap_percentage(c1, amount)
			c2.overlap += city_f.overlap_percentage(c2, amount)
	
	# Reset all cities
	query = """UPDATE cities SET overlap = 0"""
	try: cursor.execute(query)
	except Exception as e:
		raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
	
	updates = 0
	for id1, c1 in city_dict.items():
		if c1.overlap >= 1:
			updates += 1
			
			query = "UPDATE cities SET overlap = %d WHERE id = %d;" % (c1.overlap, id1)
			# print("%s - %s" % (query, c1.name))
			# continue
			
			try: cursor.execute(query)
			except Exception as e:
				raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query))
Example #2
0
	def test_overlapping(self):
		for the_city, amount, correct_answer in self.city_sets:
			real_area = map_data.map_image_size(the_city.size)/2.5
			real_area = math.pi * (real_area ** 2)
			
			r = city_f.overlap_percentage(the_city, amount)
			
			try:
				self.assertAlmostEqual(correct_answer, r, places=2)
			except Exception as e:
				print("")
				print("City size: %d" % the_city.size)
				print("City area: %d" % real_area)
				print("Overlap amount: %d" % amount)
				print("Correct amount: %d" % correct_answer)
				print("Incorrect answer: %d" % r)
				print("")
				raise