4 __all__ = [ 'Area', 'load_area_from_kml_polygon' ]
10 That class defines an area (on the Earth)
11 It provides testing whether a point is inside or not
13 def __init__(self, points=[]):
17 def addpoint(self, point):
18 self.points.append(point)
19 if len(self.points)==1:
23 if point[0] < self.min[0]:
24 self.min = (point[0], self.min[1])
25 elif point[0] > self.max[0]:
26 self.max = (point[0], self.max[1])
27 if point[1] < self.min[1]:
28 self.min = (self.min[0], point[1])
29 elif point[1] > self.max[1]:
30 self.max = (self.max[0], point[1])
33 for point in self.points:
34 if not self.contains(point):
38 def contains(self, point):
41 # first test the bounding box
42 #if point[0] < self.min[0] \
43 # or point[0] > self.max[0] \
44 # or point[1] < self.min[1] \
45 # or point[1] > self.max[1] :
47 for i in range(len(self.points)):
50 p2 = self.points[(i+1)%len(self.points)]
60 def load_area_from_kml_polygon(filename):
62 coordinates_lines = [ line for line in file.readlines() if '</coordinates>' in line ]
63 if len(coordinates_lines) != 1:
64 print >> sys.stderr, 'There should be exactly one line with coordinates in', filename
66 coordinates = coordinates_lines[0].replace('</coordinates>', '').replace('\n', '').replace('\r', '')
67 coordinates = [ xyz for xyz in coordinates.split(' ') if xyz ]
68 if coordinates[0] != coordinates[-1]:
69 print >> sys.stderr, 'First and last coordinates of', filename, 'should be the same'
70 print >> sys.stderr, coordinates[0]
71 print >> sys.stderr, coordinates[-1]
73 assert len(coordinates)>3, 'polygon should have 3 edges minimum'
76 for xyz in coordinates[0:-1]:
77 x,y,z = xyz.split(',')
78 area.addpoint((float(y),float(x)))
79 assert area.check(), 'Polygon should be counter-clockwise and convex.'
83 #if __name__ == '__main__':
84 # counter clock-wise : Positive
87 # (45.3612930132714, 10.01843703552244),
99 # if pelagos.contains(p):