Atlaslog / Field Notes / Styling & rendering
Field note · Styling & rendering
A numeric filter never matches because the property is a string
Quick answer
A filter for districts with `pop >= 10000` draws nothing even though 40 features qualify, and no error is logged. The `pop` values arrived from CSV as strings, and Mapbox expressions are strongly typed with no implicit conversion.
Symptom
- `filter: ['>=', ['get', 'pop'], 10000]` yields 0 rendered features with no console error
- `typeof f.properties.pop` is `"string"` with a value like `"12000"`
- Clearing the filter with `map.setFilter('districts', null)` makes the features appear
Checklist
- Print `typeof` of the property on a feature from `querySourceFeatures`
- Remove the filter to confirm it is filtering rather than styling
- Wrap the value in `['to-number', ['get', 'pop']]` and retest
- Check how the source was produced, since CSV-to-GeoJSON usually keeps strings
- Compare the filter literal type with the actual property type
Fix
Convert explicitly in the expression: `filter: ['>=', ['to-number', ['get', 'pop'], 0], 10000]`, and quote string comparisons (`['==', ['get', 'level'], '2']`); also coerce numeric fields to `Number` at ingest.
Code
filter: ['>=', ['get', 'pop'], 10000]const f = map.querySourceFeatures('districts')[0];
console.log(f.properties.pop, typeof f.properties.pop);
// "12000" string
map.setFilter('districts', null); // 要素出现 -> 过滤条件有问题
Watch out
- Quoting the threshold instead of converting the value (`['>=', ['get', 'pop'], '10000']`), which compares strings lexicographically
- Assuming the absence of a console error means the filter is valid, when a type mismatch silently evaluates to false
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). A numeric filter never matches because the property is a string. https://www.jingweizhi.com/en/cookbook/filter-number-vs-string.html (accessed 2026-11-03).