JayJona
JayJona

Reputation: 502

show cluster of point with folium

I have a dataframe with a list of coordinates. I want to group on the map near points and show a dot on the map with the number of points on it like this. I'm using geopandas and folium. Following this example this is my code

from folium.plugins import MarkerCluster
import geopandas as gdp
from shapely.geometry import Point, LineString, Polygon, MultiPoint

building_list = [(-1,-1),(10.6,-1),(10.6,4.4),(-1,4.4)]
building = Polygon(building_list)
gdf_building = gpd.GeoDataFrame({'building':['Building'], 'geometry':[building]})

locations = df[['x', 'y']]
locationlist = locations.values.tolist()

print(locationlist)

map_building = gdf_building.explore(style_kwds={'color':'black','weight':3,'fillColor':'gray','fillOpacity':0.2})

 

marker_cluster = MarkerCluster().add_to(map_building)
 
#add a marker to the cluster, not the map
for location in locationlist:
  marker = folium.Marker(
    location=location,
  )
  marker_cluster.add_child(marker)
    
folium.LayerControl().add_to(map_building);

map_building

this is the result of the print of the location list, as you can see there are actual points.

[[-10.975653734125247, 14.223493380357969], [-3.0008170610241343, 4.952113879109663], [1.2889747510247407, 2.204612243849062], [1.8083739029012205, 1.7796964504195971], [3.420669729369738, 0.7404826646612623], [4.754666507090696, -0.5524039121778554], [5.53171781359429, 1.310518688820813], [4.318223656736064, -0.8753972819321212], [1.389494271129264, 16.407899298913147], [2.82329764017852, 0.9072912833977023], [-4.497070654458373, 11.375240775117195], [4.388070857027747, 2.026226808689793], [5.020759388491676, 2.756120554023195], [6.001047947560095, 3.640750189810609], [4.748181698919979, 0.6264535181440452], [2.6528944739060116, 3.4802260546999184], [0.6549886054089478, 2.3877498852531], [0.9196638849958464, 2.150417536934342], [5.108903278970173, 1.7149961891938221], [4.023317826279737, -0.7706685254504961], [0.903562864987411, 2.0796324380334292], [1.9513434378658632, 1.3693238231850704], [5.644520571004791, 0.9779979070472828], [4.885749810717222, 1.8987377699798547], [0.8962029416395766, 2.1062557612048933], [4.8, 3.1279064752127708], [-0.04508236022751255, 3.0568571249844774], [4.800000000000001, 1.9140378892025933], [-1.989743839191096, 3.371668995303246], [1.1656083298774806, 1.807252953229595], [1.8713291261582097, 1.143179456971009], [4.874256276815991, 1.442130685409675], [1.4180968883141027, 1.6105182232356952], [3.190937527417693, 0.7681078921163724], [0.6152561767926022, 2.4352310754461803], [4.943357091884491, 1.3116890391870286], [4.669880127812657, -0.9679152111322677], [3.614139957564433, 4.902972429576663], [5.669098065784623, 3.913649249046372], [3.813973864130181, -0.7323384613836201], [-0.5199470910058632, 4.940176537119433], [0.6045522916902855, 2.4454362761206676], [3.997321854441272, -0.40367758748908944], [-0.7749524129532896, 3.7357518435721646], [4.398949303156029, 1.5109906728678215], [0.3021200538833131, 2.729947426419629], [5.644520571004791, 1.1699695341101362], [0.3021200538833131, 2.729947426419629], [4.038514768738497, -0.2998527712422736], [5.463665551081531, 4.137680366555347], [0.8962029416395773, 2.157030296469887], [3.4127967638900434, -0.15739477788275646], [5.3627337631140914, 0.47173132205910173], [-17.039850506402114, 17.631059209559115], [4.693589821903688, 2.645652867319201], [1.652915853616594, 1.4812029360906247], [0.7676072335427175, 2.7523632254899515], [5.108903278970173, 1.6583572860511704], [0.4132781963362031, 2.904134388098682], [3.259934813790278, -0.18044453177741993], [0.8945938791669944, 2.1032269377270914], [2.57881016023833, 3.112861191160806], [3.592593833132784, 1.4836895239891579], [3.317682346796072, -0.406574363983617], [5.23674698889199, -0.4896527267915487], [1.652915853616594, 1.3490348417130233], [4.085742861634555, 0.6349594490258132], [4.0072168062713, -1.0764854252471776], [3.9664315265442545, -0.39306568870652514], [0.6031981581824843, 2.377481733947807], [3.6039665183706195, -0.2228663982705159], [1.8714318174071345, 1.1838500546775474], [1.8733352672556218, 1.1469557225661375], [6.102634326393458, 10.469900068262838], [5.520832421942425, 2.46410701016014], [1.1747275757821485, 1.930301474093356], [4.67585787292738, 0.636918779066237], [4.491096721029827, 1.397978478475737], [1.4568609922766316, 1.6834859483416318], [0.6098506962014651, 2.302784409097839], [4.324311646727075, 2.936569893754537], [0.9333579831295232, 2.088974913476743], [4.518788267677348, 0.020784625382443522], [0.47410395349115, 4.18998422562209], [1.1656853374503982, 1.7606554558367773], [1.837995743774393, 4.916814195653471], [4.388070857027747, 2.3671414910993596], [4.174911828673442, -0.6971612141229651], [0.6031981581824846, 2.357970915575524], [1.2974476846606793, 3.9025406503211726], [-0.03437847512519587, 2.924380280250848], [4.174911828673442, -0.6971612141229651], [1.4936152461189462, 7.123830497692014], [0.6152561767926021, 2.2589820889454635], [-11.279626246016324, 13.226577973666835], [1.82048818578873, 4.061094403724194], [4.581484036209459, 5.890145802013782], [4.265190300374529, 2.452440089435306], [3.8845082501175248, -0.7686579417306034], [0.9637552343186307, 2.2334130180125245], [4.25063438297424, -0.843761982823303], [0.98431156175139, 4.194028317706987], [1.1761611467009938, 1.827117079015031], [5.449834811820658, 0.5594950288780689], [1.6724495566887114, 4.605197843492187], [3.5384417352877477, 0.07911340357174579], [4.348620907925173, -0.9172910153848934], [1.2159563418949306, 4.6362987571816525], [1.1668919640810418, 1.4915162635203172], [2.249924886746646, 5.685012235183888], [4.067409175602521, -0.536439926621362], [2.5656813438853296, 2.7056802874632795], [2.1293914647589034, 4.642559399432756], [3.4127967638900434, -0.24461452723904076], [4.346665070906969, 7.334655181225893], [4.8, 2.0482018725377187], [1.123738605408948, 3.57856337675828], [4.228966634584813, -0.5954109912309717], [0.6031981581824846, 2.2362846421499483], [2.347165380094789, 2.914571054618481], [4.388070857027747, -0.4566421607253668], [2.1979928248704255, 1.0832058812097898], [1.69634605871611, 3.8274151057051444], [1.123738605408948, 3.57856337675828], [1.1747275757821485, 1.7776761397553662], [4.4693615246118945, -0.6333756573025302], [4.53293914647589, -0.9077879799922597], [1.0361298628314102, 4.036083757910509], [2.808234500789304, 0.3071881706888375], [-0.7191638996712912, 7.874957048525548], [2.8260583427385293, 7.660845787800426], [3.9912416391547496, 2.8264553465726032], [2.1123464147922975, 3.1439793965288794], [2.6731182859013067, 0.26597049531016737], [1.1673676524343146, 1.4924116768911837], [4.121463981513893, -0.5859228214803203], [1.4342741983539433, 1.6004923338374206], [4.608830245519423, 1.4005010543426653], [0.9162643526136969, 2.1035411206240853], [1.215956341894931, 4.30182011239446], [1.4872582434657393, 1.5371751490903258], [4.388070857027747, 2.4052170210761252], [0.6031981581824842, 2.4125336286506642], [2.898777190073906, 0.7105093578091037], [0.8945938791669943, 2.1382788324299487], [5.669098065784624, 3.8755737190696062], [6.070148762628594, 0.206906059620394], [1.4568609922766318, 1.4079768509137702], [5.531717813594291, 1.830541309334829], [-1.2471810710311697, 8.743462266446604], [0.6248686316291862, 2.4533251080797505], [3.6748906604483524, 3.124197444178624], [4.893093462225972, 0.4590567027245811], [1.1657585899617324, 1.9134186772549258], [-0.024766020288611745, 3.0296940622407025], [2.3563295257006565, 0.7846312919685801], [5.205353859395272, 0.42210603989954754], [3.8437689822722936, -0.7665448437321474], [3.6748906604483524, 3.1628631330266517], [2.727173091812678, 0.639131207705715], [1.788002622023866, 6.769502536197556], [-0.4057970441397616, 3.2929812953755766], [4.965546187085682, 1.9531004552217803], [-0.046436493735313444, 2.9484252862795244], [2.9371084106171184, 0.20309215819430038], [4.707852582126202, 2.17957929497331], [1.1778166085718507, 1.6671813846379329], [2.683868551208399, 0.5036394358913796], [4.8, 2.0482018725377187], [-2.145131239743776, 4.923667400169843], [4.5024200674231105, 2.3840119264621733], [1.9101959213696633, 1.1695980304271996], [-0.40643138475388163, 3.4467173934044606], [4.259451940886286, 1.0691802220180708], [1.167367652434315, 1.7378040719362537], [1.4872582434657393, 1.700227006989037], [1.6528426011052597, 1.2481771593670978], [4.800000000000001, 2.005728687048103], [-0.04508236022751255, 3.0568571249844774], [2.3439911949358385, 3.5683856892327976], [3.910730421098827, 4.135332344591893], [-0.04508236022751233, 2.9509742434706796], [2.950134753881053, -0.04379815575361201], [4.264382707965382, 0.09902519561511669], [3.106229402999044, 3.7294157226465066], [-0.0247660202886113, 2.989216765708611], [2.1123464147922975, 3.1385816622847216], [1.652915853616594, 1.3022923888888307], [4.259451940886286, 1.0000167752411318], [1.5290924648042203, 4.310240411834759], [2.9371084106171184, 0.20309215819430038], [-2.0852792308147365, 5.03633000521274], [0.2726285121062364, 5.866558412092897], [0.2900620352731951, 2.6721980849212557], [5.108903278970173, 1.6233053913483126], [-0.8239196028969407, 5.030419621266199], [1.8466237141828281, 5.932927653853168], [4.8, 9.658382640793699], [2.950134753881053, 0.22761233374994183], [0.6045522916902855, 2.238833599341103], [2.486105104523525, 7.000796957374647], [0.98431156175139, 4.168919529596836], [-0.8239196028969404, 5.026413025884784], [0.2990310210936111, 2.6018611324034016], [0.6803390498603128, 3.601707913302718], [1.2974476846606788, 3.8503187512101014], [0.8267571383654448, 5.853058068771931], [-0.04482743126273103, 2.87869356247465], [3.2764894324988463, 0.1688700457664547], [-3.767329514700993, 8.66269152688608], [3.222434626587475, -0.03359997239130541], [4.090573653616875, -0.707077359146127], [3.8849164987684937, -0.3938707427571062], [-0.5199470910058632, 5.609801167360874], [5.233045406042793, 1.6406297256739135], [1.5290924648042197, 4.299156029478217], [-0.024766020288611745, 3.0296940622407025], [4.325545325536228, 0.3438574274307671], [0.11249999999999938, 4.508574043613446], [5.15613137186623, -0.3461798183284306], [3.192037375398367, -0.3082514746327818], [0.6031981581824848, 2.469172531793316], [4.0682821864057095, 1.190912989505398], [1.0228149047709139, 5.191509904245847], [0.8267571383654444, 5.861370182915128], [-0.3692763432639352, 3.4022034406151067], [2.285674165981368, 0.40054508668958766], [2.9582532190128603, 2.1211932808694134], [-2.5837485905052917, 9.06294581359145], [-1.9853601848610753, 8.508068840069502], [1.8745211272980575, 1.30411808065442], [0.2862090792517309, 4.825957160184751], [3.0627533820148227, 0.592225673986793], [-0.046436493735313444, 3.103355437229105], [5.205353859395272, 0.7111054161797834], [7.244188130715516, 1.6376585614535513], [1.0228149047709136, 5.216618692355997], [4.074211060786516, 0.932690737783997], [0.4381746991919173, 6.21878271740584], [1.2159563418949306, 3.8495970971114977], [1.4151343263934582, 3.2980753742742706], [3.295606407946904, 0.3243530680371258], [6.0564497139770435, 0.4578939096082548], [-0.7634664185075604, 5.151411845004738], [2.6731182859013067, 0.6900063191517116], [0.34384052085180405, 3.92734467412126], [-0.7966228863999916, 3.730012258845936], [-1.343379060194661, 6.639339649156551], [2.655747377976134, 0.7579169337318432], [0.4741039534911502, 4.183751019358796], [0.8106024824996589, 3.8469096688591344], [4.251583853701234, -0.5122336159174341], [5.433641521339686, 1.2304496491736634], [0.6098506962014656, 2.4407786937191167], [2.958196315583629, 0.42169758533305535], [0.4741039534911502, 4.186979702946429], [3.139194465647819, 7.372362643090272], [6.070148762628594, 3.5839417724562916], [0.34384052085180405, 3.9447824700656744], [-0.024766020288611745, 3.0647459569435602], [0.98431156175139, 4.194028317706987], [3.205063718662302, 0.1665897376282075], [2.249924886746646, 5.685012235183888], [0.2862090792517309, 4.857299154558197], [0.894593879166995, 1.8996977832962885], [1.1883610918565954, 5.485872011543561], [3.956287021404356, 2.025822005622866], [0.020600968107850726, 6.546459784906801], [3.4988012781265745, 3.3537031044984764], [-0.044827431262731476, 2.9919314062894187], [2.4005561755536413, 4.351152383512192], [1.5888434056451888, 2.39808430763285], [3.724642241260815, 1.5993834520787418], [2.0763857262800003, 1.0305473880108813], [3.847522797914033, 1.4837311634639483], [1.8745211272980573, 1.224717120939789], [5.752477202085966, 2.161171990200467], [0.8945938791669943, 2.1382788324299487], [3.233184891894567, 0.19323849790749126], [1.6649738722267118, 1.4776151700223363], [4.677119443346781, 3.3876549706623997], [-0.395982428616346, 3.3114558657725937], [0.6630501671115239, 1.7966830293255285], [3.769512705456301, -0.4277218437869408], [1.4180968883141025, 1.7446822065708214], [4.345201751603508, 1.0003858818660745], [0.8962029416395768, 2.019036011848609], [6.7767023598214795, 2.767717033817918], [5.811238605408949, 10.971775430203346], [0.8945938791669944, 2.1032269377270914], [3.2362710203269964, 0.7662217764717181], [2.4400930748564438, 0.44511030500033666], [2.9431537290560565, 0.43585531382959397], [0.2900620352731951, 2.707249979624113], [4.692497346929079, 0.1733884621768529], [0.8988112239109582, 2.225618323427335], [3.997321854441272, -0.4340312777679376], [3.825695452069068, 2.5535046832593484], [-0.7966228863999916, 3.760365949124784], [3.657528247574082, -0.2364971741873707], [10.728301112968886, 8.129684856516427], [3.2764894324988463, 0.30103814014405605], [-0.2848242861634557, 2.5492067505157534], [3.4972488209905226, 0.0015738001887335429], [6.1942908897012146, 2.957238766835977], [4.499254404787959, 0.8703367099355468], [1.5413130493771106, 1.8775064211159793], [-0.3745113733554064, 3.4274011610281363], [3.1948017901428534, 0.4679448192780584], [4.010305839061002, -0.7770054557893282], [0.8962029416395773, 2.124411983277796], [4.499254404787959, 0.8703367099355468], [4.213823144644389, -0.9389793892200413], [6.898694746008462, 11.99606251173315], [2.333165047686302, 0.9445567230782452], [0.2916710977457775, 2.710278803101915], [-0.5199470910058637, 5.594291058917969], [-8.768059849208484, 13.375959505727446], [-2.2942634638312485, 7.933801838660646], [1.6545249160891764, 1.1890118072921148], [-4.551431127611951, 9.141286955528566], [5.633568473455744, -0.10132487706078885], [0.98431156175139, 4.194028317706987], [-0.7191638996712908, 7.827132937551404], [1.8379957437743928, 4.909616201353765], [10.59608346486909, 12.797203128699032], [0.8267571383654444, 5.861370182915128], [0.21177072258842733, 6.987152345891302], [3.6719651469590926, 9.249671380027745], [7.556358345958318, 7.050258822639192], [-1.450881713265581, 6.478776935187961], [2.9371084106171184, 0.3253638022534422], [2.950134753881053, 0.4292849375237151], [2.478857178818972, 0.8132979823825846], [-0.6936561702575947, 5.275621376822615], [-6.31581424528901, 12.58751873434882], [0.8267571383654444, 5.861370182915128], [3.2050637186623017, 0.07489893978269802], [0.8267571383654444, 5.861370182915128], [-0.12731794307864375, 10.43702448814638], [0.6031981581824843, 2.290261984591522], [1.1883610918565952, 5.528235044517281], [-2.8148523126150256, 10.46228804188221], [3.4127967638900434, -0.15739477788275646], [-0.10237335992179664, 5.223023803192105], [-1.214196617484058, 3.9467738427772825], [-0.10237335992179664, 5.216790596928811], [0.1292714202217442, 5.652827830140181], [-1.4285166649520677, 7.991178815631317], [1.0179268928460212, 6.234650333088625], [-0.10237335992179664, 5.216790596928811], [0.8945206266556602, 1.961891958849074], [-3.7645455411158375, 14.965007331606095], [-2.5837485905052926, 9.084689128297647], [-0.6936561702575943, 5.28281937112232], [0.2900620352731955, 2.6903543069941582], [0.4741039534911502, 4.186979702946429], [1.3593134337794222, 4.90314287805327], [-1.754854225156658, 5.917020061280331], [-0.2760824391735279, 4.896042006953552], [-2.145131239743776, 4.999196591404791], [3.6039665183706195, -0.2719136677062992], [5.97912131397371, 0.7637993559893621], [4.166698914141367, -0.08485018099479191], [4.893093462225972, 0.30016716961001455], [0.2900620352731951, 2.6721980849212557], [-3.6991068907737716, 8.861726191348053], [-0.046436493735313444, 2.9484252862795244], [-0.024766020288611523, 2.9666860241457513], [4.827116471079473, -0.5364894629472379], [-1.2141966174840584, 4.087970934575141], [2.2617088319942518, 0.21893988925413344], [4.18147880047027, 0.19511841248959838], [5.9280348530409075, 8.890736658315404], [4.518788267677348, -0.04154743725049981], [4.44325205928394, -1.007852334000157], [4.290841041213988, -0.17540461714051325], [3.769294411555624, -0.34168885793470505], [4.632658506364657, 0.3070575811701284], [4.388033904986031, -1.2787941827568412], [2.8796602146258485, 0.7169976483376705], [-8.993302706302629, 18.047614948808622], [-0.40804044722646404, 3.32923571550917], [4.388524835038002, -0.22441713017397236], [5.9749455766628685, -0.5105871937116166], [0.2900620352731954, 2.6317207883891633], [0.8945938791669943, 2.1382788324299487], [-16.642526220238658, 19.623895964392474], [-0.046436493735313444, 2.9484252862795244], [0.6031981581824842, 2.4125336286506642], [3.9771730838489914, -0.7757922401415804], [4.536770649420184, -0.20454374589753987], [0.2900620352731951, 2.707249979624113], [-0.4080404472264636, 3.4209265133546793], [4.719384382974241, -0.48143166216084854], [4.544248150302466, -0.5884133974848607], [2.768366006109903, 0.9183434148624996]]

The problem is that when i display map_building I see only the map of the first polygon without any point. How to solve it? what I see

Upvotes: 2

Views: 1075

Answers (1)

flipSTAR
flipSTAR

Reputation: 623

I think its perhaps a problem that you dont have a folium map. In the docs of GeoDataFrame.explore() it says the parameter m should be linked to a map. Perhaps first you have to create a map and then use it in the explore command. Similar to this (have not tried this example in lack of geopandas installed)

map_building = gdf_building.explore(style_kwds={'color':'black','weight':3,'fillColor':'gray','fillOpacity':0.2},
                                    m=folium.Map(location=[44, -73], zoom_start=5))

I am not aware of the advantages of GeoDataFrame.explore() vs. pure folium. Perhaps, if you do not need it, you can just create a folium map and add the markercluster to it, just like it is done in your linked example. This should work:

# create map
m = folium.Map(location=[44, -73], zoom_start=5)
marker_cluster = MarkerCluster().add_to(m)

# add a marker to the cluster, not the map
for location in locationlist:
  marker = folium.Marker(location=location)
  marker_cluster.add_child(marker)

# add layercontrol    
folium.LayerControl().add_to(m);

# show map
m

Upvotes: 0

Related Questions