ropensci / bikedata

:bike: Extract data from public hire bicycle systems
https://docs.ropensci.org/bikedata
81 stars 16 forks source link

Ciudad Mexico #55

Open mpadge opened 6 years ago

mpadge commented 6 years ago

@RobinLovelace Can you please provide details of the Mexico City data you mentioned? It'd be great to incorporate that if possible

Robinlovelace commented 6 years ago

Sure:

# from: https://www.ecobici.cdmx.gob.mx/es/informacion-del-servicio/open-data
u = "https://www.ecobici.cdmx.gob.mx/sites/default/files/data/usages/2018-01.csv"
u_static = "https://www.ecobici.cdmx.gob.mx/sites/default/files/data/usages/"
download.file(url = u, destfile = "input-data/month-data.csv")

# create loop to download every month
yrs = 2011:2018
months = formatC(1:12, width = 2, flag = "0")
for(y in yrs) {
  for(m in months) {
    um = paste0(u_static, y, "-", m, ".csv")
    print(um)

    destfile = paste0("input-data/", y, "-", m, ".csv")

    download.file(url = um, destfile = destfile)

  }
}
Robinlovelace commented 6 years ago

This shows a first attempt at linking them with OSM data. The results are unreliable because the docking stations do not have correct names.

library(tidyverse)
library(osmdata)
library(stplanr)
library(sf)
data_raw = read_csv("input-data/month-data.csv")

# get cycle hire points
q = opq("mexico city") %>% 
  add_osm_feature(key = "amenity", value = "bicycle_rental")
osm_data = osmdata_sf(q = q)
stations_osm = osm_data$osm_points
class(stations_osm)
#> [1] "sf"         "data.frame"

# task: load high quality data in sf class here
stations = stations_osm # replace with official data
str_extract("Ecobici 168 balkdjf", pattern = "[1-9][1-9][1-9]")
station_ids = str_extract(stations$name, pattern = "[1-9][1-9]?[1-9]")
# station_ids[station_ids == "1" & !is.na(station_ids)] = NA
stations$ids = station_ids
stations = select(stations, ids, amenity)

plot(stations)
mapview::mapview(stations)
data_ag = data_raw %>% 
  group_by(Ciclo_Estacion_Retiro, Ciclo_Estacion_Arribo) %>% 
  summarise(flow = n()) 
sum(data_ag$flow)
data_top = data_ag %>% 
  top_n(n = 200, wt = flow)

sel = data_ag$Ciclo_Estacion_Retiro %in% stations$ids &
  data_ag$Ciclo_Estacion_Arribo %in% stations$ids
summary(sel)
data_top = data_ag[sel, ]
summary(data_top$Ciclo_Estacion_Retiro %in% stations$ids)

lines = od2line(flow = data_top, stations)
Robinlovelace commented 6 years ago

Thanks to @luis-tona for getting me onto this, he's on it!

Any updates Luis? This may lead the bikeshare data to be made available online for everyone (which could be an amazing side benefit of the project).

mpadge commented 6 years ago

Great, I'll check out the data source asap. A huge part of˙bikedata` is just data cleaning and stuff like station name matching, so no biggy there. I'll just have to ensure an ongoing commitment to data availability

mpadge commented 6 years ago

Data sources can be extracted as "https://www.ecobici.cdmx.gob.mx/sites/default/files/data/usages/YYYY-MM.csv", starting at 2010-02.

mpadge commented 6 years ago

The trip files have only station numbers. These are all given on the official station map, but the raw data are all buried in a drupal scheme, so can't be accessed. There's supposed to be an API, but I applied for a key and just received an empty email. I've written to ask them about providing data, and/or help with the API, and will have to wait for a response.

Robinlovelace commented 6 years ago

Tricky. I know Luis is struggling to make sense of the data also which must be frustrating as it seems to all be there, just not in a joined-up way. Let us know how you get on + cheers for the updates.

mpadge commented 6 years ago

code to get the data files:

data_dir <- tempdir ()
url_base <- "https://www.ecobici.cdmx.gob.mx/sites/default/files/data/usages/"
furl <- paste0 (url_base, "2010-02.csv")
furl <- paste0 (url_base, "2017-02.csv")
destfile <- file.path (data_dir, paste0 ("mx", basename(furl)))
resp <- httr::GET (furl, httr::write_disk (destfile, overwrite = TRUE))
Robinlovelace commented 6 years ago

Great progress - is that all of them though? Heads-up @luis-tona - may be of use for your project.

mpadge commented 6 years ago

nah, that's just me dumping the generic code. Any "YYYY-MM.csv" can be entered. The annoying problem remains that the stations can't be accessed, and I've not got any reply to my query to ecobici. In the absence of a response, the whole incorporation will have to be shelved until station data become available

mpadge commented 6 years ago

Note that GBFS data are available for Guadlajara, but not Cuidad Mexico. Still have to wait on this one.

Robinlovelace commented 6 years ago

We've made great progress cleaning the datasets - 100 million+ journeys (rows of data) logged.

Robinlovelace commented 6 years ago

We've made great progress cleaning the datasets - 100 million+ journeys (rows of data) logged.