# Path: rivus/utils/pandashp.py
# def read_shp(filename):
# def write_shp(filename, dataframe, write_index=True):
# def match_vertices_and_edges(vertices, edges, vertex_cols=('Vertex1', 'Vertex2')):
# def find_closest_edge(polygons, edges, to_attr='index', column='nearest'):
# def bounds(df):
# def total_bounds(df):
# 
# Path: rivus/main/rivus.py
# COLORS = { # (R,G,B) tuples with range (0-255)
#     # defaults
#     'base': (192, 192, 192),
#     'building': (192, 192, 192),
#     'decoration': (128, 128, 128),
#     # commodities
#     'Heat': (230, 112, 36),
#     'Cool': (0, 0, 255),
#     'Elec': (255, 170, 0),
#     'Demand': (0, 255, 0),
#     'Gas': (128, 64, 0),
#     # buildings
#     'industrial': (240, 198, 116),
#     'residential': (181, 189, 104),
#     'commercial': (129, 162, 190),
#     'basin': (110, 75, 56),
#     'chapel': (177, 121, 91),
#     'church': (177, 121, 91),
#     'farm': (202, 178, 214),
#     'farm_auxiliary': (106, 61, 154),
#     'garage': (253, 191, 111),
#     'greenhouse': (255, 127, 0),
#     'hospital': (129, 221, 190),
#     'hotel': (227, 26, 28),
#     'house': (181, 189, 104),
#     'office': (129, 162, 190),
#     'public': (129, 162, 190),
#     'restaurant': (227, 26, 28),
#     'retail': (129, 162, 190),
#     'school': (29, 103, 214),
#     'warehouse': (98, 134, 6),
# }
# def read_excel(filepath):
# def create_model(data, vertex, edge, peak_multiplier=None,
#                  hub_only_in_edge=True):
#     def multiply_by_area_demand(series, column):
# def peak_satisfaction_rule(m, i, j, co, t):
# def edge_equation_rule(m, i, j, co, t):
# def arc_flow_by_capacity_rule(m, i, j, co, t):
# def arc_flow_unidirectionality_rule(m, i, j, co, t):
# def arc_unidirectionality_rule(m, i, j, co, t):
# def edge_capacity_rule(m, i, j, co):
# def hub_supply_rule(m, i, j, co, t):
# def hub_output_by_capacity_rule(m, i, j, h, t):
# def hub_capacity_rule(m, i, j, h):
# def vertex_equation_rule(m, v, co, t):
# def source_vertices_rule(m, v, co, t):
# def commodity_maximum_rule(m, co):
# def process_throughput_by_capacity_rule(m, v, p, t):
# def process_capacity_min_rule(m, v, p):
# def process_capacity_max_rule(m, v, p):
# def process_input_rule(m, v, p, co, t):
# def process_output_rule(m, v, p, co, t):
# def def_costs_rule(m, cost_type):
# def obj_rule(m):
# def hub_balance(m, i, j, co, t):
# def flow_balance(m, v, co, t):
# def process_balance(m, v, co, t):
# def find_matching_edge(m, i, j):
# def line_length(line):
# def pairs(lst):
# def get_entity(instance, name):
# def get_entities(instance, names):
# def list_entities(instance, entity_type):
#     def filter_by_type(entity, entity_type):
# def get_onset_names(entity):
# def get_constants(prob):
# def get_timeseries(prob):
# def plot(prob, commodity, plot_demand=False, mapscale=False, tick_labels=True,
#          annotations=True, buildings=None, shapefiles=None, decoration=True,
#          boundary=False):
#                         def _calc_xytext_offset(line):
# def result_figures(prob, file_basename, buildings=None, shapefiles=None):
# def report(prob, filename):
# def save_log(result, filename):
# def save(prob, filepath):
# def load(filepath):


    import pyomo.environ
    import coopr.environ
import matplotlib.pyplot as plt
import os
import pandas as pd
    from pyomo.opt.base import SolverFactory
    from coopr.opt.base import SolverFactory
from rivus.utils import pandashp as pdshp
from rivus.main import rivus

base_directory = os.path.join('data', 'mnl')
building_shapefile = os.path.join(base_directory, 'building')
edge_shapefile = os.path.join(base_directory, 'edge')
vertex_shapefile = os.path.join(base_directory, 'vertex')
data_spreadsheet = os.path.join(base_directory, 'data.xlsx')


# load buildings and sum by type and nearest edge ID
# 1. read shapefile to DataFrame (with special geometry column)
# 2. group DataFrame by columns 'nearest' (ID of nearest edge) and 'type'
#    (residential, commercial, industrial, other)
# 3. sum by group and unstack, i.e. convert secondary index 'type' to columns
buildings = pdshp.read_shp(building_shapefile)
buildings_grouped = buildings.groupby(['nearest', 'type'])
total_area = buildings_grouped.sum()['total_area'].unstack()

# load edges (streets) and join with summed areas 
# 1. read shapefile to DataFrame (with geometry column)
# 2. join DataFrame total_area on index (=ID)
# 3. fill missing values with 0
edge = pdshp.read_shp(edge_shapefile)
edge = edge.set_index('Edge')
edge = edge.join(total_area)
edge = edge.fillna(0)

# load nodes
vertex = pdshp.read_shp(vertex_shapefile)

# load spreadsheet data

# Path: rivus/utils/pandashp.py
# def read_shp(filename):
# def write_shp(filename, dataframe, write_index=True):
# def match_vertices_and_edges(vertices, edges, vertex_cols=('Vertex1', 'Vertex2')):
# def find_closest_edge(polygons, edges, to_attr='index', column='nearest'):
# def bounds(df):
# def total_bounds(df):
# 
# Path: rivus/main/rivus.py
# COLORS = { # (R,G,B) tuples with range (0-255)
#     # defaults
#     'base': (192, 192, 192),
#     'building': (192, 192, 192),
#     'decoration': (128, 128, 128),
#     # commodities
#     'Heat': (230, 112, 36),
#     'Cool': (0, 0, 255),
#     'Elec': (255, 170, 0),
#     'Demand': (0, 255, 0),
#     'Gas': (128, 64, 0),
#     # buildings
#     'industrial': (240, 198, 116),
#     'residential': (181, 189, 104),
#     'commercial': (129, 162, 190),
#     'basin': (110, 75, 56),
#     'chapel': (177, 121, 91),
#     'church': (177, 121, 91),
#     'farm': (202, 178, 214),
#     'farm_auxiliary': (106, 61, 154),
#     'garage': (253, 191, 111),
#     'greenhouse': (255, 127, 0),
#     'hospital': (129, 221, 190),
#     'hotel': (227, 26, 28),
#     'house': (181, 189, 104),
#     'office': (129, 162, 190),
#     'public': (129, 162, 190),
#     'restaurant': (227, 26, 28),
#     'retail': (129, 162, 190),
#     'school': (29, 103, 214),
#     'warehouse': (98, 134, 6),
# }
# def read_excel(filepath):
# def create_model(data, vertex, edge, peak_multiplier=None,
#                  hub_only_in_edge=True):
#     def multiply_by_area_demand(series, column):
# def peak_satisfaction_rule(m, i, j, co, t):
# def edge_equation_rule(m, i, j, co, t):
# def arc_flow_by_capacity_rule(m, i, j, co, t):
# def arc_flow_unidirectionality_rule(m, i, j, co, t):
# def arc_unidirectionality_rule(m, i, j, co, t):
# def edge_capacity_rule(m, i, j, co):
# def hub_supply_rule(m, i, j, co, t):
# def hub_output_by_capacity_rule(m, i, j, h, t):
# def hub_capacity_rule(m, i, j, h):
# def vertex_equation_rule(m, v, co, t):
# def source_vertices_rule(m, v, co, t):
# def commodity_maximum_rule(m, co):
# def process_throughput_by_capacity_rule(m, v, p, t):
# def process_capacity_min_rule(m, v, p):
# def process_capacity_max_rule(m, v, p):
# def process_input_rule(m, v, p, co, t):
# def process_output_rule(m, v, p, co, t):
# def def_costs_rule(m, cost_type):
# def obj_rule(m):
# def hub_balance(m, i, j, co, t):
# def flow_balance(m, v, co, t):
# def process_balance(m, v, co, t):
# def find_matching_edge(m, i, j):
# def line_length(line):
# def pairs(lst):
# def get_entity(instance, name):
# def get_entities(instance, names):
# def list_entities(instance, entity_type):
#     def filter_by_type(entity, entity_type):
# def get_onset_names(entity):
# def get_constants(prob):
# def get_timeseries(prob):
# def plot(prob, commodity, plot_demand=False, mapscale=False, tick_labels=True,
#          annotations=True, buildings=None, shapefiles=None, decoration=True,
#          boundary=False):
#                         def _calc_xytext_offset(line):
# def result_figures(prob, file_basename, buildings=None, shapefiles=None):
# def report(prob, filename):
# def save_log(result, filename):
# def save(prob, filepath):
# def load(filepath):


# Path: runmin.py
    import pyomo.environ
    import coopr.environ
import matplotlib.pyplot as plt
import os
import pandas as pd
    from pyomo.opt.base import SolverFactory
    from coopr.opt.base import SolverFactory
from rivus.utils import pandashp as pdshp
from rivus.main import rivus


base_directory = os.path.join('data', 'mnl')
building_shapefile = os.path.join(base_directory, 'building')
edge_shapefile = os.path.join(base_directory, 'edge')
vertex_shapefile = os.path.join(base_directory, 'vertex')
data_spreadsheet = os.path.join(base_directory, 'data.xlsx')


# load buildings and sum by type and nearest edge ID
# 1. read shapefile to DataFrame (with special geometry column)
# 2. group DataFrame by columns 'nearest' (ID of nearest edge) and 'type'
#    (residential, commercial, industrial, other)
# 3. sum by group and unstack, i.e. convert secondary index 'type' to columns
buildings = pdshp.read_shp(building_shapefile)
buildings_grouped = buildings.groupby(['nearest', 'type'])
total_area = buildings_grouped.sum()['total_area'].unstack()

# load edges (streets) and join with summed areas 
# 1. read shapefile to DataFrame (with geometry column)
# 2. join DataFrame total_area on index (=ID)
# 3. fill missing values with 0
edge = pdshp.read_shp(edge_shapefile)
edge = edge.set_index('Edge')
edge = edge.join(total_area)
edge = edge.fillna(0)

# load nodes
vertex = pdshp.read_shp(vertex_shapefile)

# load spreadsheet data

data = rivus.read_excel(data_spreadsheet)