dgraph-io/dgraph · error

Last coord not same as first

Error message

Last coord not same as first

What it means

Thrown by the closed-loop validator in convertToGeom when the first ring of a polygon is not closed — the last coordinate does not repeat the first coordinate. GeoJSON requires linear rings to be closed so the loop can be converted to an S2 loop.

Solutions

  1. Close the ring by appending a copy of the first coordinate as the last coordinate
  2. Run a GeoJSON validator on data before ingesting
  3. Programmatically normalize rings (auto-close if c[0] != c[len-1]) prior to calling the API

Example fix

// before
[[[0,0],[0,5],[5,5],[5,0]]]
// after
[[[0,0],[0,5],[5,5],[5,0],[0,0]]]
Defensive patterns

Strategy: validation

Validate before calling

ring := poly.Coordinates[0]
first, last := ring[0], ring[len(ring)-1]
if first[0] != last[0] || first[1] != last[1] {
    return errors.New("linear ring must be closed (first == last)")
}

Try / catch

g, err := types.ConvertToGeom(str)
if err != nil && strings.Contains(err.Error(), "Last coord not same as first") {
    // auto-close the ring and retry, or reject input
}

Prevention

When it happens

Trigger: Passing a Polygon (or a ring of a MultiPolygon) whose first ring's last point differs from its first, e.g. [[0,0],[0,5],[5,5],[5,0]] without the closing [0,0].

Common situations: Hand-written GeoJSON, geometries exported from tools that emit open rings, or coordinates edited/truncated by upstream code.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/aa3be9d6aebfe5a9. Report an issue: GitHub.

Appendix: source

Thrown at types/s2.go:138

	}
	return intersects(l1, l2)
}

func convertToGeom(str string) (geom.T, error) {
	// validate would ensure that we have a closed loop for all the polygons. We don't support open
	// loop polygons.
	closed := func(p *geom.Polygon) error {
		coords := p.Coords()
		if len(coords) == 0 {
			return errors.Errorf("Got empty polygon.")
		}
		// Check that first ring is closed.
		c := coords[0]
		l := len(c)
		if c[0][0] == c[l-1][0] && c[0][1] == c[l-1][1] {
			return nil
		}
		return errors.Errorf("Last coord not same as first")
	}

	validate := func(g geom.T) (geom.T, error) {
		switch v := g.(type) {
		case *geom.MultiPolygon:
			for i := range v.NumPolygons() {
				if err := closed(v.Polygon(i)); err != nil {
					return nil, err
				}
			}
		case *geom.Polygon:
			if err := closed(v); err != nil {
				return nil, err
			}
		}
		return g, nil
	}

View on GitHub (pinned to 759e242be6)