How to select area value from geojson using javascript

I’m trying to right a javascript function that selects a polygon based on area value. The criterea is to select largest value if the heighest area value of a feature collection plygons is <= 20. Else, if the smalest area value a feature collection plygons is > 20, to select the smallest value. However, the results I’m getting are not the correct values.

Here is my geojson file



{
  "type": "FeatureCollection",
  "crs": { "type": "name", "properties": { "name": "EPSG:27700" } },
  "features": [
    {
      "type": "Feature",
      "id": 0,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514187.2405629959, 175713.65751711652],
            [514508.69769636169, 175828.21207611449],
            [514376.40576511156, 176106.02513174154],
            [513893.54021604639, 176218.47327330522],
            [513847.23804010917, 176006.80618330464],
            [514191.19706136081, 175887.74344518036],
            [514187.2405629959, 175713.65751711652]
          ]
        ]
      },
      "properties": { "FID": 0, "LSOA21CD": " ", "Area": 17.019149009300001 }
    },
    {
      "type": "Feature",
      "id": 1,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514187.2405629959, 175713.65751711652],
            [514184.58246479835, 175715.76393455453],
            [514182.5480875494, 175714.1718132291],
            [513880.31102292147, 175477.63845830224],
            [514230.88464073557, 175133.6794370506],
            [514535.15608261153, 175437.95087892748],
            [514187.2405629959, 175713.65751711652]
          ]
        ]
      },
      "properties": { "FID": 1, "LSOA21CD": " ", "Area": 18.966876811500001 }
    },
    {
      "type": "Feature",
      "id": 2,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514535.15608261153, 175437.95087892748],
            [514826.1983313635, 175709.14933798835],
            [514508.69769636169, 175828.21207611449],
            [514187.2405629959, 175713.65751711652],
            [514535.15608261153, 175437.95087892748]
          ]
        ]
      },
      "properties": { "FID": 2, "LSOA21CD": " ", "Area": 12.462057224500001 }
    },
    {
      "type": "Feature",
      "id": 3,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514535.15608261153, 175437.95087892748],
            [515004.79243855178, 175186.59620955214],
            [515104.01138698962, 175689.30554830283],
            [514826.1983313635, 175709.14933798835],
            [514535.15608261153, 175437.95087892748]
          ]
        ]
      },
      "properties": { "FID": 3, "LSOA21CD": " ", "Area": 17.1073790847 }
    },
    {
      "type": "Feature",
      "id": 4,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514696.78683049139, 175757.67865081504],
            [514826.1983313635, 175709.14933798835],
            [515104.01138698962, 175689.30554830283],
            [515381.82444261573, 176039.87916611508],
            [515025.50341344811, 176197.6531541571],
            [514696.78683049139, 175757.67865081504]
          ]
        ]
      },
      "properties": { "FID": 4, "LSOA21CD": " ", "Area": 19.0653141355 }
    },
    {
      "type": "Feature",
      "id": 5,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514342.30077446159, 176500.16576545127],
            [514599.64745966345, 176386.21623817086],
            [514918.25281787943, 176786.09847348183],
            [514596.61526418198, 176941.3235537447],
            [514342.30077446159, 176500.16576545127]
          ]
        ]
      },
      "properties": { "FID": 5, "LSOA21CD": " ", "Area": 16.029109829300001 }
    },
    {
      "type": "Feature",
      "id": 6,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514146.01893451158, 176159.67685941607],
            [514376.40576511156, 176106.02513174154],
            [514599.64745966624, 176386.21623817086],
            [514342.30077446159, 176500.16576545127],
            [514146.01893451158, 176159.67685941607]
          ]
        ]
      },
      "properties": { "FID": 6, "LSOA21CD": " ", "Area": 9.3259778883800006 }
    },
    {
      "type": "Feature",
      "id": 7,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514342.30077446159, 176500.16576545127],
            [514052.29053354729, 176628.57826018333],
            [514051.82526298054, 176627.37631121837],
            [513893.54021604639, 176218.47327330522],
            [514146.01893451158, 176159.67685941607],
            [514342.30077446159, 176500.16576545127]
          ]
        ]
      },
      "properties": { "FID": 7, "LSOA21CD": " ", "Area": 11.841352065100001 }
    },
    {
      "type": "Feature",
      "id": 8,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [513976.92131792102, 175961.91581790708],
            [513847.23804010917, 176006.80618330464],
            [513443.74764979444, 175590.08659986407],
            [513619.21886995155, 175544.88946740143],
            [513976.92131792102, 175961.91581790708]
          ]
        ]
      },
      "properties": { "FID": 8, "LSOA21CD": " ", "Area": 8.0748820024299999 }
    },
    {
      "type": "Feature",
      "id": 9,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [513619.21886995155, 175544.88946740143],
            [513880.31102292147, 175477.63845830224],
            [514182.5480875494, 175714.1718132291],
            [514184.58246479835, 175715.76393455453],
            [514187.2405629959, 175713.65751711652],
            [514191.19706136081, 175887.74344518036],
            [513976.92131792102, 175961.91581790708],
            [513619.21886995155, 175544.88946740143]
          ]
        ]
      },
      "properties": { "FID": 9, "LSOA21CD": " ", "Area": 14.7548642798 }
    },
    {
      "type": "Feature",
      "id": 10,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [513619.21886995155, 175544.88946740143],
            [513443.74764979444, 175590.08659986407],
            [513284.99733229354, 175411.49249267764],
            [513532.47008709889, 175269.49992844649],
            [513619.21886995155, 175544.88946740143]
          ]
        ]
      },
      "properties": { "FID": 10, "LSOA21CD": " ", "Area": 5.9491137500200004 }
    },
    {
      "type": "Feature",
      "id": 11,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514731.50091127306, 174880.67981645837],
            [514846.04212105088, 174822.79339861497],
            [515004.79243855178, 175186.59620955214],
            [514906.41315287724, 175239.24991174601],
            [514731.50091127306, 174880.67981645837]
          ]
        ]
      },
      "properties": { "FID": 11, "LSOA21CD": " ", "Area": 4.7672774107500002 }
    },
    {
      "type": "Feature",
      "id": 12,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514906.41315287724, 175239.24991174601],
            [514535.15608261153, 175437.95087892748],
            [514230.88464073557, 175133.6794370506],
            [514731.50091127306, 174880.67981645837],
            [514906.41315287724, 175239.24991174601]
          ]
        ]
      },
      "properties": { "FID": 12, "LSOA21CD": " ", "Area": 19.859035422800002 }
    },
    {
      "type": "Feature",
      "id": 13,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514917.07963621151, 176245.66169233806],
            [515025.50341344532, 176197.6531541571],
            [515320.21211783122, 176592.10942002572],
            [515205.25406012032, 176647.58917831443],
            [514917.07963621151, 176245.66169233806]
          ]
        ]
      },
      "properties": { "FID": 13, "LSOA21CD": " ", "Area": 5.9554811448900002 }
    },
    {
      "type": "Feature",
      "id": 14,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [515205.25406012032, 176647.58917831443],
            [514918.25281787943, 176786.09847348183],
            [514599.64745966345, 176386.21623817086],
            [514917.07963621151, 176245.66169233806],
            [515205.25406012032, 176647.58917831443]
          ]
        ]
      },
      "properties": { "FID": 14, "LSOA21CD": " ", "Area": 16.349272192400001 }
    },
    {
      "type": "Feature",
      "id": 15,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514594.66286209505, 175795.97513896786],
            [514696.78683049139, 175757.67865081504],
            [515025.50341344811, 176197.6531541571],
            [514917.07963621151, 176245.66169233806],
            [514594.66286209505, 175795.97513896786]
          ]
        ]
      },
      "properties": { "FID": 15, "LSOA21CD": " ", "Area": 6.0878052849299999 }
    },
    {
      "type": "Feature",
      "id": 16,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514917.07963621151, 176245.66169233806],
            [514599.64745966624, 176386.21623817086],
            [514376.40576511156, 176106.02513174154],
            [514508.69769636169, 175828.21207611449],
            [514594.66286209505, 175795.97513896786],
            [514917.07963621151, 176245.66169233806]
          ]
        ]
      },
      "properties": { "FID": 16, "LSOA21CD": " ", "Area": 16.9024733312 }
    },
    {
      "type": "Feature",
      "id": 17,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514040.11921087652, 176716.00784859248],
            [514051.82526298054, 176627.37631121837],
            [514052.29053354729, 176628.57826018333],
            [514342.30077446159, 176500.16576545127],
            [514596.61526418198, 176941.3235537447],
            [514294.3159738658, 177087.21581993997],
            [514040.11921087652, 176716.00784859248]
          ]
        ]
      },
      "properties": { "FID": 17, "LSOA21CD": " ", "Area": 18.067530070499998 }
    },
    {
      "type": "Feature",
      "id": 18,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [515631.83968934324, 176441.71524421126],
            [515320.21211783122, 176592.10942002572],
            [515025.50341344532, 176197.6531541571],
            [515381.82444261573, 176039.87916611508],
            [515640.5562169617, 176267.38469183631],
            [515631.83968934324, 176441.71524421126]
          ]
        ]
      },
      "properties": { "FID": 18, "LSOA21CD": " ", "Area": 19.848118984599999 }
    },
    {
      "type": "Feature",
      "id": 19,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [514154.5027258927, 175031.8368839249],
            [514230.88464073557, 175133.6794370506],
            [513880.31102292147, 175477.63845830224],
            [513619.21886995155, 175544.88946740143],
            [513532.47008709889, 175269.49992844649],
            [513897.89380010497, 175059.83058492094],
            [514154.5027258927, 175031.8368839249]
          ]
        ]
      },
      "properties": { "FID": 19, "LSOA21CD": " ", "Area": 19.771145470800001 }
    }
  ]
}

And here is the javascript function that I wrote.

const data = require("./area.json");

console.log(
  data.features.reduce((sampleArea, selectedArea) => {
    if (sampleArea[selectedArea.properties.Area] <= 20) {
      Math.max(sampleArea[selectedArea.properties.Area]);
    } else Math.min(sampleArea[selectedArea.properties.Area]);
    return selectedArea.properties.Area;
  }, {})
);

The answer I got is

PS E:\polygons\test> node script.js 19.7711454708

However the correct answer is

19.859035422800002

I have tried with a different geojson data, where the smallest area is 23.884209, but I got 35.5510673211

Hey @JohnH08, first we should clarify what your goal is. Are you trying to find a single match or multiple matches based on your area criteria?

If you’re trying to find any matches, .reduce() is not the method to use. .reduce() is meant to reduce a set of items to one single output, typically with an accumulator.

If you all want is a match/matches, you want to use the following: If you want to only get the first match found, you can use .find() and if you want multiple matches, you can use a method such as .filter().

Thanks for your quick reply @marcusparsons. I want to find a single match.

Do you want to find the feature that contains the minimum of all Area properties? Or just the value of that Area? Your very first feature is the one that will come back if you use .find() for a single match for an Area less than 20. But it’s a little bit of a different story to get the minimum one.

The value I want to select is one polygon everytime I run the function. When I get the data, I want the function to select the polygon with area <= 20. If there is a polygon with area value of 20, It is the best choice, if not any value closer to 20 can do. However, if the smallest value of the data is > 20, I want to select the smallest value.
For example, if I use the JSON file below, want to select the polygon with area value of 23.884209.

{
  "type": "FeatureCollection",
  "crs": { "type": "name", "properties": { "name": "EPSG:27700" } },
  "features": [
    {
      "type": "Feature",
      "id": 0,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511116.20154187363, 175591.38891189359],
            [510834.16784317698, 176081.48025717214],
            [510310.01419806667, 175638.14761470631],
            [510545.3343814835, 175302.48427530564],
            [511116.20154187363, 175591.38891189359]
          ]
        ]
      },
      "properties": {
        "FID": 0,
        "LSOA21CD": "E01002589",
        "Area": 32.076108972699998
      }
    },
    {
      "type": "Feature",
      "id": 1,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [510783.73012447171, 174962.43392336369],
            [511065.19299874175, 174560.95213407092],
            [511670.86817863118, 174855.8943842072],
            [511670.66317971516, 174856.27312230133],
            [511481.41056893207, 175205.91971606016],
            [510783.73012447171, 174962.43392336369]
          ]
        ]
      },
      "properties": {
        "FID": 1,
        "LSOA21CD": "E01002589",
        "Area": 30.825945690000001
      }
    },
    {
      "type": "Feature",
      "id": 2,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511481.41056893207, 175205.91971606016],
            [511239.10261550453, 175653.58672686294],
            [510545.3343814835, 175302.48427530564],
            [510783.73012447171, 174962.43392336369],
            [511481.41056893207, 175205.91971606016]
          ]
        ]
      },
      "properties": { "FID": 2, "LSOA21CD": "E01002589", "Area": 34.5472262567 }
    },
    {
      "type": "Feature",
      "id": 3,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [509921.09005591739, 174661.3783289697],
            [510037.89187975135, 174415.33425035886],
            [511065.19299874175, 174560.95213407092],
            [510783.73012447171, 174962.43392336369],
            [509921.09005591739, 174661.3783289697]
          ]
        ]
      },
      "properties": { "FID": 3, "LSOA21CD": " ", "Area": 35.042002141499999 }
    },
    {
      "type": "Feature",
      "id": 4,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [510783.73012447171, 174962.43392336369],
            [510545.3343814835, 175302.48427530564],
            [509695.40423042234, 175136.78752350435],
            [509921.09005591739, 174661.3783289697],
            [510783.73012447171, 174962.43392336369]
          ]
        ]
      },
      "properties": { "FID": 4, "LSOA21CD": " ", "Area": 40.328572387800001 }
    },
    {
      "type": "Feature",
      "id": 5,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [512255.23007169273, 176192.46413836256],
            [512704.89721376076, 174928.62694361061],
            [512879.52256301232, 174928.62694361061],
            [512509.1051555099, 176516.13011861779],
            [512255.23007169273, 176192.46413836256]
          ]
        ]
      },
      "properties": { "FID": 5, "LSOA21CD": " ", "Area": 37.180851322400002 }
    },
    {
      "type": "Feature",
      "id": 6,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511535.81214587204, 176132.41452069953],
            [512006.39581675828, 176891.83920336887],
            [511032.72720275354, 176987.08939386904],
            [510972.8317725556, 176891.67279308103],
            [511237.44918055646, 176422.57829708047],
            [511467.8129753042, 176022.67792819999],
            [511535.81214587204, 176132.41452069953]
          ]
        ]
      },
      "properties": { "FID": 6, "LSOA21CD": " ", "Area": 49.954760330200003 }
    },
    {
      "type": "Feature",
      "id": 7,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511824.04491915181, 175642.74510737322],
            [511535.81214587204, 176132.41452069953],
            [511239.10261550453, 175653.58672686294],
            [511481.41056893207, 175205.91971606016],
            [511824.04491915181, 175642.74510737322]
          ]
        ]
      },
      "properties": { "FID": 7, "LSOA21CD": " ", "Area": 27.126790326999998 }
    },
    {
      "type": "Feature",
      "id": 8,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511824.04491915181, 175642.74510737322],
            [512255.23007169273, 176192.46413836256],
            [512006.39581675828, 176891.83920336887],
            [511535.81214587204, 176132.41452069953],
            [511824.04491915181, 175642.74510737322]
          ]
        ]
      },
      "properties": { "FID": 8, "LSOA21CD": " ", "Area": 44.383529100700002 }
    },
    {
      "type": "Feature",
      "id": 9,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [512262.6076927837, 174897.68487835862],
            [511824.04491915181, 175642.74510737322],
            [511481.41056893207, 175205.91971606016],
            [511670.66317971516, 174856.27312230133],
            [512262.6076927837, 174897.68487835862]
          ]
        ]
      },
      "properties": { "FID": 9, "LSOA21CD": " ", "Area": 33.083362414500002 }
    },
    {
      "type": "Feature",
      "id": 10,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [512262.6076927837, 174897.68487835862],
            [512704.89721376076, 174928.62694361061],
            [512255.23007169273, 176192.46413836256],
            [511824.04491915181, 175642.74510737322],
            [512262.6076927837, 174897.68487835862]
          ]
        ]
      },
      "properties": { "FID": 10, "LSOA21CD": " ", "Area": 56.762039448000003 }
    },
    {
      "type": "Feature",
      "id": 11,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [510834.16784317698, 176081.48025717214],
            [510619.92045149952, 176453.77900336869],
            [510022.6422551889, 176048.05816549063],
            [510023.86025670078, 176046.32079452462],
            [510310.01419806667, 175638.14761470631],
            [510834.16784317698, 176081.48025717214]
          ]
        ]
      },
      "properties": {
        "FID": 11,
        "LSOA21CD": "E01002589",
        "Area": 32.577401352499997
      }
    },
    {
      "type": "Feature",
      "id": 12,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [509515.79092483688, 175515.14454592392],
            [509695.40423042234, 175136.78752350435],
            [510545.3343814835, 175302.48427530564],
            [510310.01419806667, 175638.14761470631],
            [509515.79092483688, 175515.14454592392]
          ]
        ]
      },
      "properties": { "FID": 12, "LSOA21CD": " ", "Area": 32.343756168200002 }
    },
    {
      "type": "Feature",
      "id": 13,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [510953.06521456596, 176182.04444542527],
            [511237.4491805546, 176422.57829708047],
            [510972.8317725556, 176891.67279308103],
            [510755.96415068023, 176546.19115562737],
            [510619.92045149952, 176453.77900336869],
            [510834.16784317698, 176081.48025717214],
            [510953.06521456596, 176182.04444542527]
          ]
        ]
      },
      "properties": { "FID": 13, "LSOA21CD": " ", "Area": 23.8842091629 }
    },
    {
      "type": "Feature",
      "id": 14,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511239.10261550453, 175653.58672686294],
            [511467.8129753042, 176022.67792819999],
            [511237.44918055646, 176422.57829708047],
            [510953.06521456596, 176182.04444542527],
            [510834.16784317698, 176081.48025717214],
            [511116.20154187363, 175591.38891189359],
            [511239.10261550453, 175653.58672686294]
          ]
        ]
      },
      "properties": { "FID": 14, "LSOA21CD": " ", "Area": 28.2472585804 }
    },
    {
      "type": "Feature",
      "id": 15,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511346.81505686138, 177172.97812385298],
            [511032.72720275354, 176987.08939386904],
            [512006.39581675828, 176891.83920336887],
            [512485.38870628458, 177234.09983179346],
            [511346.81505686138, 177172.97812385298]
          ]
        ]
      },
      "properties": { "FID": 15, "LSOA21CD": " ", "Area": 28.566150963999998 }
    },
    {
      "type": "Feature",
      "id": 16,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [512485.38870628458, 177234.09983179346],
            [512929.28317776043, 177551.28115970641],
            [512318.60477450956, 177669.71575912274],
            [512064.72303233296, 177597.86243586615],
            [511346.81505686138, 177172.97812385298],
            [512485.38870628458, 177234.09983179346]
          ]
        ]
      },
      "properties": { "FID": 16, "LSOA21CD": " ", "Area": 40.436474060199998 }
    },
    {
      "type": "Feature",
      "id": 17,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511346.81505686138, 177172.97812385298],
            [510560.69539462589, 177441.62779398635],
            [510263.01747861132, 177185.22454699688],
            [510903.59826454427, 176781.38013847359],
            [511032.72720275354, 176987.08939386904],
            [511346.81505686138, 177172.97812385298]
          ]
        ]
      },
      "properties": { "FID": 17, "LSOA21CD": " ", "Area": 33.538415608900003 }
    },
    {
      "type": "Feature",
      "id": 18,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [511346.81505686138, 177172.97812385298],
            [512064.72303233296, 177597.86243586615],
            [512069.89594375808, 177600.92395487241],
            [510831.64346725307, 177675.00743637234],
            [510560.69539462589, 177441.62779398635],
            [511346.81505686138, 177172.97812385298]
          ]
        ]
      },
      "properties": { "FID": 18, "LSOA21CD": " ", "Area": 41.986388527800003 }
    },
    {
      "type": "Feature",
      "id": 19,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [512647.26189985964, 176588.49793708883],
            [512485.38870628458, 177234.09983179346],
            [512006.39581675828, 176891.83920336887],
            [512255.23007169273, 176192.46413836256],
            [512509.1051555099, 176516.13011861779],
            [512647.26189985964, 176588.49793708883]
          ]
        ]
      },
      "properties": { "FID": 19, "LSOA21CD": " ", "Area": 35.5510673211 }
    }
  ]
}

Here’s what I did to solve this problem:

  • I used filter() to go through every feature in the GeoJSON data.
  • I tested each Area property against a given value combined with a marginOfError (this is where you would decide how close to exact you want to match to a given value i.e. 20)
  • If a property fit in the criteria, I set the feature and area to their own variables and return them.

The first problem I see is that you are not returning anything into the reduce callback but the selectedArea value. In fact the result u got is the last one in the json data.

You should return a value in the if else statement

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.