Plotting Polygons with Folium and Pyproj - json

I'm trying to plot the boundaries of the localities of Brussels. The system of coordinates of my json file has to be converted to a longlat system to display the Polygons on Folium maps. The issue I get is that my coordinates are projected into the Pacific ocean. I guess it is probably due to the fact that the parameters I set are not the good ones. Please find below my code:
import json
import pyproj
import folium
# Load JSON file
with open("districts.json", "r") as f:
data = json.load(f)
# Create a transformation object
in_proj = pyproj.Proj(proj='utm',zone=31,datum='WGS84')
out_proj = pyproj.Proj(proj='longlat',datum='WGS84')
# Transform the coordinates
features = data["features"]
for feature in features:
coords = feature["geometry"]["coordinates"][0]
coords = [pyproj.transform(in_proj, out_proj, coord[0], coord[1]) for coord in coords]
feature["geometry"]["coordinates"] = [coords]
# Plot the polyggon on a map
m = folium.Map()
folium.GeoJson(data).add_to(m)
m
This corresponds to how my json file is structured:
{"type":"FeatureCollection","features":[{"geometry":{"type":"Polygon","coordinates":[[[152914.748398394,173305.19242333],[152947.4133984,173326.530423339],...,[152961.983398418,173225.325423267],[152914.748398394,173305.19242333]]]},...
(https://i.stack.imgur.com/SuU4Q.png)
(https://i.stack.imgur.com/oIKJN.png)
Does anyone has an idea how to solve this? How could I find the right parameters?
I tried different zones but I would rather know of to find the right zone number and understand how it works.

Related

Click on folium multipolygon overlays and recive a name of active layers

I have many GeoJson layers in the folium map, some are overlapped and some are not, at one point. And I would like to know how I can click on the map and receive a popup (probably) with the name of the layer that exists at this point of the click.
It is area of specie distribution, and I want to click and know what species are there.
I try:
import geopandas as gpd
import folium
import webbrowser
maptotal=folium.Map()
#loading
sp1 = gpd.read_file('sp1.json', driver='GeoJSON')
sp2 = gpd.read_file('sp2.json', driver='GeoJSON')
sp3 = gpd.read_file('sp3.json', driver='GeoJSON')
sp4 = gpd.read_file('sp4.json', driver='GeoJSON')
#Try to create popup
for index, in sp1.iterrows():
mapsp1 = folium.features.GeoJson(sp1.geometry)
popup=folium.Popup("""Specie one""")
popup.add_to(mapsp1)
mapsp1.add_to(maptotal)
for index, in sp2.iterrows():
mapsp2 = folium.features.GeoJson(sp2.geometry)
popup=folium.Popup("""Specie two""")
popup.add_to(mapsp2)
mapsp2.add_to(maptotal)
for index, in sp3.iterrows():
mapsp3 = folium.features.GeoJson(sp3.geometry)
popup=folium.Popup("""Specie three""")
popup.add_to(mapsp3)
mapsp3.add_to(maptotal)
for index, in sp4.iterrows():
mapsp4 = folium.features.GeoJson(sp4.geometry)
popup=folium.Popup("""Specie four""")
popup.add_to(mapsp4)
mapsp4.add_to(maptotal)
#create layers
principal=folium.plugins.MarkerCluster(control=False)
maptotal.add_child(principal)
camadasub1=plugins.FeatureGroupSubGroup(camadaprincipal,'sp1')
maptotal.add_child(camadasub1)
camadasub2=plugins.FeatureGroupSubGroup(camadaprincipal,'sp2')
maptotal.add_child(camadasub2)
camadasub3=plugins.FeatureGroupSubGroup(camadaprincipal,'sp3')
maptotal.add_child(camadasub3)
camadasub4=plugins.FeatureGroupSubGroup(camadaprincipal,'sp4')
maptotal.add_child(camadasub4)
folium.GeoJson('sp1.json').add_to(camadasub1)
folium.GeoJson('sp2.json').add_to(camadasub2)
folium.GeoJson('sp3.json').add_to(camadasub3)
folium.GeoJson('sp4.json').add_to(camadasub4)
folium.LayerControl().add_to(maptotal)
#save and open
maptotal.save("map.html")
webbrowser.open("map.html")
My idea of result:
Map

Gets all polygons from a json file

I have a json file, with the coordinates of several polygons. What I want to do is automate the getting the coordinates but saving them separately by polygons. In the file that I currently use, I have 2 polygons:
{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[
[[[0.03990083369766674,41.65117297365802],[0.03986585098450487,41.6511738730363],[0.0398193071116094,41.65118861345209],[0.03978640897789055,41.651211227828185],[0.039785212880236676,41.65121205009294],[0.039761889395081365,41.65124414021116],[0.03975127958141444,41.65127097990489],[0.03973309671778063,41.65131697476312],[0.039706482497244025,41.651425988964064],[0.03969658765996025,41.65146652049745],[0.039683272167788275,41.65150292059053],[0.03968380609505212,41.65151410540278],[0.03968455879000179,41.6515298809837],[0.03969278143750318,41.65153853529924],[0.03969588944738445,41.651541805918015],[0.03971376972429572,41.65156062329174],[0.039752407785648564,41.65158215221135],[0.03979465677110295,41.65160350427274],[0.03988602119749911,41.651632606242316],[0.03992955596697664,41.65164948823451],[0.03993958742929026,41.65165619711021],[0.039968351608118506,41.651675434417314],[0.03999355934521909,41.651701840766634],[0.04000585559790465,41.65173745966629],[0.04000155651951174,41.65177364266802],[0.03998752269819514,41.65181002432095],[0.03993384247251406,41.65191499844431],[0.0399064931590255,41.65198778102845],[0.03987869457550017,41.65209657478536],[0.03987154648804941,41.65213727647139],[0.03986267424301351,41.65216458136081],[0.039847923768933036,41.65220997439829],[0.03980755819723356,41.65227854926681],[0.03978735697119576,41.65231062932676],[0.0397695957173166,41.65234709479867],[0.039757844288369676,41.65238344543852],[0.03975297859328857,41.65242411611149],[0.03975461138812371,41.65245838049552],[0.039754692692589313,41.652460093756645],[0.03976241996958375,41.65249586520693],[0.039775312175620324,41.65253159055675],[0.039792208417040606,41.65257165102716],[0.0398126778639959,41.65260712072893],[0.03984532034965278,41.652678297340344],[0.039888420936520154,41.652749203216246],[0.039899750755712435,41.652784978019326],[0.03990246230155005,41.65282539304425],[0.03990100971764384,41.65284035977143],[0.03989973315371473,41.652853510978275],[0.03989900644266644,41.65286150731441],[0.03988340855812481,41.65289793925871],[0.03982299431103434,41.65300310029855],[0.03980279308499652,41.653035180358444],[0.039796679324458795,41.65304764676378],[0.03978491196989489,41.653071643315826],[0.0397440929372068,41.65317631904341],[0.0397286140756973,41.65321275350229],[0.03970857042944851,41.65324925082547],[0.03966616386420511,41.6533061086308],[0.03966059408921564,41.6533135760688],[0.03962721818698753,41.653337031149846],[0.039608159414420085,41.65335700606448],[0.03959644067489746,41.653369288906106],[0.039574675385634625,41.65340141925745],[0.03955691413175548,41.653437884729385],[0.03954504200339567,41.653474232016464],[0.03952742659464041,41.65350945529025],[0.03952465972823856,41.65351498818487],[0.03951080444147017,41.653542693729264],[0.03949758031204822,41.6535615806728],[0.03948831998487233,41.6535748056404],[0.03938851917243118,41.65370353659338],[0.039367413538987255,41.65375064205381],[0.039358610025561444,41.653770290912405],[0.039341569615398024,41.653808324638504],[0.03932925156976292,41.65384916294957],[0.039294849722525456,41.653976004621455],[0.03927882436089661,41.65403509285139],[0.03927713624549725,41.654056445750996],[0.03927596780812548,41.65407122304717],[0.039281407663608055,41.65410711603502],[0.039310323554892744,41.654178376465495],[0.03936399288409906,41.65425614795873],[0.039382605739391195,41.65428311924833],[0.03939218541709694,41.654297000518994],[0.0394054858218423,41.65431627386832],[0.03942630395599031,41.65434644117781],[0.039450928312595376,41.6544222747749],[0.03945708817360331,41.65445818620299],[0.039456164487818746,41.654516564485675],[0.03945594907289441,41.654530202681144],[0.039453803305554584,41.65455188918051],[0.03945192324456115,41.65457089514709],[0.03943704788011598,41.65460725500702],[0.03941627919919963,41.65464382357635],[0.03934394253232732,41.654732627331185],[0.0393319027658941,41.654747407980096],[0.039311307590383746,41.65477269199252],[0.03926260956824479,41.6548369987957],[0.03925610688737672,41.65484747114613],[0.03921995489865273,41.65490569352545],[0.03921109941742416,41.65492864736862],[0.0392059194009554,41.6549420751783],[0.03919861708647711,41.65497826991475],[0.03919375139139601,41.65501894058772],[0.039193012107492216,41.655039557556165],[0.03919245806365955,41.655055020492085],[0.03919789791914211,41.655090913479995],[0.03921464077172523,41.65512646783898],[0.03923784355746767,41.65515732497917],[0.03926289874392143,41.65517922521707],[0.03928896814071942,41.655192054558825],[0.03933606186649572,41.65518633390957],[0.0394175440284636,41.655148178645845],[0.039442782778607764,41.65513278192702],[0.03945621058828894,41.65512459029256],[0.039527098862206865,41.65506869053877],[0.039586379037730636,41.65501163156773],[0.039623324792732284,41.654976069665025],[0.039725422246779336,41.654896838882365],[0.03976381052740279,41.654868829913084],[0.03980034640731491,41.65485428311925],[0.039848573366467556,41.654835081854316],[0.039941219385935034,41.654810092046716],[0.039994916375423455,41.6547901758054],[0.04003271457002846,41.654776157071524],[0.04003523752303347,41.65477534486507],[0.04007854095196967,41.65476139737734],[0.04011864668465535,41.654737845904435],[0.040155591601466624,41.654709888902886],[0.040182720470888646,41.654686606489065],[0.040188089918380375,41.654681998118434],[0.040242230310573,41.65462206415444],[0.04027888772808837,41.654571541391874],[0.040315545145603725,41.6545210177911],[0.04037410196284068,41.65445642432684],[0.04040103721028788,41.65442415735046],[0.040431813884187574,41.654391899594174],[0.04046347317254383,41.65436398701678],[0.04046785858454457,41.65436054037798],[0.04047288018303395,41.65435659333954],[0.040498973887352574,41.65433608282126],[0.04050050609934366,41.65433434776722],[0.04052434842433622,41.65430734714093],[0.040555966641364495,41.65427153964842],[0.04057773193062733,41.65423940929708],[0.04060973320065338,41.65417113617707],[0.04065305339339694,41.65410239115596],[0.04065650087037687,41.6540996050112],[0.04068771927060004,41.65407437631933],[0.04072866319365282,41.65405084580112],[0.040770455365357124,41.654027156026785],[0.040860539036872326,41.65399777745441],[0.040907804591673896,41.653983054640634],[0.04095089763482795,41.65397290750806],[0.04100058555979046,41.6539760792204],[0.04104998095631486,41.65398824890633],[0.04109435643073061,41.65400515185328],[0.04114391192161512,41.65403052052292],[0.04116956473780064,41.65404365328962],[0.04119017248616651,41.65406561806819],[0.04119417065421764,41.6541015638621],[0.04119530388759398,41.65412534919008],[0.04119588475351839,41.65413754066901],[0.04120276964919386,41.65417338085089],[0.04122281245725229,41.65419983413889],[0.04124421732465728,41.654211061698874],[0.04126361975527523,41.654221238168105],[0.04131172601501501,41.654237967609646],[0.04136353875255816,41.65424896382905],[0.04138485477178423,41.65425348754249],[0.04140638955869612,41.654258057356344],[0.04145406834134867,41.65426576954596],[0.04160685871060841,41.654275199187566],[0.04165954987165629,41.65427835748889],[0.04181220445407655,41.65426977106674],[0.041937059614781114,41.65426650044793],[0.04196128164000998,41.654265865937816],[0.04198126661293308,41.65426063060079],[0.04198913302952844,41.65425856949068],[0.04198995948523041,41.654258353237566],[0.04199706314859171,41.65425649161676],[0.04201822074983839,41.654250948663844],[0.04202009075254742,41.65425045916069],[0.04202343094116037,41.65424958408994],[0.042057054109546785,41.65424077554738],[0.042151707594943494,41.65421124274798],[0.04219497246712282,41.65419209345086],[0.04220322026033522,41.65418708861617],[0.04227241455152121,41.65414509695515],[0.04227500791251706,41.65414385391884],[0.042302370637051494,41.65413073875416],[0.042353914315486796,41.6541248613633],[0.042403152132222184,41.65413261378603],[0.042413530605347784,41.654134562578605],[0.042414217083258446,41.65413469165995],[0.04243573091541116,41.65413873089932],[0.042487086839204234,41.6541483734413],[0.04250871131248541,41.654161268161914],[0.04251341356044492,41.65417922052319],[0.04249180752735182,41.65421576813779],[0.04243910882259064,41.654275740658534],[0.042402066675696765,41.6543058694113],[0.04224077202697229,41.65443706045298],[0.042230817678172446,41.654445157371924],[0.042212416885043194,41.65446012326092],[0.042203314137656336,41.654467526996456],[0.042190945800599206,41.654477586957256],[0.04216446569052133,41.65449912509689],[0.042158375399313885,41.65450340238232],[0.04212451502305359,41.65452718519583],[0.04208356690904897,41.65455080623855],[0.04195181511800379,41.65460378908983],[0.04190542295756477,41.65462303729345],[0.04185803335058887,41.654637846440835],[0.04181464861718705,41.65465699238521],[0.0416459468801884,41.6547380219244],[0.041602715535623766,41.654761673980175],[0.041584732161297336,41.6547697449152],[0.0415343996679425,41.65479233414561],[0.041517508455664424,41.65479991473927],[0.041206645441451396,41.65492519234792],[0.041072091580008956,41.654967805108015],[0.041067007117242045,41.654969415271694],[0.04098074475561051,41.65500777505386],[0.0409415702524227,41.655031294675524],[0.040926787088917914,41.655043202846116],[0.04091264346466323,41.65505459552958],[0.04090137650974849,41.65507725684432],[0.04089677233006193,41.655086517171526],[0.04089737917988772,41.65510847356819],[0.04089776642383733,41.65512247553826],[0.040903428399767176,41.65513149865757],[0.04091724010063649,41.65515350702202],[0.04094500934749897,41.655184301297936],[0.04096908636579433,41.65521968382788],[0.04098133567981932,41.65523302865674],[0.04105383830838432,41.65531201552599],[0.04109895139032312,41.655378379248276],[0.04112916228572502,41.65541355977439],[0.04115775631190874,41.65544364997035],[0.04118758834526468,41.65547504355237],[0.04120909547189447,41.65548727945534],[0.041225389892631165,41.65549654984082],[0.04127064379055165,41.655517888491175],[0.04131000940114316,41.65553934616457],[0.041343394523465304,41.655565511953284],[0.04137345119183965,41.65559618552976],[0.04137836801653314,41.65560232024504],[0.04139821468804566,41.65562708290304],[0.04142181226145102,41.65566441590201],[0.04177131668977836,41.6555764746451],[0.04208765141070791,41.65549688008781],[0.04280846076062087,41.655315512456184],[0.04502639796742188,41.65475740088567],[0.04503264248565688,41.65475582927874],[0.04491913726071341,41.65471054688234],[0.04481832056154731,41.65463496222779],[0.04490633893199795,41.65458509157733],[0.04516969080163186,41.654501499690184],[0.0454428671005587,41.654456392475595],[0.04567295370909296,41.65444145676145],[0.04576299295651871,41.65449938493592],[0.04582947067272489,41.65455530648262],[0.04584204604280269,41.65455214147582],[0.04669402212688459,41.6543377323799],[0.04674658839756776,41.65432450405953],[0.04758604107937462,41.6541131971062],[0.04747183177452276,41.65406386792671],[0.04745190379854463,41.6540552605498],[0.04730123069815222,41.653990180935125],[0.04693486523240001,41.653832617071714],[0.04660293514149995,41.653688814617794],[0.046272875891499275,41.65354614120625],[0.04589455364016018,41.65338232192815],[0.04559773953469034,41.65325319954027],[0.04547678028281212,41.65320111690542],[0.04547537212299538,41.65320051089381],[0.04545764355854056,41.65319287749412],[0.04542093836417426,41.65317707257657],[0.0454185159940133,41.65317602902954],[0.04540317375753365,41.653169423251285],[0.04538960261729971,41.653163579388035],[0.04530522534580381,41.65312724886476],[0.045198634353198666,41.65308073013756],[0.044926965120551894,41.65296216727194],[0.044682048409852286,41.65285887288209],[0.044474430332298885,41.65278022380352],[0.04404397599288677,41.65262487360093],[0.043744324613024264,41.65251593567527],[0.04336616161785493,41.652380391072825],[0.04304298726352973,41.652263371315655],[0.04280758401349688,41.652177968937366],[0.042621828455288904,41.6521115809075],[0.042493676691870486,41.65206577967129],[0.04213919341276249,41.651937684904794],[0.04190932641010441,41.65185728652298],[0.04175594344025513,41.65180033484029],[0.041663035905393116,41.651765838277555],[0.04139794143798598,41.65166988811163],[0.041386050031247734,41.65166558400412],[0.04132406082445745,41.65164317163189],[0.04112134448417094,41.651569877751626],[0.041083383680634716,41.65155590847098],[0.040914122032470836,41.65149362003021],[0.04079188789304423,41.65144863770598],[0.04053546785774635,41.651355575105924],[0.04031697006922782,41.65127609705709],[0.040218658721069014,41.65124693138503],[0.040136785962390065,41.65122264230462],[0.04003391569682453,41.65119818390971],[0.03995920862759403,41.6511804218176],[0.03990083369766674,41.65117297365802]]],
Here starts second polygon
[[[0.04758604107937462,41.6541131971062],[0.04674658839756776,41.65432450405953],[0.04669402212688459,41.6543377323799],[0.04584204604280269,41.65455214147582],[0.04582947067272489,41.65455530648262],[0.04595265951097963,41.65465893279591],[0.04610042744355961,41.65482261628716],[0.04614809197697591,41.654895094608236],[0.045911219379229505,41.654906380003325],[0.045561253108009875,41.65490780408874],[0.045443292901265184,41.65490303143281],[0.04522180361132643,41.654831294910224],[0.04503264248565688,41.65475582927874],[0.04502639796742188,41.65475740088567],[0.04280846076062087,41.655315512456184],[0.04208765141070791,41.65549688008781],[0.04177131668977836,41.6555764746451],[0.04142181226145102,41.65566441590201],[0.0414310650449136,41.65567905489678],[0.04144861088386839,41.65570681408536],[0.041465396484160214,41.655733370470756],[0.04149721083773439,41.655762378563],[0.04149725945277568,41.65576451343384],[0.04150375878088228,41.65605208229957],[0.041496843710353594,41.656059615954575],[0.041444482796310356,41.656116656485466],[0.04141456275296585,41.656135429435096],[0.04136614468640951,41.65614120624315],[0.04132706825149465,41.65614227074488],[0.04128124354593417,41.65612551029031],[0.04119501722648842,41.65607768063336],[0.04116495469078151,41.65606100567422],[0.04112186332400818,41.656039632658064],[0.04108033518226952,41.656018209350464],[0.041008348878970674,41.655995706453695],[0.040987399987125396,41.65598915767234],[0.040937129519857736,41.655972462596594],[0.040889723149074506,41.6559692598712],[0.04078962477905301,41.65596289884451],[0.04076305582079624,41.65595886211969],[0.04074718720076604,41.65595645148423],[0.0407389427603151,41.65595519838962],[0.04072028045179132,41.65594841407679],[0.04069311805475462,41.65593843793502],[0.04065432325180311,41.65591240206584],[0.04061847971713421,41.65588179722096],[0.040587874034069424,41.65585566495989],[0.04056173925842281,41.655836514824585],[0.04055220568118726,41.65582952934605],[0.04054751935884471,41.65582596871337],[0.04051785663994292,41.655803428936224],[0.0404244830712368,41.65571582798458],[0.04030450534027847,41.655592959335024],[0.04026526042909982,41.65557150501439],[0.04022374821297813,41.6555680935796],[0.0401938801374364,41.6555724697715],[0.0401866339817127,41.655573531758705],[0.04014841753011451,41.65559253940168],[0.04012136158325442,41.65562480302526],[0.04012063068125431,41.65562618352479],[0.040102036266150255,41.65566131878862],[0.040089356960466914,41.65569211977004],[0.04005893316471178,41.65576602552929],[0.040051750711455975,41.65580222361845],[0.04004544165256271,41.655842947097426],[0.040032126160390744,41.65587934802872],[0.040015807432133406,41.655915760694654],[0.03997528260426417,41.655979917461735],[0.039961119701631055,41.65598799258771],[0.03995049815329898,41.65599404851312],[0.039942853857150906,41.6559942496788],[0.03985371398798907,41.65599659745004],[0.03980228933258589,41.65600247651727],[0.03975174226249707,41.656012791287935],[0.03970507517561764,41.65602752835093],[0.03966397115820474,41.65604664160588],[0.03963545676010589,41.65606094616271],[0.039617866497061636,41.65608841114647],[0.03961844149565348,41.65616497480735],[0.03961414577002202,41.6562010672846],[0.039597102845287495,41.65623755119668],[0.03956400354588053,41.656265426893725],[0.03952633359440169,41.65629345430321],[0.039458177821080555,41.65634474149539],[0.0394236686854761,41.65637717275709],[0.03940910093689566,41.65639558109393],[0.03939817512546034,41.656409386927464],[0.03938098048826936,41.65644315007364],[0.039379568975691136,41.65644592113097],[0.03936309685859661,41.65647782684732],[0.03934677310119708,41.656514330037794],[0.03932541265788153,41.656568643097216],[0.039303821712215053,41.65662354288986],[0.03925695932698007,41.65669230970397],[0.03923426867555194,41.65671936732721],[0.039229902541929644,41.65672457332758],[0.03919439679797868,41.65678399683367],[0.039194100916779086,41.656824514956014],[0.039190951835569844,41.65686964228718],[0.03919596421396519,41.65689651886129],[0.03922662018845202,41.65692720836341],[0.039295667796323756,41.65696968449845],[0.03930393654429534,41.65697477063759],[0.03930853485664933,41.65697727682681],[0.03934330215488685,41.65699622831099],[0.039396235552950826,41.657026310125076],[0.0395476487754374,41.65711235707178],[0.03966764578477417,41.65719028782115],[0.03969654491225152,41.65721413098436],[0.039736377394877516,41.657246994752256],[0.03979621748156652,41.657291811953],[0.039811843864580615,41.65730351476691],[0.03987315665174465,41.65736489293292],[0.039900202540320305,41.65739576013138],[0.03990380759708926,41.65740158639261],[0.03994403067642632,41.65746659392292],[0.03996293773653732,41.65750211391611],[0.039985982104300384,41.657550193353785],[0.039997026100577475,41.65757323688334],[0.04004428075890426,41.65768459550276],[0.04005657533520909,41.657720304926954],[0.04006502261772887,41.65775609565563],[0.040096527679057574,41.6578678582828],[0.04011371812529672,41.657898920779616],[0.040169337085296936,41.657958161560174],[0.040171425855691785,41.65796038611745],[0.04020865827179899,41.657986472278054],[0.04022491162120769,41.65799241672411],[0.04025448381554984,41.6580032327326],[0.04025571427900875,41.65800344479476],[0.040299878529452,41.65801106729796],[0.04034742403983617,41.658000765100184],[0.04052384467192559,41.65793758817762],[0.040566616688169035,41.65792652238841],[0.04091163679803233,41.65783725930527],[0.0410018461981026,41.65780788324747],[0.0410316874515526,41.657801838218546],[0.04105311914104934,41.65779749723063],[0.04106175166364024,41.6577980143941],[0.04110581197847795,41.65780065553193],[0.04113990704804105,41.65780873820165],[0.04117413706625324,41.65783483693511],[0.041195893973612424,41.65786583656762],[0.04122952468571213,41.65789191937549],[0.041266762130961546,41.65791791417334],[0.04137407564366314,41.657976714065796],[0.041394564368996876,41.65798793994935],[0.041404999839067445,41.65799365808405],[0.04141233987211226,41.657997679721404],[0.04143147156724164,41.65800816296834],[0.04147856696939867,41.65803060300087],[0.0414794789205181,41.65803103718349],[0.04148032633097924,41.65803144119124],[0.0414911649706164,41.658036605282064],[0.041556750014081593,41.65806785553352],[0.04159884477250843,41.658084790331685],[0.04174148968556461,41.65813502391859],[0.04178860185152899,41.658147315142145],[0.041837880739592354,41.65815948147531],[0.04190665761197552,41.65818563888212],[0.04192652272367613,41.65819319433007],[0.04199795917409414,41.6582092632776],[0.04205417995477795,41.658222021373206],[0.04212590725725336,41.65823829819192],[0.04219359029120745,41.658253657192205],[0.04219840653305688,41.658254578363426],[0.042251606475657615,41.65826475147992],[0.04233834912036951,41.658281340105475],[0.042386751261308864,41.658289070735236],[0.042434450998720584,41.65828327464885],[0.04244071647171477,41.658280066056136],[0.042449364919922634,41.658275636220026],[0.04246224203853262,41.658269040500016],[0.042466747311755856,41.65826276832152],[0.0424715836701741,41.658256035138294],[0.042481994832724024,41.65824154115046],[0.04248371396116697,41.65821447849808],[0.04249443776872383,41.65810614490769],[0.04251305816772927,41.658087623415156],[0.04252125650771001,41.6581165426592],[0.042528257073656144,41.658141238262004],[0.04252997201114726,41.65817721506892],[0.042525380404316214,41.65825391619305],[0.04252770805896568,41.6583269493961],[0.042530111988938565,41.65840236305979],[0.042517528236957085,41.65856888801639],[0.04251579402108752,41.65859183515411],[0.042511053216371136,41.658633764789016],[0.04250610789320516,41.65867749988601],[0.04254461184409927,41.65862743226083],[0.04255568350065848,41.658613035503095],[0.042704411161208815,41.65841963983965],[0.04389609658098795,41.656869663241935],[0.043987920335705304,41.65675023201109],[0.044215711140824024,41.656453861308336],[0.0448310778189348,41.65671995154588],[0.04495933435614909,41.65677541041151],[0.04496666852186133,41.656778585476644],[0.04500843722423537,41.656796666919234],[0.045306469220308614,41.65692552527713],[0.04549528584973728,41.65700716334251],[0.045545746586218266,41.657028981437776],[0.04558705093112892,41.65704683992175],[0.04568211848256698,41.65708794393916],[0.04574764988184867,41.65711627728814],[0.04576830792163658,41.65712520904469],[0.04580129155077288,41.65713947001558],[0.0457895258725897,41.65711052897859],[0.04577522969768819,41.65705288411249],[0.04577237314491708,41.65699193174714],[0.04578172818760443,41.65692901801643],[0.045785033172221955,41.65690679256062],[0.04581981304331499,41.65687886908674],[0.045835246642544666,41.656861878968016],[0.04584914970616399,41.6568465727737],[0.04588982037915708,41.65681853027678],[0.045990798849064034,41.65676628251842],[0.04599208128032572,41.656765130006676],[0.04599548600959694,41.65676206893548],[0.046000725537581784,41.656757359143796],[0.04600219404710496,41.65675603899396],[0.046002961829481236,41.65675534916326],[0.04600879479624598,41.656750106282544],[0.04615034335630198,41.656622871499366],[0.0461992794245052,41.656603146365455],[0.04624594567319427,41.656588406787876],[0.0464349617919303,41.65653842214354],[0.046531061155710284,41.65650883318537],[0.04661638809689212,41.65647059158803],[0.04672136557304057,41.65639124848792],[0.04680226351632258,41.6563395639935],[0.04681761497289628,41.656332575162224],[0.046844204885912226,41.65632047001694],[0.04693414019773245,41.65628648894125],[0.04697996238872184,41.65627181725705],[0.047126162067124965,41.656236401199465],[0.04721121408186638,41.65619364846165],[0.0472491547688338,41.65617012883993],[0.04725368099681621,41.65616549113261],[0.04733935830157151,41.65607770745547],[0.04737087928851719,41.65602779405734],[0.0473798764239178,41.65601354733565],[0.0473980157016525,41.65598699178841],[0.04742684945028122,41.655944779683324],[0.04744455622178651,41.65592366063885],[0.04745390204437982,41.655912514383346],[0.04748784204872501,41.65588456827834],[0.04752534939127262,41.655852121091016],[0.047538546698602836,41.6558445078079],[0.04756629666708687,41.65582849753369],[0.04761112476430087,41.655809297107],[0.04765050797689009,41.65578572384109],[0.047725545293125234,41.65573875165293],[0.04775933023225249,41.65570629859829],[0.04778409540483924,41.655674153997694],[0.04780185246776655,41.65563768684939],[0.047806942797866035,41.65561827519866],[0.04781190739941044,41.65561704138247],[0.04781096359705707,41.6556121924512],[0.04783401467034309,41.655517336961935],[0.04786700165224085,41.6554508089543],[0.047859350650569836,41.655290596409316],[0.04779664311463493,41.65510650968815],[0.04767336794277238,41.65501912079867],[0.04745843833064664,41.654971486440076],[0.04732087117483444,41.654951105003136],[0.047169374133311154,41.6549079080244],[0.04712811924163216,41.65490965481308],[0.047008370336644074,41.6549907941552],[0.04684413950706359,41.65506236890701],[0.04678805702779036,41.655060926381395],[0.046692642465540314,41.655019170251876],[0.04661758922368819,41.654941443182764],[0.04660133084513729,41.654887686681775],[0.0466046643282273,41.654814781721825],[0.046499787434922904,41.65457065710103],[0.04650519292460042,41.654534300593866],[0.046667114539719494,41.65451319915135],[0.046984142444082645,41.65453819566446],[0.047065498877495456,41.65455993832262],[0.047178475204317276,41.65470882356297],[0.047246051788094746,41.65478635784831],[0.04724850852406075,41.654814494222535],[0.04725853244266107,41.65481555537153],[0.04725382432736899,41.654811863981166],[0.047251745615258536,41.6548102345391],[0.04728646094569332,41.654799684236934],[0.04731677158574888,41.65479047252478],[0.04736320900846773,41.65477777226437],[0.04740797256502346,41.65476552965586],[0.04742911591703391,41.65476109981975],[0.04747934698935431,41.65475057633972],[0.04749266751066849,41.65474778600395],[0.047493383325242,41.65474763596788],[0.04749877624206401,41.654746506087264],[0.04755576648150482,41.6547345669037],[0.04760358356556794,41.65472464692073],[0.04760559186968755,41.654724230340065],[0.04761344487523703,41.65472404929099],[0.047614034961255484,41.65472403587992],[0.0476151380197786,41.654724009896],[0.04761622850544622,41.65472398475029],[0.04762048735070153,41.654723886682035],[0.04762115119747228,41.6547238715946],[0.0476230496986538,41.65472382800871],[0.04762590708961529,41.654723761791644],[0.0478007703640026,41.65471006492285],[0.04784846255770102,41.65470435684648],[0.04789396204539906,41.65469408901447],[0.04793521693707806,41.65467947935636],[0.04802098644277373,41.654636745058696],[0.04802561912093276,41.65463492953836],[0.048070382677488484,41.654617391243136],[0.04811936484616189,41.654607123411154],[0.04815142730408473,41.65460315290338],[0.04816573772822247,41.65460138013074],[0.048209999208748286,41.654586668213426],[0.0482446240146234,41.6545796123269],[0.048337147658297275,41.654560757234606],[0.04849303849372496,41.654521229015074],[0.04849366881288103,41.6545210689207],[0.04849580955107865,41.65452052661152],[0.048506965864864936,41.65451811178508],[0.04848377313740689,41.65450714406413],[0.04846406644368568,41.65449782674],[0.04835557694989901,41.65444649680012],[0.048056706763458655,41.65431729394601],[0.047935193467747765,41.65426476287928],[0.04789959384647571,41.65424928150321],[0.047679226055248145,41.6541534461694],[0.04758604107937462,41.6541131971062]]]
]}}
saved as poly1.json
Is there any way to save them separately, I would like the final result to be an like this:
array = [coordinates of polygon 1], [coordinates of polygon 2] where in the case of using more polygons than in the example, they will be saved automatically without having to change the code, array = [coordinates of polygon 1], [coordinates of polygon 2],[coordinates of polygon 3] and so on.
So far i have this :
require 'rubygems'
require 'json'
file = File.read('poly1.json')
coordinates = JSON.parse(file)
string=coordinates.to_s[0...-4]
path = string.split"["
path.shift()
path.shift()
path.shift()
This just gets all coordinates from both polygons in path. But what i want is to get polygon 1 coordinates separated from polygon 2 coordinates and in a case where i use a 3 polygon file, to get it separated whit out doing manualy.
Sorry for my bad english, and if you need something else ask in comments.
Your example is quite big and therefore not easy to understand. But from key structure at the beginning of the file
{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[ [[[0.03990083369766674,41.65117297365802], ...
it looks like the coordinates are a simple hash nested below the geometry key. Therefore I would try the following:
require 'rubygems'
require 'json'
file = File.read('poly1.json')
data = JSON.parse(file)
coordinates = data["geometry"]["coordinates"]
coordinates[0] #=> should now return the first coordonate from the array

Exporting Interactive Map as a HTML Issue in R

I have used the following code to produce a map on R. The issue is when I try and export as a HTML I get this error:
popup.css not found in resource path
When I export as a local html it works fine. Does anyone know why this is and how to fix it? Any help would be greatly appreciated!
#Load shape file
UK_Map<-gadm36_GBR_2_sp
plot(UK_Map)
#Retail Data and Postcodes
Benchmark_Retail <- read_excel("C:/Users/andrew.smith2/Desktop/Benchmark output_v3 (1920).xlsx")
Postcodes <- read_excel("C:/Users/andrew.smith2/Desktop/Postcodes.xlsx")
#Merge Datasets
Retail_data<-merge(Benchmark_Retail,Postcodes,by="Postcode")
Retail_data2<-as.tibble(Retail_data)
locations_sf <- st_as_sf(Retail_data2, coords = c("longitude", "latitude"), crs = 4326)
m=mapview(locations_sf)
mapview(locations_sf)
mapshot(m, url = paste0(getwd(), "/map.html"))

How different colors to different sections of a route on leaflet map? [R Studio]

I have a JSON file of a long route. The file contains the lat and long of of this route.
I'm trying to mark different sections of this route based on a set of criteria (which I've compiled in a dataframe). However, I'm facing to problems:
1) How do I break up this long set of lat and longs into segments? (can't do this manually because I have many route variations)
2) How do I assign a variable color to each segment?
I intend to use leaflet map (for its interactivity), but I'm open to better suggestions.
When working with spatial data, it helps to know spatial classes! I am assuming you know hoe to read your JSON file as a data frame into R.
Here's a reproducible example:
library(mapview)
library(sp)
### create some random data with coordinates from (data("breweries91", package = "mapview"))
set.seed(123)
dat <- data.frame(val = as.integer(rnorm(32, 10, 2)),
lon = coordinates(breweries91)[, 1],
lat = coordinates(breweries91)[, 2])
### state condition for creation of lines
cond <- c(8, 9, 10)
### loop through conditions and create a SpatialLines object for each condition
lns <- lapply(seq(cond), function(i) {
ind <- dat$val == cond[i]
sub_dat <- dat[ind, ]
coords <- cbind(sub_dat$lon, sub_dat$lat)
ln <- coords2Lines(coords, ID = as.character(cond[i]))
proj4string(ln) <- "+init=epsg:4326"
return(ln)
})
### view lines with mapview
mapview(lns[[1]], col = "darkred") +
mapview(lns[[2]], col = "forestgreen") +
mapview(lns[[3]], col = "cornflowerblue")
Essentially, what we are doing here is create a valid sp::SpatialLines object for each condition we specify. The we plot those using mapview given you mentioned interactivity. Plotting of spatial objects can be achieved in many ways (base, lattice, ggplot2, leaflet, ...) so there's many options to choose. Have a look at sp Gallery for a nice tutorial.
Note: This answer is only valid for non-projected geographic coordinates (i.e. latitude/longitude)!

Can I load a .csv into Simplegeo Storage?

Is there a straightforward way to load a .csv file into Simplegeo Storage? I don't have great coding skills and I'm trying to get things set up so I can ask a freelancer to create some maps for my app. If someone has existing code to do this I can probably figure out how to make it work for my situation.
I just skimmed over the api. Here's a basic example in python
Assumed csv format:
layer, id, lat, lon
python
from simplegeo.models import Record, Client
lines = open('file.csv').split('\n')
client = Client('your-oauth-token', 'your-oauth-secret')
for line in lines:
parts = line.split(',')
if len(parts) == 4:
layer = parts[0].strip()
id = parts[1].strip()
lat = float(parts[2].strip())
lon = float(parts[3].strip())
r = Record(layer, id, lat, lon)
client.storage.add_record(r)
After a bit more digging, I found a python example on their site for this exact purpose
https://simplegeo.com/docs/tutorials/general-hackery#how-import-csv-file-simplegeo
import csv
import simplegeo
OAUTH_TOKEN = '[insert_oauth_token_here]'
OAUTH_SECRET = '[insert_oauth_secret_here]'
CSV_FILE = '[insert_csv_file_here]'
LAYER = '[insert_layer_name_here]'
client = simplegeo.Client(OAUTH_TOKEN, OAUTH_SECRET)
def insert(data):
layer = LAYER
id=data.pop("id")
lat=data.pop("latitude")
lon=data.pop("longitude")
# Grab more columns if you wish
record = simplegeo.Record(layer,id,lat,lon,**data)
client.add_record(record)
r = csv.DictReader(open(CSV_FILE, mode='U'))
for l in r:
insert(l)