1

I am trying to add the GeoJSON to the map on Leaflet

$.ajax({
        dataType: "json",
        url: "contour_communes.geojson",
        success: function(data) {
            $(data.features).each(function(key, data) {
                geojsonLayerWells.addData(data);
                console.log(data)
            });
        }
        }).error(function() {console.log('erreur geojson')});

        geojson_layer.eachLayer(function (layer) {  

    layer.setStyle({fillColor :'blue'}) 

});

geojsonLayerWells.addTo(mymap);

but the layer is not showing on the map. Any idea?

exemple of a feature from the geojsonfile

{
"type": "FeatureCollection",
"name": "communes_last",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "gid": 461, "insee_comm": "02042", "nom_commun": "AZY-SUR-MARNE" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 371907.61918287910521, 6275053.340028743259609 ], [ 372036.291736110404599, 6275133.823878624476492 ], [ 372036.230170833470766, 6275119.570710268802941 ], [ 372035.928191001003142, 6275089.622116575948894 ], [ 372035.528258231759537, 6275036.995363153517246 ], [ 372035.340574892878067, 6274993.543284893967211 ], [ 372034.921209115767851, 6274936.416128330864012 ], [ 371966.947272408462595, 6274945.020814280956984 ], [ 371900.008886745141353, 6274953.62049803417176 ], [ 371897.261963931727223, 6274957.094747267663479 ], [ 371897.498650426452514, 6274971.981879002414644 ], [ 371900.001680019078776, 6274992.052782591432333 ], [ 371905.010754124785308, 6275032.88718868046999 ], [ 371906.434155185648706, 6275042.748838956467807 ], [ 371907.61918287910521, 6275053.340028743259609 ] ] ], [ [ [ 373393.210992991458625, 6276123.821156785823405 ], [ 374034.200834281975403, 6276921.605571278370917 ], [ 374109.993985108332708, 6276879.270939180627465 ], [ 374142.007709467085078, 6276857.306635057553649 ], [ 374196.117390273138881, 6276834.548235200345516 ], [ 374244.020882009819616, 6276813.89593933802098 ], [ 374246.261377746413928, 6276813.019868864677846 ], [ 374276.578909289150033, 6276797.816038311459124 ], [ 374313.753174892975949, 6276771.845031293109059 ], [ 374330.790745071251877, 6276759.818978087045252 ], [ 374345.749342846160289, 6276746.244110056199133 ], [ 374360.678532869496848, 6276726.26272027567029 ], [ 374386.502288563060574, 6276672.466466556303203 ], [ 374416.967313924280461, 6276614.320273972116411 ], [ 374422.098221540218219, 6276603.734411896206439 ], [ 374430.070146294601727, 6276610.277318624779582 ], [ 374433.204011533234734, 6276615.976813580840826 ], [ 374434.584386824921239, 6276615.79728835914284 ], [ 374514.544096583209466, 6276583.221713749691844 ], [ 374544.011497062863782, 6276571.13797393720597 ], [ 374573.824107075692154, 6276559.052551437169313 ], [ 374589.16079698264366, 6276552.748089022934437 ], [ 374611.391284299956169, 6276543.814393405802548 ], [ 374622.764280178584158, 6276539.086608270183206 ], [ 374637.926657739561051, 6276532.436585241928697 ], [ 374664.809650035575032, 6276521.576542722992599 ], [ 374667.91005336336093, 6276520.00380415096879 ], [ 374672.90874381136382, 6276518.249098104424775 ], [ 374675.683849047694821, 6276521.006585536524653 ], [ 374679.334975093428511, 6276526.530383843928576 ], [ 374685.887467264255974, 6276524.76844990439713 ], [ 374699.337732562504243, 6276521.242963360622525 ], [ 374726.410875152680092, 6276514.191129519604146 ], [ 374765.551430370775051, 6276503.273775695823133 ], [ 374804.520849321968853, 6276492.703353982418776 ], [ 374861.420207442890387, 6276476.681527484208345 ], [ 374928.321398752741516, 6276458.188546299934387 ], [ 374972.977973539149389, 6276445.513073583133519 ], [ 374999.877215807442553, 6276438.288119771517813 ], [ 375032.296943909546826, 6276430.171484984457493 ], [ 375014.800447240355425, 6276380.387270805425942 ], [ 375012.559331503289286, 6276381.090352742932737 ], [ 374992.171672468539327, 6276340.669749520719051 ], [ 375002.268002353492193, 6276321.403375016525388 ], [ 375020.241214152832981, 6276288.248583330772817 ], [ 375025.925422669912223, 6276285.451644198969007 ], [ 375029.528271267598029, 6276280.586726791225374 ], [ 375028.097143106453586, 6276269.858507523313165 ], [ 375024.075623217271641, 6276258.969294789247215 ], [ 375044.963357241475023, 6276258.525115165859461 ], [ 375057.194105054833926, 6276252.754033394157887 ], [ 375183.030379768577404, 6276211.474426261149347 ], [ 375171.69984337920323, 6276151.274247669614851 ], [ 375233.100289383495692, 6276138.519129499793053 ], [ 375217.69921325170435, 6276057.042461900040507 ], [ 375319.890351138019469, 6276054.656265079043806 ], [ 375370.237808956531808, 6276077.965550701133907 ], [ 375371.267070674861316, 6276095.235090886242688 ], [ 375655.358399097400252, 6275805.480343820527196 ], [ 375794.608457287191413, 6275709.341197880916297 ], [ 375786.334534263878595, 6275685.220971060916781 ], [ 375788.184151575027499, 6275674.824199188500643 ], [ 375790.365053391258698, 6275661.482612621039152 ], [ 375786.46949488070095, 6275640.898514776490629 ], [ 375732.742138575878926, 6275559.783257661387324 ], [ 375668.388236830593087, 6275458.11673117056489 ], [ 375656.562217240629252, 6275439.648318434134126 ], [ 375652.560770152951591, 6275433.088504314422607 ], [ 375649.953412247705273, 6275429.292101546190679 ], [ 375644.561168459593318, 6275420.661378893069923 ], [ 375629.257431971491314, 6275396.842613297514617 ], [ 375618.828096846700646, 6275381.65704164840281 ], [ 375605.446235198294744, 6275362.67673671618104 ], [ 375602.141889637161512, 6275357.498653049580753 ], [ 375599.187697368266527, 6275353.35766363888979 ], [ 375596.580386028683279, 6275349.561279123649001 ], [ 375591.715100598114077, 6275342.83248226903379 ], [ 375582.512224101345055, 6275331.449896609410644 ], [ 375551.961022831208538, 6275295.584885844029486 ], [ 375539.990609186817892, 6275283.003577402792871 ], [ 375516.404100934334565, 6275259.743651252239943 ], [ 375487.784032068273518, 6275230.621370580047369 ], [ 375417.891233662958257, 6275198.058841142803431 ], [ 375387.288956223288551, 6275187.816174931824207 ], [ 375337.5237100936356, 6275177.144538952037692 ], [ 375322.861737716302741, 6275179.464341819286346 ], [ 375309.92187852866482, 6275180.910382777452469 ], [ 375237.984712061879691, 6275190.771237989887595 ], [ 375160.355828788189683, 6275201.697009364143014 ], [ 375159.177435890131164, 6275208.108102712780237 ], [ 375157.703157257870771, 6275225.081116830930114 ], [ 375156.525568356912117, 6275231.665346525609493 ], [ 375155.174549430317711, 6275238.077272400259972 ], [ 375128.733890933624934, 6275231.795937768183649 ], [ 375117.499099106877111, 6275228.732480057515204 ], [ 375112.682737489289138, 6275232.390691735781729 ], [ 375108.017172363353893, 6275231.373856137506664 ], [ 375104.365506096393801, 6275225.67792329005897 ], [ 375082.930868464522064, 6275219.372957076877356 ], [ 375054.021197462745477, 6275201.84999271761626 ], [ 375023.4386841202504, 6275195.760909278877079 ], [ 375014.798087833623867, 6275193.72391471080482 ], [ 375011.169877024367452, 6275193.048418404534459 ], [ 374988.706927676219493, 6275188.306166622787714 ], [ 374974.887799533840735, 6275186.29337559454143 ], [ 374958.985181735595688, 6275181.693470566533506 ], [ 374926.496764802897815, 6275174.054876696318388 ], [ 374878.034346049011219, 6275146.408598080277443 ], [ 374814.86288469057763, 6275110.867138945497572 ], [ 374794.619407323771156, 6275100.747335773892701 ], [ 374785.819970427604858, 6275101.653935019858181 ], [ 374780.488897218601778, 6275106.006793469190598 ], [ 374775.586414610676002, 6275128.362277531065047 ], [ 374775.10303001862485, 6275135.808758347295225 ], [ 374763.880503411521204, 6275135.341649401932955 ], [ 374723.305839599866886, 6275133.452999785542488 ], [ 374677.034026087319944, 6275131.417510945349932 ], [ 374656.653208711126354, 6275128.915349072776735 ], [ 374655.473660027026199, 6275135.153204115107656 ], [ 374620.745647649979219, 6275128.389525578357279 ], [ 374619.916897691204213, 6275135.837599526159465 ], [ 374614.953949484101031, 6275182.603599355556071 ], [ 374607.871015310869552, 6275181.424594231881201 ], [ 374603.102147608704399, 6275232.864262391813099 ], [ 374589.116003893257584, 6275232.063492407090962 ], [ 374589.161471459781751, 6275354.289353722706437 ], [ 374589.015979143790901, 6275360.176312134601176 ], [ 374546.697362638136838, 6275354.659231220372021 ], [ 374526.467177020444069, 6275347.481605947948992 ], [ 374493.959418690181337, 6275335.686339747160673 ], [ 374486.004516932182014, 6275332.606867237016559 ], [ 374480.676339176134206, 6275337.652144349180162 ], [ 374478.604884185944684, 6275337.661721285432577 ], [ 374478.413960788340773, 6275333.680715900845826 ], [ 374463.541494526318274, 6275327.863194877281785 ], [ 374430.858887478127144, 6275315.549153982661664 ], [ 374390.394947255903389, 6275300.327785030007362 ], [ 374382.785354515654035, 6275297.24663847964257 ], [ 374380.032158955000341, 6275299.163709535263479 ], [ 374383.250120268319733, 6275360.954653289169073 ], [ 374378.590127764560748, 6275361.149271340109408 ], [ 374333.547751774487551, 6275363.953771366737783 ], [ 374342.810406913456973, 6275539.462706225924194 ], [ 374342.656816045229789, 6275543.61852369364351 ], [ 374344.148063100874424, 6275567.676701451651752 ], [ 374177.791311170498375, 6275580.387649769894779 ], [ 374187.08388309692964, 6275649.597399909049273 ], [ 374190.894327568705194, 6275652.350016636773944 ], [ 374192.126424177957233, 6275657.538314022123814 ], [ 374163.156580524402671, 6275664.596527382731438 ], [ 374125.046881983929779, 6275673.774126929230988 ], [ 374122.655333154427353, 6275679.325316727161407 ], [ 374139.056154452438932, 6275755.255524314008653 ], [ 374134.052267238614149, 6275755.79786403849721 ], [ 373953.44586937193526, 6275786.749198349192739 ], [ 373854.777455754519906, 6275803.992936966940761 ], [ 373855.769895236939192, 6275832.555683122947812 ], [ 373856.888741106085945, 6275850.902941288426518 ], [ 373859.189370134612545, 6275863.358237725682557 ], [ 373871.72370639781002, 6275924.591455661691725 ], [ 373886.910461685911287, 6275999.664063380099833 ], [ 373889.116960725863464, 6276029.43387888930738 ], [ 373893.790650847833604, 6276032.182810087688267 ], [ 373894.332869999867398, 6276037.547653555870056 ], [ 373890.898992334143259, 6276041.718641518615186 ], [ 373891.275633625336923, 6276048.64250008482486 ], [ 373883.684388485155068, 6276049.715920338407159 ], [ 373589.189786223636474, 6276094.16430907510221 ], [ 373477.207910404365975, 6276107.828612858429551 ], [ 373469.825672636099625, 6276117.038349548354745 ], [ 373450.848056440823711, 6276119.894133505411446 ], [ 373450.148202350595966, 6276117.819597787223756 ], [ 373402.004275503277313, 6276122.884335730224848 ], [ 373393.210992991458625, 6276123.821156785823405 ] ] ] ] } }```
  • With the info you provided in your question: no idea. What do you see in the console? Any errors? How does data look like in the console? And you are setting feature styles outside success function, so this will very likely fail. – TomazicM Nov 15 at 14:26
  • I have features properties in console , wich mean I have features with success, but it is invisible. is ther any idea to verifi if the problem is a coordinate system problem – Namoat Nov 15 at 14:30
  • 1
    You see your GeoJSON and know your CRS settings, but there is no info about that in the question. – TomazicM Nov 15 at 14:41
  • 1
    Go to geojson.io to check your GeoJSON file itself. Look at the coordinates, Long should be less then 181 and Lat less than 91. Beyond that I've post some Leaflet stuff at gistechsolutions.com/leaflet/DEMO for a class, it may help. – Bill Chappell Nov 15 at 14:45
  • @Namoat can you please add more info and a sample of the geojson file. – Mapperz Nov 15 at 14:45
1

Your GeoJSON is in projected coordinates, so you have to unproject it to CRS EPSG3857. This can be done with L.CRS.EPSG3857.unproject method in coordsToLatLng option of GeoJSON layer creation with L.geoJSON.

In your case this would look something like this:

var geojsonLayerWells = L.geoJSON(null, {
  style: function(layer) {
    return {fillColor :'blue'};
  },
  coordsToLatLng: function(coords) {
    var point = L.point(coords[0], coords[1]);
    var latLng = L.CRS.EPSG3857.unproject(point);
    return latLng;
  }
});

$.ajax({
  dataType: "json",
  url: "contour_communes.geojson",
  success: function(data) {
    $(data.features).each(function(key, data) {
        geojsonLayerWells.addData(data);
        console.log(data)
    });
    geojsonLayerWells.addTo(map);
    map.fitBounds(geojsonLayerWells.getBounds());
  },
  error: function(err) {
    console.log('erreur geojson')
  }
});

That's how it looks then:

enter image description here

  • thanks for help :) – Namoat Nov 25 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.