skip to Main Content

I am creating a map based on online datasets using the following code:

<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.1.0/chroma.min.js" integrity="sha512-yocoLferfPbcwpCMr8v/B0AB4SWpJlouBwgE0D3ZHaiP1nuu5djZclFEIj9znuqghaZ3tdCMRrreLoM8km+jIQ==" crossorigin="anonymous"></script>

<script>
const API_IRIS_CONTOUR = new Request("https://www.loicbertrand.eu/accidentologie/json/iris_75.json");
const API_INSEE_STAT = new Request("https://www.loicbertrand.eu/accidentologie/json/base-ic-logement-2020_paris.json");
var layer_iris = L.layerGroup()
var color_scale = chroma.scale(['yellow', 'red']);
const maxval = 400
var insee_value;    
const field = "P20_RP_VOIT1P"
const vniris = [751010101, 751010102, 751010103, 751010104, 751010105, 751010199, 751010201, 751010202, 751010203, 751010204, 751010205, 751010206, 751010301, 751010302, 751010303, 751010401, 751010402, 751020501, 751020502, 751020503, 751020601, 751020602, 751020701, 751020702, 751020703, 751020704, 751020801, 751020802, 751020803, 751020804, 751020805, 751030901, 751030902, 751030903, 751030904, 751030905, 751031001, 751031002, 751031003, 751031004, 751031101, 751031102, 751031103, 751031104, 751031201, 751031202, 751031203, 751031204, 751041301, 751041302, 751041303, 751041304, 751041305, 751041399, 751041401, 751041402, 751041403, 751041404, 751041499, 751041501, 751041502, 751041503, 751041504, 751041588, 751041599, 751041601, 751041602, 751041603, 751041699, 751051701, 751051702, 751051703, 751051704, 751051705, 751051706, 751051799, 751051801, 751051802, 751051803, 751051804, 751051805, 751051806, 751051807, 751051808, 751051809, 751051899, 751051901, 751051902, 751051903, 751051904, 751051905, 751051906, 751051907, 751051908, 751052001, 751052002, 751052003, 751052004, 751052005, 751052099, 751062101, 751062102, 751062103, 751062199, 751062201, 751062202, 751062203, 751062204, 751062205, 751062301, 751062302, 751062303, 751062304, 751062305, 751062306, 751062307, 751062308, 751062309, 751062310, 751062311, 751062401, 751062402, 751062499, 751072501, 751072502, 751072503, 751072504, 751072505, 751072506, 751072507, 751072508, 751072599, 751072601, 751072602, 751072603, 751072604, 751072605, 751072606, 751072699, 751072701, 751072702, 751072703, 751072704, 751072705, 751072706, 751072707, 751072801, 751072802, 751072803, 751072804, 751072805, 751072806, 751072807, 751072808, 751072809, 751072810, 751072811, 751072812, 751072899, 751082901, 751082902, 751082903, 751082904, 751082905, 751082906, 751082907, 751082999, 751083001, 751083002, 751083003, 751083004, 751083005, 751083006, 751083101, 751083102, 751083103, 751083104, 751083105, 751083106, 751083107, 751083108, 751083201, 751083202, 751083203, 751083204, 751083205, 751083206, 751083207, 751083208, 751083209, 751083210, 751093301, 751093302, 751093303, 751093304, 751093305, 751093306, 751093307, 751093308, 751093309, 751093310, 751093311, 751093401, 751093402, 751093403, 751093404, 751093405, 751093406, 751093407, 751093408, 751093409, 751093501, 751093502, 751093503, 751093504, 751093601, 751093602, 751093603, 751093604, 751093605, 751093606, 751093607, 751093608, 751093609, 751093610, 751103701, 751103702, 751103703, 751103704, 751103705, 751103706, 751103707, 751103708, 751103709, 751103710, 751103711, 751103712, 751103801, 751103802, 751103803, 751103804, 751103805, 751103806, 751103901, 751103902, 751103903, 751103904, 751103905, 751103906, 751103907, 751103908, 751103909, 751103910, 751103988, 751104001, 751104002, 751104003, 751104004, 751104005, 751104006, 751104007, 751104008, 751104009, 751104010, 751104011, 751104012, 751104088, 751114101, 751114102, 751114103, 751114104, 751114105, 751114106, 751114107, 751114108, 751114109, 751114110, 751114111, 751114112, 751114113, 751114114, 751114115, 751114201, 751114202, 751114203, 751114204, 751114205, 751114206, 751114207, 751114208, 751114209, 751114210, 751114211, 751114212, 751114213, 751114214, 751114301, 751114302, 751114303, 751114304, 751114305, 751114306, 751114307, 751114308, 751114309, 751114310, 751114311, 751114312, 751114313, 751114314, 751114315, 751114316, 751114317, 751114318, 751114319, 751114320, 751114321, 751114401, 751114402, 751114403, 751114404, 751114405, 751114406, 751114407, 751114408, 751114409, 751114410, 751114411, 751114412, 751114413, 751124501, 751124502, 751124503, 751124504, 751124505, 751124506, 751124507, 751124508, 751124509, 751124510, 751124511, 751124512, 751124513, 751124514, 751124577, 751124601, 751124602, 751124603, 751124604, 751124605, 751124606, 751124608, 751124609, 751124610, 751124611, 751124612, 751124613, 751124614, 751124615, 751124616, 751124617, 751124618, 751124619, 751124620, 751124621, 751124622, 751124623, 751124624, 751124677, 751124701, 751124702, 751124703, 751124704, 751124705, 751124706, 751124707, 751124708, 751124799, 751124801, 751124802, 751124803, 751124804, 751124805, 751124806, 751124807, 751124808, 751124809, 751124810, 751124811, 751124812, 751124813, 751124888, 751124899, 751134901, 751134902, 751134903, 751134904, 751134905, 751134906, 751134907, 751134908, 751134999, 751135001, 751135002, 751135003, 751135004, 751135005, 751135006, 751135007, 751135008, 751135009, 751135010, 751135011, 751135012, 751135013, 751135014, 751135015, 751135016, 751135017, 751135018, 751135019, 751135020, 751135021, 751135023, 751135024, 751135025, 751135026, 751135027, 751135028, 751135029, 751135030, 751135031, 751135032, 751135099, 751135101, 751135102, 751135103, 751135104, 751135105, 751135106, 751135107, 751135108, 751135109, 751135110, 751135111, 751135112, 751135113, 751135114, 751135115, 751135116, 751135117, 751135118, 751135119, 751135120, 751135121, 751135122, 751135123, 751135124, 751135125, 751135201, 751135202, 751135203, 751135204, 751135205, 751135206, 751135207, 751145301, 751145302, 751145303, 751145304, 751145305, 751145306, 751145307, 751145308, 751145309, 751145310, 751145311, 751145401, 751145402, 751145403, 751145404, 751145405, 751145406, 751145407, 751145501, 751145502, 751145503, 751145504, 751145505, 751145506, 751145507, 751145508, 751145509, 751145510, 751145511, 751145512, 751145513, 751145514, 751145515, 751145516, 751145601, 751145602, 751145603, 751145604, 751145605, 751145606, 751145607, 751145608, 751145609, 751145610, 751145611, 751145612, 751145613, 751145614, 751145615, 751145616, 751145617, 751145618, 751145619, 751145620, 751145621, 751145622, 751145623, 751145624, 751145625, 751155701, 751155702, 751155703, 751155704, 751155705, 751155706, 751155707, 751155708, 751155709, 751155710, 751155711, 751155712, 751155713, 751155714, 751155715, 751155716, 751155717, 751155718, 751155719, 751155720, 751155721, 751155722, 751155723, 751155724, 751155725, 751155726, 751155727, 751155728, 751155729, 751155730, 751155731, 751155732, 751155733, 751155801, 751155802, 751155803, 751155804, 751155805, 751155806, 751155807, 751155808, 751155809, 751155810, 751155811, 751155812, 751155813, 751155814, 751155815, 751155816, 751155817, 751155818, 751155819, 751155901, 751155902, 751155903, 751155904, 751155905, 751155906, 751155907, 751155908, 751155909, 751155910, 751155911, 751155912, 751155913, 751155914, 751155915, 751155916, 751155917, 751155918, 751155919, 751155920, 751155999, 751156001, 751156002, 751156003, 751156004, 751156005, 751156006, 751156007, 751156008, 751156009, 751156010, 751156012, 751156013, 751156014, 751156015, 751156016, 751156017, 751156018, 751156019, 751156020, 751156021, 751156022, 751156023, 751156099, 751166101, 751166102, 751166103, 751166104, 751166105, 751166106, 751166107, 751166108, 751166109, 751166110, 751166111, 751166112, 751166113, 751166114, 751166115, 751166116, 751166117, 751166118, 751166119, 751166120, 751166121, 751166122, 751166123, 751166124, 751166125, 751166126, 751166127, 751166128, 751166129, 751166130, 751166131, 751166177, 751166199, 751166201, 751166202, 751166203, 751166204, 751166205, 751166206, 751166207, 751166208, 751166209, 751166210, 751166211, 751166212, 751166213, 751166214, 751166215, 751166216, 751166217, 751166218, 751166219, 751166220, 751166221, 751166222, 751166223, 751166277, 751166299, 751166301, 751166302, 751166303, 751166304, 751166305, 751166306, 751166307, 751166308, 751166309, 751166310, 751166311, 751166377, 751166401, 751166402, 751166403, 751166404, 751166405, 751166406, 751166407, 751166409, 751166410, 751166411, 751166412, 751166413, 751166499, 751176501, 751176502, 751176503, 751176504, 751176505, 751176506, 751176507, 751176508, 751176509, 751176510, 751176511, 751176512, 751176513, 751176514, 751176515, 751176516, 751176601, 751176602, 751176603, 751176604, 751176605, 751176606, 751176607, 751176608, 751176609, 751176610, 751176611, 751176612, 751176613, 751176614, 751176615, 751176616, 751176701, 751176702, 751176703, 751176704, 751176705, 751176706, 751176707, 751176708, 751176709, 751176710, 751176711, 751176712, 751176713, 751176714, 751176715, 751176716, 751176801, 751176802, 751176803, 751176804, 751176805, 751176806, 751176807, 751176808, 751176809, 751176810, 751176811, 751176812, 751176813, 751176814, 751176815, 751176816, 751176817, 751186901, 751186902, 751186903, 751186904, 751186905, 751186906, 751186907, 751186908, 751186909, 751186910, 751186911, 751186912, 751186913, 751186914, 751186915, 751186916, 751186917, 751186918, 751186919, 751186920, 751186921, 751186922, 751186923, 751186924, 751186925, 751186926, 751186927, 751186928, 751186929, 751187001, 751187002, 751187003, 751187004, 751187005, 751187006, 751187007, 751187008, 751187009, 751187010, 751187011, 751187012, 751187013, 751187014, 751187015, 751187016, 751187017, 751187018, 751187019, 751187020, 751187021, 751187022, 751187023, 751187024, 751187025, 751187026, 751187027, 751187101, 751187102, 751187103, 751187104, 751187105, 751187106, 751187107, 751187108, 751187109, 751187110, 751187201, 751187202, 751187203, 751187204, 751187205, 751187206, 751187207, 751187208, 751187209, 751197301, 751197302, 751197303, 751197304, 751197305, 751197306, 751197307, 751197308, 751197309, 751197310, 751197311, 751197312, 751197313, 751197314, 751197315, 751197316, 751197317, 751197318, 751197388, 751197401, 751197402, 751197403, 751197404, 751197405, 751197406, 751197407, 751197408, 751197409, 751197410, 751197411, 751197412, 751197413, 751197488, 751197501, 751197502, 751197503, 751197504, 751197505, 751197506, 751197507, 751197508, 751197509, 751197510, 751197511, 751197512, 751197513, 751197514, 751197515, 751197516, 751197517, 751197518, 751197519, 751197520, 751197521, 751197522, 751197523, 751197524, 751197525, 751197601, 751197602, 751197603, 751197604, 751197605, 751197606, 751197607, 751197608, 751197609, 751197610, 751197611, 751197612, 751197613, 751197614, 751197615, 751197616, 751197617, 751207701, 751207702, 751207703, 751207704, 751207705, 751207706, 751207707, 751207708, 751207709, 751207710, 751207711, 751207712, 751207713, 751207714, 751207801, 751207802, 751207803, 751207804, 751207805, 751207806, 751207807, 751207808, 751207809, 751207810, 751207811, 751207812, 751207813, 751207814, 751207815, 751207816, 751207901, 751207902, 751207903, 751207904, 751207905, 751207906, 751207907, 751207908, 751207909, 751207910, 751207911, 751207912, 751207913, 751207914, 751207915, 751207916, 751207917, 751208001, 751208002, 751208003, 751208004, 751208005, 751208006, 751208007, 751208008, 751208009, 751208010, 751208011, 751208012, 751208013, 751208014, 751208015, 751208016, 751208017, 751208018, 751208019, 751208020, 751208021, 751208022, 751208023, 751208024, 751208025, 751208026]

vniris.forEach((niris) => {
    fetch(API_INSEE_STAT)
    .then(response => response.json())
    .then((data) => {
        data.forEach(function(item){
    if(item.IRIS == niris){
        //console.log(item[field])
        insee_value = item[field];
    }
        });
    })
    .catch(console.error)
    .then(() => {
        fetch(API_IRIS_CONTOUR)
        .then(response => response.json())
        .then(data => {
    //console.log("value now needed: " + insee_value)
    process_iris_contour(data, niris, color_scale(insee_value/maxval).hex())
        })
        .catch(console.error)
    });
});
</script>

Where the scripts used are:

function switch_coordinate_columns3D(arr) {
    var result = []
    result = structuredClone(arr);
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr[i].length; j++) {
            result[i][j][0] = arr[i][j][1];
            result[i][j][1] = arr[i][j][0];
        }
    }
    return result
}

function process_iris_contour(data, niris, color) {
    // polygon requires latlngs
    data.forEach(function(item){
       if(item.code_iris == niris){
            L.polygon(switch_coordinate_columns3D(item.geo_shape.geometry.coordinates), {stroke: false, fillColor: color}).addTo(layer_iris);
       }
    });
}

As vniris contains hundreds of values, the map requires very long time to complete, due to the multiple fetch statements.

How could I make this process faster?

Edits

  • Added full paths to json datasets
  • Added the reference to outer scripts

2

Answers


  1. Chosen as BEST ANSWER

    I could solve my issue by using the advice from @Tim Robert and combining it with a suggestion in the post How to return the Promise.all fetch api json data?

    const API_IRIS_CONTOUR = new Request("json/iris_75.json");
    const API_INSEE_STAT = new Request("json/base-ic-logement-2020_paris.json");
    var layer_iris = L.layerGroup()
    var color_scale = chroma.scale(['yellow', 'red']);
    const maxval = 400
    var insee_value
    const field = "P20_RP_VOIT1P"
          
    const vniris = [751010101, 751010102, 751010103, 751010104, 751010105, 751010199, 751010201, 751010202, 751010203, 751010204, 751010205, 751010206, 751010301, 751010302, 751010303, 751010401, 751010402, 751020501, 751020502, 751020503, 751020601, 751020602, 751020701, 751020702, 751020703, 751020704, 751020801, 751020802, 751020803, 751020804, 751020805, 751030901, 751030902, 751030903, 751030904, 751030905, 751031001, 751031002, 751031003, 751031004, 751031101, 751031102, 751031103, 751031104, 751031201, 751031202, 751031203, 751031204, 751041301, 751041302, 751041303, 751041304, 751041305, 751041399, 751041401, 751041402, 751041403, 751041404, 751041499, 751041501, 751041502, 751041503, 751041504, 751041588, 751041599, 751041601, 751041602, 751041603, 751041699, 751051701, 751051702, 751051703, 751051704, 751051705, 751051706, 751051799, 751051801, 751051802, 751051803, 751051804, 751051805, 751051806, 751051807, 751051808, 751051809, 751051899, 751051901, 751051902, 751051903, 751051904, 751051905, 751051906, 751051907, 751051908, 751052001, 751052002, 751052003, 751052004, 751052005, 751052099, 751062101, 751062102, 751062103, 751062199, 751062201, 751062202, 751062203, 751062204, 751062205, 751062301, 751062302, 751062303, 751062304, 751062305, 751062306, 751062307, 751062308, 751062309, 751062310, 751062311, 751062401, 751062402, 751062499, 751072501, 751072502, 751072503, 751072504, 751072505, 751072506, 751072507, 751072508, 751072599, 751072601, 751072602, 751072603, 751072604, 751072605, 751072606, 751072699, 751072701, 751072702, 751072703, 751072704, 751072705, 751072706, 751072707, 751072801, 751072802, 751072803, 751072804, 751072805, 751072806, 751072807, 751072808, 751072809, 751072810, 751072811, 751072812, 751072899, 751082901, 751082902, 751082903, 751082904, 751082905, 751082906, 751082907, 751082999, 751083001, 751083002, 751083003, 751083004, 751083005, 751083006, 751083101, 751083102, 751083103, 751083104, 751083105, 751083106, 751083107, 751083108, 751083201, 751083202, 751083203, 751083204, 751083205, 751083206, 751083207, 751083208, 751083209, 751083210, 751093301, 751093302, 751093303, 751093304, 751093305, 751093306, 751093307, 751093308, 751093309, 751093310, 751093311, 751093401, 751093402, 751093403, 751093404, 751093405, 751093406, 751093407, 751093408, 751093409, 751093501, 751093502, 751093503, 751093504, 751093601, 751093602, 751093603, 751093604, 751093605, 751093606, 751093607, 751093608, 751093609, 751093610, 751103701, 751103702, 751103703, 751103704, 751103705, 751103706, 751103707, 751103708, 751103709, 751103710, 751103711, 751103712, 751103801, 751103802, 751103803, 751103804, 751103805, 751103806, 751103901, 751103902, 751103903, 751103904, 751103905, 751103906, 751103907, 751103908, 751103909, 751103910, 751103988, 751104001, 751104002, 751104003, 751104004, 751104005, 751104006, 751104007, 751104008, 751104009, 751104010, 751104011, 751104012, 751104088, 751114101, 751114102, 751114103, 751114104, 751114105, 751114106, 751114107, 751114108, 751114109, 751114110, 751114111, 751114112, 751114113, 751114114, 751114115, 751114201, 751114202, 751114203, 751114204, 751114205, 751114206, 751114207, 751114208, 751114209, 751114210, 751114211, 751114212, 751114213, 751114214, 751114301, 751114302, 751114303, 751114304, 751114305, 751114306, 751114307, 751114308, 751114309, 751114310, 751114311, 751114312, 751114313, 751114314, 751114315, 751114316, 751114317, 751114318, 751114319, 751114320, 751114321, 751114401, 751114402, 751114403, 751114404, 751114405, 751114406, 751114407, 751114408, 751114409, 751114410, 751114411, 751114412, 751114413, 751124501, 751124502, 751124503, 751124504, 751124505, 751124506, 751124507, 751124508, 751124509, 751124510, 751124511, 751124512, 751124513, 751124514, 751124577, 751124601, 751124602, 751124603, 751124604, 751124605, 751124606, 751124608, 751124609, 751124610, 751124611, 751124612, 751124613, 751124614, 751124615, 751124616, 751124617, 751124618, 751124619, 751124620, 751124621, 751124622, 751124623, 751124624, 751124677, 751124701, 751124702, 751124703, 751124704, 751124705, 751124706, 751124707, 751124708, 751124799, 751124801, 751124802, 751124803, 751124804, 751124805, 751124806, 751124807, 751124808, 751124809, 751124810, 751124811, 751124812, 751124813, 751124888, 751124899, 751134901, 751134902, 751134903, 751134904, 751134905, 751134906, 751134907, 751134908, 751134999, 751135001, 751135002, 751135003, 751135004, 751135005, 751135006, 751135007, 751135008, 751135009, 751135010, 751135011, 751135012, 751135013, 751135014, 751135015, 751135016, 751135017, 751135018, 751135019, 751135020, 751135021, 751135023, 751135024, 751135025, 751135026, 751135027, 751135028, 751135029, 751135030, 751135031, 751135032, 751135099, 751135101, 751135102, 751135103, 751135104, 751135105, 751135106, 751135107, 751135108, 751135109, 751135110, 751135111, 751135112, 751135113, 751135114, 751135115, 751135116, 751135117, 751135118, 751135119, 751135120, 751135121, 751135122, 751135123, 751135124, 751135125, 751135201, 751135202, 751135203, 751135204, 751135205, 751135206, 751135207, 751145301, 751145302, 751145303, 751145304, 751145305, 751145306, 751145307, 751145308, 751145309, 751145310, 751145311, 751145401, 751145402, 751145403, 751145404, 751145405, 751145406, 751145407, 751145501, 751145502, 751145503, 751145504, 751145505, 751145506, 751145507, 751145508, 751145509, 751145510, 751145511, 751145512, 751145513, 751145514, 751145515, 751145516, 751145601, 751145602, 751145603, 751145604, 751145605, 751145606, 751145607, 751145608, 751145609, 751145610, 751145611, 751145612, 751145613, 751145614, 751145615, 751145616, 751145617, 751145618, 751145619, 751145620, 751145621, 751145622, 751145623, 751145624, 751145625, 751155701, 751155702, 751155703, 751155704, 751155705, 751155706, 751155707, 751155708, 751155709, 751155710, 751155711, 751155712, 751155713, 751155714, 751155715, 751155716, 751155717, 751155718, 751155719, 751155720, 751155721, 751155722, 751155723, 751155724, 751155725, 751155726, 751155727, 751155728, 751155729, 751155730, 751155731, 751155732, 751155733, 751155801, 751155802, 751155803, 751155804, 751155805, 751155806, 751155807, 751155808, 751155809, 751155810, 751155811, 751155812, 751155813, 751155814, 751155815, 751155816, 751155817, 751155818, 751155819, 751155901, 751155902, 751155903, 751155904, 751155905, 751155906, 751155907, 751155908, 751155909, 751155910, 751155911, 751155912, 751155913, 751155914, 751155915, 751155916, 751155917, 751155918, 751155919, 751155920, 751155999, 751156001, 751156002, 751156003, 751156004, 751156005, 751156006, 751156007, 751156008, 751156009, 751156010, 751156012, 751156013, 751156014, 751156015, 751156016, 751156017, 751156018, 751156019, 751156020, 751156021, 751156022, 751156023, 751156099, 751166101, 751166102, 751166103, 751166104, 751166105, 751166106, 751166107, 751166108, 751166109, 751166110, 751166111, 751166112, 751166113, 751166114, 751166115, 751166116, 751166117, 751166118, 751166119, 751166120, 751166121, 751166122, 751166123, 751166124, 751166125, 751166126, 751166127, 751166128, 751166129, 751166130, 751166131, 751166177, 751166199, 751166201, 751166202, 751166203, 751166204, 751166205, 751166206, 751166207, 751166208, 751166209, 751166210, 751166211, 751166212, 751166213, 751166214, 751166215, 751166216, 751166217, 751166218, 751166219, 751166220, 751166221, 751166222, 751166223, 751166277, 751166299, 751166301, 751166302, 751166303, 751166304, 751166305, 751166306, 751166307, 751166308, 751166309, 751166310, 751166311, 751166377, 751166401, 751166402, 751166403, 751166404, 751166405, 751166406, 751166407, 751166409, 751166410, 751166411, 751166412, 751166413, 751166499, 751176501, 751176502, 751176503, 751176504, 751176505, 751176506, 751176507, 751176508, 751176509, 751176510, 751176511, 751176512, 751176513, 751176514, 751176515, 751176516, 751176601, 751176602, 751176603, 751176604, 751176605, 751176606, 751176607, 751176608, 751176609, 751176610, 751176611, 751176612, 751176613, 751176614, 751176615, 751176616, 751176701, 751176702, 751176703, 751176704, 751176705, 751176706, 751176707, 751176708, 751176709, 751176710, 751176711, 751176712, 751176713, 751176714, 751176715, 751176716, 751176801, 751176802, 751176803, 751176804, 751176805, 751176806, 751176807, 751176808, 751176809, 751176810, 751176811, 751176812, 751176813, 751176814, 751176815, 751176816, 751176817, 751186901, 751186902, 751186903, 751186904, 751186905, 751186906, 751186907, 751186908, 751186909, 751186910, 751186911, 751186912, 751186913, 751186914, 751186915, 751186916, 751186917, 751186918, 751186919, 751186920, 751186921, 751186922, 751186923, 751186924, 751186925, 751186926, 751186927, 751186928, 751186929, 751187001, 751187002, 751187003, 751187004, 751187005, 751187006, 751187007, 751187008, 751187009, 751187010, 751187011, 751187012, 751187013, 751187014, 751187015, 751187016, 751187017, 751187018, 751187019, 751187020, 751187021, 751187022, 751187023, 751187024, 751187025, 751187026, 751187027, 751187101, 751187102, 751187103, 751187104, 751187105, 751187106, 751187107, 751187108, 751187109, 751187110, 751187201, 751187202, 751187203, 751187204, 751187205, 751187206, 751187207, 751187208, 751187209, 751197301, 751197302, 751197303, 751197304, 751197305, 751197306, 751197307, 751197308, 751197309, 751197310, 751197311, 751197312, 751197313, 751197314, 751197315, 751197316, 751197317, 751197318, 751197388, 751197401, 751197402, 751197403, 751197404, 751197405, 751197406, 751197407, 751197408, 751197409, 751197410, 751197411, 751197412, 751197413, 751197488, 751197501, 751197502, 751197503, 751197504, 751197505, 751197506, 751197507, 751197508, 751197509, 751197510, 751197511, 751197512, 751197513, 751197514, 751197515, 751197516, 751197517, 751197518, 751197519, 751197520, 751197521, 751197522, 751197523, 751197524, 751197525, 751197601, 751197602, 751197603, 751197604, 751197605, 751197606, 751197607, 751197608, 751197609, 751197610, 751197611, 751197612, 751197613, 751197614, 751197615, 751197616, 751197617, 751207701, 751207702, 751207703, 751207704, 751207705, 751207706, 751207707, 751207708, 751207709, 751207710, 751207711, 751207712, 751207713, 751207714, 751207801, 751207802, 751207803, 751207804, 751207805, 751207806, 751207807, 751207808, 751207809, 751207810, 751207811, 751207812, 751207813, 751207814, 751207815, 751207816, 751207901, 751207902, 751207903, 751207904, 751207905, 751207906, 751207907, 751207908, 751207909, 751207910, 751207911, 751207912, 751207913, 751207914, 751207915, 751207916, 751207917, 751208001, 751208002, 751208003, 751208004, 751208005, 751208006, 751208007, 751208008, 751208009, 751208010, 751208011, 751208012, 751208013, 751208014, 751208015, 751208016, 751208017, 751208018, 751208019, 751208020, 751208021, 751208022, 751208023, 751208024, 751208025, 751208026]
    
    
    Promise.all([
        fetch(API_IRIS_CONTOUR).catch(err => {
            throw {url: API_IRIS_CONTOUR, err};
        }),
        fetch(API_INSEE_STAT).catch(err => {
            throw {url: API_INSEE_STAT, err};
        })
    ]).then(async([irisContourResponse, inseeStatResponse]) => {
        const irisContourData = await irisContourResponse.json();
        const inseeStatData = await inseeStatResponse.json();
        vniris.forEach((niris) => {
            inseeStatData.forEach(function(item){
        if(item.IRIS == niris){
            process_iris_contour(irisContourData, niris, color_scale(item[field] / maxval).hex())
        }
            });
        });
    });
    

  2. Just fetch the data once and save the result as a variable. The below may not be perfect since your code doesn’t include some of the referenced functions, and there is no absolute path to the JSON data so I can’t test it. But you should get the basic idea. See this question for more details

    (async() => {
      const API_IRIS_CONTOUR = new Request("json/iris_75.json");
      const API_INSEE_STAT = new Request("json/base-ic-logement-2020_paris.json");
    
      const irisCountourResponse = await fetch(API_IRIS_CONTOUR);
      const inseeStatResponse = await fetch(API_INSEE_STAT);
    
      const irisCountourData = irisCountourResponse.json();
      const inseeStatData = inseeStatResponse.json();
    
      var layer_iris = L.layerGroup()
      var color_scale = chroma.scale(['yellow', 'red']);
      const maxval = 400
      var insee_value;    
      const field = "P20_RP_VOIT1P"
      const vniris = [751010101, 751010102, 751010103] //... trimmed for demo
    
      vniris.forEach((niris) => {
        inseeStatData.forEach(function(item){
          if(item.IRIS == niris){
              //console.log(item[field])
              insee_value = item[field];
          }
        });
    
        //console.log("value now needed: " + insee_value)
        process_iris_contour(irisCountourData, niris, color_scale(insee_value/maxval).hex())
      });
    })();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search