Пример #1
0
def Plotpath_and_calculate_time(G, Path, filename):
    m_bcn = stm.StaticMap(1000, 1000)
    time = 0
    color = 'green'
    for i in range(len(Path) - 1):
        node1, node2 = Path[i], Path[i + 1]
        distance = haversine((node1.lat, node1.lon), (node2.lat, node2.lon))
        if G[node1][node2]['weight'] == distance:
            time += distance / 10
            line = stm.Line(((node1.lon, node1.lat), (node2.lon, node2.lat)),
                            'blue', 2)
        else:
            time += distance / 4
            line = stm.Line(((node1.lon, node1.lat), (node2.lon, node2.lat)),
                            'orange', 2)

        marker1 = stm.CircleMarker((node1.lon, node1.lat), color, 5)
        color = 'red'
        marker2 = stm.CircleMarker((node2.lon, node2.lat), color, 5)
        m_bcn.add_marker(marker1)
        m_bcn.add_marker(marker2)
        m_bcn.add_line(line)

    image = m_bcn.render()
    image.save(filename)
    return time_complete(time)
Пример #2
0
def Plotgraph_graph_to_nearest(n_station, coord, filename):
    m_bcn = stm.StaticMap(1000, 1000)
    line = stm.Line(((coord[1], coord[0]), (n_station.lon, n_station.lat)),
                    'orange', 2)
    marker1 = stm.CircleMarker((coord[1], coord[0]), 'green', 5)
    marker2 = stm.CircleMarker((n_station.lon, n_station.lat), 'red', 5)
    m_bcn.add_marker(marker1)
    m_bcn.add_marker(marker2)
    m_bcn.add_line(line)
    image = m_bcn.render()
    image.save(filename)
Пример #3
0
def make_image(raid):
    team_colors = ["#e5e5e5", "#0576ee", "#f2160a", "#fad107"]
    lon, lat = raid["lon"], raid["lat"]
    m = staticmap.StaticMap(800, 300)
    pokemon = staticmap.IconMarker(
        (lon, lat), "./img/pkmn/{}.png".format(raid["pokemon_id"]), 60, 60)
    m.add_marker(pokemon)
    marker = staticmap.CircleMarker((lon, lat), "black", 12)
    m.add_marker(marker)
    marker = staticmap.CircleMarker((lon, lat), team_colors[raid["team"]], 10)
    m.add_marker(marker)
    img = m.render(zoom=16)
    img.save("img/tmp_raid.png")
Пример #4
0
def generateMap(lon_lat,
                dest_file,
                zoom=12,
                width=640,
                height=480,
                template=DEFAULT_TEMPLATE):

    m = staticmap.StaticMap(width,
                            height,
                            10,
                            url_template=URL_TEMPLATES[template])
    for coord in lon_lat:
        marker_outline = staticmap.CircleMarker(coord, "white", 18)
        marker = staticmap.CircleMarker(coord, "#0036FF", 10)
        m.add_marker(marker_outline)
        m.add_marker(marker)
    image = m.render(zoom=zoom)
    image.save(dest_file)
Пример #5
0
def Plotgraph(G, filename):
    m_bcn = stm.StaticMap(1000, 1000)

    for node in G.nodes:
        marker = stm.CircleMarker((node.lon, node.lat), 'red', 3)
        m_bcn.add_marker(marker)

    for edge in G.edges:
        line = stm.Line(
            ((edge[0].lon, edge[0].lat), (edge[1].lon, edge[1].lat)), 'blue',
            1)
        m_bcn.add_line(line)

    image = m_bcn.render()
    image.save(filename)
Пример #6
0
def create_app(results_json, path_sim_output=None):
    r"""Initializes the app and calls all the other functions, resulting in the web app as well as pdf.

    This function specifies the layout of the web app, loads the external styling sheets, prepares the necessary data
    from the json results file, calls all the helper functions on the data, resulting in the auto-report.

    Parameters
    ----------
    results_json: json results file
        This file is the result of the simulation and contains all the data necessary to generate the auto-report.

    path_sim_output: str
        Path to the mvs simulation's output files' folder
        Default: output path saved in the result_json

    Returns
    -------
    app: instance of the Dash class within the dash library
        This app holds together all the html elements wrapped in Python, necessary for the rendering of the auto-report.
    """

    if path_sim_output is None:
        path_sim_output = results_json[SIMULATION_SETTINGS][PATH_OUTPUT_FOLDER]

    # create a "report" folder containing an "asset" folder
    asset_folder = os.path.abspath(copy_report_assets(path_sim_output))

    # external CSS stylesheets
    external_stylesheets = [
        {
            "href":
            "https://cdnjs.cloudflare.com/ajax/libs/foundation/6.6.3/css/foundation.min.css",
            "rel": "stylesheet",
            "integrity": "sha256-ogmFxjqiTMnZhxCqVmcqTvjfe1Y/ec4WaRj/aQPvn+I=",
            "crossorigin": "anonymous",
            "media": "screen",
        },
    ]

    app = dash.Dash(
        assets_folder=asset_folder,
        external_stylesheets=external_stylesheets,
    )

    # Reading the relevant user-inputs from the JSON_WITH_RESULTS.json file into Pandas dataframes

    # .iloc[0] is used as PROJECT_DATA includes LES_ENERGY_VECTOR_S, which can have multiple entries.
    # Pased to a DF, we have multiple rows - for eah sector one row.
    # This messes up reading the data from the DF later, so we only take one row which then contains all relevant data.
    dfprojectData = pd.DataFrame.from_dict(results_json[PROJECT_DATA]).iloc[0]
    dfeconomicData = pd.DataFrame.from_dict(
        results_json[ECONOMIC_DATA]).loc[VALUE]

    # Obtaining the latlong of the project location
    latlong = (
        float(dfprojectData.latitude),
        float(dfprojectData.longitude),
    )
    # Determining the geographical location of the project
    geo_dict_path = os.path.join(asset_folder, "geolocation")
    if os.path.exists(geo_dict_path):
        with open(geo_dict_path, "rb") as handle:
            geo_dict = pickle.load(handle)
    else:
        geoList = rg.search(latlong)
        geo_dict = geoList[0]
        with open(geo_dict_path, "wb") as handle:
            pickle.dump(geo_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

    location = geo_dict["name"]

    leaflet_map_path = os.path.join(asset_folder, "proj_map.html")
    static_map_path = os.path.join(asset_folder, "proj_map_static.png")

    # Add a dynamic leaflet map to the dash app
    if not os.path.exists(leaflet_map_path):
        mapy = folium.Map(location=latlong, zoom_start=14)
        tooltip = "Click here for more info"
        folium.Marker(
            latlong,
            popup="Location of the project",
            tooltip=tooltip,
            icon=folium.Icon(color="red", icon="glyphicon glyphicon-flash"),
        ).add_to(mapy)
        mapy.save(leaflet_map_path)

    # Adds a static map for the PDF report
    MAP_STATIC = None
    if not os.path.exists(static_map_path):
        longitude = latlong[1]
        latitude = latlong[0]
        coords = longitude, latitude

        map_static = staticmap.StaticMap(600, 600, 80)
        marker = staticmap.CircleMarker(coords, "#13074f", 15)
        map_static.add_marker(marker)
        try:
            map_image = map_static.render(zoom=14)
            map_image.save(static_map_path)
            MAP_STATIC = encode_image_file(static_map_path)
        except RuntimeError as e:
            if "could not download" in repr(e):
                logging.warning(
                    "You might not be connected to internet, skipping the download of "
                    "location map for the report")

    dict_projectdata = {
        "Country": dfprojectData.country,
        "Project ID": dfprojectData.project_id,
        "Scenario ID": dfprojectData.scenario_id,
        "Currency": dfeconomicData.currency,
        "Project Location": location,
        "Discount Factor": dfeconomicData.discount_factor,
        "Tax": dfeconomicData.tax,
    }

    df_projectData = pd.DataFrame(list(dict_projectdata.items()),
                                  columns=["Label", "Value"])

    dict_simsettings = {
        "Evaluated period":
        results_json[SIMULATION_SETTINGS][EVALUATED_PERIOD][VALUE],
        "Start date":
        results_json[SIMULATION_SETTINGS][START_DATE],
        "Timestep length":
        results_json[SIMULATION_SETTINGS][TIMESTEP][VALUE],
    }

    df_simsettings = pd.DataFrame(list(dict_simsettings.items()),
                                  columns=["Setting", "Value"])

    projectName = (results_json[PROJECT_DATA][PROJECT_NAME] + " (ID: " +
                   str(results_json[PROJECT_DATA][PROJECT_ID]) + ")")

    scenarioName = (results_json[PROJECT_DATA][SCENARIO_NAME] + " (ID: " +
                    str(results_json[PROJECT_DATA][SCENARIO_ID]) + ")")

    simDate = time.strftime("%Y-%m-%d")

    ELAND_LOGO = encode_image_file(
        os.path.join(asset_folder, "logo-eland-original.jpg"))

    ENERGY_SYSTEM_GRAPH = encode_image_file(
        results_json[PATHS_TO_PLOTS][PLOTS_ES])

    # Determining the sectors which were simulated

    sectors = list(results_json[PROJECT_DATA][LES_ENERGY_VECTOR_S].keys())
    sec_list = """"""
    for sec in sectors:
        sec_list += "\n" + f"\u2022 {sec.upper()}"

    df_comp = convert_components_to_dataframe(results_json)
    df_scalar_matrix = convert_scalar_matrix_to_dataframe(results_json)
    df_cost_matrix = convert_cost_matrix_to_dataframe(results_json)
    df_kpi_scalars = convert_scalars_to_dataframe(results_json)
    df_kpi_sectors = convert_kpi_sector_to_dataframe(results_json)

    # Obtain the scenario description text provided by the user from the JSON results file
    scenario_description = results_json[PROJECT_DATA].get(
        SCENARIO_DESCRIPTION, "")

    # App layout and populating it with different elements
    app.layout = html.Div(
        id="main-div",
        className="grid-x align-center",
        children=[
            html.Div(
                className="cell small-10 small_offset-1 header_title_logo",
                children=[
                    html.Img(
                        id="mvslogo",
                        src="data:image/png;base64,{}".format(
                            ELAND_LOGO.decode()),
                        width="500px",
                    ),
                    html.H1("MULTI VECTOR SIMULATION - REPORT SHEET"),
                ],
            ),
            html.Section(
                className="cell small-10 small_offset-1 grid-x",
                children=[
                    insert_headings("Information"),
                    html.Div(
                        className="cell imp_info",
                        children=[
                            html.
                            P(f"MVS Release: {version_num} ({version_date})"),
                            html.P(f"Simulation date: {simDate}"),
                            html.Div(
                                className="cell imp_info2",
                                children=[
                                    html.Span(
                                        "Project name   : ",
                                        style={"font-weight": "bold"},
                                    ),
                                    f"{projectName}",
                                ],
                            ),
                            html.Div(
                                className="cell imp_info2",
                                children=[
                                    html.Span(
                                        "Scenario name  : ",
                                        style={"font-weight": "bold"},
                                    ),
                                    f"{scenarioName}",
                                ],
                            ),
                            html.Div(
                                className="cell imp_info2",
                                children=[]
                                if scenario_description == "" else [
                                    html.Span(
                                        "Scenario description  : ",
                                        style={"font-weight": "bold"},
                                    ),
                                    f"{scenario_description}",
                                ],
                            ),
                            html.Div(
                                className="blockoftext",
                                children=[
                                    "The energy system with the ",
                                    html.Span(f"{projectName}",
                                              style={"font-style": "italic"}),
                                    " for the scenario ",
                                    html.Span(
                                        f"{scenarioName}",
                                        style={"font-style": "italic"},
                                    ),
                                    " was simulated with the Multi-Vector simulation tool MVS 0.0x developed from the E-LAND toolbox "
                                    "developed in the scope of the Horizon 2020 European research project. The tool was developed by "
                                    "Reiner Lemoine Institute and utilizes the OEMOF framework.",
                                ],
                            ),
                        ],
                    ),
                ],
            ),
            html.Section(
                className="cell small-10 small_offset-1 grid-x",
                style={"pageBreakBefore": "always"},
                children=[
                    insert_headings("Input Data"),
                    insert_subsection(
                        title="Project Data",
                        content=[
                            insert_body_text(
                                "The most important simulation data will be presented below. "
                                "Detailed settings, costs, and technological parameters can "
                                "be found in the appendix."),
                            html.Div(
                                className="grid-x",
                                id="location-map-div",
                                children=[
                                    html.Div(
                                        className=
                                        "cell small-6 location-map-column",
                                        children=[
                                            html.H4(["Project Location"]),
                                            html.Iframe(
                                                srcDoc=open(
                                                    leaflet_map_path,
                                                    "r",
                                                ).read(),
                                                height="400",
                                            ),
                                            html.Div(
                                                className=
                                                "staticimagepdf print-only",
                                                children=[
                                                    insert_body_text(
                                                        "The blue dot in the below map indicates "
                                                        "the location of the project."
                                                    ),
                                                    html.Img(
                                                        id="staticmapimage",
                                                        src="" if
                                                        MAP_STATIC is None else
                                                        "data:image/png;base64,{}"
                                                        .format(MAP_STATIC.
                                                                decode()),
                                                        width="400px",
                                                        style={
                                                            "marginLeft":
                                                            "30px"
                                                        },
                                                        alt=
                                                        "Map of the location",
                                                    ),
                                                ],
                                            ),
                                        ],
                                    ),
                                    html.Div(
                                        className=
                                        "cell small-6 location-map-column",
                                        children=make_dash_data_table(
                                            df_projectData,
                                            "Project at a Glance"),
                                    ),
                                ],
                            ),
                            make_dash_data_table(df_simsettings,
                                                 "Simulation Settings"),
                            html.Div(
                                className="grid-x",
                                id="energy-system-graph-div",
                                children=[
                                    html.H4(["Energy system"]),
                                    html.Img(
                                        src="data:image/png;base64,{}".format(
                                            ENERGY_SYSTEM_GRAPH.decode()),
                                        alt="Energy System Graph",
                                        style={"maxWidth": "100%"},
                                    ),
                                ],
                            ),
                        ],
                    ),
                    html.Div(children=create_demands_section(
                        output_JSON_file=results_json, sectors=sectors)),
                    insert_subsection(
                        title="Resources",
                        content=ready_timeseries_plots(results_json,
                                                       data_type=RESOURCES),
                    ),
                    insert_subsection(
                        title="Energy System Components",
                        content=[
                            insert_body_text(
                                "The energy system is comprised of "
                                "the following components:"),
                            make_dash_data_table(df_comp),
                        ],
                    ),
                ],
            ),
            html.Section(
                className="cell small-10 small_offset-1 grid-x",
                style={"pageBreakBefore": "always"},
                children=[
                    html.H2(className="cell", children="Simulation Results"),
                    insert_subsection(
                        title="Dispatch & Energy Flows",
                        content=[
                            insert_body_text(
                                "The capacity optimization of components that were to be used resulted in:"
                            ),
                            make_dash_data_table(df_scalar_matrix),
                            insert_body_text(
                                "With this, the demands are met with the following dispatch schedules:"
                            ),
                            html.Div(children=ready_flows_plots(
                                dict_values=results_json, )),
                            html.Div(
                                className="add-cap-plot",
                                children=ready_capacities_plots(
                                    dict_values=results_json),
                            ),
                            insert_body_text(
                                "This results in the following KPI of the dispatch per energy sector:"
                            ),
                            make_dash_data_table(df_kpi_sectors),
                        ],
                    ),
                    insert_subsection(
                        title="Economic Evaluation",
                        content=[
                            insert_body_text(
                                "The following installation and operation costs "
                                "result from capacity and dispatch optimization:"
                            ),
                            make_dash_data_table(df_cost_matrix),
                            html.Div(
                                className="add-pie-plots",
                                children=ready_costs_pie_plots(
                                    dict_values=results_json,
                                    only_print=False,
                                ),
                            ),
                        ],
                    ),
                    insert_subsection(
                        title=
                        "Energy System: Key Performance Indicators (KPIs)",
                        content=[
                            insert_body_text(
                                f"In the following the key performance indicators of the of {projectName}, "
                                f"scenario {scenarioName} are displayed. For more information on their definition, "
                                f"please reference `mvs-eland.readthedocs.io`."
                            ),
                            make_dash_data_table(df_kpi_scalars),
                        ],
                    ),
                ],
            ),
            html.Section(
                className="cell small-10 small_offset-1 grid-x",
                children=[
                    html.Div(
                        className="cell",
                        children=[
                            insert_headings(heading_text="Logging Messages"),
                        ],
                    ),
                    html.Div(children=[
                        insert_subsection(
                            title="Warning Messages",
                            content=insert_log_messages(
                                log_dict=results_json[SIMULATION_RESULTS][LOGS]
                                [WARNINGS]),
                        ),
                        insert_subsection(
                            title="Error Messages",
                            content=insert_log_messages(
                                log_dict=results_json[SIMULATION_RESULTS][LOGS]
                                [ERRORS]),
                        ),
                    ]),
                ],
            ),
        ],
    )
    return app