Skip to content

Commit

Permalink
Refactor ring validation in Polygon's Validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Sep 1, 2023
1 parent a55e2fb commit 2fb5d3b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,11 @@ func (p Polygon) Validate() error {
}

for i, r := range p.rings {
if err := r.Validate(); err != nil {
if err := validateRing(r); err != nil {
return wrap(err, "validating ring at index %d", i)
}
}

for _, r := range p.rings {
if r.IsEmpty() {
return violateRingEmpty.err()
}
if !r.IsClosed() {
return violateRingClosed.errAtXY(r.Coordinates().GetXY(0))
}
if !r.IsSimple() {
return violateRingSimple.errAtXY(r.Coordinates().GetXY(0))
}
}

// Data structures used to track connectedness.
nextInterVert := len(p.rings)
interVerts := make(map[XY]int)
Expand Down Expand Up @@ -165,6 +153,22 @@ func (p Polygon) Validate() error {
return nil
}

func validateRing(r LineString) error {
if err := r.Validate(); err != nil {
return err
}
if r.IsEmpty() {
return violateRingEmpty.err()
}
if !r.IsClosed() {
return violateRingClosed.errAtXY(r.Coordinates().GetXY(0))
}
if !r.IsSimple() {
return violateRingSimple.errAtXY(r.Coordinates().GetXY(0))
}
return nil
}

// Type returns the GeometryType for a Polygon
func (p Polygon) Type() GeometryType {
return TypePolygon
Expand Down

0 comments on commit 2fb5d3b

Please sign in to comment.