# Press the Blue Button to preview this as a CrossCompute Tool
drop_pole_table_path = 'drop-pole-points.csv'
distribution_pole_interval_in_meters = 1
target_folder = '/tmp'
from pandas import read_csv
drop_pole_table = read_csv(drop_pole_table_path)
drop_pole_table
from shapely import wkt
from shapely.geometry import MultiPoint
drop_pole_points = [wkt.loads(x) for x in drop_pole_table['wkt']]
MultiPoint(drop_pole_points)
import networkx as nx
from itertools import combinations
from scipy.spatial.distance import euclidean as get_distance
graph = nx.Graph()
for p1, p2 in combinations(drop_pole_points, 2):
distance = get_distance(p1, p2)
graph.add_edge(
tuple(p1.coords[0]),
tuple(p2.coords[0]), weight=distance)
graph.edges(data=True)
tree = nx.minimum_spanning_tree(graph)
tree.edges(data=True)
# Convert edges into poles
from shapely.geometry import LineString, MultiPoint, Point
distribution_pole_points = []
for p1_coords, p2_coords in tree.edges():
line_length_in_meters = get_distance(p1_coords, p2_coords)
distribution_pole_count = int(
line_length_in_meters / distribution_pole_interval_in_meters)
segment_count = distribution_pole_count + 1
adjusted_interval_in_meters = line_length_in_meters / float(
segment_count)
line = LineString([p1_coords, p2_coords])
print(p1_coords, p2_coords)
for segment_index in range(segment_count):
distribution_pole_point = line.interpolate(
adjusted_interval_in_meters * segment_index)
print(distribution_pole_point)
distribution_pole_points.append(distribution_pole_point)
print(p2_coords)
distribution_pole_points.append(Point(p2_coords))
print('***')
MultiPoint(distribution_pole_points)
import csv
from os.path import join
target_path = join(target_folder, 'distribution-pole-points.csv')
with open(target_path, 'wt') as target_file:
target_csv = csv.writer(target_file)
target_csv.writerow(['wkt'])
for distribution_pole_point in distribution_pole_points:
target_csv.writerow([distribution_pole_point.wkt])
print('distribution_pole_table_path = %s' % target_path)