def normalize_grid_mv_line_geotable(grid_mv_line_geotable, demand_point_table): 'Make sure that grid mv lines use (longitude, latitude) coordinate order' raw_geometries = [wkt.loads(x) for x in grid_mv_line_geotable['wkt']] # Remove incompatible geometries geometries, xs = [], [] for x, geometry in enumerate(raw_geometries): if geometry.type.endswith('LineString'): geometries.append(geometry) else: L.warn('Ignoring incompatible geometry ' 'in grid_mv_line_geotable (%s)' % geometry.wkt) xs.append(x) grid_mv_line_geotable.drop(grid_mv_line_geotable.index[xs]) # Remove elevation geometries = transform_geometries(geometries, drop_z) # Match coordinate order if geometries: regular = tuple(GeometryCollection(geometries).centroid.coords[0])[:2] flipped = regular[1], regular[0] reference = tuple(demand_point_table[['longitude', 'latitude']].mean()) # If the flipped coordinates are closer, if get_distance(reference, flipped) < get_distance(reference, regular): # Flip coordinates to get (longitude, latitude) coordinate order geometries = transform_geometries(geometries, flip_xy) grid_mv_line_geotable['wkt'] = [x.wkt for x in geometries] return {'grid_mv_line_geotable': grid_mv_line_geotable}
def normalize_grid_mv_line_geotable(grid_mv_line_geotable, demand_point_table): 'Make sure that grid mv lines use (latitude, longitude) coordinate order' raw_geometries = [wkt.loads(x) for x in grid_mv_line_geotable['wkt']] # Remove incompatible geometries geometries, xs = [], [] for x, geometry in enumerate(raw_geometries): if geometry.type.endswith('LineString'): geometries.append(geometry) else: LOG.warn( 'Ignoring incompatible geometry ' 'in grid_mv_line_geotable (%s)' % geometry.wkt) xs.append(x) grid_mv_line_geotable.drop(grid_mv_line_geotable.index[xs]) # Remove elevation geometries = transform_geometries(geometries, drop_z) # Match coordinate order if geometries: regular = tuple(GeometryCollection(geometries).centroid.coords[0])[:2] flipped = regular[1], regular[0] reference = tuple(demand_point_table[['latitude', 'longitude']].mean()) # If the flipped coordinates are closer, if get_distance(reference, flipped) < get_distance(reference, regular): # Flip coordinates to get (latitude, longitude) coordinate order geometries = transform_geometries(geometries, flip_xy) grid_mv_line_geotable['wkt'] = [x.wkt for x in geometries] return {'grid_mv_line_geotable': grid_mv_line_geotable}
def get_nearest_metro_distances(latitude: int, longitude: int, location_objs): distances = [] for obj in location_objs: distance_to_metro = get_distance((obj.latitude, obj.longitude), (latitude, longitude)) distances.append(distance_to_metro) return sorted(distances)
def run(location_geotable): graph = Graph() for index, row in location_geotable.iterrows(): graph.add_node(index, **{ 'lat': row['Latitude'], 'lon': row['Longitude'] }) for node1_id in range(min(graph), max(graph)): for node2_id in range(node1_id + 1, max(graph) + 1): node1_d = graph.node[node1_id] node2_d = graph.node[node2_id] node1_ll = node1_d['lat'], node1_d['lon'] node2_ll = node2_d['lat'], node2_d['lon'] distance = get_distance(node1_ll, node2_ll).m graph.add_edge(node1_id, node2_id, weight=distance) tree = minimum_spanning_tree(graph) total_distance = sum( edge_d['weight'] for node1_id, node2_id, edge_d in tree.edges(data=True)) location_count = len(graph) average_distance = divide_safely(total_distance, location_count, 0) return [ ('total_distance_between_locations_in_meters', total_distance), ('location_count', location_count), ('average_distance_between_locations_in_meters', average_distance), ]
def estimate_external_distribution_cost(node_id, latitude, longitude, line_length_adjustment_factor, grid_mv_line_cost_per_meter_by_year, infrastructure_graph, **keywords): d = defaultdict(float) key = 'grid_mv_line_adjusted_length_in_meters' relative_keys = [ 'grid_mv_line_raw_cost_per_meter', 'grid_mv_line_installation_cost_per_meter', 'grid_mv_line_maintenance_cost_per_meter_per_year', 'grid_mv_line_replacement_cost_per_meter_per_year', 'grid_mv_line_final_cost_per_meter_per_year', 'grid_mv_line_discounted_cost_per_meter', ] # Note that node_id is real but edge_node_id can be fake for edge_node_id, edge_d in infrastructure_graph.edge[node_id].items(): edge_node_d = infrastructure_graph.node[edge_node_id] edge_node_ll = edge_node_d['latitude'], edge_node_d['longitude'] line_length = get_distance((latitude, longitude), edge_node_ll).meters if 'name' in edge_node_d: # If both nodes are real, then the computation will reappear when # we process the other node, so we halve it here line_length /= 2. line_adjusted_length = line_length * line_length_adjustment_factor # Aggregate over each node that is connected to the edge edge_d[key] = edge_d.get(key, 0) + line_adjusted_length d[key] += line_adjusted_length for relative_key in relative_keys: cost_per_meter = keywords[relative_key] x = cost_per_meter * line_adjusted_length k = relative_key.replace('_per_meter', '') edge_d[k] = edge_d.get(k, 0) + x d[k] += x line_adjusted_length = d.get(key, 0) return merge_dictionaries( d, { 'external_distribution_cost_by_year': line_adjusted_length * grid_mv_line_cost_per_meter_by_year, })
def run(location_geotable): graph = Graph() for index, row in location_geotable.iterrows(): graph.add_node(index, { 'lat': row['Latitude'], 'lon': row['Longitude']}) for node1_id in xrange(min(graph), max(graph)): for node2_id in xrange(node1_id + 1, max(graph) + 1): node1_d = graph.node[node1_id] node2_d = graph.node[node2_id] node1_ll = node1_d['lat'], node1_d['lon'] node2_ll = node2_d['lat'], node2_d['lon'] distance = get_distance(node1_ll, node2_ll).m graph.add_edge(node1_id, node2_id, weight=distance) tree = minimum_spanning_tree(graph) total_distance = sum(edge_d[ 'weight'] for node1_id, node2_id, edge_d in tree.edges(data=True)) location_count = len(graph) average_distance = divide_safely(total_distance, location_count, 0) return [ ('total_distance_between_locations_in_meters', total_distance), ('location_count', location_count), ('average_distance_between_locations_in_meters', average_distance), ]
def estimate_external_distribution_cost( node_id, latitude, longitude, line_length_adjustment_factor, grid_mv_line_cost_per_meter_by_year, infrastructure_graph, **keywords): d = defaultdict(float) key = 'grid_mv_line_adjusted_length_in_meters' relative_keys = [ 'grid_mv_line_raw_cost_per_meter', 'grid_mv_line_installation_cost_per_meter', 'grid_mv_line_maintenance_cost_per_meter_per_year', 'grid_mv_line_replacement_cost_per_meter_per_year', 'grid_mv_line_final_cost_per_meter_per_year', 'grid_mv_line_discounted_cost_per_meter', ] # Note that node_id is real but edge_node_id can be fake for edge_node_id, edge_d in infrastructure_graph.edge[node_id].items(): edge_node_d = infrastructure_graph.node[edge_node_id] edge_node_ll = edge_node_d['latitude'], edge_node_d['longitude'] line_length = get_distance((latitude, longitude), edge_node_ll).meters if 'name' in edge_node_d: # If both nodes are real, then the computation will reappear when # we process the other node, so we halve it here line_length /= 2. line_adjusted_length = line_length * line_length_adjustment_factor # Aggregate over each node that is connected to the edge edge_d[key] = edge_d.get(key, 0) + line_adjusted_length d[key] += line_adjusted_length for relative_key in relative_keys: cost_per_meter = keywords[relative_key] x = cost_per_meter * line_adjusted_length k = relative_key.replace('_per_meter', '') edge_d[k] = edge_d.get(k, 0) + x d[k] += x line_adjusted_length = d.get(key, 0) return merge_dictionaries(d, { 'external_distribution_cost_by_year': line_adjusted_length * grid_mv_line_cost_per_meter_by_year, })
def _distance_between(dictionary1, dictionary2): location1 = _location(dictionary1) location2 = _location(dictionary2) return get_distance(location1, location2)
def _distance_between(dictionary1,dictionary2): location1=_location(dictionary1) location2=_location(dictionary2) return get_distance(location1,location2)