Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 48 additions & 35 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@

def get_map_center():
"""Returns center of the map"""
lat = (config.MAP_END[0] + config.MAP_START[0]) / 2
lon = (config.MAP_END[1] + config.MAP_START[1]) / 2
return lat, lon
if isinstance(config.GRID[0], (int, long, float, complex)):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of checking everywhere whether config value is an int, or a list. It's better to convert old value to new format in a single place.
Moreover, if GRID can be a list, then GRID[0] may raise an IndexError.

lat = (config.MAP_END[0] + config.MAP_START[0]) / 2
lon = (config.MAP_END[1] + config.MAP_START[1]) / 2
return lat, lon
else:
lat = (config.MAP_END[0][0] + config.MAP_START[0][0]) / 2
lon = (config.MAP_END[0][1] + config.MAP_START[0][1]) / 2
return lat, lon


def get_scan_area():
def get_scan_area_single(GRID, MAP_START, MAP_END):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are not constants, use lowercase.

"""Returns the square kilometers for configured scan area"""
lat1 = config.MAP_START[0]
lat2 = config.MAP_END[0]
lon1 = config.MAP_START[1]
lon2 = config.MAP_END[1]
lat1 = MAP_START[0]
lat2 = MAP_END[0]
lon1 = MAP_START[1]
lon2 = MAP_END[1]
p1 = Point(lat1, lon1)
p2 = Point(lat1, lon2)
p3 = Point(lat1, lon1)
Expand All @@ -28,19 +33,20 @@ def get_scan_area():
area = int(width * height)
return area

def get_scan_area():
if isinstance(config.GRID[0], (int, long, float, complex)):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - better convert config.GRID to list early and have a single version of this function.

return get_scan_area_single(config.GRID, config.MAP_START, config.MAP_END)
else:
area = 0
for i in range(len(config.GRID)):
area = area + get_scan_area_single(config.GRID[i], config.MAP_START[i], config.MAP_END[i])
return area


def get_start_coords(worker_no):
def get_start_coords(p):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

points, not p

"""Returns center of square for given worker"""
grid = config.GRID
total_workers = grid[0] * grid[1]
per_column = total_workers / grid[0]
column = worker_no % per_column
row = worker_no / per_column
part_lat = (config.MAP_END[0] - config.MAP_START[0]) / float(grid[0])
part_lon = (config.MAP_END[1] - config.MAP_START[1]) / float(grid[1])
start_lat = config.MAP_START[0] + part_lat * row + part_lat / 2
start_lon = config.MAP_START[1] + part_lon * column + part_lon / 2
return start_lat, start_lon
center = [sum(y) / len(y) for y in zip(*p)]
return center[0], center[1]


def float_range(start, end, step):
Expand All @@ -55,39 +61,46 @@ def float_range(start, end, step):
start += step


def get_points_per_worker():
def get_points_per_worker_single(GRID, MAP_START, MAP_END):
"""Returns all points that should be visited for whole grid"""
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete docstring.

total_workers = config.GRID[0] * config.GRID[1]
total_workers = GRID[0] * GRID[1]
lat_gain = getattr(config, 'LAT_GAIN', 0.0015)
lon_gain = getattr(config, 'LON_GAIN', 0.0025)
points = [[] for _ in range(total_workers)]
total_rows = math.ceil(
abs(config.MAP_START[0] - config.MAP_END[0]) / lat_gain
abs(MAP_START[0] - MAP_END[0]) / lat_gain
)
total_columns = math.ceil(
abs(config.MAP_START[1] - config.MAP_END[1]) / lon_gain
abs(MAP_START[1] - MAP_END[1]) / lon_gain
)
for map_row, lat in enumerate(
float_range(config.MAP_START[0], config.MAP_END[0], lat_gain)
float_range(MAP_START[0], MAP_END[0], lat_gain)
):
for map_col, lon in enumerate(
float_range(config.MAP_START[1], config.MAP_END[1], lon_gain)
float_range(MAP_START[1], MAP_END[1], lon_gain)
):
# Figure out which worker this should go to
grid_row = int(map_row / float(total_rows) * config.GRID[0])
grid_col = int(map_col / float(total_columns) * config.GRID[1])
worker_no = grid_row * config.GRID[1] + grid_col
grid_row = int(map_row / float(total_rows) * GRID[0])
grid_col = int(map_col / float(total_columns) * GRID[1])
worker_no = grid_row * GRID[1] + grid_col
points[worker_no].append((lat, lon))
points = [
sort_points_for_worker(p, i)
for i, p in enumerate(points)
]
points = [sort_points_for_worker(p) for p in points]
return points

def get_points_per_worker():
if isinstance(config.GRID[0], (int, long, float, complex)):
return get_points_per_worker_single(config.GRID, config.MAP_START, config.MAP_END)
else:
points = []
for i in range(len(config.GRID)):
points.extend(get_points_per_worker_single(config.GRID[i], config.MAP_START[i], config.MAP_END[i]))
return points



def sort_points_for_worker(points, worker_no):
center = get_start_coords(worker_no)
return sorted(points, key=lambda p: get_distance(p, center))
def sort_points_for_worker(p):
center = get_start_coords(p)
return sorted(p, key=lambda p: get_distance(p, center))


def get_distance(p1, p2):
Expand Down
2 changes: 1 addition & 1 deletion web.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def get_worker_markers():
points = utils.get_points_per_worker()
# Worker start points
for worker_no, worker_points in enumerate(points):
coords = utils.get_start_coords(worker_no)
coords = utils.get_start_coords(worker_points)
markers.append({
'icon': icons.dots.green,
'lat': coords[0],
Expand Down
2 changes: 1 addition & 1 deletion worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ def start_worker(worker_no, points):

def spawn_workers(workers, status_bar=True):
points = utils.get_points_per_worker()
count = len(points)
start_date = datetime.now()
count = config.GRID[0] * config.GRID[1]
for worker_no in range(count):
start_worker(worker_no, points[worker_no])
lenghts = [len(p) for p in points]
Expand Down