def _get_zoom(coordinate_array, width): """ If mapbox is being used for tranmission plotting, get the zoom based on the bounding area of the input data and the width (in pixels) of the map """ # Keys are zoom levels, values are m/pixel at that zoom level zoom_dict = { 0: 156412, 1: 78206, 2: 39103, 3: 19551, 4: 9776, 5: 4888, 6: 2444, 7: 1222, 8: 610.984, 9: 305.492, 10: 152.746, 11: 76.373, 12: 38.187, 13: 19.093, 14: 9.547, 15: 4.773, 16: 2.387, 17: 1.193, 18: 0.596, 19: 0.298} bounds = [ coordinate_array.max(dim='locs').values, coordinate_array.min(dim='locs').values] max_distance = vincenty(*bounds) metres_per_pixel = max_distance / width for k, v in zoom_dict.items(): if v > metres_per_pixel: continue else: zoom = k - 4 break return zoom
def process_per_distance_constraints(tech_name, tech_settings, locations, locations_comments, loc_from, loc_to): # Process distance, if any per_distance constraints exist if any('per_distance' in i for i in tech_settings.keys_nested(subkeys_as='list')): # If no distance was given, we calculate it from coordinates if 'distance' not in tech_settings: # Simple check - earlier sense-checking already ensures # that all locations have either lat/lon or x/y coords loc1 = locations[loc_from].coordinates loc2 = locations[loc_to].coordinates if 'lat' in locations[loc_from].coordinates: distance = vincenty([loc1.lat, loc1.lon], [loc2.lat, loc2.lon]) else: distance = math.sqrt((loc1.x - loc2.x)**2 + (loc1.y - loc2.y)**2) tech_settings.distance = distance locations_comments.set_key( '{}.links.{}.techs.{}.distance'.format(loc_from, loc_to, tech_name), 'Distance automatically computed from coordinates') # Add per-distance values to their not-per-distance cousins # FIXME these are hardcoded for now if 'energy_eff_per_distance' in tech_settings.constraints: distance_energy_eff = ( tech_settings.constraints.energy_eff_per_distance** tech_settings.distance) tech_settings.constraints.energy_eff = ( tech_settings.constraints.get_key('energy_eff', 1.0) * distance_energy_eff) del tech_settings.constraints['energy_eff_per_distance'] locations_comments.set_key( '{}.links.{}.techs.{}.constraints.energy_eff'.format( loc_from, loc_to, tech_name), 'Includes value computed from energy_eff_per_distance') for k in tech_settings.costs.keys_nested(subkeys_as='list'): if 'energy_cap_per_distance' in k: energy_cap_costs_per_distance = ( tech_settings.costs.get_key(k) * tech_settings.distance) tech_settings.costs[k.split('.')[0]].energy_cap = ( tech_settings.costs[k.split('.')[0]].get_key( 'energy_cap', 0) + energy_cap_costs_per_distance) tech_settings.costs.del_key(k) locations_comments.set_key( '{}.links.{}.techs.{}.costs.{}'.format( loc_from, loc_to, tech_name, k), 'Includes value computed from energy_cap_per_distance') elif 'purchase_per_distance' in k: purchase_costs_per_distance = (tech_settings.costs.get_key(k) * tech_settings.distance) tech_settings.costs[k.split('.')[0]].purchase = ( tech_settings.costs[k.split('.')[0]].get_key( 'purchase', 0) + purchase_costs_per_distance) tech_settings.costs.del_key(k) locations_comments.set_key( '{}.links.{}.techs.{}.costs.{}'.format( loc_from, loc_to, tech_name, k), 'Includes value computed from purchase_per_distance') return tech_settings
def process_per_distance_constraints(tech_name, tech_settings, locations, locations_comments, loc_from, loc_to): # Process distance, if any per_distance constraints exist if any('per_distance' in i for i in tech_settings.keys_nested(subkeys_as='list')): # If no distance was given, we calculate it from coordinates if 'distance' not in tech_settings: # Simple check - earlier sense-checking already ensures # that all locations have either lat/lon or x/y coords loc1 = locations[loc_from].coordinates loc2 = locations[loc_to].coordinates if 'lat' in locations[loc_from].coordinates: distance = vincenty( [loc1.lat, loc1.lon], [loc2.lat, loc2.lon] ) else: distance = math.sqrt( (loc1.x - loc2.x)**2 + (loc1.y - loc2.y)**2 ) tech_settings.distance = distance locations_comments.set_key( '{}.links.{}.techs.{}.distance'.format(loc_from, loc_to, tech_name), 'Distance automatically computed from coordinates' ) # Add per-distance values to their not-per-distance cousins # FIXME these are hardcoded for now if 'energy_eff_per_distance' in tech_settings.constraints: distance_energy_eff = ( tech_settings.constraints.energy_eff_per_distance ** tech_settings.distance ) tech_settings.constraints.energy_eff = ( tech_settings.constraints.get_key('energy_eff', 1.0) * distance_energy_eff ) del tech_settings.constraints['energy_eff_per_distance'] locations_comments.set_key( '{}.links.{}.techs.{}.constraints.energy_eff'.format(loc_from, loc_to, tech_name), 'Includes value computed from energy_eff_per_distance' ) for k in tech_settings.costs.keys_nested(subkeys_as='list'): if 'energy_cap_per_distance' in k: energy_cap_costs_per_distance = ( tech_settings.costs.get_key(k) * tech_settings.distance ) tech_settings.costs[k.split('.')[0]].energy_cap = ( tech_settings.costs[k.split('.')[0]].get_key('energy_cap', 0) + energy_cap_costs_per_distance ) tech_settings.costs.del_key(k) locations_comments.set_key( '{}.links.{}.techs.{}.costs.{}'.format(loc_from, loc_to, tech_name, k), 'Includes value computed from energy_cap_per_distance' ) elif 'purchase_per_distance' in k: purchase_costs_per_distance = ( tech_settings.costs.get_key(k) * tech_settings.distance ) tech_settings.costs[k.split('.')[0]].purchase = ( tech_settings.costs[k.split('.')[0]].get_key('purchase', 0) + purchase_costs_per_distance ) tech_settings.costs.del_key(k) locations_comments.set_key( '{}.links.{}.techs.{}.costs.{}'.format(loc_from, loc_to, tech_name, k), 'Includes value computed from purchase_per_distance' ) return tech_settings