from bbox import BBox
from geometry import Geometrypolyline
class PolyLine(Geometry): def __init__(self, contours):
self.contours = contoursreturns the bounding box of the line
def bbox(self): bbox = BBox()
for pts in self.contours:
for pt in pts:
bbox.update(pt)
return bboxprojects the line to a map projection
def project(self, proj): contours = []
for points in self.contours:
pts = []
for pt in points:
if proj._visible(pt[0], pt[1]):
p = proj.project(pt[0], pt[1])
if p is not None:
pts.append(p)
else:
if len(pts) > 0:
contours.append(pts)
pts = []
if len(pts) > 0:
contours.append(pts)
return PolyLine(contours)transforms the line to a new view
def project_view(self, view): contours = []
for points in self.contours:
pts = []
for pt in points:
p = view.project(pt)
pts.append(p)
contours.append(pts)
return PolyLine(contours)constructs a svg representation of this line
def to_svg(self, svg, round): path_str = ""
if round is False:
fmt = '%f,%f'
else:
fmt = '%.' + str(round) + 'f'
fmt = fmt + ',' + fmt
for points in self.contours:
path_str += "M "
pts = []
for pt in points:
pts.append(fmt % (pt[0], pt[1]))
path_str += 'L '.join(pts)
path = svg.node('path', d=path_str)
return path def crop_to(self, geom):skip
return self def is_empty(self):
return len(self.contours) == 0 def unify(self, point_store, precision):
from kartograph.simplify import unify_polygon
for i in range(len(self.contours)):
self.contours[i] = unify_polygon(self.contours[i], point_store, precision) def points(self):
return self.contoursis called after the points of this geometry have been changed from outside this class
def update(self): passsimple line (= list of points)
class Line(Geometry): def __init__(self, points):
self.pts = pointsreturns the bounding box of the line
def bbox(self): bbox = BBox()
for pt in self.pts:
bbox.update(pt)
return bboxprojects the line to a map projection
def project(self, proj): pts = []
for pt in self.pts:
p = proj.project(pt[0], pt[1])
if p is not None:
pts.append(p)
return Line(pts)transforms the line to a new view
def project_view(self, view): pts = []
for pt in self.pts:
p = view.project(pt)
pts.append(p)
return Line(pts)constructs a svg representation of this line
def to_svg(self, svg, round): path_str = ""
if round is False:
fmt = '%f,%f'
else:
fmt = '%.' + str(round) + 'f'
fmt = fmt + ',' + fmt
for pt in self.pts:
if path_str == "":
path_str = "M"
else:
path_str += "L"
path_str += fmt % (pt[0], pt[1])
path = svg.node('path', d=path_str)
return path def crop_to(self, geom):skip
return self def is_empty(self):
return len(self.pts) == 0 def unify(self, point_store, precision):
from kartograph.simplify import unify_polygon
self.pts = unify_polygon(self.pts, point_store, precision) def points(self):
return [self.pts]is called after the points of this geometry have been changed from outside this class
def update(self): pass