# pip install sparqlwrapper
# https://rdflib.github.io/sparqlwrapper/

from SPARQLWrapper import SPARQLWrapper, JSON

endpoint_url = "https://query.wikidata.org/sparql"

query = """#Public libraries of the Netherlands

SELECT DISTINCT ?coordinates ?itemLabel #?item #?dplDescription ?lat ?long ?dpl ?dplImage ?dplLoc 
WHERE {
#Is Dutch public library dpl
?item p:P31/ps:P31 wd:Q28564,wd:Q11396180;wdt:P17 wd:Q55.
?item wdt:P625 ?itemLoc. #Location of the item
BIND(STRBEFORE(STRAFTER(STR(?itemLoc), ' '), ')') AS ?lat)
BIND(STRBEFORE(STRAFTER(STR(?itemLoc), 'Point('), ' ') AS ?long)
BIND(CONCAT('(',?lat,',',?long,')') AS ?coordinates) 
     
SERVICE wikibase:label {bd:serviceParam wikibase:language "nl" .}
}
ORDER BY ?itemLabel"""

def get_results(endpoint_url, query):
    sparql = SPARQLWrapper(endpoint_url)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    return sparql.query().convert()

results = get_results(endpoint_url, query)

coor_list=[]
#Convert string tuple like '(51.86132,4.99204)' into float tuple (51.86132,4.99204)
#https://blender.stackexchange.com/questions/131827/converting-string-int-tuple-to-float-tuple
for result in results["results"]["bindings"]:
    str_tuple = result['coordinates']['value']
    #print(str_tuple)
    float_tuple = tuple(float(s) for s in str_tuple.strip("()").split(",")) 
    #print(float_tuple)
    coor_list.append(float_tuple)
print(coor_list)    
    
[(51.86132, 4.99204), (53.1825842, 7.2063955), (53.2449773, 6.978697), (51.8566786, 4.3884626), (51.8571861, 4.4717617), (53.17614, 6.97188), (51.8731333, 6.2454886), (52.2679063, 4.7496987), (51.7841643, 5.1259789), (52.7313345, 5.0160659), (52.1365067, 5.4973655), (53.2599615, 6.4570135), (52.5633782, 4.7322704), (51.86473, 4.65723), (52.63346, 4.74329), (52.6535348, 4.7588585), (52.6340305, 4.7657342), (52.618834, 4.746962), (52.35731, 6.66154), (52.3940622, 5.2766666), (52.3363865, 5.2186265), (52.3459733, 5.1511196), (52.3712, 5.2206), (52.1302169, 4.6665075), (51.7506792, 5.2201121), (52.3031455, 4.8582323), (52.281472, 4.8260837), (52.7480265, 5.2207147), (51.9154331, 5.9556748), (52.1935377, 6.0037069), (52.19222, 5.9735438), (53.3208318, 6.8555762), (51.9581628, 5.8973571), (51.9806911, 5.9487595), (51.9524139, 5.8683071), (51.8806175, 5.1045091), (52.9946471, 6.558683), (51.4414079, 4.9263588), (52.2121967, 5.2931546), (52.3372851, 4.7798208), (53.3634121, 6.5141149), (52.599639, 6.3924253), (52.1403331, 5.5877823), (52.2101276, 6.7431278), (53.30199, 6.59927), (51.8313277, 5.9288985), (51.8868067, 5.1930425), (53.2491097, 6.6008124), (52.8605795, 6.5127028), (53.1073764, 7.1510036), (51.88823, 5.89421), (51.8777727, 5.516266), (52.3193856, 4.6014461), (52.7030175, 5.0134646), (52.6696634, 4.7033516), (51.4954501, 4.2859508), (52.5285154, 6.6176505), (51.5873853, 5.1369317), (52.6398631, 4.9986387), (51.5116928, 5.3965446), (51.5000761, 5.3831417), (51.8620136, 5.771956), (52.4866009, 4.658514), (52.4566861, 5.6914176), (53.1086035, 7.0740944), (52.4009986, 4.6182348), (50.8157359, 6.0039912), (51.849597222, 4.324752777), (52.0798075, 4.2684171), (52.3022979, 6.7581469), (52.0760436, 4.6560706), (52.9737389, 6.4800476), (52.1663723, 4.5090711), (52.1772347, 4.5043177), (52.1584323, 4.4931796), (52.1504661, 4.4538703), (52.1594145, 4.5285396), (51.58766, 4.77856), (51.623815, 4.7432848), (51.5688907, 4.7940441), (50.9387418, 5.970752539), (52.2530506, 5.3785464), (53.1928014, 5.9919202), (51.9317043, 4.5886421), (51.8485988, 4.5018405), (52.5517198, 4.6752715), (51.893778, 4.4670911), (51.7899803, 4.6631379), (51.9574438, 5.223026), (52.0087609, 4.3629297), (51.8480017, 5.8435591), (52.62786, 4.94746), (51.84541, 5.86633), (52.0801082, 5.0388408), (52.5540528, 4.848159), (52.7215282, 4.9999289), (53.1927447, 6.5680969), (53.1171242, 6.2534869), (52.6026628, 6.4570224), (51.9125405, 4.4417934), (51.99642, 4.34765), (53.3299846, 6.9267437), (52.003134, 4.331144), (51.9389282, 6.1296912), (52.8528604, 6.313289), (51.6379174, 4.3647439), (51.9090619, 5.6607362), (51.6269748, 4.9419711), (51.8906758, 6.0008965), (53.1522194, 6.762120771), (53.1044062, 6.0986522), (51.846106, 5.4329593), (52.5236844, 5.7193634), (51.8864962, 5.6023973), (52.8348958, 6.3697596), (52.6179096, 4.6313127), (51.4394388, 5.4747474), (52.4453091, 5.8488598), (51.91723, 5.84411), (52.7115512, 5.2769056), (52.2151377, 6.9729812), (52.2504249, 6.9143418), (52.21149, 6.85954), (52.2287494, 6.8580599), (52.191905, 6.8816226), (52.2956093, 6.5792184), (52.3475076, 5.9869872), (51.9807831, 5.904308), (52.0449965, 4.2804703), (51.5717097, 4.640338), (51.8662134, 5.7367693), (50.8942062, 6.0583066), (51.8894299, 4.5073691), (53.1864827, 7.0817392), (51.87324, 5.28966), (51.4227157, 5.5601343), (51.876516, 5.9705598), (51.85164, 4.88956), (51.54049445, 4.941347123), (51.5201768, 5.0661126), (51.8293032, 4.974148), (52.6100931, 6.6703703), (53.2645122, 6.3056918), (51.7757214, 5.9353596), (53.2190669, 6.5642461), (53.2142789, 6.2772892), (53.0890931, 5.8261502), (50.8151497, 5.8882573), (53.2411023, 5.8922686), (51.8222877, 5.209602), (52.08745, 4.34021), (52.1553502, 6.7440102), (51.609979, 4.8255962), (51.8851623, 5.9303022), (52.380472, 4.630469), (52.4083687, 4.6481001), (52.37535, 4.65948), (52.355739, 4.6558299), (51.525984, 4.2695517), (52.5088705, 5.4698154), (52.5758295, 6.6228007), (51.8268461, 4.837803), (53.1732298, 6.605538), (53.2103507, 6.6999968), (51.8053076, 5.8272408), (52.468463, 6.0700533), (52.7072967, 5.1016129), (52.7738645, 6.2337536), (52.0960473, 4.5826353), (52.1283131, 4.5930699), (52.5074556, 4.6703182), (52.3525449, 4.6238156), (52.3874073, 6.0469698), (52.6646543, 4.8243033), (52.7070327, 4.8502983), (51.3793269, 5.5742359), (52.60065, 4.69958), (52.4357657, 6.232057), (51.8447994, 4.6387055), (52.28513, 6.82948), (52.26358, 6.79621), (51.8278308, 5.132461), (51.8562621, 4.5367152), (51.9416646, 4.5471513), (51.9556444, 5.7582423), (51.8717037, 5.075112), (52.28778, 4.58527), (51.4849325, 5.1342892), (51.9780243, 4.1284841), (52.2811026, 6.421706), (52.3010493, 4.6978423), (52.3058599, 4.6442936), (51.4232902, 4.3255204), (52.7291871, 6.4800902), (53.1537282, 6.7574058), (52.69456, 5.1784501), (53.2056575, 6.5033773), (52.1882533, 5.3831883), (52.0727704, 5.1234708), (51.865394, 4.3573069), (52.2074042, 4.4156539), (51.9367414, 5.9417313), (52.2988481, 5.2411363), (53.2135675, 5.9417194), (51.888145, 4.5484259), (51.6596066, 5.0325607), (52.0731017, 5.0952564), (52.2010933, 4.3966954), (51.8639055, 6.0106415), (51.7712143, 5.3380551), (50.8656576, 6.0627223), (51.9321838, 5.5680087), (52.1481277, 5.6705879), (51.9269848, 4.4996065), (51.9155672, 4.5913219), (52.0596372, 4.3316055), (51.7115157, 4.6988219), (52.6867126, 4.8011256), (52.2565935, 5.2262182), (53.1617161, 6.3708239), (53.3607521, 6.3734917), (51.8935428, 5.0921427), (53.2001111, 5.8005258), (52.0656019, 4.4023217), (52.2236879, 4.6660532), (52.5078979, 5.4747252), (51.8708491, 5.8685434), (51.9220374, 4.4132563), (52.1303058, 5.427725), (52.119461, 5.4096888), (51.83912, 5.99353), (53.237446, 6.623101), (52.5727204, 4.7015233), (51.8750582, 4.5253209), (51.627082, 5.075822), (52.0534952, 4.2355295), (53.3309263, 6.7467198), (52.0640879, 5.136226), (51.9289685, 4.2375541), (50.8459909, 5.7010801), (50.8440096, 5.7258326), (50.8559091, 5.6614522), (51.6808234, 4.7952109), (51.7803468, 5.8526575), (53.1457748, 6.2679449), (52.7625947, 5.1014132), (50.8876671, 5.7500047), (51.4963215, 3.6157947), (53.3438117, 6.6418201), (53.1998031, 7.0375762), (52.7233202, 5.0765075), (51.4439572, 5.6172263), (51.8637665, 6.0467414), (51.54516, 5.18395), (51.8304025, 5.8352615), (52.9295968, 7.0153814), (51.98005, 4.58367), (52.6909085, 5.0579246), (52.06367, 4.24303), (52.2648298, 4.6332474), (53.0796136, 6.9656551), (52.0278997, 5.0848778), (52.150362, 4.7750318), (53.1969759, 6.874977), (52.7107009, 5.746911), (53.0671982, 6.4616009), (51.4794348, 5.5536504), (52.67651, 4.90962), (51.9084892, 5.5680429), (51.5822928, 5.1969427), (52.4462333, 5.9077775), (53.3044483, 6.3971509), (52.3121005, 6.9309973), (52.336444, 6.1115118), (52.525349, 6.4197765), (51.96129, 4.54291), (52.7435271, 5.1406597), (53.03712, 7.0397348), (52.0888051, 5.089665), (51.855102, 5.9400186), (51.4677808, 3.6048608), (51.87717, 5.84334), (51.6458408, 4.8608678), (51.6585543, 4.8644966), (51.6284338, 4.8587223), (51.8441046, 5.3849607), (51.9319108, 5.6276707), (52.7043209, 4.9473936), (51.3941117, 4.3323237), (53.1063122, 7.0056057), (51.76127, 5.78581), (51.9350926, 4.4290857), (52.1148908, 5.111179), (51.8280766, 4.6850648), (52.0916222, 5.064352), (53.1484106, 6.4993715), (52.3858869, 6.2768179), (51.6976753, 4.8737319), (51.86992, 4.60124), (51.5896294, 4.9071622), (52.1898781, 4.4435159), (52.3078081, 6.5227012), (53.139851, 6.4294479), (52.1945223, 4.6255224), (51.1942953, 5.9857115), (52.6126616, 6.1838072), (51.9049388, 4.2468596), (51.9805817, 5.9048654), (52.7587964, 6.3522069), (52.7235509, 6.2515855), (52.6344143, 5.1219979), (52.084693, 5.4898798), (52.10281, 4.27912), (51.9599642, 4.4717142), (51.9331906, 4.375652), (51.9046727, 4.4031663), (51.9176272, 4.3962976), (51.9156133, 4.4037165), (51.9127199, 4.3926644), (51.9380821, 4.370517), (52.0689042, 4.3141238), (52.714569, 4.6827329), (52.072389, 4.2748307), (53.23143, 6.54869), (53.2477501, 6.8636425), (52.6280552, 6.5586341), (51.8235529, 4.7721289), (51.8734673, 4.4785935), (53.2202819, 6.8053507), (52.945958, 6.4483896), (51.509423, 5.4937159), (52.4135403, 4.6842274), (53.3919922, 6.8386678), (51.6734929, 5.0116851), (52.9889638, 6.9566776), (51.80247, 4.71613), (52.6434649, 6.2113228), (52.6974, 5.23489), (51.5858478, 4.3200014), (53.2631556, 5.7596027), (52.5713382, 5.6428296), (53.2766876, 6.6960079), (52.1680212, 4.7110972), (52.87426, 7.06195), (51.6410168, 4.7509994), (52.3054958, 4.7525267), (51.8845787, 5.4366236), (51.5702702, 5.0485733), (51.57851, 5.00552), (51.5613301, 5.0858224), (51.5833894, 5.0847647), (52.0671043, 4.2966797), (52.1033548, 5.124173), (52.7412331, 5.0534808), (51.6097923, 5.1425866), (52.5195892, 5.486443), (52.2329426, 4.8315796), (53.4084446, 6.6737042), (53.416195, 6.7235609), (52.6640473, 5.6020868), (52.6269623, 4.8940511), (52.2884676, 5.9655509), (52.1807187, 4.4326076), (52.1944158, 5.4313447), (53.107356, 6.8763073), (52.0273575, 5.5594884), (51.4209084, 5.4041785), (52.4331462, 4.6608218), (52.66342, 5.19984), (53.2247992, 6.5248243), (51.9091422, 4.3397147), (51.9294344, 4.350516), (51.9084433, 4.3123879), (53.0265994, 7.1068695), (52.8575015, 6.206885), (52.1063576, 5.0116317), (51.4444824, 3.5691138), (50.8821112, 5.9273869), (52.1830472, 5.6060763), (51.3918101, 5.4618972), (51.6892274, 5.0691372), (51.9675573, 5.665798), (51.8225158, 4.6538706), (52.4289062, 6.0691886), (53.3918428, 6.5533901), (51.6846048, 4.9476775), (52.0312024, 4.2871554), (52.7296016, 5.1567162), (52.8483199, 6.6106635), (52.4646057, 5.9944671), (52.3564478, 6.5929183), (52.6416391, 5.165148), (52.3881722, 6.1349009), (53.14736, 7.03377), (53.3317677, 6.5141725), (52.685821, 5.0137789), (52.169838, 4.6327734), (52.0817375, 5.4175759), (52.0415963, 4.3706405), (51.8034576, 5.2321253), (52.3719, 4.53332), (52.3308504, 5.5416925), (51.9278408, 5.7129212), (52.1184314, 4.4959915), (53.1614407, 6.8610594), (53.24459, 6.40338), (52.6715312, 6.4289521), (52.1133977, 5.0874259), (52.3767151, 4.748797), (51.8238279, 5.7977373), (52.6581966, 6.289634), (52.2387919, 7.034954), (52.6767626, 6.2909147), (52.0940625, 5.0116907), (51.5657, 4.8304967), (51.5880916, 4.8025816), (51.5729336, 4.7511568), (51.950948, 4.5928356), (52.1938046, 4.4244188), (51.6068113, 4.7883534), (51.5998006, 4.7173071), (51.5516139, 4.7950965), (52.4956405, 4.5924236), (52.4172699, 5.877917), (52.6778105, 6.488065), (52.6495825, 6.6184239), (52.3166146, 6.9862726), (52.3330098, 6.280498), (50.8566454, 6.0265822), (52.4953277, 6.6704069), (51.5711486, 4.4107877), (52.6110498, 6.5763698), (52.4043298, 6.3679786), (51.9351061, 4.2722157), (52.3847443, 6.3246231), (52.5115156, 6.5773642), (52.3272745, 6.3448989), (51.588441, 4.2170916), (51.9764642, 4.3167383), (50.8341119, 5.975716), (52.6596347, 4.7850064), (52.4177241, 5.8172478), (52.43064, 5.84032), (51.4666303, 5.549687), (51.5068064, 5.4891661), (52.4521051, 4.6030904), (52.4706695, 4.6449189), (52.3175118, 5.9737885), (52.473826, 6.0228942), (51.9591278, 4.4944888), (52.4461502, 4.6437511), (51.4171638, 5.5635393), (50.8760025, 5.8822928), (52.3429444, 6.0473279), (52.4658999, 5.8916643), (50.8636518, 5.8934344), (51.4390835, 5.6188914), (52.4691, 4.64619), (51.3499459, 5.6116734), (52.2530729, 5.3314514), (52.48192, 6.636495), (52.6000468, 4.8907497), (52.158934, 5.962537176), (52.1601363, 5.9659213), (52.2567326, 5.9902803), (52.2042807, 5.9659291), (52.1177654, 5.881892), (52.2325231, 5.9671942), (52.2337861, 5.9656009), (52.22964, 5.95894), (52.1680676, 6.0637738), (52.1636915, 5.9843314), (52.1138389, 6.0186643), (52.1226947, 6.021276), (52.1157594, 6.0205456), (52.1582552, 6.0068594), (52.202303, 5.9310077), (52.2153906, 5.9967018), (52.2551231, 5.7798369), (52.2019387, 5.9706496), (52.21063, 5.98345), (52.2548017, 5.9628771), (52.1977294, 5.9564375), (52.19218, 5.9595), (52.2358801, 5.9973631), (52.1823286, 5.9348702), (52.095737, 4.3164207), (52.0356015, 4.2649606), (52.0549172, 4.3039721), (52.0529682, 4.3168613), (52.21538, 5.95845), (52.1589224, 5.3824207), (52.07743115, 4.315999145), (52.4610936, 4.6233078), (51.92106, 4.48911), (52.0915106, 5.1191159), (52.2191018, 6.8933855), (52.3010493, 4.6978423), (52.2628288, 7.0059411), (53.2430882, 6.5290584), (52.4781819, 5.5015475), (52.38054, 4.63066), (52.1589224, 5.3824207), (51.8453713, 5.86686), (52.5758295, 6.6228007), (52.091656341, 5.119627565), (53.2001111, 5.8005258), (52.1688211, 5.3910188), (52.1505086, 5.4010114), (52.216499, 6.0067153), (52.22528, 5.98329), (52.004084, 5.911696), (51.9470968, 5.8594146), (51.9881064, 5.8809763), (51.959103, 5.920837), (51.967847, 5.9159154), (51.9691969, 5.8977331), (51.9395759, 5.903116), (51.9403057, 5.894747), (51.9541887, 5.8469812), (51.957662, 5.838568), (52.1836277, 5.9486564), (52.23181, 6.89583), (51.9429556, 4.3657598), (52.10483, 5.0434), (51.9142065, 4.5623313), (51.45006, 3.5898), (52.4080958, 4.9168938), (52.3154929, 4.953512), (52.3787404, 4.8512146), (52.3326071, 4.8759451), (52.35329, 4.90622), (52.366904, 4.8688921), (52.3414979, 4.9612808), (52.3324721, 4.9385384), (52.3780277, 4.8025806), (52.3566548, 4.996847), (52.3641458, 4.9388798), (52.358075, 4.9262497), (52.3693887, 4.8502319), (52.4178054, 4.8929718), (52.3453125, 4.85578), (52.37616, 4.9082), (52.3589218, 4.8068695), (52.2953275, 4.9075754), (52.296222, 4.9761618), (52.3525874, 4.8825724), (52.3793298, 4.8201747), (52.3559939, 4.8256208), (52.3889092, 4.8799997), (52.3796205, 4.8720052), (52.3909375, 4.9117961), (52.3931052, 4.9549234), (51.9807831, 5.904308), (51.9617013, 5.8618218), (52.0039373, 5.9423836), (51.9555103, 4.9621087), (51.8723937, 4.7832548), (51.8924575, 4.5792452), (51.8239651, 4.8845321), (51.7925931, 4.6981566), (51.8349051, 4.9608966), (51.9240592, 4.8217493), (51.8342758, 4.5640387), (51.9635677, 5.0333362), (51.8143817, 4.6023327), (51.9204653, 4.9957), (51.8871058, 4.6824299), (51.9306078, 4.8741061), (51.8849161, 4.604296), (51.7848671, 4.6767646), (52.253322, 6.152572), (51.8145238, 4.6648565), (51.92448, 4.46995), (51.8853943, 5.5459884), (51.8141786, 5.0926245), (51.8902209, 5.6643959), (51.8840328, 5.2459089), (51.8547778, 5.6090658), (51.9457687, 5.2348588), (51.8199371, 5.5353727), (51.7636824, 5.1667443), (51.8676547, 5.4053427), (51.8248737, 5.0489027)]
#https://jupyter-gmaps.readthedocs.io/en/latest/tutorial.html
#$ jupyter nbextension enable --py --sys-prefix widgetsnbextension  
#$ pip install gmaps 
#$ jupyter nbextension enable --py --sys-prefix gmaps

import gmaps
from ipywidgets.embed import embed_minimal_html
gmaps.configure(api_key="AIzaSyD6pYFqFOwAe2FPbtu0FjqWrY6CFfi8zGU") # Your Google API key

figure_layout = {
    'width': '800px',
    'height': '1000px',
    'border': '1px solid black',
    'padding': '1px',
    'margin': '0 auto 0 auto'
}

fig = gmaps.figure(map_type='ROADMAP',layout=figure_layout, zoom_level=8,center=(52.088889, 5.115556))

heatmap_layer = gmaps.heatmap_layer(coor_list)# ,max_intensity=2,point_radius=7)
fig.add_layer(heatmap_layer)

embed_minimal_html('DutchPublicLibrariesGoogleMapsHeatmap.html', views=[fig], title='Dutch public libraries heat map') #https://stackoverflow.com/questions/28952112/python-have-gps-coordinates-and-corrsponding-values-generate-a-2d-heat-map