Repository navigation
Description
Before you start
- I searched the existing issues and the roadmap and did not find a duplicate request.
What problem does this solve?
Since #2768 and #2779, a Shapefile read in place (over 100,000 records) reads only the features near the view, and keeps the view it read for the frames after. That only applies to a Shapefile in longitude/latitude drawn on Web Mercator. A projected Shapefile (UTM, state plane), or any Shapefile on another map projection, still reads, parses and reprojects every record on every frame.
Measured on main (dd3d011): Tennessee's 3,212,306 building footprints, and the same file reprojected to UTM zone 16N (EPSG:32616). Frames of 1480 × 900 on Web Mercator, drawn as the desktop app draws a settled frame, CPU path, i7-6700. Each figure is the median of a repeat of the view and four pans of a tenth of the view. The machine was heavily loaded (load average 20–30), so absolute times are high; the ratio is the point:
| View | longitude/latitude file | UTM 16N file |
|---|---|---|
| Memphis, 1:24,000 | 0.48 s | 27 s |
| Memphis area, 1:250,000 | 3.3 s | 31 s |
| most of the state, 1:2,500,000 | 13 s | 22 s |
Projected Shapefiles are common: much agency and government data is published in UTM or a state plane system.
Why the boxes in the file aren't enough
The map draws a feature as straight lines between its vertices after they are projected to the map. The Shapefile's index holds each record's box in the file's own system. For a file in longitude/latitude on Web Mercator the two agree exactly, as Web Mercator maps longitude and latitude each on its own: a box of longitude/latitude becomes a box on the map, and no straight edge leaves it. In any other pairing, a box in the file becomes a curved shape on the map. An edge drawn straight between two projected vertices can cross the view while the record's box misses the read's box. A long line on Robinson does this (#2768's review), and a map projection that cuts features at its edge moves their parts as well.
Growing the read's box by the largest feature's size isn't a fix. It is safe only where the projection's scale barely changes across a feature, which can't be checked in general. One large feature (a county boundary, a long river) grows the box to the whole layer.
Proposed solution
Index each record's box as it is drawn: in map units, after projection, the box the draw loop already culls by (Layer::projected_extents). A feature is drawn only when that box meets the cull extent, so a read of exactly the records whose drawn boxes meet it gives the same pixels for any projection and any source system, the cut at a projection's edge included.
- Boxes come from the whole reads already made. Until it has an index, a frame reads the whole source, as today, and projects every feature to draw it. Each chunk's projected extents are kept with the records' ids (a Shapefile's feature id is its record number + 1). When a whole read completes, the boxes are packed into an index for that source, projection and set of datum grids. They are rounded outward to f32, so a read gets every record the draw loop draws, plus at most a few at the edge.
- Later frames read only the records the index finds. The index gives the records whose boxes meet the cull extent; the source reads them by id in file order (perf(render): read only the view of a streamed Shapefile that draws nothing from beyond it #2768's bitmap path already does that from the R-tree, so it needs a way in that takes ids). The same conditions as perf(render): read only the view of a streamed Shapefile that draws nothing from beyond it #2768 decide when a frame may read only its view: no labels, a selection,
@row_number, GPU frames and so on read everything. - The kept view (perf(render): keep a streamed Shapefile view with the geometry drawn from it #2779) then applies too. It is keyed by projection already.
- Invalidation follows the kept view: another projection or set of datum grids, a source reset or reopened, or a file changed on disk (
FeatureSource::same_as_opened).
A packed static R-tree (boxes sorted into leaves of 16, x slices then y, parents built the same way) prototyped on the 3.2 million records, against an external R-tree crate's bulk load:
| Build | Memory | Query, city (21,852 hits) | County (354,674) | State (2,575,406) | |
|---|---|---|---|---|---|
| packed, in RustGIS | 4.3 s | 67 MB | 0.3 ms | 15 ms | 81 ms |
| external crate | 6.6 s | 64 MB | 0.4 ms | 7 ms | 128 ms |
So no new dependency is needed. The build runs once per source and projection, after a whole read, on a worker.
Memory: about 21 bytes a record, about 4 MB for 200,000 records and 67 MB for 3.2 million. It is a fraction of the layer's own size: the Tennessee .shp is about 160 bytes a record before its attributes. It has to fit the source's share of the map's view memory beside its interaction rows and kept view, or no index is built and the source reads whole, as now. Keeping it on disk beside the Shapefile's own index (in the cache, keyed by projection) would remove that limit and survive a restart, at the cost of a slower build; it could come later.
A cheaper first step: once #2774 lands, a map in the source's own system (a UTM file on a UTM map) can read the draw pass by the box in that system, exactly and without an index, since the projection there is the identity.
Tests
- Pixel parity with the layer held in memory on Web Mercator, Robinson and a UTM map, for a projected file and a longitude/latitude one, including features cut at a projection's edge and a long two-vertex line that crosses the view while its box in the file misses it.
- Exact reads per frame (none before a whole read has built the index, then only by the index), and invalidation by projection, datum grids, reset and a changed file.
- The index's own query against a linear scan, and its memory refused past the budget.
Coordination
- Streamed reads in the source's own system: a map in it reads its window in map units (#2032 C2f-4a) #2774 (Tracking: terrio-crs as coordinate engine and a coordinate system per layer #2032 C2f-4a) changes how streamed sources are read by area, adding a box in the source's own system. This builds on it, so it starts after Streamed reads in the source's own system: a map in it reads its window in map units (#2032 C2f-4a) #2774 lands.
- [Feature]: Reusable spatial indexing for map queries and snapping #2330 asks for reusable spatial indexes for map queries and snapping. An index of drawn boxes per streamed source could serve hit-testing on streamed layers too, so its interface should keep that use open.
Estimated scope
Medium: a broader feature that is feasible in a reasonable timeframe. Three PRs: (1) The own-system read after #2774. (2) The drawn-box index and reading by ids. (3) Kept views and memory accounting for it.
Alternatives considered
- Growing the read box by the largest feature's size: not exact, and useless with one large feature (above).
- Reprojecting the file's boxes on open: the same curved-edge problem, and a whole pass over the file before the first frame.
- An external R-tree crate: no faster here, and a new dependency.
Follows #1065.
Activity
perf(render): read a streamed Shapefile's view in its own units on a …
perf(render): read a streamed Shapefile's view by its features' boxes…