Skip to content

Commit b1d8518

Browse files
Added parameters for grid size & resolution
1 parent 129fbce commit b1d8518

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

TapWork.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_taps_demand(taps,houses):
3030
lowest_distance = tap_dist
3131
house_tap = taps.index(tap)
3232
# weight the distance^2 by the size to give a score
33-
houses_tap.append((house_tap,(lowest_distance**4)*(house[0]**2)))
33+
houses_tap.append((house_tap,((lowest_distance**4)*(house[0]**2))/ 10000))
3434

3535
for tap in houses_tap:
3636
tap_demand[tap[0]]+=tap[1]
@@ -51,7 +51,7 @@ def total_demand(tap_demands):
5151
total+=tap
5252
return total
5353

54-
def draw_network(houses,taps,image, display = True):
54+
def draw_network(houses,taps, size, image, meters_squared_per_pixel):
5555

5656
plt.clf()
5757

@@ -73,7 +73,7 @@ def draw_network(houses,taps,image, display = True):
7373
pos[i]=(taps[i][0],taps[i][1])
7474
names[i]= str((i + 1))
7575

76-
nx.draw_networkx(g, pos=pos, names=names, node_color='b',node_size=120, font_size=10, font_color='w')
76+
nx.draw_networkx(g, pos=pos, labels=names, node_color='b',node_size=120, font_size=10, font_color='w')
7777

7878
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
7979
fig1 = plt.gcf()
@@ -89,7 +89,6 @@ def draw_network(houses,taps,image, display = True):
8989
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
9090

9191
crop_img = img[5:455, 94:544]
92-
# cv2.imwrite("figureTaps.png",crop_img)
9392
return crop_img
9493

9594
def total(demands):
@@ -100,9 +99,7 @@ def total(demands):
10099

101100
def greedy_brute(houses,amount_of_taps,grid_size, downscale):
102101
total_tick = (amount_of_taps*grid_size[0]*grid_size[1]) / (downscale**2)
103-
happy_taps = []
104102
stored_taps = []
105-
possible_coords = []
106103

107104
with tqdm(total=total_tick) as t:
108105
for tap_placed in range(amount_of_taps):
@@ -111,7 +108,7 @@ def greedy_brute(houses,amount_of_taps,grid_size, downscale):
111108
for x in range(grid_size[0] + 1):
112109
for y in range(grid_size[1] + 1):
113110
if (x % downscale == 0 and y % downscale == 0):
114-
coord = (x, y);
111+
coord = (x, y)
115112
t.update(1)
116113
taps = list(copy.copy(stored_taps))
117114
taps.append(coord)
@@ -126,4 +123,5 @@ def greedy_brute(houses,amount_of_taps,grid_size, downscale):
126123
best_tap = coord
127124
min_total_differnces = total_differnce
128125
stored_taps.append(best_tap)
126+
print(stored_taps)
129127
return stored_taps

api.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ def toBase64(image):
1515
png_as_text = base64.b64encode(buffer).decode('utf-8')
1616
return png_as_text
1717

18-
def process(image, max_size=200,taps=3, downscale=10):
19-
resized_image = resize.resize(image,max_size)
20-
height, width, _ = resized_image.shape
21-
recoloured_image = recolour.find_silver(resized_image)
22-
houses, percentage, housearea = contours.get_contour_nodes(recoloured_image)
23-
print(downscale)
18+
def process(image, meters_squared_per_pixel, size ,taps=3, grid=20):
19+
downscale = int(size / grid)
20+
height, width, _ = image.shape
21+
recoloured_image = recolour.find_silver(image)
22+
houses, percentage, housearea = contours.get_contour_nodes(recoloured_image, meters_squared_per_pixel)
2423
tap_locations = TapWork.greedy_brute(houses,taps,(height,width), downscale)
25-
image = TapWork.draw_network(houses,tap_locations,resized_image, (height,width))
24+
image = TapWork.draw_network(houses,tap_locations, size, image, meters_squared_per_pixel)
2625
population = round(housearea / 7)
2726
recommendation = round(population / 250)
2827
if recommendation == 0:
@@ -33,9 +32,9 @@ def process(image, max_size=200,taps=3, downscale=10):
3332
def giveLocation():
3433
lon = request.args.get('long')
3534
lat = request.args.get('lat')
36-
max_size = request.args.get('size')
35+
size = request.args.get('size')
3736
taps = request.args.get('taps')
38-
downscale = request.args.get('downscale')
37+
grid = request.args.get('grid')
3938
zoom = request.args.get('zoom')
4039
print(lon,lat)
4140
if lon is None or lat is None:
@@ -44,19 +43,16 @@ def giveLocation():
4443
return response
4544
if zoom is None:
4645
zoom = 19
47-
image = map_downloader.download_patch((lat,lon), credentials.API_KEY, zoom=int(zoom))
48-
image_array = numpy.asarray(bytearray(image.content), dtype=numpy.uint8)
49-
map_image = cv2.imdecode(image_array, -1)
50-
if max_size is not None and taps is not None and downscale is not None:
51-
result = process(map_image, max_size=int(max_size),taps=int(taps), downscale=int(downscale))
52-
elif max_size is not None and taps is not None:
53-
result = process(map_image, max_size=int(max_size),taps=int(taps))
54-
elif max_size is not None:
55-
result = process(map_image, max_size=int(max_size))
46+
if size is None:
47+
size = 1000
48+
49+
map_image, meters_squared_per_pixel = map_downloader.download_patch((lat,lon), credentials.API_KEY, size, zoom=int(zoom))
50+
if size is not None and taps is not None and grid is not None:
51+
result = process(map_image, meters_squared_per_pixel, int(size),taps=int(taps), grid=int(grid))
5652
elif taps is not None:
57-
result = process(map_image, taps=taps)
53+
result = process(map_image, meters_squared_per_pixel, int(size), taps=int(taps))
5854
else:
59-
result = process(map_image)
55+
result = process(map_image, meters_squared_per_pixel, int(size))
6056

6157
return jsonify(result)
6258

contours.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
import cv2
44

55
# construct the argument parse and parse the arguments
6-
def get_contour_nodes(image):
6+
def get_contour_nodes(image, meters_squared_per_pixel):
77
# Total area is 83,321 m^2
88
height, width = image.shape
99
totalpixels = height * width
1010

1111
print(height, width)
1212

13-
areaperpixel = 83321 / totalpixels
14-
1513
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
1614
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
1715
# thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
@@ -30,15 +28,15 @@ def get_contour_nodes(image):
3028
# compute the center of the contour
3129
try:
3230
# consider buildings greater than 8m^2
33-
if cv2.contourArea(c) * areaperpixel > 8:
31+
if cv2.contourArea(c) * meters_squared_per_pixel >10:
3432
M = cv2.moments(c)
3533
cX = round(M["m10"] / M["m00"])
3634
cY = round(M["m01"] / M["m00"])
37-
totalarea += cv2.contourArea(c) * areaperpixel
38-
nodes.append([cv2.contourArea(c) * areaperpixel, [cX, cY]])
35+
totalarea += cv2.contourArea(c) * meters_squared_per_pixel
36+
nodes.append([cv2.contourArea(c) * meters_squared_per_pixel, [cX, cY]])
3937
except:
4038
pass
4139

4240
print("average {}".format(totalarea / len(cnts)))
43-
percentage = round((totalarea / (totalpixels * areaperpixel)) * 100)
41+
percentage = round((totalarea / (totalpixels * meters_squared_per_pixel)) * 100)
4442
return nodes, percentage, totalarea

map_downloader.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import requests
2+
import math
3+
import numpy
4+
import cv2
25

3-
def download_patch(lat_long,api_key,zoom=19,dimensions=(1000,1000),file_format='png'):
6+
def download_patch(lat_long,api_key, size, zoom=19,file_format='png'):
47
latitude, longitude = lat_long
5-
x, y = dimensions
8+
extra = 100
9+
x, y = size, str((int(size) + extra))
610
URL = ('https://dev.virtualearth.net/REST/v1/Imagery/Map/Aerial/{},{}/'
711
'{}?mapSize={},{}&format={}').format(latitude,longitude,zoom,x,y,file_format)
8-
print("Downloading the map from {}".format(URL))
12+
print("Downloading the map from {}&key={}".format(URL, api_key))
913
image = requests.get(("{}&key={}").format(URL, api_key))
14+
15+
meters_squared_per_pixel = (156543.04 * math.cos(math.radians(float(latitude))) / (2**zoom))**2
16+
print("Meters squared per pixel: {} ".format(str(meters_squared_per_pixel)))
1017
# metadata = requests.get(("{}&mapMetadata=1&key={}").format(URL, api_key))
1118
# boundingbox = metadata.json()["resourceSets"][0]["resources"][0]["bbox"]
1219
# print(metadata.text);
13-
return image
20+
21+
image_array = numpy.asarray(bytearray(image.content), dtype=numpy.uint8)
22+
map_image = cv2.imdecode(image_array, -1)
23+
24+
# Crop
25+
map_image = map_image[0:int(size), 0:int(size)]
26+
return map_image, meters_squared_per_pixel

test.png

1.72 MB
Loading

0 commit comments

Comments
 (0)