我有一个geojson文件,我想知道从特定字段获取最大值和最小值。 这是我的geojson文件:
var commune = {"type": "FeatureCollection","features": [{
"type": "Feature",
"id":"",
"properties":
{
"nom":"Ain (01)",
"CODE_DEPT":"01",
"MARIE_2007":"242937",
"CELIB_2007":"150068",
"VEUF_2007":"31475",
"DIVOR_2007":"32177",
"MARIE_2011":"248332",
"CELIB_2011":"162880",
"VEUF_2011":"32059",
"DIVOR_2011":"36617",
"P11_POP":"603827",
"P07_POP":"574391",
"NAIS12":"7242",
"NAIS11":"7265",
"DECE12":"4577",
"DECE11":"4305",
"ENCTOT11":"4329",
"ENCTOT10":"4828",
"ENNTOT11":"27186"
},
"geometry": {
"type": "Polygon",
"coordinates": [[ [4.92452011,45.80404],[4.91857895,45.8093526],[4.91976994,45.82948887],[4.933601220000001,46.51245471000001],[4.93475135,46.51332864000001],[4.94725813,46.51446477999999],[4.94737243,46.50843994000001],[4.95280079,46.50448051],[4.96265289,46.5065581],[4.970033120000001,46.51410222],[4.98021847,46.51520743],[5.006366140000001,46.50970907],[5.01059281,46.51107719],[5.01244901,46.50297671],[5.01424122,46.50045988000001],[5.05090592,46.48625806],[5.0547548,46.4842847],[5.06721345,46.48577781],[5.07144999,46.48582009],[5.09526702,46.49798349],[5.112817620000001,46.49296539],[5.85065585,46.26170731],[5.86085144,46.26877548000001],[5.867698190000001,46.26822003],[5.86918965,46.26498538000001],[5.89197324,46.28359891000002],[5.89462537,46.28661469],[5.909195170000001,46.28394973],[5.91592224,46.29281579000001],[5.91892961,46.30930141],[5.9259046,46.31377309],[5.93732476,46.30977235000001],[5.94152708,46.30926953],[5.94401041,46.31220679],[5.97513797,46.35030159000001],[5.984121710000001,46.36265843],[6.01338356,46.37599880000001],[6.031020600000001,46.38810776],[6.052803630000001,46.41141842],[6.06057745,46.41751374],[6.0640057,46.41623036],[6.09777972,46.40858757000001],[5.01668616,45.80561526],[5.0122926,45.80523386],[5.00120298,45.81012438],[4.996959160000001,45.80914469],[4.99291668,45.80780002000001],[4.980338380000001,45.80584619],[4.97037128,45.80738551],[4.96692373,45.80904157],[4.951514780000001,45.80877107],[4.94683539,45.80941096],[4.92452011,45.80404] ]]
}
}, {
"type": "Feature",
"id":"",
"properties":
{
"nom":"Allier (03)",
"CODE_DEPT":"03",
"MARIE_2007":"142567",
"CELIB_2007":"94352",
"VEUF_2007":"30977",
"DIVOR_2007":"22520",
"MARIE_2011":"136735",
"CELIB_2011":"97444",
"VEUF_2011":"30224",
"DIVOR_2011":"24318",
"P11_POP":"342729",
"P07_POP":"343124",
"NAIS12":"3288",
"NAIS11":"3196",
"DECE12":"4346",
"DECE11":"4221",
"ENCTOT11":"1934",
"ENCTOT10":"2311",
"ENNTOT11":"15148"
},
"geometry": {
"type": "Polygon",
"coordinates": [[ [3.77334923,46.22719224],[3.78504663,46.22044738],[3.793573530000001,46.20769509],[3.78865075,46.19803992],[3.78979534,46.18748797],[3.803437390000001,46.16864814000001],[3.80973786,46.16310508000001],[3.80631061,46.16135084],[3.791626770000001,46.15659053000001],[3.79005403,46.15339828],[3.80126092,46.13143229],[3.79348746,46.112203],[3.80283828,46.11013932],[3.81835242,46.09301073],[3.82129095,46.0902456],[3.81658482,46.08456626000001],[3.81820687,46.07803905],[3.80561326,46.05306164000001],[3.80754254,46.04977906],[3.81587514,46.01910220000001],[3.82470625,46.00660501],[3.82733464,46.00021645000001],[3.83207127,45.99966784],[3.831742450000001,45.99944723],[3.82820481,45.99708246],[3.823224290000001,45.98815868],[3.80971781,45.98466361000001],[3.805150300000001,45.98329332],[3.781716630000001,45.97278891],[3.76002135,45.98068649],[3.75521038,45.97982028000001],[3.75064829,45.97003337],[3.742083070000001,45.9666941],[3.70961012,45.97413986000001],[3.70179941,45.96573133],[3.70450027,45.95554857],[3.69416443,45.94816809],[3.01428986,46.10527975],[3.00897068,46.11451573],[2.99427508,46.12320352],[2.980135230000001,46.12452646000001],[2.97175641,46.12169378],[2.96870044,46.12383515],[2.96440126,46.1290685],[2.96162502,46.13185592],[2.95213046,46.17133301000001],[2.94832477,46.1691754],[2.915704579999999,46.17413433],[2.913346020000001,46.17645055],[2.91076592,46.17865808],[2.919069420000001,46.18241252],[2.91992196,46.18947185],[2.91214414,46.20252971000001],[2.915816280000001,46.21278583],[2.923872110000001,46.21709903],[2.93274196,46.21868123000001],[2.93635804,46.22090247],[2.93912319,46.22374893000001],[2.93228017,46.23231447],[2.94598622,46.24167975],[2.94125766,46.24208205],[2.92590548,46.24938855000001],[2.90571884,46.24872598000001],[2.886322980000001,46.2531322],[2.86601825,46.25107846],[2.85976804,46.25659574000001],[2.85530571,46.25518568],[2.819168900000001,46.24179544],[2.819100750000001,46.23848478],[2.81768315,46.23211183],[2.5980228,46.59546224],[2.58332169,46.59434333],[2.57766143,46.60700494],[2.56839,46.60939027],[2.57292466,46.61045161000001],[2.58866016,46.61695059],[2.58593117,46.61976268],[2.58460338,46.62637012],[2.59632897,46.63672549999999],[2.59310888,46.64658424999999],[2.5890008,46.64809584],[2.56700624,46.65248768000001],[2.57056311,46.65927302000001],[2.606155,46.66930708],[2.60703665,46.6626474],[2.62392751,46.65663147000001],[2.63157087,46.66880445000001],[2.63185212,46.67221260000001],[2.62164231,46.67927395],[2.62348625,46.68930971000001],[2.62822321,46.69031437],[2.648089880000001,46.68895750000002],[2.65619177,46.69735612],[2.67426887,46.70227942],[2.67780141,46.7046171],[2.67617951,46.71083867],[2.6886199,46.72118332],[2.70316089,46.7222083],[2.70525376,46.7356157],[2.70484521,46.73903576],[2.73065122,46.74800057000001],[2.73453852,46.74622377],[2.74380344,46.73055578],[2.75512498,46.72392033],[2.758528720000001,46.71766317],[2.77137721,46.72126778],[2.7747598,46.7189517],[2.78275538,46.72246554],[2.79341957,46.73356149],[2.82257668,46.7354207],[2.827496070000001,46.73533674],[2.84117422,46.72732408000001],[2.84573507,46.72662012000001],[2.84382406,46.73923313000001],[2.8446387,46.7423694],[2.87535324,46.76064543],[2.88042249,46.77003211],[2.89434484,46.77417565],[2.89896986,46.77567349000001],[2.91093521,46.78135406],[2.90892388,46.78447702],[2.912953410000001,46.79019676],[2.93610446,46.79539505],[2.93989199,46.79318895],[2.94438298,46.79299427],[2.95250127,46.79269000000001],[2.95992679,46.80387786000001],[2.97990239,46.80342556],[2.99660733,46.79696079000001],[3.01615045,46.79984158000001],[3.027596990000001,46.79337813000001],[3.03207072,46.79491561],[3.03626634,46.77596958000002],[3.03754839,46.77283891],[3.04955339,46.75772189],[3.07961559,46.73996855],[3.875371550000001,46.27254662],[3.86891928,46.26367132],[3.85055547,46.26209134],[3.8456955,46.26181420000001],[3.80760052,46.25740175],[3.802695,46.23851437],[3.79880443,46.23991442000001],[3.778241490000001,46.24305742000001],[3.77332368,46.24247199],[3.76928843,46.24042644],[3.77334923,46.22719224] ]]
}
}, {
"type": "Feature",
"id":"",
"properties":
{
"nom":"Alpes-de-Haute Provence (04)",
"CODE_DEPT":"04",
"MARIE_2007":"64195",
"CELIB_2007":"41983",
"VEUF_2007":"11250",
"DIVOR_2007":"11881",
"MARIE_2011":"64385",
"CELIB_2011":"45210",
"VEUF_2011":"11200",
"DIVOR_2011":"13050",
"P11_POP":"160959",
"P07_POP":"156071",
"NAIS12":"1561",
"NAIS11":"1578",
"DECE12":"1674",
"DECE11":"1657",
"ENCTOT11":"1688",
"ENCTOT10":"1854",
"ENNTOT11":"11459"
},
"geometry": {
"type": "Polygon",
"coordinates": [[ [5.872687580000001,44.22420892],[5.86942591,44.2306969],[5.86181712,44.23514602000001],[5.859302830000001,44.24541601000001],[5.83536747,44.25742682],[5.83122619,44.25931444],[5.82845601,44.26089708],[5.826495130000001,44.26301187],[5.82290993,44.27290108],[5.82483743,44.27956321],[5.83588881,44.2908753],[5.836125290000001,44.29753744],[5.844586150000001,44.29997573],[5.84904057,44.30066089],[5.87211785,44.29290556],[5.87549409,44.29096914],[5.875075,44.277112],[5.88013816,44.27115134],[5.909235700000001,44.25229298],[5.91224047,44.24951149],[5.92182526,44.24840539],[5.9208934,44.25146034],[5.920626590000001,44.25753589000001],[5.92476028,44.26367414],[5.913979740000001,44.26846506],[5.91619447,44.28166044],[5.913504560000001,44.2882474],[5.92574479,44.29295018],[5.92447117,44.29931096000001],[5.923531300000001,44.30244818],[5.925112490000001,44.31059577],[5.925141350000001,44.3133943],[5.921117230000001,44.31517348],[5.908464190000001,44.31176166],[5.89714671,44.31756739],[5.905080480000001,44.32921786],[5.90310496,44.33585335],[5.91129844,44.33769153],[5.919483680000001,44.34057537000001],[5.92270858,44.34710399000001],[5.92603021,44.34962362000001],[5.937463340000001,44.35990719],[6.0801436,44.47435454],[6.08288177,44.47695302],[6.09157617,44.47684485],[6.102649110000001,44.47188266],[6.109721800000001,44.47553805],[6.115318970000001,44.47557949],[6.1276234,44.46819538],[6.13176763,44.46814211],[6.14985254,44.46244596],[6.15478321,44.46211236],[6.156217180000001,44.44546008],[6.15625493,44.4421095],[6.15980261,44.4368179],[6.162352930000001,44.43442406],[6.17038981,44.43746556],[6.173951,44.43958284],[6.184780330000001,44.42407738],[6.20290122,44.41224828000001],[6.22426095,44.38449989],[6.226633170000001,44.3813995],[6.2444005,44.38741166],[6.26249245,44.41225443],[6.244371970000001,44.44617938],[6.24025774,44.44774945],[6.236394670000001,44.46074448],[6.232769140000001,44.46287221],[6.233523670000001,44.46374579],[6.26217195,44.46566850000001],[6.27206808,44.47268828],[6.28956682,44.47886634],[6.29349633,44.48092984],[6.298022160000001,44.47479662],[6.32233675,44.46406677],[6.32710946,44.46346101],[6.32948298,44.46891148],[6.33766466,44.47081045000001],[6.33961699,44.47684433],[6.34023708,44.47990127],[6.347137220000001,44.49658708],[6.33555619,44.50728407],[6.332402200000002,44.5100013],[6.33482964,44.51139869],[6.347639010000002,44.51543375],[6.3573791,44.52263253],[6.36200897,44.52207565],[6.370458080000001,44.51037425],[6.394661890000001,44.49563229],[6.398808860000001,44.49415511],[6.40637697,44.48330274],[6.408373960000001,44.48048211],[6.409319850000001,44.47391836],[6.41591509,44.46913475],[6.43849225,44.4721208],[6.44514037,44.4670632],[6.45900093,44.46433590000001],[6.46807791,44.45235951],[6.47739516,44.45235924],[6.48165428,44.4540264],[6.49892832,44.44940962],[6.50797427,44.45085792],[6.512445540000001,44.44998529],[6.79605594,44.30018039],[6.78645035,44.28876425],[6.79034444,44.27234008],[6.78585561,44.27359467],[6.76309509,44.27842756],[6.756925250000001,44.26224102],[6.74344913,44.25866057],[6.73756498,44.253269],[6.72828649,44.25256165],[6.724335630000001,44.25088319],[6.72306129,44.22545705],[6.714085900000001,44.21822545],[6.717373080000001,44.20863774],[6.69847991,44.19014966000001],[6.70200432,44.18085031],[6.701234900000001,44.17753111],[6.697890380000001,44.17505331],[6.687248010000001,44.16830103],[6.70494711,44.14479261],[6.70796033,44.12438426],[6.71909018,44.11829841],[6.722173190000001,44.11340261],[6.72411684,44.11108212],[6.74074436,44.09859675],[6.74652858,44.08913345000001],[6.749818420000001,44.08660398],[6.752846010000001,44.08106922],[6.75649183,44.0795886],[6.75423983,44.07651917],[6.74769107,44.06347517],[6.74569609,44.04282669],[6.77114536,44.02649358],[6.78898218,44.02128084000001],[6.79557112,44.00811591],[6.7980875,44.00535629],[6.83808555,43.98981364],[6.834710880000001,43.98328049],[6.837539150000001,43.9807321],[6.845485200000001,43.97762125],[6.848077570000001,43.96522089],[6.84725661,43.96179612],[6.378919550000001,43.73937256],[6.376259410000001,43.74229801],[6.3544977,43.73650989],[6.329210580000001,43.74622928],[6.31280667,43.75880308],[6.26778983,43.77854497],[6.25711536,43.79759819],[6.249635250000002,43.80157651],[6.245318130000002,43.80240722],[6.233041730000001,43.79897888],[6.224190920000001,43.79983654],[6.21950238,43.79977951000001],[6.20731711,43.79528217],[6.18839017,43.77613922],[6.18503894,43.76827542],[6.18416308,43.76560659],[6.18273841,43.76216526],[6.16530613,43.74945410000001],[6.14060547,43.73760795000001],[6.1367238,43.73674526],[6.12862107,43.7367952],[6.12439326,43.73833798],[6.11574914,43.74555126],[6.11101581,43.74566225],[6.09529512,43.72961443],[6.09028867,43.72896913],[6.0041663,43.68827690000001],[5.987771890000001,43.69541687],[5.98679535,43.69856499],[5.98452905,43.71448643],[5.97990192,43.71547902],[5.96525787,43.72280033],[5.95622721,43.72068196],[5.94379994,43.72564961],[5.939519290000001,43.73171288],[5.94185867,43.73850070000001],[5.93802751,43.7485892],[5.92936515,43.74997453],[5.92565876,43.75596422],[5.75582118,43.73271739],[5.74802035,43.7414837],[5.71626352,43.75693727],[5.71355019,43.7811166],[5.705313,43.77958234000001],[5.69499369,43.78421214],[5.69067502,43.78512857],[5.684297220000001,43.78944639],[5.674481380000001,43.80735574],[5.67012574,43.80871511000001],[5.65703091,43.81850897],[5.65444738,43.82511108],[5.6505891,43.82524697],[5.64690908,43.82609707],[5.64309339,43.82692418],[5.63136974,43.82856736],[5.62769553,43.83079320000001],[5.599496320000001,43.82712809000001],[5.57188397,43.82934029],[5.56389637,43.82086008],[5.5512507,43.81721384],[5.54790067,43.81639536],[5.54455585,43.81723204],[5.55049408,43.85086677],[5.55750429,43.85544548],[5.56132404,43.85746532],[5.57273405,43.86350397],[5.59028282,43.8872275],[5.59136314,43.89066726],[5.60656102,43.91607995],[5.58337503,43.91591128],[5.56947257,43.93953949],[5.56763154,43.94263542],[5.56293885,43.94245084],[5.5449881,43.93905004],[5.54116737,43.94082725000001],[5.5252063,43.94685692],[5.51267137,43.94539725000001],[5.51157556,43.94851537],[5.50797078,43.95779797],[5.51316488,43.97335614],[5.5142541,43.97678752],[5.51954194,43.99395445],[5.90990333,44.19160469],[5.912350730000001,44.20478191],[5.884462340000001,44.21020085],[5.87672489,44.21433213],[5.87318422,44.22074555],[5.872687580000001,44.22420892] ]]
}}]};
如果您有任何输入来获取“P11_POP”字段的最大值和最小值,那将非常有用。
我已经尝试过这个功能,但我一定错过了一些东西:
function getMax(commune, prop) {
var max;
for (var i=0 ; i<commune.length ; i++) {
if (!max || parseInt(commune[i][prop]) > parseInt(max[prop]))
max = commune[i];
}
return max;
}
var maxPpg = getMax(commune.features.properties, "P11_POP");
console.log(maxPpg.nom);
谢谢
答案 0 :(得分:3)
这里存在许多问题(特别是Adrian May指出的问题)。我重写了这个函数,让你知道如何修复它。然而,由于硬编码的“属性”字符串,它不再是一般功能。我还使用了Math.max函数来实现更简单:
function getMax(commune, prop) {
var max = -10000000;
for (var i=0 ; i<commune.length ; i++) {
max = Math.max(parseInt(commune[i]["properties"][prop]), max);
}
return max;
}
var maxPpg = getMax(commune.features, "P11_POP");
alert(maxPpg);
答案 1 :(得分:0)
你想要commune.features [i] .properties。各种各样的commune.features.something和commune [i]
commune.features.map( function (i) { return i.properties.POP11 })
为您提供了一个可以添加的整数列表。
答案 2 :(得分:0)
使用Underscore.js可以轻松搞定。
_.max(commune.features, function(feature) {
return parseInt(feature.properties.P11_POP)
})
这将返回整个对象。你可以从中提取你想要的东西;没有必要重新发明轮子。 min
函数会为您提供最低价值。