diff --git a/lib/xmet/spc.py b/lib/xmet/spc.py index b3bad88..61ea894 100644 --- a/lib/xmet/spc.py +++ b/lib/xmet/spc.py @@ -48,7 +48,7 @@ RE_HAZARD = re.compile(r''' RE_POINTS_START = re.compile(r''' ^(?P[A-Z0-9\.]+) - (?P\s+\d{8}){1,6} + (?P(?:\s+\d{8}){1,6}) ''', re.X) RE_POINTS = re.compile(r'^(?:\s+\d{8}){1,6}$') @@ -68,8 +68,18 @@ def parse_coord(coord: str) -> tuple[float, float]: 0.01 * int(coord[0:4]) ) -def parse_poly(points: list[str]) -> shapely.Polygon: - return shapely.Polygon([parse_coord(p) for p in points]) +def each_poly(parts: list[str]): + points = list() + + for part in parts: + if part == '99999999': + yield shapely.Polygon(points) + points = list() + else: + points.append(parse_coord(part)) + + if len(points) > 0: + yield shapely.Polygon(points) class SPCOutlookArea(DatabaseTable): __slots__ = ('id', 'outlook_id', 'poly') @@ -276,19 +286,20 @@ class SPCOutlookParser(): self.state = SPCOutlookParserState.AREA_THREAT def handle_area(self): - if self.area_type == 'PROBABILISTIC': - area = SPCOutlookProbabilityArea() - area.hazard = self.hazard - area.probability = self.category - area.poly = parse_poly(self.points) + for poly in each_poly(self.points): + if self.area_type == 'PROBABILISTIC': + area = SPCOutlookProbabilityArea() + area.hazard = self.hazard + area.probability = self.category + area.poly = poly - self.outlook.probabilities.append(area) - elif self.area_type == 'CATEGORICAL': - area = SPCOutlookCategoryArea() - area.category = self.category - area.poly = parse_poly(self.points) + self.outlook.probabilities.append(area) + elif self.area_type == 'CATEGORICAL': + area = SPCOutlookCategoryArea() + area.category = self.category + area.poly = poly - self.outlook.categories.append(area) + self.outlook.categories.append(area) self.category = None self.points = list()