In this document, we will explain how we create our data set for ISPARK Analysis.
ISPARK (Istanbul Parking Operations Co.) is parking management services company of IBB. They manages on-street and closed parking areas belonging to IBB. We have used Istanbul Metropolitan Municipality(IBB)’s Open Data Portal ISPARK API for collecting our data.
There are only two API call in this service and service returns JSON data to request. First call is gives you basic informations of all park areas. Second call gives you detailed information about selected park area.
The first call largely met our needs. We have used second call for only taking monthly subcription price for every park area.
For this gathering informations of park areas, we used three libraries:
httr
for API calls.jsonlite
for processing JSON file.dplyr
for manipulating data.library(httr)
library(jsonlite)
library(dplyr)
We have created two main functions:
getparklist <- function(){
res <- GET("https://api.ibb.gov.tr/ispark/Park")
dt <- res$content %>% rawToChar() %>% fromJSON()
return(dt)
}
getprice <- function(ParkID){
res <- GET(paste0("https://api.ibb.gov.tr/ispark/ParkDetay?id=",ParkID))
aylikucret <- res$content %>% rawToChar(.) %>% fromJSON(.)
aylikucret <- aylikucret$AylikAbonelikUcreti
return(aylikucret)
}
In this part of script, we create datafreame called df using our first function. After that, we mutate this dataframe by our second function. At the end, we save this df as RDS.
df <- getparklist()
df <- df %>% group_by(ParkID) %>% mutate("AylıkAbonelikÜcreti"=getprice(ParkID))
saveRDS(df,"ispark-parkbilgileri.RDS")
When we look API results, we saw that API results give us real-time free capacity information for all park areas. We decided to take this data in hourly basis and use that in the project.
Same libraries for same tasks.
library(httr)
library(jsonlite)
library(dplyr)
We have two new function for this job.
getparks <- function(){
res <- GET("https://api.ibb.gov.tr/ispark/Park")
dt <- res$content %>% rawToChar() %>% fromJSON()
dt <- dt %>% mutate("DolulukYuzdesi"=(BosKapasite/Kapasitesi)*100) %>% select(ParkID,ParkAdi,DolulukYuzdesi)
dt$OlcumZamanı <- Zaman
return(dt)
}
savedata <- function(){
x = as.POSIXct(Sys.time(), tz = "Europe/London")
Zaman <- .POSIXct(as.integer(x), tz = 'Europe/Istanbul')
parks <- getparks()
if(file.exists("parkcapacitylog.RDS")){
rds <- readRDS("parkcapacitylog.RDS")
rds <- rbind(rds,parks)
saveRDS(rds, file = "parkcapacitylog.RDS")
} else{
saveRDS(parks, file = "parkcapacitylog.RDS")
}
}
We used CRONR package for scheduling our script in Ubuntu. You can check this link for detailed information about CRON and CRONR.
## cronR job
## id: job_9b894c616238f3e8db2b75d122705aa8
## tags:
## desc: ISPARK Saatlik Doluluk Yüzdesi
35 * * * * /usr/lib/R/bin/Rscript '/home/alihan/BDA-203/Week 7/takecapacitylog.R' >> '/home/alihan/BDA-203/Week 7/takecapacitylog.log' 2>&1
We used save()
function saving multiple object as .RData file.
save(isparkparkbilgileri, parkcapacitylog, file = "ISPARK.RData")