18

I have a shapefile and I want to change the order of the features. Is that possible?

I want to sort the features in the shapefile because I'm embedding this shapefile in a mobile app. The app will display a list of features, and although I could do the sorting at load time, I'd prefer to have them sorted.

  • 3
    The goal is legitimate, the mean is not. If you need them sorted, use the result of a query, don't assume they are and will remain sorted. – GuillaumeC Feb 8 '11 at 7:49
6

You can always open the attribute table in ArcMap and right click the columns header and select Sort Ascending.../Descending... for single fields.

For multiple fields you would instead click Advanced Sorting to select multiple fields.

NOTE: Adding, for instance, a sequential ID field based on the current sorting (which is temporary, stored only in this map/mxd) when using the above method will be ordered by the original OID/ObjectID of the feature class.

There is an ArcScripts available that will sort records permanently and output a new shapefile: http://arcscripts.esri.com/details.asp?dbid=16771

I do hope that when you state 'order of features' that this is the order found in the attribute table and not drawing order for the table of contents/symbology levels.

16

I don't know about Arc*, but for QGIS there is a Sort plugin: http://michaelminn.com/linux/mmqgis/

enter image description here

7

Here is a solution to rewrite a new and sorted shapefile using the GDAL/OGR command-line tool ogr2ogr.

For example, a shapefile orig.shp has a numeric field volume to sort on. This particular SQL statement does a reverse sort (with DESC) so that features with large volume values are drawn before (underneath) features with small values:

ogr2ogr -sql "SELECT * FROM orig ORDER BY volume DESC" sorted.shp orig.shp
5

If you have access to an arcinfo license, you can use the Sort in the (Data Management toolbox) which writes the records into a new shapefiles or gdb fc based spatial or attribute sorting

  • unfortunately it works with FGDB, not with shapes – tato Feb 7 '11 at 21:11
  • 1
    tato, the tool works with fgdb or shapefile as input and output, i've no idea what your comment above means. – gotchula Feb 8 '11 at 0:05
4

An utility software of MapServer (free and open-source) can sort shapes according to attributes values :

http://mapserver.org/utilities/sortshp.html

2

A very, very FAST and easy method to sort a shapefile layer( using various fields) .

1 - Export shapefile to CSV (select good separator, like semicolon) and add GEOMETRY using "layer options" -> "AS WKT"

enter image description here

2 - Open file.csv with LIBREOFFICE (calc) and use MENU->DATA->SORT (very intuitive, powerfull and VERY FAST (Sort on the fly 50000 features using 3 columns like sort-key, ascending and descending options, and i have a very slow notebook ;) )

3 - Save again file from LIBREOFFICE (calc) as "Text CSV" ( mark "Edit filter settings" and choose separator as 'semicolon', don't matter warning from libreoffice, save like CSV and select 'semicolon' as separator )

4 - From Qgis open the new file.csv (and sorted) from 'Add Layer" menu -> Add Delimiter Text Layer.

PROS: - Very,very fast, sort from various fields - Works ok with UTF_8 encoded data

CONS: - Need LIBREOFFICE (but is it open source software)

Alternative method to sort (sustitute points 2 and 3, and is the most fast) using console (BASH)

  • Open console and go where you have file.csv

    Suppose you want sort file with the key:

    field6(desc)+field1(ascen)+field3(desce)

    so the command will be:

    sort -t';' -k6,6r -k1,1 -k3,3r file.csv > file_sort.csv

    NOTES:

    • you can add -kn,n for every column number 'n' (field) you needed add to your "sort key"
    • adding 'r' after every n,n will sort in reverse mode (descent)
    • Must pass the character separator used into csv file with -t param
  • Alternative method to sort without LibreOffice, using bash (console): – Juanma Font Nov 24 '16 at 11:59
-3
  1. Export data as csv
  2. Open with Excel go to Data---- Sort ---Expand the selection and it's done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.