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