require(RJSONIO)
## Loading required package: RJSONIO
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:RJSONIO':
## 
##     fromJSON, toJSON
linha = 5983
path = paste('http://www.poatransporte.com.br/php/facades/process.php?a=il&p=', linha, sep = "")
path
## [1] "http://www.poatransporte.com.br/php/facades/process.php?a=il&p=5983"
json_file <- fromJSON(path)

json_file <- lapply(json_file, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

#View(json_file)

json_file$idlinha
## [1] "5983"
json_file$nome
## [1] "CARLOS GOMES / PETROPOLIS"
json_file$codigo
## [1] "432-1"
json_file$`0`
##                     lat                     lng 
## "-30.02077606310900000" "-51.18162228456700000"
length(json_file)
## [1] 166
aa = do.call("rbind", json_file)
class(aa)
## [1] "matrix"
dim(aa)
## [1] 166   2
aa = as.data.frame(aa)
head(aa)
bb = fromJSON(path)
class(bb)
## [1] "list"
length(bb)
## [1] 166
bb[4]
## $`0`
## $`0`$lat
## [1] "-30.02077606310900000"
## 
## $`0`$lng
## [1] "-51.18162228456700000"
loop = c(4:length(aa$lat))
for(i in loop){
  print( paste((i-3), aa[i,1], aa[i,2], sep = ";"))
}
## [1] "1;-30.02077606310900000;-51.18162228456700000"
## [1] "2;-30.02092506310900000;-51.18160628456700000"
## [1] "3;-30.02128306310900000;-51.18260528456700000"
## [1] "4;-30.02160406310900000;-51.18348528456700000"
## [1] "5;-30.02177706310900000;-51.18393028456700000"
## [1] "6;-30.02198906310900000;-51.18452228456700000"
## [1] "7;-30.02199206310900000;-51.18458428456700000"
## [1] "8;-30.02199306310900000;-51.18505128456700000"
## [1] "9;-30.02198606310900000;-51.18517828456700000"
## [1] "10;-30.02190506310900000;-51.18586928456700000"
## [1] "11;-30.02191906310900000;-51.18614928456700000"
## [1] "12;-30.02055806310900000;-51.18628628456700000"
## [1] "13;-30.02051906310900000;-51.18570728456700000"
## [1] "14;-30.02129606310900000;-51.18512528456700000"
## [1] "15;-30.02162406310900000;-51.18485928456700000"
## [1] "16;-30.02183606310900000;-51.18468828456700000"
## [1] "17;-30.02199206310900000;-51.18458428456700000"
## [1] "18;-30.02377506310900000;-51.18368428456700000"
## [1] "19;-30.02556706310900000;-51.18280028456700000"
## [1] "20;-30.02734506310900000;-51.18189328456700000"
## [1] "21;-30.02854706310900000;-51.18136328456700000"
## [1] "22;-30.02947906310900000;-51.18096428456700000"
## [1] "23;-30.03114006310900000;-51.18024928456700000"
## [1] "24;-30.03139006310900000;-51.18014128456700000"
## [1] "25;-30.03170006310900000;-51.18000128456700000"
## [1] "26;-30.03191706310900000;-51.17986028456700000"
## [1] "27;-30.03275406310900000;-51.17926428456700000"
## [1] "28;-30.03355606310900000;-51.17866328456700000"
## [1] "29;-30.03383406310900000;-51.17844328456700000"
## [1] "30;-30.03440106310900000;-51.17804428456700000"
## [1] "31;-30.03699006310900000;-51.17971928456700000"
## [1] "32;-30.03780106310900000;-51.18024928456700000"
## [1] "33;-30.03856406310900000;-51.18074428456700000"
## [1] "34;-30.03938106310900000;-51.18127928456700000"
## [1] "35;-30.03971506310900000;-51.18230328456700000"
## [1] "36;-30.04006606310900000;-51.18349128456700000"
## [1] "37;-30.04017506310900000;-51.18386128456700000"
## [1] "38;-30.04095106310900000;-51.18649428456700000"
## [1] "39;-30.04177606310900000;-51.18617528456700000"
## [1] "40;-30.04261306310900000;-51.18583928456700000"
## [1] "41;-30.04343706310900000;-51.18552128456700000"
## [1] "42;-30.04380106310900000;-51.18538528456700000"
## [1] "43;-30.04375806310900000;-51.18575628456700000"
## [1] "44;-30.04365006310900000;-51.18661528456700000"
## [1] "45;-30.04355406310900000;-51.18748528456700000"
## [1] "46;-30.04332006310900000;-51.18824728456700000"
## [1] "47;-30.04330306310900000;-51.18830428456700000"
## [1] "48;-30.04305806310900000;-51.18913528456700000"
## [1] "49;-30.04287906310900000;-51.18976328456700000"
## [1] "50;-30.04264806310900000;-51.19057928456700000"
## [1] "51;-30.04258606310900000;-51.19073828456700000"
## [1] "52;-30.04250906310900000;-51.19088628456700000"
## [1] "53;-30.04241206310900000;-51.19103828456700000"
## [1] "54;-30.04227906310900000;-51.19117428456700000"
## [1] "55;-30.04201906310900000;-51.19137328456700000"
## [1] "56;-30.04155906310900000;-51.19168528456700000"
## [1] "57;-30.04121606310900000;-51.19191928456700000"
## [1] "58;-30.04114106310900000;-51.19199928456700000"
## [1] "59;-30.04064206310900000;-51.19269428456700000"
## [1] "60;-30.04052506310900000;-51.19296728456700000"
## [1] "61;-30.04047806310900000;-51.19323428456700000"
## [1] "62;-30.04044706310900000;-51.19453028456700000"
## [1] "63;-30.04029006310900000;-51.19598428456700000"
## [1] "64;-30.04018806310900000;-51.19687228456700000"
## [1] "65;-30.04006506310900000;-51.19749528456700000"
## [1] "66;-30.04004806310900000;-51.19756628456700000"
## [1] "67;-30.03992106310900000;-51.19801928456700000"
## [1] "68;-30.03971306310900000;-51.19870228456700000"
## [1] "69;-30.03968906310900000;-51.19877728456700000"
## [1] "70;-30.03941906310900000;-51.19963228456700000"
## [1] "71;-30.03934706310900000;-51.19988328456700000"
## [1] "72;-30.03925206310900000;-51.20031128456700000"
## [1] "73;-30.03908406310900000;-51.20113928456700000"
## [1] "74;-30.03898906310900000;-51.20157628456700000"
## [1] "75;-30.03888406310900000;-51.20181728456700000"
## [1] "76;-30.03868206310900000;-51.20224428456700000"
## [1] "77;-30.03833406310900000;-51.20298128456700000"
## [1] "78;-30.03823306310900000;-51.20322828456700000"
## [1] "79;-30.03799406310900000;-51.20373728456700000"
## [1] "80;-30.03789306310900000;-51.20395428456700000"
## [1] "81;-30.03779706310900000;-51.20416228456700000"
## [1] "82;-30.03773606310900000;-51.20429428456700000"
## [1] "83;-30.03761306310900000;-51.20455528456700000"
## [1] "84;-30.03753406310900000;-51.20472928456700000"
## [1] "85;-30.03746806310900000;-51.20500028456700000"
## [1] "86;-30.03735606310900000;-51.20579028456700000"
## [1] "87;-30.03724306310900000;-51.20659628456700000"
## [1] "88;-30.03713006310900000;-51.20735528456700000"
## [1] "89;-30.03696606310900000;-51.20838528456700000"
## [1] "90;-30.03692406310900000;-51.20864828456700000"
## [1] "91;-30.03685806310900000;-51.20887128456700000"
## [1] "92;-30.03683306310900000;-51.20895528456700000"
## [1] "93;-30.03675006310900000;-51.20911528456700000"
## [1] "94;-30.03639706310900000;-51.20978828456700000"
## [1] "95;-30.03619006310900000;-51.21018328456700000"
## [1] "96;-30.03587706310900000;-51.21079728456700000"
## [1] "97;-30.03580306310900000;-51.21094028456700000"
## [1] "98;-30.03570906310900000;-51.21112728456700000"
## [1] "99;-30.03546606310900000;-51.21158828456700000"
## [1] "100;-30.03473106310900000;-51.21301428456700000"
## [1] "101;-30.03424606310900000;-51.21394228456700000"
## [1] "102;-30.03387706310900000;-51.21465128456700000"
## [1] "103;-30.03345306310900000;-51.21545728456700000"
## [1] "104;-30.03343006310900000;-51.21549828456700000"
## [1] "105;-30.03326506310900000;-51.21625528456700000"
## [1] "106;-30.03301906310900000;-51.21734428456700000"
## [1] "107;-30.03286906310900000;-51.21803928456700000"
## [1] "108;-30.03257206310900000;-51.21935228456700000"
## [1] "109;-30.03220106310900000;-51.21948728456700000"
## [1] "110;-30.03203806310900000;-51.21951328456700000"
## [1] "111;-30.03173006310900000;-51.21934828456700000"
## [1] "112;-30.03146806310900000;-51.21914628456700000"
## [1] "113;-30.03139906310900000;-51.21911928456700000"
## [1] "114;-30.03124206310900000;-51.21910728456700000"
## [1] "115;-30.03109806310900000;-51.21912228456700000"
## [1] "116;-30.03107206310900000;-51.21912728456700000"
## [1] "117;-30.03085006310900000;-51.21917228456700000"
## [1] "118;-30.03074006310900000;-51.21919428456700000"
## [1] "119;-30.03054106310900000;-51.21931028456700000"
## [1] "120;-30.03047106310900000;-51.21935128456700000"
## [1] "121;-30.02974406310900000;-51.21940828456700000"
## [1] "122;-30.02951106310900000;-51.21940828456700000"
## [1] "123;-30.02947806310900000;-51.21941628456700000"
## [1] "124;-30.02939506310900000;-51.21943728456700000"
## [1] "125;-30.02930506310900000;-51.21947928456700000"
## [1] "126;-30.02884706310900000;-51.21952728456700000"
## [1] "127;-30.02792006310900000;-51.21956828456700000"
## [1] "128;-30.02710206310900000;-51.21960528456700000"
## [1] "129;-30.02697606310900000;-51.21959428456700000"
## [1] "130;-30.02676906310900000;-51.21954328456700000"
## [1] "131;-30.02663806310900000;-51.21949228456700000"
## [1] "132;-30.02659106310900000;-51.21946928456700000"
## [1] "133;-30.02643206310900000;-51.21938928456700000"
## [1] "134;-30.02637506310900000;-51.21936328456700000"
## [1] "135;-30.02604906310900000;-51.21921328456700000"
## [1] "136;-30.02597506310900000;-51.21917828456700000"
## [1] "137;-30.02559606310900000;-51.21900428456700000"
## [1] "138;-30.02548506310900000;-51.21897328456700000"
## [1] "139;-30.02519706310900000;-51.21897528456700000"
## [1] "140;-30.02497106310900000;-51.21901628456700000"
## [1] "141;-30.02470306310900000;-51.21920528456700000"
## [1] "142;-30.02451106310900000;-51.21937628456700000"
## [1] "143;-30.02427706310900000;-51.21958528456700000"
## [1] "144;-30.02349306310900000;-51.22024528456700000"
## [1] "145;-30.02332806310900000;-51.22043128456700000"
## [1] "146;-30.02331806310900000;-51.22044728456700000"
## [1] "147;-30.02326106310900000;-51.22054028456700000"
## [1] "148;-30.02320306310900000;-51.22063228456700000"
## [1] "149;-30.02317006310900000;-51.22084628456700000"
## [1] "150;-30.02315406310900000;-51.22115628456700000"
## [1] "151;-30.02361106310900000;-51.22199328456700000"
## [1] "152;-30.02424306310900000;-51.22329028456700000"
## [1] "153;-30.02441206310900000;-51.22363828456700000"
## [1] "154;-30.02496706310900000;-51.22478228456700000"
## [1] "155;-30.02515806310900000;-51.22517428456700000"
## [1] "156;-30.02563506310900000;-51.22613528456700000"
## [1] "157;-30.02598406310900000;-51.22685328456700000"
## [1] "158;-30.02616106310900000;-51.22710328456700000"
## [1] "159;-30.02618906310900000;-51.22715928456700000"
## [1] "160;-30.02635806310900000;-51.22772528456700000"
## [1] "161;-30.02684806310900000;-51.22757428456700000"
## [1] "162;-30.02787206310900000;-51.22721228456700000"
## [1] "163;-30.02777206310900000;-51.22697528456700000"
library(magrittr)
library(dplyr)  
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(leaflet)
library(leaflet.extras)
lim = length(aa$lat)
lim = (lim - 3)
lim
## [1] 163
aa = aa[4:lim,]
dim(aa)
## [1] 160   2
aa$lat =  as.numeric(as.character(aa$lat))
aa$lng =  as.numeric(as.character(aa$lng))


map <- leaflet(data = aa) %>%
  addTiles() %>%
  addPolylines(aa[, 2],aa[, 1],weight=2.25,color="red") 
map
## paradas
path = paste('http://www.poatransporte.com.br/php/facades/process.php?a=tp&p=((-30.14296222668432,%20-51.87917968750003),%20(-29.79200328961529,%20-50.56082031250003))))')
path
## [1] "http://www.poatransporte.com.br/php/facades/process.php?a=tp&p=((-30.14296222668432,%20-51.87917968750003),%20(-29.79200328961529,%20-50.56082031250003))))"
json_file <- fromJSON(path)

paradas = as.data.frame(json_file)
loop = c(1:3)
for(i in loop){
  print(paradas[i, ])
}
##   codigo              latitude             longitude terminal
## 1      1 -30.06408491467800000 -51.17093963227500000        N
##                                                               linhas
## 1 5592, 5605, 345-2, 3462-1, SANTA CATARINA, SAO JOSE/SANTA CATARINA
##   codigo              latitude             longitude terminal
## 2    203 -30.01632191467800000 -51.17204163227500000        N
##                                                                                                                                                                                                                                                     linhas
## 2 5421, 5434, 5435, 5609, 5471, 5418, 5858, 5959, 608-1, 617-1, 620-1, 6171-1, B05-1, 520-1, 5202-1, T13-1, IAPI, IGUATEMI, IGUATEMI / V. JARDIM, IGUATEMI I, JARDIM FLORESTA/OBIRICI, TRIANGULO/24 DE OUTUBRO, TRIANGULO/24 DE OUTUBRO/IPA, TRIÂNGULO/PUC
##   codigo              latitude             longitude terminal
## 3    414 -30.11344591467800000 -51.10945963227500000        N
##                                                                                                                                                                                                                                                                                                                                                                                                             linhas
## 3 5835, 5290, 5291, 5376, 5135, 5139, 5141, 5149, 5116, 5958, 5147, A321-1, C98-1, C981-1, M98-2, 398-2, 3982-2, 3983-2, 3987-2, T12-1, T12A-2, 3986-2, ALIMENTADORA BONSUCESSO/PARADA 13, CIRCULAR LOMBA DO PINHEIRO, CIRCULAR LOMBA/QUINTA DO PORTAL, NOTURNA (PINHEIRO), PINHEIRO, PINHEIRO / ATE TERMINAL AZENHA, PINHEIRO / VIA IPIRANGA, PINHEIRO/LAMI-VERAO, RESTINGA/CAIRÚ, RESTINGA/PUC, VIÇOSA(PINHEIRO)
paradas$latitude = as.numeric(as.character(paradas$latitude))
paradas$longitude  = as.numeric(as.character(paradas$longitude))

loop = c(1:length(paradas$codigo))

for(i in loop){
  paradas$IdLinha[i] = paradas$linhas[[i]][1]
  paradas$codLinha[i] = paradas$linhas[[i]][2]
  paradas$nomeLinha[i] = paradas$linhas[[i]][3]
}

map <- leaflet(data = paradas) %>%
  addTiles() %>%
  addPolylines(group="Linha", aa[, 2],aa[, 1],weight=2.25,color="red")  %>%
  addCircles(group="POs", ~paradas$longitude, ~paradas$latitude, weight = 1, radius=20, color= 'blue', stroke = TRUE, fillOpacity = 0.8) %>% 
  addLayersControl(overlayGroups = c("Linha", "POs"), options = layersControlOptions(collapsed = FALSE))
map
head(paradas)
a = paradas[2,]$linhas[[1]]
class(a)
## [1] "data.frame"
a$codigoLinha
## [1] "608-1"  "617-1"  "620-1"  "6171-1" "B05-1"  "520-1"  "5202-1" "T13-1"
### Adicionando informações da parada

map <- leaflet(data = paradas) %>%
  addTiles() %>%
  addPolylines(group="Linha", aa[, 2],aa[, 1],weight=2.25,color="red")  %>%
  addCircles(group="POs", ~paradas$longitude, ~paradas$latitude, popup=~paste("<br>Id", IdLinha, "<p>Cod", codLinha, "<p>Nome", nomeLinha,  sep = " "),
             weight = 1, radius=20, color= 'blue', stroke = TRUE, fillOpacity = 0.8) %>% 
  addLayersControl(overlayGroups = c("Linha", "POs"), options = layersControlOptions(collapsed = FALSE))
map
head(paradas)