dgraph-io/dgraph · error

Got empty polygon.

Error message

Got empty polygon.

What it means

Thrown by the closed-loop validator inside convertToGeom (types/s2.go) when a GeoJSON Polygon has zero rings — an empty coordinates array. The library requires at least one closed ring to convert the polygon to S2 loops.

Source

Thrown at types/s2.go:130

}

// Intersects returns true if the two loops intersect.
func Intersects(l1 *s2.Loop, l2 *s2.Loop) bool {
	if l2.NumEdges() > l1.NumEdges() {
		// Use the larger loop for edge indexing.
		return intersects(l2, l1)
	}
	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
				}
			}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure the polygon has at least one ring in its coordinates before submitting
  2. Validate GeoJSON with a schema/linter (e.g. geojsonhint) before sending
  3. Reject empty coordinates at API ingress and return a 400 to the client

Example fix

// before
{"type":"Polygon","coordinates":[]}
// after
{"type":"Polygon","coordinates":[[[0,0],[0,5],[5,5],[5,0],[0,0]]]}
Defensive patterns

Strategy: validation

Validate before calling

var pl geojson.Polygon
json.Unmarshal(raw, &pl)
if len(pl.Coordinates) == 0 || len(pl.Coordinates[0]) == 0 {
    return errors.New("polygon must contain at least one ring")
}

Type guard

func hasRings(p *geom.Polygon) bool {
    return p != nil && len(p.Coords()) > 0
}

Try / catch

g, err := types.ConvertToGeom(str)
if err != nil && strings.Contains(err.Error(), "Got empty polygon") {
    // return 400 invalid geometry to client
}

Prevention

When it happens

Trigger: Parsing a GeoJSON value like {"type":"Polygon","coordinates":[]} or [[ ]] through convertToGeom (used by GetGeoTokens), so p.Coords() returns an empty slice.

Common situations: Malformed GeoJSON from client uploads, truncated JSON payloads, or empty polygon defaults written by other tools.

Related errors


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