Introduction
GeoSort is a QGIS plugin that sorts the features of a vector layer
(points, lines or polygons) by a chosen geometric or attribute criterion, and
writes a progressive field (default sort_order: 1 = first
feature in the chosen order) to the layer or to an in-memory copy. It is
available both as an interactive dialog (Vector โ GeoSort menu) and as
a Processing Toolbox algorithm, usable in batch, in the graphical
modeler and via headless PyQGIS.
This guide explains every available criterion, its parameters (in the dialog and in Processing), when it's worth using and a typical use case with a before/after screenshot. For a compact table summary see also the project README.
Installation
- Download the plugin
.zipfrom the releases page. - In QGIS: Plugins โ Manage and Install Plugins โ Install from ZIP.
- Select the
geosort.zipfile. - The plugin appears under Vector โ GeoSort and in the Processing Toolbox under GeoSort.
.zip file.Basic concepts
- Progressive field โ GeoSort writes an integer field (default
sort_order, name/start value/step all customizable โ see Custom numbering) with each feature's position in the chosen order:1= first feature. - Direction โ Ascending or Descending, applies to any criterion.
NULLvalues โ for attribute/expression criteria, choose whether features with aNULLvalue go first or last.- NULL or incompatible geometries โ features with no geometry, or with a type incompatible with the chosen criterion (e.g. points with an "area" criterion), don't break the sort: they are pushed to the bottom.
- Preview โ the Previewโฆ button opens a popup with the first sorted features (FID,
sort_ordervalue, criterion value) to check the result before applying changes.
Attribute / Expression
Table attribute all types
Sorts by the value of any field on the layer (text, integer, float, date).
Dialog: "By attribute / expression" radio + field combo โข Processing: CRITERION=0, ATTRIBUTE_FIELD
When to use it: the most direct criterion โ street name, cadastral code, category, survey date, any column already present in the attribute table.
QGIS expression all types
Sorts by the result of an arbitrary QGIS expression, the same syntax as the Field Calculator. Click the ฮต button to open the expression editor; the active expression is highlighted in green, with a Remove button to go back to a single field.
Dialog: "By attribute / expression" radio + expression editor โข Processing: CRITERION=16, EXPRESSION
- Combining fields:
"area_kmq" / "pop" - Geometric functions:
length($geometry),area($geometry) - Conditions:
CASE WHEN "type"='A' THEN 1 ELSE 2 END - Concatenation for composite keys:
"fid" || "id_poly"
When to use it: when the criterion isn't a single field but a combination, an on-the-fly geometric calculation, or a conditional classification.
Text sort modes
For both criteria above, a text value can be compared in two ways:
| Mode | Behavior | Example |
|---|---|---|
| Lexicographic (default) | character-by-character comparison | "1010" < "11" < "1111" |
| Natural Sort | embedded digit sequences are compared as integers | "11" < "1010" < "1111" |
Useful with alphanumeric fields like FILE1, FILE2, FILE10 โ with lexicographic order FILE10 would end up before FILE2.
SHEET_1โฆSHEET_120: without Natural Sort, SHEET_100 would appear right after SHEET_1.Centroid coordinates
Centroid โ X / Y coordinate all types
Sorts by the geographic position of the geometry's centroid, along the X axis (westโeast) or Y axis (southโnorth).
Processing: CRITERION=1 (X) / CRITERION=2 (Y)
When to use it: a progressive "left to right" or "bottom to top" numbering โ poles, manholes, trees along a dominant axis.
Centroid โ distance from reference point all types
Sorts by the Euclidean (or geodesic, see Geodesic measurement) distance of the centroid from a configurable reference point. In the dialog the point is set via the X/Y spin boxes or by clicking Pick point on map โ the dialog lowers (without closing) and the next click on the map canvas captures the coordinates; Esc cancels the pick.
Dialog: X/Y spin boxes + map picker โข Processing: CRITERION=3, REF_POINT (optional, format "x,y [EPSG:xxxx]"; empty = origin 0,0)
Geometric property
Area / Perimeter polygons
Sorts polygons by surface area or perimeter length.
Processing: CRITERION=4 (area) / CRITERION=5 (perimeter)
Length lines
Sorts lines by total length.
Processing: CRITERION=6
Vertex count all types
Sorts by the number of vertices in the geometry.
Processing: CRITERION=7
Bounding Box โ width / height / area / Xmin / Ymin all types
Five variants on the size and position of the geometry's bounding box: width, height, area, minimum X coordinate, minimum Y coordinate.
Processing: CRITERION=8 (width), 9 (height), 10 (area), 11 (Xmin), 12 (Ymin)
Distance from line
Distance from reference line all types
Sorts features by perpendicular distance from a reference line (a second, line-type layer), in two modes:
- Distance from centroid โ distance between the feature's centroid and the line
- Distance from element โ distance from the nearest point of the whole geometry to the line
Dialog: line-layer combo + mode โข Processing: CRITERION=14, REF_LAYER, LINE_DISTANCE_MODE (advanced). The reference layer is automatically reprojected if its CRS differs from the input layer's.
Position along line
Position along reference line all types
Sorts features by their projected position along a reference line (a mileage-marker style progression), in three modes:
- Centroid projection โ all features โ every feature is projected onto the line and sorted by the resulting position, even if it doesn't intersect it
- Intersecting only โ via projected centroid โ as above, but only features that intersect the line; the others are excluded from the sort
- Intersecting only โ via first contact point โ as above, but the position used is that of the first intersection point, not the projected centroid
Dialog: line-layer combo + mode โข Processing: CRITERION=13, REF_LAYER, LINE_MODE (advanced)
Hilbert curve
Hilbert curve (spatial sorting) all types
Sorts features along a Hilbert curve computed on centroids, normalized to the layer's overall extent: features that are close in space stay close in the resulting order too โ unlike a row-based sort (X or Y) which can produce long jumps between features that are spatially close but at opposite ends of a row.
Processing: CRITERION=15, advanced parameter HILBERT_ORDER (default 16, grid resolution โ you almost never need to change it)
Serpentine (boustrophedon)
Serpentine all types
Sorts features into bands, with the cross axis alternating ascending/descending from one band to the next โ the classic ordering for numbering the plates of a regular map series or a photogrammetric flight path, without the long jump from the end of one band to the start of the next that a simple row-based sort produces.
- Band orientation โ horizontal (by Y, X alternating โ default) or vertical (by X, Y alternating)
- Band size โ height (horizontal) or width (vertical) of each band, in the layer's CRS units; 0/empty = automatic, from the average bounding-box size of the features
- Starting corner โ combining the general Direction (which band comes first) with First band ascending (which way the cross axis runs in the first band) freely selects any of the four corners of the grid as the starting point
Dialog: orientation combo + band-size spin box + "First band ascending" checkbox โข Processing: CRITERION=17, BAND_AXIS, BAND_SIZE, CROSS_ASCENDING (all advanced)
Hierarchical multi-criterion sorting
Set a secondary criterion to break ties in the primary criterion. Each level has its own direction (ascending/descending).
Dialog: "Secondary criterion" combo โข Processing: SECONDARY_CRITERION, SECONDARY_FIELD, SECONDARY_EXPRESSION, SECONDARY_DIRECTION (all advanced)
Geodesic measurement (geographic CRS)
On a layer with a degree-based CRS (e.g. EPSG:4326), planar area and length measurements are expressed in degreesยฒ/degrees โ metrically meaningless values that can skew the sort order by latitude. GeoSort can automatically compute area, perimeter, length and distances on the ellipsoid (geodesic measurement, in mยฒ or m).
| Mode | Behavior |
|---|---|
| Auto (default) | geodesic measurement is enabled automatically when the CRS is geographic; planar measurements are kept on projected CRS. A non-blocking warning signals the automatic activation. |
| Always | geodesic measurement always enabled, regardless of the CRS. |
| Never | always planar, regardless of the CRS. |
Criteria affected: polygon area, polygon perimeter, line length, centroid distance from reference point, distance from reference line.
Dialog: "Measurement on geographic CRS" combo โข Processing: advanced parameter GEODESIC (Auto/Always/Never)
Custom numbering
The progressive field is configurable along three axes, both in the dialog and in Processing:
| Parameter | Default | Example |
|---|---|---|
| Field name | sort_order | rank, order โ if the field already exists on the layer, it's overwritten instead of duplicated |
| Start value | 1 | 0 for zero-based numbering |
| Step | 1 | 10 โ 10, 20, 30โฆ, to leave room for future insertions |
Processing: advanced parameters ORDER_FIELD, START, STEP
Optionally, GeoSort can also add a sort_value field with the raw value of the criterion used (e.g. the area, the distance) โ "Add field with the criterion value used" checkbox in the dialog, ADD_VALUE_FIELD in Processing.
Selected features only
- With the Sort selected features only checkbox, the sort considers only the features currently selected on the layer; in Update current layer mode, the
sort_orderfield is written only on those features. - In the Processing Toolbox, the same behavior is offered by QGIS's native Selected features only toggle on the input layer (
QgsProcessingFeatureSourceDefinition(..., selectedFeaturesOnly=True)from PyQGIS).
Output
- Update current layer โ adds or updates the progressive field (default
sort_order). If the field already exists, confirmation is requested before overwriting it. The write is wrapped in a single undo command, undoable with Ctrl+Z even on thousands of features. - Create new in-memory layer โ creates a sorted copy without modifying the original.
Processing Toolbox & PyQGIS
GeoSort is also available in the Processing Toolbox: Toolbox โ GeoSort โ Sort features (GeoSort), usable in batch, in the graphical modeler and via headless PyQGIS:
import processing
result = processing.run("geosort:geosort_sort", {
'INPUT': layer,
'CRITERION': 0, # 0 = Table attribute (see criteria list above)
'ATTRIBUTE_FIELD': 'region',
'DIRECTION': True, # True = Ascending
'NULLS_LAST': True,
'NATURAL_SORT': False, # True = Natural Sort (digits as numbers)
'SECONDARY_CRITERION': 5, # 5 = Area (polygons)
'SECONDARY_DIRECTION': False, # False = Descending
'ADD_VALUE_FIELD': False,
'START': 0, # default 1
'STEP': 10, # default 1 โ 0, 10, 20, 30...
'ORDER_FIELD': 'rank', # default 'sort_order'
'OUTPUT': 'memory:'
})
output_layer = result['OUTPUT']
For the centroid_dist criterion (index 3) you can specify a reference point other than the origin:
result = processing.run("geosort:geosort_sort", {
'INPUT': layer,
'CRITERION': 3,
'REF_POINT': '500000,4649776 [EPSG:32633]',
'DIRECTION': True,
'OUTPUT': 'memory:'
})
To sort only the selected features from PyQGIS:
from qgis.core import QgsProcessingFeatureSourceDefinition
result = processing.run("geosort:geosort_sort", {
'INPUT': QgsProcessingFeatureSourceDefinition(layer.id(), selectedFeaturesOnly=True),
'CRITERION': 1,
'OUTPUT': 'memory:'
})
Typical use cases
| Goal | Recommended criterion |
|---|---|
| Number the plates of a regular map series, or plan a photogrammetric flight | Serpentine |
| Write a GeoPackage with faster spatial reads, or a "continuous path" atlas | Hilbert curve |
| Sort cadastral parcels by size, largest to smallest | Area (descending) |
| Number poles, mileage markers or supports along a road/railway/power line | Position along line |
| Sort buildings by proximity to a road or the coastline | Distance from line |
| Report sorted by region and, within each region, by area | Multi-criterion (region โ area) |
Sort alphanumeric codes like FILE1, FILE2, FILE10 in "natural" order | Attribute + Natural Sort |
| Radial ordering from an operations center or network node | Centroid โ distance |
FAQ / Troubleshooting
Some features always end up at the bottom โ why?
They have a NULL/empty geometry, or a type incompatible with the chosen criterion (e.g. points with an "area" criterion on a mixed-geometry layer). GeoSort doesn't raise an error in these cases: it simply pushes those features to the end of the order.
Why does the area order change when I reproject the layer to another CRS?
On geographic CRS (degrees) planar measurements aren't metrically meaningful. See Geodesic measurement: in Auto mode (default) GeoSort automatically switches to measuring on the ellipsoid when it detects a geographic CRS.
Why aren't Serpentine and Hilbert curve available as a secondary criterion?
Both require computing bands or extent over the entire layer before an order can be assigned โ incompatible with the "tie-break" role of a secondary criterion, which only operates within groups already ordered by the primary one. The same applies to criteria based on a reference line.
Can I undo the sort after applying it?
Yes, with Ctrl+Z: writing the sort_order field is wrapped in a single undo command, even on thousands of features.