def perform_sampling(self): if not self.prepared: self.prepare_sampling() # performing a conforming Delaunay triangulation using Triangle self.tw = TriangleWrapper(self.polygon) # self.tw.toggle_verbosity() self.tw.apply_triangle() # creating skeleton line from triangulation self.skel = self.create_skeleton_line() self.convert_skeleton_to_sample_points()
def perform_sampling(self): from triangle_wrapper import TriangleWrapper self.tw = TriangleWrapper() self.skel = LineString() for src in self.src: self.tw.set_polygon(src) self.tw.create_poly_data() tmp_name = self.tw.write_poly_file() self.tw.build_triangle_cmd(tmp_name) self.tw.execute_triangle() self.tw.read_node_file() self.tw.read_ele_file() single_skel = self.tw.create_skeleton_line() self.skel = self.skel.union(single_skel) #print self.skel self.tw.cleanup() self.convert_skeleton_to_sample_points()
def perform_sampling(self): if not self.prepared: self.prepare_sampling() # performing a conforming Delaunay triangulation using Triangle self.tw = TriangleWrapper(self.polygon) #self.tw.toggle_verbosity() self.tw.apply_triangle() # creating skeleton line from triangulation self.skel = self.create_skeleton_line() self.convert_skeleton_to_sample_points()
def perform_sampling(self): from triangle_wrapper import TriangleWrapper self.tw = TriangleWrapper() self.skel = LineString() for src in self.src: self.tw.set_polygon(src) self.tw.create_poly_data() tmp_name = self.tw.write_poly_file() self.tw.build_triangle_cmd(tmp_name) self.tw.execute_triangle() self.tw.read_node_file() self.tw.read_ele_file() single_skel = self.tw.create_skeleton_line() self.skel = self.skel.union(single_skel) #print self.skel self.tw.cleanup() self.convert_skeleton_to_sample_points()
class SkeletonLineSampler(PolygonToPointSampler): SIMPLIFY_TOLERANCE = 20 def __init__(self, polygon='', simplify=True, simplify_tolerance=SIMPLIFY_TOLERANCE): super(self.__class__, self).__init__(polygon) self.simplify = simplify self.simplify_tolerance = simplify_tolerance def perform_sampling(self): from triangle_wrapper import TriangleWrapper self.tw = TriangleWrapper() self.skel = LineString() for src in self.src: self.tw.set_polygon(src) self.tw.create_poly_data() tmp_name = self.tw.write_poly_file() self.tw.build_triangle_cmd(tmp_name) self.tw.execute_triangle() self.tw.read_node_file() self.tw.read_ele_file() single_skel = self.tw.create_skeleton_line() self.skel = self.skel.union(single_skel) #print self.skel self.tw.cleanup() self.convert_skeleton_to_sample_points() def convert_skeleton_to_sample_points(self): # converting straight skeleton line to its vertices lines = list() if hasattr(self.skel, 'geoms'): for line in self.skel: lines.append(line) else: lines.append(self.skel) for line in lines: for x, y in line.coords: sp = Point((x, y)) self.samples.append(sp) print sp def distance(self, from_pt, to_pt): return sqrt((from_pt[0] - to_pt[0])**2 + (from_pt[1] - to_pt[1])**2)
class SkeletonLineSampler(PolygonPointSampler): SIMPLIFY_TOLERANCE = 20 def __init__( self, polygon='', simplify=True, simplify_tolerance=SIMPLIFY_TOLERANCE ): super(self.__class__, self).__init__(polygon) self.simplify = simplify self.simplify_tolerance = simplify_tolerance def perform_sampling(self): if not self.prepared: self.prepare_sampling() # performing a conforming Delaunay triangulation using Triangle self.tw = TriangleWrapper(self.polygon) # self.tw.toggle_verbosity() self.tw.apply_triangle() # creating skeleton line from triangulation self.skel = self.create_skeleton_line() self.convert_skeleton_to_sample_points() def create_skeleton_line(self, simplify=True, simplify_tolerance=''): """ Create a skeleton line of the polygon by using the circumcenters of the triangles created by the Conforming Delaunay Triangulation applied by Triangle Optionally simplify (by default) the skeleton by using an algorithm provided by Shapely. """ self.circumcenters = list() for t in self.tw.triangles: self.circumcenters.append(self.calculate_circumcenter(t)) # list of skeleton segments skel_segments = list() for key in sorted(self.tw.shared_edges.keys()): if self.tw.shared_edges[key] != 2: continue # retrieve endpoints of the skeleton segment from_pt = self.circumcenters[key[0] - 1] to_pt = self.circumcenters[key[1] - 1] # creating skeleton segment skel_segment = LineString( [(cc.x, cc.y) for cc in (from_pt, to_pt)]) skel_segments.append(skel_segment) else: # merging all skeleton segments to a single (possibly multiline) # object skel_line = linemerge(skel_segments) # simplifying skeleton line if simplify: if not simplify_tolerance: simplify_tolerance = self.SIMPLIFY_TOLERANCE skel_line = skel_line.simplify(simplify_tolerance, False) return skel_line def convert_skeleton_to_sample_points(self): """ Convert a straight skeleton line to its vertices. """ # converting straight skeleton line to its vertices lines = list() if hasattr(self.skel, 'geoms'): for line in self.skel: lines.append(line) else: lines.append(self.skel) for line in lines: for x, y in line.coords: sp = Point((x, y)) self.samples.append(sp) def calculate_circumcenter(self, triangle): u""" Calculate circumcenter of given triangle in cartesian coordinates according to formula given by: http://is.gd/ctPx80 """ a, b, c = [Point(triangle.exterior.coords[i]) for i in [0, 1, 2]] d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) cx = ( (a.y**2 + a.x**2) * (b.y - c.y) + (b.y**2 + b.x**2) * (c.y - a.y) + (c.y**2 + c.x**2) * (a.y - b.y)) / d cy = ( (a.y**2 + a.x**2) * (c.x - b.x) + (b.y**2 + b.x**2) * (a.x - c.x) + (c.y**2 + c.x**2) * (b.x - a.x)) / d return Point((cx, cy))
class SkeletonLineSampler(PolygonPointSampler): SIMPLIFY_TOLERANCE = 20 def __init__(self, polygon = '', simplify = True, simplify_tolerance = SIMPLIFY_TOLERANCE): super(self.__class__, self).__init__(polygon) self.simplify = simplify self.simplify_tolerance = simplify_tolerance def perform_sampling(self): if not self.prepared: self.prepare_sampling() # performing a conforming Delaunay triangulation using Triangle self.tw = TriangleWrapper(self.polygon) #self.tw.toggle_verbosity() self.tw.apply_triangle() # creating skeleton line from triangulation self.skel = self.create_skeleton_line() self.convert_skeleton_to_sample_points() def create_skeleton_line(self, simplify = True, simplify_tolerance = ''): u""" Create a skeleton line of the polygon by using the circumcenters of the triangles created by the Conforming Delaunay Triangulation applied by Triangle Optionally simplify (by default) the skeleton by using an algorithm provided by Shapely. """ self.circumcenters = list() for t in self.tw.triangles: self.circumcenters.append(self.calculate_circumcenter(t)) # list of skeleton segments skel_segments = list() for key in sorted(self.tw.shared_edges.iterkeys()): if self.tw.shared_edges[key] != 2: continue # retrieve endpoints of the skeleton segment from_pt = self.circumcenters[key[0] - 1] to_pt = self.circumcenters[key[1] - 1] # creating skeleton segment skel_segment = LineString([(cc.x, cc.y) for cc in (from_pt, to_pt)]) skel_segments.append(skel_segment) else: # merging all skeleton segments to a single (possibly multiline) # object skel_line = linemerge(skel_segments) # simplifying skeleton line if simplify: if not simplify_tolerance: simplify_tolerance = self.SIMPLIFY_TOLERANCE skel_line = skel_line.simplify(simplify_tolerance, False) return skel_line def convert_skeleton_to_sample_points(self): u""" Convert a straight skeleton line to its vertices. """ # converting straight skeleton line to its vertices lines = list() if hasattr(self.skel, 'geoms'): for line in self.skel: lines.append(line) else: lines.append(self.skel) for line in lines: for x, y in line.coords: sp = Point((x, y)) self.samples.append(sp) def calculate_circumcenter(self, triangle): u""" Calculate circumcenter of given triangle in cartesian coordinates according to formula given by: http://is.gd/ctPx80 """ a, b, c = [Point(triangle.exterior.coords[i]) for i in [0, 1, 2]] d = 2 * (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y)) cx = ((a.y**2 + a.x**2) * (b.y - c.y) + (b.y**2 + b.x**2) * (c.y - a.y) + (c.y**2 + c.x**2) * (a.y - b.y)) / d cy = ((a.y**2 + a.x**2) * (c.x - b.x) + (b.y**2 + b.x**2) * (a.x - c.x) + (c.y**2 + c.x**2) * (b.x - a.x)) / d return Point((cx, cy))
class SkeletonLineSampler(PolygonToPointSampler): SIMPLIFY_TOLERANCE = 20 def __init__(self, polygon = '', simplify = True, simplify_tolerance = SIMPLIFY_TOLERANCE): super(self.__class__, self).__init__(polygon) self.simplify = simplify self.simplify_tolerance = simplify_tolerance def perform_sampling(self): from triangle_wrapper import TriangleWrapper self.tw = TriangleWrapper() self.skel = LineString() for src in self.src: self.tw.set_polygon(src) self.tw.create_poly_data() tmp_name = self.tw.write_poly_file() self.tw.build_triangle_cmd(tmp_name) self.tw.execute_triangle() self.tw.read_node_file() self.tw.read_ele_file() single_skel = self.tw.create_skeleton_line() self.skel = self.skel.union(single_skel) #print self.skel self.tw.cleanup() self.convert_skeleton_to_sample_points() def convert_skeleton_to_sample_points(self): # converting straight skeleton line to its vertices lines = list() if hasattr(self.skel, 'geoms'): for line in self.skel: lines.append(line) else: lines.append(self.skel) for line in lines: for x, y in line.coords: sp = Point((x, y)) self.samples.append(sp) print sp def distance(self, from_pt, to_pt): return sqrt((from_pt[0] - to_pt[0]) ** 2 + (from_pt[1] - to_pt[1]) ** 2)