0

I am looking to sort a GeoJSON file on a property so I can then slice it keeping only the top 5 features. For example, I wish to take this GeoJSON and sort the file in descending order by the incidents property:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                -75.1972382872565,
                39.9294288475177
              ],
              [
    ...
         [
                    -75.1972382872565,
                    39.9294288475177
                  ]
                ]
              ]
            ]
          },
          "properties": {
            "area": "",
            "location": "24th St. & Wolf St.",
            "perimeter": "81903.64182498",
            "dist_numc": "01",
            "sum_area": "",
            "area_sqmi": "216350124.153",
            "district_i": "",
            "objectid": "321",
            "globalid": "c040618c-ac93-4b22-b174-0ea08e0f805d",
            "district_": "1",
            "_feature_id": "1",
            "_feature_id_string": "Police_Districts.1",
            "dist_num": "1",
            "div_code": "SPD",
            "phone": "686-3010",
            "incidents": 11553
          }
        },
    ...

I'm looking to basically rearrange the order of the features array so that it's in descending order and the highest incident features are first.

How would I go about this?

I am used to creating a compare function and then sorting an array with something like this:

//FUNCTION: SORT
 export const compare = ( a, b ) => {
    if ( a.incidents < b.incidents ){
      return -1;
    }
    if ( a.incidents > b.incidents ){
      return 1;
    }
    return 0;
  }

But in this case I need to sort a JSON and only sort an array inside of it so I am unsure how to proceed.

1
  • 2
    This not really a GIS question, but pure JS one, so it's more suitable for StackOverflow site. – TomazicM Dec 22 '20 at 17:41
1

As .geojson is a javascript object, apply Array.prototype.sort() on feature, as follow:

const sortArrayofObjects = (property, order) => {
  var sortOrder = order === 'asc' ? 1 : -1;
  return function (a, b) {
      var result = (a['properties'][property] < b['properties'][property]) ? -1 : (a['properties'][property] > b['properties'][property]) ? 1 : 0;
      return result * sortOrder;
  }
};

const rearrangedFeatures = geojson['features'].sort(sortArrayofDicts('property_name', 'asc')) //ascending

This function sorts the array of features based on feature's property in a descending way. If you want an ascending method, use asc as the second argument.

Finally update .geojson.

3
  • I tried applying this but it's not ranking the geojson properly, unless I am applying it incorrectly? I put together a working example here where i click a button and the function fires but the first incidents value is not the lowest value. I am using the "asc" method as you do in the function. Example is here: codesandbox.io/s/slicing-geojson-qg1hb?file=/src/App.js – LoF10 Dec 22 '20 at 21:43
  • 1
    make this change inside function body as it is a nested object var result = (a['properties'][property] < b['properties'][property]) ? -1 : (a['properties'][property] > b['properties'][property]) ? 1 : 0; – ArashMad Dec 22 '20 at 22:25
  • Thank you sir, works great – LoF10 Dec 22 '20 at 22:27

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