dgraph-io/dgraph · error

Invalid coordinates

Error message

Invalid coordinates

What it means

Thrown by convertToGeom when the whitespace-stripped coordinate string is shorter than 5 characters (the minimum for a minimal coordinate pair like [1,2]). The string cannot possibly contain a valid geometry, so parsing is aborted before JSON decode.

Source

Thrown at types/s2.go:168

			if err := closed(v); err != nil {
				return nil, err
			}
		}
		return g, nil
	}

	var g geojson.Geometry
	if err := json.Unmarshal([]byte(str), &g); err == nil {
		t, err := g.Decode()
		if err != nil {
			return nil, err
		}
		return validate(t)
	}

	s := x.WhiteSpace.Replace(str)
	if len(s) < 5 { // [1,2]
		return nil, errors.Errorf("Invalid coordinates")
	}
	var m json.RawMessage
	var err error

	if s[0:4] == "[[[[" {
		g.Type = "MultiPolygon"
		err = m.UnmarshalJSON([]byte(s))
		if err != nil {
			return nil, errors.Wrapf(err, "Invalid coordinates")
		}
		g.Coordinates = &m
		g1, err := g.Decode()
		if err != nil {
			return nil, errors.Wrapf(err, "Invalid coordinates")
		}
		return validate(g1)
	}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Pass a well-formed GeoJSON coordinate string such as "[1,2]" or deeper nesting
  2. Check input length/non-emptiness before invoking geo conversion
  3. Fix upstream serialization so the full coordinates JSON is stored/sent

Example fix

// before
geom, err := convertToGeom("[1")
// after
geom, err := convertToGeom("[1,2]")
Defensive patterns

Strategy: validation

Validate before calling

s := strings.Join(strings.Fields(str), "")
if len(s) < 5 {
    return errors.New("coordinates too short to be valid GeoJSON")
}

Try / catch

g, err := types.ConvertToGeom(str)
if err != nil && strings.Contains(err.Error(), "Invalid coordinates") && len(strings.TrimSpace(str)) < 5 {
    // reject empty/truncated geometry input
}

Prevention

When it happens

Trigger: Calling GetGeoTokens/convertToGeom with strings like '', '[]', '[1]', or ' [ ' — anything under 5 chars after whitespace removal.

Common situations: Empty database fields, truncated GeoJSON in transit, or callers passing raw lat/lon numbers ('37.7') instead of a GeoJSON coordinate array.

Related errors


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