From cad96ee21206d33c8ab057febea7bc923d3756e5 Mon Sep 17 00:00:00 2001 From: "francois.nzale" Date: Tue, 11 May 2021 08:44:54 +0200 Subject: [PATCH 01/12] fix issue with sgdot_lite package and requirements --- fastapi_app/main.py | 4 ++-- fastapi_app/requirements.txt | 32 +++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/fastapi_app/main.py b/fastapi_app/main.py index 9d4aeec..f7353f5 100644 --- a/fastapi_app/main.py +++ b/fastapi_app/main.py @@ -6,8 +6,8 @@ import models from sqlalchemy.orm import Session import sqlite3 -from sgdot_lite.grids import Grid -from sgdot_lite.tools.grid_optimizer import GridOptimizer +from sgdotlite.grids import Grid +from sgdotlite.tools.grid_optimizer import GridOptimizer import math import urllib.request import json diff --git a/fastapi_app/requirements.txt b/fastapi_app/requirements.txt index 0576bd2..c05f590 100644 --- a/fastapi_app/requirements.txt +++ b/fastapi_app/requirements.txt @@ -1,8 +1,26 @@ +aiofiles==0.6.0 +autopep8==1.5.5 +certifi==2020.12.5 +chardet==4.0.0 +click==7.1.2 +fastapi==0.63.0 +gunicorn==20.1.0 +h11==0.12.0 +idna==2.10 +Jinja2==2.11.3 +lxml==4.6.2 +multitasking==0.0.9 networkx==2.5.1 -fastapi -SQLAlchemy>=1.4.13 -pandas>=1.2.4 -numpy>=1.20.2 -scipy -pydantic -sgdot_lite>=1.2 +pickleshare==0.7.5 +pycodestyle==2.6.0 +pydantic==1.7.3 +python-dateutil==2.8.1 +requests==2.25.1 +six==1.15.0 +sgdotlite==1.0.2 +SQLAlchemy==1.3.23 +starlette==0.13.6 +toml==0.10.2 +typing-extensions==3.7.4.3 +urllib3==1.26.3 +uvicorn==0.13.3 From 1ac5c92a8d5a0d6900ddf180be28fbaa4ab01a67 Mon Sep 17 00:00:00 2001 From: "francois.nzale" Date: Tue, 11 May 2021 08:58:59 +0200 Subject: [PATCH 02/12] make changes to paths and dependencies to make code deployment (heroku) ready --- fastapi_app/main.py | 27 +++++++++++++++------------ fastapi_app/models.py | 2 +- fastapi_app/requirements.txt | 2 ++ fastapi_app/static/js/interactions.js | 9 +++++---- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/fastapi_app/main.py b/fastapi_app/main.py index f7353f5..7825944 100644 --- a/fastapi_app/main.py +++ b/fastapi_app/main.py @@ -1,9 +1,11 @@ +import fastapi_app.tools.boundary_identification as bi +import fastapi_app.tools.shs_identification as shs_ident +import fastapi_app.models as models from fastapi.param_functions import Query from fastapi import FastAPI, Request, Depends, BackgroundTasks from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles -from database import SessionLocal, engine -import models +from fastapi_app.database import SessionLocal, engine from sqlalchemy.orm import Session import sqlite3 from sgdotlite.grids import Grid @@ -11,21 +13,18 @@ import math import urllib.request import json -import tools.boundary_identification as bi -import tools.shs_identification as shs_ident import pandas as pd import numpy as np - import time - app = FastAPI() -app.mount("/static", StaticFiles(directory="static"), name="static") +app.mount("/fastapi_app/static", + StaticFiles(directory="fastapi_app/static"), name="static") models.Base.metadata.create_all(bind=engine) -templates = Jinja2Templates(directory="templates") +templates = Jinja2Templates(directory="fastapi_app/templates") grid_db = "grid.db" @@ -231,7 +230,8 @@ async def optimize_grid(optimize_grid_request: models.OptimizeGridRequest, x_to = grid.get_nodes().loc[row['to']]['x_coordinate'] y_to = grid.get_nodes().loc[row['to']]['y_coordinate'] - long_from = math.degrees(longitude_0 + x_from / (r * math.cos(latitude_0))) + long_from = math.degrees( + longitude_0 + x_from / (r * math.cos(latitude_0))) lat_from = math.degrees(latitude_0 + y_from / r) @@ -251,7 +251,8 @@ async def optimize_grid(optimize_grid_request: models.OptimizeGridRequest, distance)) count += 1 - cursor.executemany('INSERT INTO links VALUES(?, ?, ?, ?, ?, ?, ?)', records) + cursor.executemany( + 'INSERT INTO links VALUES(?, ?, ?, ?, ?, ?, ?)', records) # commit the changes to db conn.commit() @@ -310,7 +311,8 @@ def identify_shs(shs_identification_request: models.ShsIdentificationRequest, required_capacity = node[5] max_power = node[6] - shs_ident.add_node(nodes_df, node_label, x, y, required_capacity, max_power) + shs_ident.add_node(nodes_df, node_label, x, y, + required_capacity, max_power) links_df = shs_ident.mst_links(nodes_df) start_time = time.time() if shs_identification_request.algo == "mst1": @@ -320,7 +322,8 @@ def identify_shs(shs_identification_request: models.ShsIdentificationRequest, cable_price_per_meter=cable_price_per_meter, additional_price_for_connection_per_node=additional_price_for_connection_per_node, shs_characteristics=shs_characteristics) - print(f"execution time for shs identification (mst1): {time.time() - start_time} s") + print( + f"execution time for shs identification (mst1): {time.time() - start_time} s") else: print("issue with version parameter of shs_identification_request") return 0 diff --git a/fastapi_app/models.py b/fastapi_app/models.py index 1a50b75..9fc057d 100644 --- a/fastapi_app/models.py +++ b/fastapi_app/models.py @@ -2,7 +2,7 @@ from sqlalchemy.orm import relationship from pydantic import BaseModel from typing import Optional -from database import Base +from fastapi_app.database import Base # Models diff --git a/fastapi_app/requirements.txt b/fastapi_app/requirements.txt index c05f590..4f19671 100644 --- a/fastapi_app/requirements.txt +++ b/fastapi_app/requirements.txt @@ -6,6 +6,7 @@ click==7.1.2 fastapi==0.63.0 gunicorn==20.1.0 h11==0.12.0 +httptools==0.2.0 idna==2.10 Jinja2==2.11.3 lxml==4.6.2 @@ -24,3 +25,4 @@ toml==0.10.2 typing-extensions==3.7.4.3 urllib3==1.26.3 uvicorn==0.13.3 +uvloop==0.15.2 diff --git a/fastapi_app/static/js/interactions.js b/fastapi_app/static/js/interactions.js index 8e12228..c157497 100644 --- a/fastapi_app/static/js/interactions.js +++ b/fastapi_app/static/js/interactions.js @@ -180,21 +180,21 @@ var dashedBoundaryLine = null; L.control.scale().addTo(mainMap); var householdMarker = new L.Icon({ - iconUrl: "static/images/markers/marker-household.png", + iconUrl: "fastapi_app/static/images/markers/marker-household.png", iconSize: [10, 10], iconAnchor: [5, 5], popupAnchor: [0, 0], }); var hubMarker = new L.Icon({ - iconUrl: "/static/images/markers/marker-hub.png", + iconUrl: "fastapi_app/static/images/markers/marker-hub.png", iconSize: [14, 14], iconAnchor: [7, 7], popupAnchor: [0, 0], }); var shsMarker = new L.Icon({ - iconUrl: "static/images/markers/marker-shs.png", + iconUrl: "fastapi_app/static/images/markers/marker-shs.png", iconSize: [10, 10], iconAnchor: [5, 5], popupAnchor: [0, 0], @@ -385,7 +385,8 @@ function identify_shs( contentType: "application/json", data: JSON.stringify({ cable_price_per_meter_for_shs_mst_identification: cable_price_per_meter, - additional_connection_price_for_shs_mst_identification: additional_connection_price, + additional_connection_price_for_shs_mst_identification: + additional_connection_price, algo, shs_characteristics, }), From e73d840ad6f1e859e1c0de45a5af7f4374050151 Mon Sep 17 00:00:00 2001 From: "francois.nzale" Date: Tue, 11 May 2021 09:29:51 +0200 Subject: [PATCH 03/12] restructure JS files --- fastapi_app/static/js/interactions.js | 417 +++++--------------------- fastapi_app/static/js/map.js | 202 +++++++++++++ fastapi_app/templates/home.html | 11 +- 3 files changed, 291 insertions(+), 339 deletions(-) create mode 100644 fastapi_app/static/js/map.js diff --git a/fastapi_app/static/js/interactions.js b/fastapi_app/static/js/interactions.js index c157497..2926ab1 100644 --- a/fastapi_app/static/js/interactions.js +++ b/fastapi_app/static/js/interactions.js @@ -1,120 +1,6 @@ $(document).ready(function () { refreshNodeFromDataBase(); refreshLinksFromDatBase(); - - $("#button_add_undefined_node").click(function () { - mapClickEvent = "add_node"; - }); - - $("#button_add_household").click(function () { - mapClickEvent = "add_fixed_household"; - }); - - $("#button_add_meterhub").click(function () { - mapClickEvent = "add_fixed_meterhub"; - }); - - $("#button_add_node").click(function () { - const latitude = new_node_lat.value; - const longitude = new_node_long.value; - const node_type = new_node_type.value; - const fixed_type = new_node_type_fixed.value; - const required_capacity = default_household_required_capacity; - const max_power = default_household_max_power; - - addNodeToDatBase( - latitude, - longitude, - node_type, - fixed_type, - required_capacity, - max_power - ); - }); - - $("#button_optimize").click(function () { - const price_hub = hub_price.value; - const price_household = household_price.value; - const price_interhub_cable = interhub_cable_price.value; - const price_distribution_cable = distribution_cable_price.value; - optimize_grid( - price_hub, - price_household, - price_interhub_cable, - price_distribution_cable - ); - }); - - $("#button_identify_shs").click(function () { - const cable_price_per_meter = - cable_price_per_meter_for_shs_mst_identification.value; - const additional_connection_price = - additional_connection_price_for_shs_mst_identification.value; - const algo = "mst1"; - const shs_characteristics = logShsCharacteristics(); - - identify_shs( - cable_price_per_meter, - additional_connection_price, - algo, - shs_characteristics - ); - }); - - $("#button_clear_node_db").click(function () { - $.ajax({ - url: "clear_node_db/", - type: "POST", - statusCode: { - 200: function () { - refreshNodeFromDataBase(); - refreshLinksFromDatBase(); - }, - }, - }); - }); - - $("#button_select_boundaries").click(function () { - mapClickEvent = "draw_boundaries"; - var textSelectBoundaryButton = document.getElementById( - "button_select_boundaries" - ); - if (textSelectBoundaryButton.innerHTML === "Reset") { - mainMap.removeLayer(siteGeojson); - } - textSelectBoundaryButton.innerHTML = "Reset"; - - removeBoundaries(); - }); - - $("#button_validate_boundaries").click(function () { - mapClickEvent = "select"; - - // Close polygone by changing dashed line to solid - if (dashedBoundaryLine != null) { - mainMap.removeLayer(dashedBoundaryLine); - } - siteBoundaryLines.push( - L.polyline([siteBoundaries[0], siteBoundaries.slice(-1)[0]], { - color: "black", - }) - ); - siteBoundaryLines[siteBoundaryLines.length - 1].addTo(mainMap); - - // Find most extreme latitudes and longitudes - const latitudeList = siteBoundaries.map((x) => x[0]); - const longitudeList = siteBoundaries.map((x) => x[1]); - - minLatitude = Math.min(...latitudeList); - maxLatitude = Math.max(...latitudeList); - - minLongitude = Math.min(...longitudeList); - maxLongitude = Math.max(...longitudeList); - - // TODO implement if close to check that area is not too large - - getBuildingCoordinates((boundariesCoordinates = siteBoundaries)); - }); }); // --------------------VARIABLES DECLARATION----------------------// @@ -122,165 +8,41 @@ $(document).ready(function () { default_household_required_capacity = 10; default_household_max_power = 20; -var markers = []; -var lines = []; -siteGeojson = ""; - -var osmLayer = L.tileLayer( - "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", - { - tileSize: 512, - zoomOffset: -1, - minZoom: 1, - attribution: - '© OpenStreetMap contributors', - crossOrigin: true, - } -); - -var osmMap = { - osmBaseMap: osmLayer, -}; - -var esriWorldImageryLayer = L.tileLayer( - "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}", - { - tileSize: 512, - zoomOffset: -1, - minZoom: 1, - maxZoom: 18, - attribution: - "Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community", - } -); - -var esriSatelliteMap = { - esriBaseMap: esriWorldImageryLayer, -}; - -var mainMap = L.map("leafletMap", { - center: [11.3929, 9.1248], - zoom: 17, - layers: [osmLayer], -}); - -L.control.layers(osmMap, esriSatelliteMap).addTo(mainMap); +// --------------------FUNCTIONS DECLARATION----------------------// -var mapClickEvent = "add_node"; +// SET FUNCTIONS function setMapClickEventToAddNode() { mapClickEvent = "add_node"; } -var siteBoundaries = []; - -var siteBoundaryLines = []; -var dashedBoundaryLine = null; - -L.control.scale().addTo(mainMap); - -var householdMarker = new L.Icon({ - iconUrl: "fastapi_app/static/images/markers/marker-household.png", - iconSize: [10, 10], - iconAnchor: [5, 5], - popupAnchor: [0, 0], -}); - -var hubMarker = new L.Icon({ - iconUrl: "fastapi_app/static/images/markers/marker-hub.png", - iconSize: [14, 14], - iconAnchor: [7, 7], - popupAnchor: [0, 0], -}); - -var shsMarker = new L.Icon({ - iconUrl: "fastapi_app/static/images/markers/marker-shs.png", - iconSize: [10, 10], - iconAnchor: [5, 5], - popupAnchor: [0, 0], -}); - -mainMap.on("click", function (e) { - var poplocation = e.latlng; - - if (mapClickEvent == "add_node") { - if (document.getElementsByName("radio_button_new_node_type")[0].checked) { - addNodeToDatBase( - poplocation.lat, - poplocation.lng, - "undefinded", - false, - default_household_required_capacity, - default_household_max_power - ); - drawDefaultMarker(poplocation.lat, poplocation.lng); - } - - if (document.getElementsByName("radio_button_new_node_type")[1].checked) { - addNodeToDatBase( - poplocation.lat, - poplocation.lng, - "household", - true, - default_household_required_capacity, - default_household_max_power - ); - drawHouseholdMarker(poplocation.lat, poplocation.lng); - } - - if (document.getElementsByName("radio_button_new_node_type")[2].checked) { - addNodeToDatBase( - poplocation.lat, - poplocation.lng, - "meterhub", - true, - 2 * default_household_required_capacity, - 2 * default_household_max_power - ); - drawMeterhubMarker(poplocation.lat, poplocation.lng); - } +function displayShsCharacteristicsInput() { + if (document.getElementById("shs_inputs").style.display === "block") { + document.getElementById("shs_inputs").style.display = "none"; + } else { + document.getElementById("shs_inputs").style.display = "block"; } +} - if (mapClickEvent == "draw_boundaries") { - siteBoundaries.push([poplocation.lat, poplocation.lng]); - - // add new solid line to siteBoundaryLines and draw it on map - siteBoundaryLines.push(L.polyline(siteBoundaries, { color: "black" })); +function logShsCharacteristics() { + shsCharacteristics = []; + for (var i = 0; i < 4; ++i) { + shsCapacity = document.getElementById(`shs_capacity_${i}`).value; + maxPower = document.getElementById(`shs_max_power_${i}`).value; + price = document.getElementById(`shs_price_${i}`).value; - siteBoundaryLines[siteBoundaryLines.length - 1].addTo(mainMap); - // Remove dashed line - if (dashedBoundaryLine) { - mainMap.removeLayer(dashedBoundaryLine); + if (shsCapacity > 0 && maxPower > 0 && price > 0) { + shsCharacteristics.push({ + price: price, + capacity: shsCapacity, + max_power: maxPower, + }); } - - // Create new dashed line closing the polygon - dashedBoundaryLine = L.polyline( - [siteBoundaries[0], siteBoundaries.slice(-1)[0]], - { color: "black", dashArray: "10, 10", dashOffset: "20" } - ); - - // Add new dashed line to map - dashedBoundaryLine.addTo(mainMap); } -}); - -// --------------------FUNCTIONS DECLARATION----------------------// - -function drawDefaultMarker(latitude, longitude) { - markers.push(L.marker([latitude, longitude]).addTo(mainMap)); -} - -function drawMeterhubMarker(latitude, longitude) { - markers.push( - L.marker([latitude, longitude], { icon: hubMarker }).addTo(mainMap) - ); + return shsCharacteristics; } -function drawHouseholdMarker(latitude, longitude) { - markers.push( - L.marker([latitude, longitude], { icon: householdMarker }).addTo(mainMap) - ); -} +// POST REQUESTS function getBuildingCoordinates(boundariesCoordinates) { var xhr = new XMLHttpRequest(); @@ -334,25 +96,11 @@ function addNodeToDatBase( }); } -function removeBoundaries() { - // Remove all boundary lines and markers - for (line of siteBoundaryLines) { - mainMap.removeLayer(line); - } - if (dashedBoundaryLine != null) { - mainMap.removeLayer(dashedBoundaryLine); - } - siteBoundaries.length = 0; - siteBoundaryLines.length = 0; - dashedBoundaryLine = null; -} - -function optimize_grid( - price_meterhub, - price_household, - price_interhub_cable, - price_distribution_cable -) { +function optimize_grid() { + const price_meterhub = hub_price.value; + const price_household = household_price.value; + const price_interhub_cable = interhub_cable_price.value; + const price_distribution_cable = distribution_cable_price.value; $.ajax({ url: "optimize_grid/", type: "POST", @@ -373,12 +121,14 @@ function optimize_grid( }); } -function identify_shs( - cable_price_per_meter, - additional_connection_price, - algo, - shs_characteristics -) { +function identify_shs() { + const cable_price_per_meter = + cable_price_per_meter_for_shs_mst_identification.value; + const additional_connection_price = + additional_connection_price_for_shs_mst_identification.value; + const algo = "mst1"; + const shs_characteristics = logShsCharacteristics(); + $.ajax({ url: "shs_identification/", type: "POST", @@ -458,36 +208,6 @@ function refreshNodeFromDataBase() { }; } -function drawLinkOnMap( - latitude_from, - longitude_from, - latitude_to, - longitude_to, - color, - map, - weight = 3, - opacity = 0.5 -) { - var pointA = new L.LatLng(latitude_from, longitude_from); - var pointB = new L.LatLng(latitude_to, longitude_to); - var pointList = [pointA, pointB]; - - var link_polyline = new L.polyline(pointList, { - color: color, - weight: weight, - opacity: 0.5, - smoothFactor: 1, - }); - lines.push(link_polyline.addTo(map)); -} - -function ereaseLinksFromMap(map) { - for (line of lines) { - map.removeLayer(line); - } - lines.length = 0; -} - function refreshLinksFromDatBase() { var tbody_links = document.getElementById("tbody_links"); var xhr = new XMLHttpRequest(); @@ -541,28 +261,57 @@ function clearLinksDataBase() { }); } -function logShsCharacteristics() { - shsCharacteristics = []; - for (var i = 0; i < 4; ++i) { - shsCapacity = document.getElementById(`shs_capacity_${i}`).value; - maxPower = document.getElementById(`shs_max_power_${i}`).value; - price = document.getElementById(`shs_price_${i}`).value; +function clear_node_db() { + $.ajax({ + url: "clear_node_db/", + type: "POST", + statusCode: { + 200: function () { + refreshNodeFromDataBase(); + refreshLinksFromDatBase(); + }, + }, + }); +} - if (shsCapacity > 0 && maxPower > 0 && price > 0) { - shsCharacteristics.push({ - price: price, - capacity: shsCapacity, - max_power: maxPower, - }); - } +function validateBoundaries() { + mapClickEvent = "select"; + + // Close polygone by changing dashed line to solid + if (dashedBoundaryLine != null) { + mainMap.removeLayer(dashedBoundaryLine); } - return shsCharacteristics; + siteBoundaryLines.push( + L.polyline([siteBoundaries[0], siteBoundaries.slice(-1)[0]], { + color: "black", + }) + ); + siteBoundaryLines[siteBoundaryLines.length - 1].addTo(mainMap); + + // Find most extreme latitudes and longitudes + const latitudeList = siteBoundaries.map((x) => x[0]); + const longitudeList = siteBoundaries.map((x) => x[1]); + + minLatitude = Math.min(...latitudeList); + maxLatitude = Math.max(...latitudeList); + + minLongitude = Math.min(...longitudeList); + maxLongitude = Math.max(...longitudeList); + + // TODO implement if close to check that area is not too large + + getBuildingCoordinates((boundariesCoordinates = siteBoundaries)); } -function displayShsCharacteristicsInput() { - if (document.getElementById("shs_inputs").style.display === "block") { - document.getElementById("shs_inputs").style.display = "none"; - } else { - document.getElementById("shs_inputs").style.display = "block"; +function selectBoundaries() { + mapClickEvent = "draw_boundaries"; + var textSelectBoundaryButton = document.getElementById( + "button_select_boundaries" + ); + if (textSelectBoundaryButton.innerHTML === "Reset") { + mainMap.removeLayer(siteGeojson); } + textSelectBoundaryButton.innerHTML = "Reset"; + + removeBoundaries(); } diff --git a/fastapi_app/static/js/map.js b/fastapi_app/static/js/map.js new file mode 100644 index 0000000..9c226b6 --- /dev/null +++ b/fastapi_app/static/js/map.js @@ -0,0 +1,202 @@ +// --------------------VARIABLES DECLARATION----------------------// + +var markers = []; +var lines = []; +siteGeojson = ""; + +var osmLayer = L.tileLayer( + "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", + { + tileSize: 512, + zoomOffset: -1, + minZoom: 1, + attribution: + '© OpenStreetMap contributors', + crossOrigin: true, + } +); + +var osmMap = { + osmBaseMap: osmLayer, +}; + +var esriWorldImageryLayer = L.tileLayer( + "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}", + { + tileSize: 512, + zoomOffset: -1, + minZoom: 1, + maxZoom: 18, + attribution: + "Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community", + } +); + +var esriSatelliteMap = { + esriBaseMap: esriWorldImageryLayer, +}; + +var mainMap = L.map("leafletMap", { + center: [11.3929, 9.1248], + zoom: 17, + layers: [osmLayer], +}); + +L.control.layers(osmMap, esriSatelliteMap).addTo(mainMap); + +var mapClickEvent = "add_node"; + +var siteBoundaries = []; + +var siteBoundaryLines = []; +var dashedBoundaryLine = null; + +L.control.scale().addTo(mainMap); + +var householdMarker = new L.Icon({ + iconUrl: "fastapi_app/static/images/markers/marker-household.png", + iconSize: [10, 10], + iconAnchor: [5, 5], + popupAnchor: [0, 0], +}); + +var hubMarker = new L.Icon({ + iconUrl: "fastapi_app/static/images/markers/marker-hub.png", + iconSize: [14, 14], + iconAnchor: [7, 7], + popupAnchor: [0, 0], +}); + +var shsMarker = new L.Icon({ + iconUrl: "fastapi_app/static/images/markers/marker-shs.png", + iconSize: [10, 10], + iconAnchor: [5, 5], + popupAnchor: [0, 0], +}); + +mainMap.on("click", function (e) { + var poplocation = e.latlng; + + if (mapClickEvent == "add_node") { + if (document.getElementsByName("radio_button_new_node_type")[0].checked) { + addNodeToDatBase( + poplocation.lat, + poplocation.lng, + "undefinded", + false, + default_household_required_capacity, + default_household_max_power + ); + drawDefaultMarker(poplocation.lat, poplocation.lng); + } + + if (document.getElementsByName("radio_button_new_node_type")[1].checked) { + addNodeToDatBase( + poplocation.lat, + poplocation.lng, + "household", + true, + default_household_required_capacity, + default_household_max_power + ); + drawHouseholdMarker(poplocation.lat, poplocation.lng); + } + + if (document.getElementsByName("radio_button_new_node_type")[2].checked) { + addNodeToDatBase( + poplocation.lat, + poplocation.lng, + "meterhub", + true, + 2 * default_household_required_capacity, + 2 * default_household_max_power + ); + drawMeterhubMarker(poplocation.lat, poplocation.lng); + } + } + + if (mapClickEvent == "draw_boundaries") { + siteBoundaries.push([poplocation.lat, poplocation.lng]); + + // add new solid line to siteBoundaryLines and draw it on map + siteBoundaryLines.push(L.polyline(siteBoundaries, { color: "black" })); + + siteBoundaryLines[siteBoundaryLines.length - 1].addTo(mainMap); + // Remove dashed line + if (dashedBoundaryLine) { + mainMap.removeLayer(dashedBoundaryLine); + } + + // Create new dashed line closing the polygon + dashedBoundaryLine = L.polyline( + [siteBoundaries[0], siteBoundaries.slice(-1)[0]], + { color: "black", dashArray: "10, 10", dashOffset: "20" } + ); + + // Add new dashed line to map + dashedBoundaryLine.addTo(mainMap); + } +}); + +// --------------------FUNCTIONS DECLARATION----------------------// + +// INTERACTION WITH LEAFLET MAP + +function drawDefaultMarker(latitude, longitude) { + markers.push(L.marker([latitude, longitude]).addTo(mainMap)); +} + +function drawMeterhubMarker(latitude, longitude) { + markers.push( + L.marker([latitude, longitude], { icon: hubMarker }).addTo(mainMap) + ); +} + +function drawHouseholdMarker(latitude, longitude) { + markers.push( + L.marker([latitude, longitude], { icon: householdMarker }).addTo(mainMap) + ); +} + +function drawLinkOnMap( + latitude_from, + longitude_from, + latitude_to, + longitude_to, + color, + map, + weight = 3, + opacity = 0.5 +) { + var pointA = new L.LatLng(latitude_from, longitude_from); + var pointB = new L.LatLng(latitude_to, longitude_to); + var pointList = [pointA, pointB]; + + var link_polyline = new L.polyline(pointList, { + color: color, + weight: weight, + opacity: 0.5, + smoothFactor: 1, + }); + lines.push(link_polyline.addTo(map)); +} + +function ereaseLinksFromMap(map) { + for (line of lines) { + map.removeLayer(line); + } + lines.length = 0; +} + +function removeBoundaries() { + // Remove all boundary lines and markers + for (line of siteBoundaryLines) { + mainMap.removeLayer(line); + } + if (dashedBoundaryLine != null) { + mainMap.removeLayer(dashedBoundaryLine); + } + siteBoundaries.length = 0; + siteBoundaryLines.length = 0; + dashedBoundaryLine = null; +} diff --git a/fastapi_app/templates/home.html b/fastapi_app/templates/home.html index 025cea0..b764344 100644 --- a/fastapi_app/templates/home.html +++ b/fastapi_app/templates/home.html @@ -71,10 +71,10 @@

Geospatial Optimization Tool

Site Boundary
- +
- +
@@ -237,14 +237,14 @@

Geospatial Optimization Tool

- +
@@ -302,7 +302,7 @@

Geospatial Optimization Tool

- @@ -342,4 +342,5 @@

Geospatial Optimization Tool

+ {% endblock %} From b5eb7458496c2ebba3e14dec3199be9f1c971024 Mon Sep 17 00:00:00 2001 From: SaSa821 Date: Tue, 11 May 2021 11:11:43 +0200 Subject: [PATCH 04/12] Number of the relaxation steps added. --- fastapi_app/main.py | 4 +- fastapi_app/models.py | 1 + fastapi_app/run | 4 - fastapi_app/static/js/interactions.js | 2 + fastapi_app/templates/home.html | 415 +++++++++----------------- 5 files changed, 149 insertions(+), 277 deletions(-) delete mode 100755 fastapi_app/run diff --git a/fastapi_app/main.py b/fastapi_app/main.py index 7825944..f2b0cdf 100644 --- a/fastapi_app/main.py +++ b/fastapi_app/main.py @@ -188,7 +188,9 @@ async def optimize_grid(optimize_grid_request: models.OptimizeGridRequest, type_fixed=bool(node[4])) number_of_hubs = opt.get_expected_hub_number_from_k_means(grid=grid) - opt.nr_optimization(grid=grid, number_of_hubs=number_of_hubs, number_of_relaxation_step=10, + number_of_relaxation_steps_nr = optimize_grid_request.number_of_relaxation_steps_nr + print(f"number of relaxation steps: {number_of_relaxation_steps_nr}") + opt.nr_optimization(grid=grid, number_of_hubs=number_of_hubs, number_of_relaxation_steps_nr=number_of_relaxation_steps_nr, save_output=False, plot_price_evolution=False) sqliteConnection = sqlite3.connect(grid_db) diff --git a/fastapi_app/models.py b/fastapi_app/models.py index 9fc057d..fcc56d0 100644 --- a/fastapi_app/models.py +++ b/fastapi_app/models.py @@ -46,6 +46,7 @@ class OptimizeGridRequest(BaseModel): price_household: float price_interhub_cable: float price_distribution_cable: float + number_of_relaxation_steps_nr: int class ShsIdentificationRequest(BaseModel): diff --git a/fastapi_app/run b/fastapi_app/run deleted file mode 100755 index b46cff9..0000000 --- a/fastapi_app/run +++ /dev/null @@ -1,4 +0,0 @@ - -source venv/bin/activate -cd fastapi_app -uvicorn main:app --reload diff --git a/fastapi_app/static/js/interactions.js b/fastapi_app/static/js/interactions.js index 2926ab1..674aad2 100644 --- a/fastapi_app/static/js/interactions.js +++ b/fastapi_app/static/js/interactions.js @@ -101,6 +101,7 @@ function optimize_grid() { const price_household = household_price.value; const price_interhub_cable = interhub_cable_price.value; const price_distribution_cable = distribution_cable_price.value; + const number_of_relaxation_steps_nr = number_of_relaxation_steps_nr.value; $.ajax({ url: "optimize_grid/", type: "POST", @@ -110,6 +111,7 @@ function optimize_grid() { price_household: price_household, price_interhub_cable: price_interhub_cable, price_distribution_cable: price_distribution_cable, + number_of_relaxation_steps_nr: number_of_relaxation_steps_nr, }), dataType: "json", statusCode: { diff --git a/fastapi_app/templates/home.html b/fastapi_app/templates/home.html index b764344..e9e993b 100644 --- a/fastapi_app/templates/home.html +++ b/fastapi_app/templates/home.html @@ -4,11 +4,7 @@

Geospatial Optimization Tool

- +
@@ -16,65 +12,49 @@

Geospatial Optimization Tool

@@ -83,264 +63,155 @@

Geospatial Optimization Tool

Solar Home Sytem Identification

Cable price

- +

Connection

- +

- +
-
- -

kWh

- -

W

- -

-
- -
- -

kWh

- -

W

- -

-
- -
- -

kWh

- -

W

- -

-
- -
- -

kWh

- -

W

- -

-
- +
+ +

kWh

+ +

W

+ +

+
+ +
+ +

kWh

+ +

W

+ +

+
+ +
+ +

kWh

+ +

W

+ +

+
+ +
+ +

kWh

+ +

W

+ +

+
+
- +
- -
-
-
- -
- Nodes table -
-
-
-
- -
+
+
+
+
-
- -
-
- -
-
- +
+ Nodes table +
+
+
+
+ +
+ +
+ +
+
+ +
+
+ +
+
-
-
- -
- - - - - - - - - - - - -
IndexLatitudeLongitudeNode TypeType FixedRequired CapacityMax Power
+ + + + + + + + + + + + + + +
IndexLatitudeLongitudeNode TypeType FixedRequired CapacityMax Power
+ - -