Leaflet/Leaflet · error · Error

Invalid GeoJSON object.

Error message

Invalid GeoJSON object.

What it means

Thrown by GeoJSON.geometryToLayer() in the default branch of its switch on geometry.type. Recognized types are Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, FeatureCollection. Any other (or misspelled) type falls through and throws. Note: a geometry with no coordinates returns null earlier (line 179) and does not throw.

Source

Thrown at src/layer/GeoJSON.js:230

				if (geoLayer) {
					layers.push(geoLayer);
				}
			}
			return new FeatureGroup(layers);

		case 'FeatureCollection':
			for (const f of geometry.features) {
				const featureLayer = GeoJSON.geometryToLayer(f, options);

				if (featureLayer) {
					layers.push(featureLayer);
				}
			}
			return new FeatureGroup(layers);

		default:
			throw new Error('Invalid GeoJSON object.');
		}
	}

	static _pointToLayer(pointToLayerFn, geojson, latlng, options) {
		return pointToLayerFn ?
			pointToLayerFn(geojson, latlng) :
			new Marker(latlng, options?.markersInheritOptions && options);
	}

	// @function coordsToLatLng(coords: Array): LatLng
	// Creates a `LatLng` object from an array of 2 numbers (longitude, latitude)
	// or 3 numbers (longitude, latitude, altitude) used in GeoJSON for points.
	static coordsToLatLng(coords) {
		return new LatLng(coords[1], coords[0], coords[2]);
	}

	// @function coordsToLatLngs(coords: Array, levelsDeep?: Number, coordsToLatLng?: Function): Array
	// Creates a multidimensional array of `LatLng`s from a GeoJSON coordinates array.

View on GitHub (pinned to c96f31a7a3)

Solutions

  1. Validate geometry.type against the allowed set before calling geometryToLayer.
  2. Use a JSON-Schema validator (e.g. @turf helpers or geojson-validation) on incoming features.
  3. Normalize/correct known typos (e.g. 'Poin' -> 'Point') upstream.
  4. Skip features whose type is unrecognized rather than letting them reach the switch.

Example fix

// before
L.geoJSON(rawFeature); // rawFeature.geometry.type === 'Poin' -> throws
// after
const ALLOWED = new Set(['Point','MultiPoint','LineString','MultiLineString','Polygon','MultiPolygon','GeometryCollection','FeatureCollection']);
if (ALLOWED.has(rawFeature.geometry?.type)) {
  L.geoJSON(rawFeature);
} else { console.warn('Unsupported geometry', rawFeature); }
Defensive patterns

Strategy: validation

Validate before calling

const GEOJSON_TYPES = new Set(['Point','MultiPoint','LineString','MultiLineString','Polygon','MultiPolygon','GeometryCollection','FeatureCollection','Feature']);
function isKnownGeometry(g) {
  return g && GEOJSON_TYPES.has(g.type) && (g.type === 'GeometryCollection' ? Array.isArray(g.geometries) : (g.coordinates !== undefined || g.geometry !== undefined || g.features !== undefined));
}
if (isKnownGeometry(feature)) L.geoJSON(feature);

Type guard

function isValidGeoJSONFeature(v) {
  if (!v || typeof v !== 'object') return false;
  const t = v.type;
  return ['Feature','Point','MultiPoint','LineString','MultiLineString','Polygon','MultiPolygon','GeometryCollection','FeatureCollection'].includes(t);
}

Try / catch

try { return GeoJSON.geometryToLayer(feature, options); }
catch (e) { if (/Invalid GeoJSON object/.test(e.message)) { console.warn('Skipping unsupported geometry', feature); return null; } throw e; }

Prevention

When it happens

Trigger: GeoJSON.geometryToLayer({type:'Feature', geometry:{type:'Poin', coordinates:[0,0]}}) (typo); a custom geometry type not in the RFC 7946 list; geometry.type undefined reaching the switch (when coordinates are present but type is missing).

Common situations: Third-party GeoJSON with a non-standard geometry type; hand-built features with a typo; truncated JSON where 'type' was dropped; mixing older GeoJSON variants.

Related errors


AI-assisted analysis of Leaflet/Leaflet@c96f31a7a3 (2026-08-13). Data as JSON: /api/errors/4660bd5f24c20692. Report an issue: GitHub.