from mgo import mgo
from IPython.display import display, Markdown
import folium
import matplotlib.pyplot as plt
file_dir = 'mgo/uploads'
village = 'Nakiu'
min_area = 70 # exclude any buildings with area (in m2) below this value
max_length = 10000 # The furthest a single building can be (in total) from the PV point
cost_wire = 25 # per metre
cost_connection = 150 # per node
opex_ratio = 0.02 # % of the above per year
years = 20 # years over which to amortize (and maintain)
tariff = 0.25 # USD/kWh
demand = 6 # 6kWh/person/month = MTF Tier 2
gen_cost = 7000 # USD/kW
discount_rate = 0.08
buildings = mgo.load_buildings(village=village,
file_dir=file_dir,
min_area=min_area)
x_mean = buildings.geometry.centroid.x.mean()
y_mean = buildings.geometry.centroid.y.mean()
village_map = folium.Map([y_mean, x_mean], zoom_start=15, control_scale=True)
popup_html = '<h2>Latitude: " + lat + "</h2><h2>Longitude: " + lng + "</h2><h3>Copy these values into the variables below.</h3>'
folium.ClickForMarker(popup=popup_html).add_to(village_map)
display(Markdown('### Please click on the desired location for the PV point.'))
display(Markdown('### Then click on the marker that appears and copy the values in below (with all decimals values!).'))
display(village_map)
get_lat = -9.6266
gen_lng = 39.1824
network, nodes = mgo.create_network(buildings,
gen_lat=get_lat,
gen_lng=gen_lng)
xs = [n[1] for n in network]
ys = [n[2] for n in network]
xe = [n[3] for n in network]
ye = [n[4] for n in network]
fig = plt.figure(figsize=(10, 10))
plt.scatter([n[1] for n in nodes[1:]], [n[2] for n in nodes[1:]], s=20, lw=0, c='blue')
plt.scatter(nodes[0][1], nodes[0][2], s=80, lw=0, c='green')
plt.plot([xs, xe], [ys, ye], c='red', lw=1)
display(Markdown('### A small graph is shown below, with the PV point in green'))
fig.axes[0].tick_params(labelbottom=False, labelleft=False)
plt.axis('equal')
plt.show()
results, network, nodes = mgo.run_model(network, nodes,
demand=demand,
tariff=tariff,
gen_cost=gen_cost,
cost_wire=cost_wire,
cost_connection=cost_connection,
opex_ratio=opex_ratio,
years=years,
discount_rate=discount_rate)
display(Markdown(f'Total houses connected: {results["connected"]} out of {len(nodes)-1}'))
display(Markdown(f'Generator installation size: {results["gen_size"]:.0f} kW'))
display(Markdown(f'Total length of lines is {results["length"]:.0f}m'))
display(Markdown(f'CAPEX: ${results["capex"]:.0f}'))
display(Markdown(f'Annual OPEX: ${results["opex"]:.0f}'))
display(Markdown(f'Annual Income: ${results["income"]:.0f}'))
display(Markdown(f'**NPV over {years} years is ${results["npv"]:.0f}**'))
Total houses connected: 141 out of 150
Generator installation size: 110 kW
Total length of lines is 5683m
CAPEX: $933568
Annual OPEX: $18671
Annual Income: $39617
NPV over 20 years is $-732413
network_gdf, buildings_gdf = mgo.network_to_spatial(buildings, network, nodes)
village_map = folium.Map([y_mean, x_mean], zoom_start=16, control_scale=True)
folium.GeoJson(network_gdf).add_to(village_map)
icon = folium.Icon(icon='bolt', color='green', prefix='fa')
folium.Marker([get_lat, gen_lng], icon=icon, popup='PV plant location').add_to(village_map)
def highlight_function(feature):
return {
'fillColor': '#b2e2e2',
'fillOpacity': 0.5,
'color': 'black',
'weight': 3,
}
styles = []
max_area = buildings_gdf['area'].max()
for index, row in buildings_gdf.iterrows():
if row['area'] > max_area*0.8: fill_color = '#006d2c'
elif row['area'] > max_area*0.6: fill_color = '#2ca25f'
elif row['area'] > max_area*0.4: fill_color = '#66c2a4'
elif row['area'] > max_area*0.2: fill_color = '#b2e2e2'
else: fill_color = '#edf8fb'
styles.append({'fillColor': fill_color, 'weight': 1, 'color': 'black', 'fillOpacity': 1})
buildings_gdf['style'] = styles
folium.GeoJson(buildings_gdf, highlight_function=highlight_function).add_to(village_map)
display(village_map)
len(buildings), len(buildings_gdf)
(150, 141)
buildings_gdf
type | id | tags | meta | geometry | area | index | X | Y | marg_dist | tot_dist | connected | style | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | way | 543041768 | {'building': 'yes'} | {} | POLYGON ((39.1904398 -9.625552500000355, 39.19... | 234.564190 | 2 | 1464108 | -1144842 | 28.792360 | 2125.881186 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
2 | way | 543041770 | {'building': 'yes'} | {} | POLYGON ((39.1901281 -9.62556700000016, 39.190... | 250.245536 | 3 | 1464077 | -1144843 | 11.704700 | 2108.793525 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
3 | way | 543041775 | {'building': 'yes'} | {} | POLYGON ((39.1887618 -9.625144900000011, 39.18... | 127.014864 | 4 | 1463929 | -1144795 | 25.709920 | 1895.387391 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
4 | way | 543041777 | {'building': 'yes'} | {} | POLYGON ((39.1894811 -9.624799500000131, 39.18... | 247.794520 | 5 | 1464009 | -1144754 | 89.894382 | 1985.281773 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
5 | way | 543041782 | {'building': 'yes'} | {} | POLYGON ((39.1896567 -9.624462300000044, 39.18... | 239.726859 | 6 | 1464022 | -1144719 | 37.336309 | 2022.618083 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
6 | way | 543041783 | {'building': 'yes'} | {} | POLYGON ((39.189641 -9.624392399999982, 39.189... | 264.799264 | 7 | 1464022 | -1144688 | 31.000000 | 2053.618083 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
7 | way | 543041785 | {'building': 'yes'} | {} | POLYGON ((39.190197 -9.623478100000101, 39.190... | 262.447767 | 8 | 1464085 | -1144599 | 96.176920 | 2258.606767 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
8 | way | 543041789 | {'building': 'yes'} | {} | POLYGON ((39.189415 -9.623191799999955, 39.189... | 80.418789 | 9 | 1463998 | -1144553 | 32.015621 | 2194.445468 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
9 | way | 543041794 | {'building': 'yes'} | {} | POLYGON ((39.1893828 -9.623356299999898, 39.18... | 71.573659 | 10 | 1463990 | -1144584 | 108.811764 | 2162.429847 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
10 | way | 543044727 | {'building': 'yes'} | {} | POLYGON ((39.1882158 -9.626891599999935, 39.18... | 86.439146 | 11 | 1463863 | -1144987 | 76.026311 | 1586.242421 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
11 | way | 543044728 | {'building': 'yes'} | {} | POLYGON ((39.1883645 -9.626769099999883, 39.18... | 73.276014 | 12 | 1463879 | -1144973 | 21.260292 | 1607.502712 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
12 | way | 543044732 | {'building': 'yes'} | {} | POLYGON ((39.18848490000001 -9.626659499999995... | 75.952526 | 13 | 1463899 | -1144971 | 20.099751 | 1627.602464 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
13 | way | 543049551 | {'building': 'yes'} | {} | POLYGON ((39.1883947 -9.632379300000153, 39.18... | 99.896645 | 14 | 1463885 | -1145632 | 51.546096 | 1168.159130 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
14 | way | 543049553 | {'building': 'yes'} | {} | POLYGON ((39.188415 -9.631569400000007, 39.188... | 218.245874 | 15 | 1463897 | -1145538 | 22.360680 | 1064.174409 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
15 | way | 543049554 | {'building': 'yes'} | {} | POLYGON ((39.1886512 -9.631751899999648, 39.18... | 114.917663 | 16 | 1463912 | -1145557 | 24.207437 | 1088.381846 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
16 | way | 543049555 | {'building': 'yes'} | {} | POLYGON ((39.1882995 -9.63165709999998, 39.188... | 82.678650 | 17 | 1463877 | -1145548 | 61.983869 | 1041.813729 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
17 | way | 543049556 | {'building': 'yes'} | {} | POLYGON ((39.1883913 -9.6318752, 39.1883832999... | 184.098757 | 18 | 1463877 | -1145567 | 19.000000 | 1060.813729 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
18 | way | 543049557 | {'building': 'yes'} | {} | POLYGON ((39.1884414 -9.631976500000127, 39.18... | 283.181884 | 19 | 1463901 | -1145583 | 28.231188 | 1116.613034 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
19 | way | 543049571 | {'building': 'yes'} | {} | POLYGON ((39.1869165 -9.630327799999966, 39.18... | 80.190846 | 20 | 1463730 | -1145389 | 54.037024 | 1117.774252 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
20 | way | 543049608 | {'building': 'yes'} | {} | POLYGON ((39.18925049999999 -9.629519999999983... | 102.253511 | 21 | 1463967 | -1145297 | 102.107786 | 1418.805928 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
21 | way | 543049626 | {'building': 'yes'} | {} | POLYGON ((39.1883393 -9.629003500000392, 39.18... | 71.330257 | 22 | 1463875 | -1145236 | 35.014283 | 1359.652735 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
22 | way | 543058863 | {'building': 'yes'} | {} | POLYGON ((39.1874013 -9.633807399999739, 39.18... | 147.914821 | 23 | 1463776 | -1145797 | 38.470768 | 1102.870384 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
23 | way | 543068153 | {'building': 'yes'} | {} | POLYGON ((39.1858343 -9.632513400000116, 39.18... | 86.284967 | 24 | 1463620 | -1145645 | 18.601075 | 902.326608 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
24 | way | 543068164 | {'building': 'yes'} | {} | POLYGON ((39.1849595 -9.633267400000117, 39.18... | 72.972538 | 25 | 1463528 | -1145734 | 45.607017 | 1061.978451 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
25 | way | 543068333 | {'building': 'yes'} | {} | POLYGON ((39.1850399 -9.633892300000037, 39.18... | 88.950194 | 26 | 1463541 | -1145814 | 41.048752 | 1249.577966 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
26 | way | 543068351 | {'building': 'yes'} | {} | POLYGON ((39.18551 -9.633962400000065, 39.1855... | 72.789441 | 27 | 1463582 | -1145816 | 29.274562 | 1208.529214 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
27 | way | 543068373 | {'building': 'yes'} | {} | POLYGON ((39.1853722 -9.633427400000052, 39.18... | 92.436770 | 28 | 1463570 | -1145752 | 45.694639 | 1107.673090 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
28 | way | 543068388 | {'building': 'yes'} | {} | POLYGON ((39.1864329 -9.633610700000085, 39.18... | 87.470835 | 29 | 1463678 | -1145775 | 21.931712 | 1092.465283 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
29 | way | 543068451 | {'building': 'yes'} | {} | POLYGON ((39.18678600000001 -9.633339200000044... | 76.819090 | 30 | 1463721 | -1145749 | 24.207437 | 1014.389617 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
30 | way | 543068459 | {'building': 'yes'} | {} | POLYGON ((39.1864505 -9.633800399999796, 39.18... | 88.425838 | 31 | 1463687 | -1145800 | 12.206556 | 1082.740126 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
120 | way | 606324581 | {'building': 'yes'} | {} | POLYGON ((39.1867733 -9.631876599999758, 39.18... | 71.403833 | 121 | 1463720 | -1145578 | 9.433981 | 828.505088 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
121 | way | 606324582 | {'building': 'yes'} | {} | POLYGON ((39.1866881 -9.631908299999571, 39.18... | 93.201070 | 122 | 1463712 | -1145583 | 24.413111 | 819.071107 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
122 | way | 606324589 | {'building': 'yes'} | {} | POLYGON ((39.1869047 -9.632155599999988, 39.18... | 75.593758 | 123 | 1463726 | -1145605 | 26.076810 | 845.147917 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
123 | way | 606324608 | {'building': 'yes'} | {} | POLYGON ((39.1876397 -9.633126700000037, 39.18... | 71.475632 | 124 | 1463808 | -1145723 | 52.345009 | 1116.744625 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
124 | way | 606324609 | {'building': 'yes'} | {} | POLYGON ((39.1877308 -9.632835799999867, 39.18... | 92.261592 | 125 | 1463814 | -1145699 | 24.738634 | 1141.483259 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
125 | way | 606324616 | {'building': 'yes'} | {} | POLYGON ((39.1877925 -9.632669199999922, 39.18... | 78.378389 | 126 | 1463825 | -1145670 | 31.016125 | 1172.499384 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
126 | way | 606324623 | {'building': 'yes'} | {} | POLYGON ((39.1870023 -9.632486000000307, 39.18... | 73.210570 | 127 | 1463742 | -1145654 | 16.643317 | 897.484370 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
127 | way | 606324624 | {'building': 'yes'} | {} | POLYGON ((39.1865 -9.631856699999785, 39.18651... | 109.414335 | 128 | 1463692 | -1145569 | 39.446166 | 794.657996 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
128 | way | 606324629 | {'building': 'yes'} | {} | POLYGON ((39.1861336 -9.632004800000264, 39.18... | 252.792031 | 129 | 1463659 | -1145595 | 13.928388 | 837.586384 | 1 | {'fillColor': '#2ca25f', 'weight': 1, 'color':... |
129 | way | 606324630 | {'building': 'yes'} | {} | POLYGON ((39.186303 -9.631981199999812, 39.186... | 73.597710 | 130 | 1463672 | -1145590 | 29.000000 | 823.657996 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
130 | way | 606324634 | {'building': 'yes'} | {} | POLYGON ((39.186086 -9.632194599999545, 39.186... | 80.844751 | 131 | 1463650 | -1145615 | 21.931712 | 859.518096 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
131 | way | 606324635 | {'building': 'yes'} | {} | POLYGON ((39.1858868 -9.632371500000048, 39.18... | 165.499792 | 132 | 1463631 | -1145630 | 24.207437 | 883.725533 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
132 | way | 606324642 | {'building': 'yes'} | {} | POLYGON ((39.1869973 -9.632704600000062, 39.18... | 72.795404 | 133 | 1463743 | -1145674 | 20.024984 | 917.509355 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
133 | way | 606324644 | {'building': 'yes'} | {} | POLYGON ((39.1869255 -9.632912900000081, 39.18... | 71.997158 | 134 | 1463729 | -1145694 | 24.413111 | 941.922466 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
134 | way | 606324646 | {'building': 'yes'} | {} | POLYGON ((39.1865089 -9.632623599999844, 39.18... | 166.286644 | 135 | 1463696 | -1145662 | 22.203603 | 988.539180 | 1 | {'fillColor': '#66c2a4', 'weight': 1, 'color':... |
135 | way | 606324648 | {'building': 'construction'} | {} | POLYGON ((39.1865829 -9.63320939999986, 39.186... | 130.312453 | 136 | 1463702 | -1145734 | 48.259714 | 990.182180 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
136 | way | 606331254 | {'building': 'yes'} | {} | POLYGON ((39.1874527 -9.63069170000005, 39.187... | 84.508380 | 137 | 1463790 | -1145444 | 21.023796 | 1023.698823 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
137 | way | 606344131 | {'building': 'yes'} | {} | POLYGON ((39.1875887 -9.631038299999881, 39.18... | 82.610928 | 138 | 1463805 | -1145479 | 13.601471 | 973.830616 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
138 | way | 606344134 | {'building': 'yes'} | {} | POLYGON ((39.18819489999999 -9.630165000000083... | 92.793881 | 139 | 1463869 | -1145376 | 66.483081 | 1152.045802 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
139 | way | 606344135 | {'building': 'yes'} | {} | POLYGON ((39.1881815 -9.629884700000046, 39.18... | 91.926587 | 140 | 1463867 | -1145349 | 27.073973 | 1179.119775 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
140 | way | 606344136 | {'building': 'yes'} | {} | POLYGON ((39.18800439999999 -9.630016899999802... | 100.920074 | 141 | 1463847 | -1145354 | 20.615528 | 1199.735303 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
141 | way | 606344140 | {'building': 'yes'} | {} | POLYGON ((39.1877979 -9.629574000000353, 39.18... | 100.779492 | 142 | 1463828 | -1145304 | 53.488316 | 1253.223620 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
142 | way | 606344148 | {'building': 'yes'} | {} | POLYGON ((39.1880018 -9.627636899999711, 39.18... | 83.694390 | 143 | 1463847 | -1145086 | 51.000000 | 1474.369213 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
143 | way | 606344155 | {'building': 'yes'} | {} | POLYGON ((39.18799909999999 -9.628074600000188... | 70.483770 | 144 | 1463847 | -1145137 | 20.099751 | 1423.369213 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
144 | way | 606344161 | {'building': 'yes'} | {} | POLYGON ((39.1880125 -9.628451399999722, 39.18... | 94.243862 | 145 | 1463848 | -1145183 | 52.611786 | 1377.250238 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
145 | way | 606344163 | {'building': 'yes'} | {} | POLYGON ((39.1880085 -9.62824119999968, 39.187... | 98.554492 | 146 | 1463849 | -1145157 | 26.019224 | 1403.269462 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
146 | way | 606344167 | {'building': 'yes'} | {} | POLYGON ((39.187936 -9.62891150000009, 39.1879... | 73.467663 | 147 | 1463840 | -1145235 | 35.014283 | 1324.638452 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
147 | way | 606344173 | {'building': 'yes'} | {} | POLYGON ((39.1874251 -9.628617999999706, 39.18... | 80.600827 | 148 | 1463787 | -1145201 | 36.000000 | 1413.676175 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
148 | way | 606344174 | {'building': 'yes'} | {} | POLYGON ((39.1874123 -9.628928700000186, 39.18... | 91.428968 | 149 | 1463787 | -1145237 | 53.037722 | 1377.676175 | 1 | {'fillColor': '#b2e2e2', 'weight': 1, 'color':... |
149 | way | 606344177 | {'building': 'yes'} | {} | POLYGON ((39.1880085 -9.629304200000259, 39.18... | 74.954287 | 150 | 1463841 | -1145270 | 36.400549 | 1289.624169 | 1 | {'fillColor': '#edf8fb', 'weight': 1, 'color':... |
141 rows × 13 columns
buildings_gdf.to_csv('before_change.csv')