通过将数组名称与对象匹配,将新值添加到GeoJSON的属性中

问题描述 投票:0回答:1

我有一个具有以下结构的对象数组:

 population = 
     [{county: "Alachua", population: 269956}
     {county: "Baker", population: 28355}
     {county: "Bay", population: 185287}
     {county: "Bradford", population: 27732}
     {county: "Brevard", population: 596849}...]

在我的GeoJSON文件的属性数组中,有一个名为County_1的键值对,它与我的人口数组中的县名匹配。

    {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": []
},
"properties": {
"OBJECTID_12_13": 1,
"OBJECTID": 1,
"DEPCODE": 21,
"COUNTY": "041",
"COUNTYNAME": "GILCHRIST",
"DATESTAMP": "2000-05-16T00:00:00.000Z",
"ShapeSTAre": 9908353355.45099,
"ShapeSTLen": 487300.011359113,
"OBJECTID_1": 21,
"County_1": "Gilchrist",
"State": "FL",
"OBJECTID_12": "1",
"DEPCODE_1": 21,
"COUNTYN": "41",
"PUIsTotal": 177,
"Age_0_4": 4,
"Age_5_14": 2,
"Age_15_24": 20,
"Age_25_34": 27,
"Age_35_44": 30,
"Age_45_54": 26,
"Age_55_64": 30,
"Age_65_74": 21,
"Age_75_84": 14,
"Age_85plus": 3,
"Age_Unkn": 0,
"PUIAgeRange": "0 to 89",
"PUIAgeMedian": 46,
"PUIFemale": 108,
"PUIMale": 69,
"PUISexUnkn": 0,
"PUIFLRes": 177,
"PUINotFLRes": 0,
"PUIFLResOut": 0,
"PUIContNo": 3,
"PUIContUnkn": 172,
"PUIAgeAvrg": "2",
"PUITravelNo": 11,
"PUITravelYes": 1,
"TPositive": 4,
"TNegative": 172,
"TInconc": 0,
"TPending": 1,
"T_Total_Res": 176,
"T_LabDOH_Res": 0,
"T_LabDOH_NonRes": 0,
"T_LabPrivate_Res": 4,
"T_LabPrivate_NonRes": 0,
"C_Female": 2,
"C_Male": 2,
"C_SexUnkn": 0,
"C_AllResTypes": 4,
"C_Age_0_4": 0,
"C_Age_5_14": 0,
"C_Age_15_24": 0,
"C_Age_25_34": 0,
"C_Age_35_44": 1,
"C_Age_45_54": 2,
"C_Age_55_64": 1,
"C_Age_65_74": 0,
"C_Age_75_84": 0,
"C_Age_85plus": 0,
"C_Age_Unkn": 0,
"C_AgeRange": "38 to 56",
"C_AgeMedian": 50,
"C_RaceWhite": 4,
"C_RaceBlack": 0,
"C_RaceOther": 0,
"C_RaceUnknown": 0,
"C_HispanicYES": 0,
"C_HispanicNO": 4,
"C_HispanicUnk": 0,
"C_EDYes_Res": 0,
"C_EDYes_NonRes": 0,
"C_HospYes_Res": 0,
"C_HospYes_NonRes": 0,
"C_NonResDeaths": 0,
"C_FLResDeaths": 0,
"CasesAll": 4,
"C_Men": 2,
"C_Women": 2,
"C_FLRes": 4,
"C_NotFLRes": 0,
"C_FLResOut": 0,
"T_NegRes": 172,
"T_NegNotFLRes": 0,
"T_total": 177,
"T_negative": 172,
"T_positive": 4,
"Deaths": 0,
"EverMon": 11,
"MonNow": 8,
"Shape__Area": 920490830.042053,
"Shape__Length": 148547.348006441
}
},
.....

如何使用Javascript将每个县的人口(使用人口数组)添加到我的GeoJSON文件中?

javascript geojson
1个回答
0
投票
  1. 下一次您需要明确定义的问题。该网站不会为您编写代码-我们只能为您指明正确的方向。
  2. 如果要素的几何中有非空坐标,则您的geojson会更有用。

由于您是新手,因此可以帮助您入门:

const populations = [
    {
        county: "Bay",
        population: 185287
    },
    {
        county: "Bradford",
        population: 27732
    }
];

const geojson = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": []
        },
        "properties": {
            "County_1": "Bradford"
        }
    }]
}

const getPopulation = (county_to_find) => (populations.find(({
    county
}) => county === county_to_find) || {}).population;


const assignPopulation = (my_geojson) => {
    const geojson_copy = my_geojson;
    for (let index = 0; index < geojson_copy.features.length; index++) {
        const feature = geojson_copy.features[index];
        const found_population = getPopulation(feature.properties.County_1);
        if (found_population) {
            geojson_copy.features[index].population = found_population;
        }
    }
    return geojson_copy
}

const enriched_geojson = assignPopulation(geojson);
console.info(enriched_geojson);
© www.soinside.com 2019 - 2024. All rights reserved.