This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfs.c
153 lines (120 loc) · 3.71 KB
/
gtfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// gtfs.c - gtfs support
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2015 Joris van der Geer.
*/
/* Write gtfs files from processed tripover input.
Tripover does not read gtfs files directly. Instead, gtfstool converts them.
Export to gtfs allows operations like timezone conversion.
This is needed for combining feeds.
*/
#include <string.h>
#include <stdarg.h>
#include "base.h"
#include "cfg.h"
#include "mem.h"
#include "math.h"
static ub4 msgfile;
#include "msg.h"
#include "time.h"
#include "util.h"
#include "netbase.h"
#include "netio.h"
#include "event.h"
#include "gtfs.h"
static double lat2gt(ub4 lat,ub4 scale) { return ((double)lat / scale) - 90; }
static double lon2gt(ub4 lon,ub4 scale) { return ((double)lon / scale) - 180; }
int writegtfs(struct networkbase *net,char *outdir)
{
struct portbase *ports,*pp;
struct routebase *rp,*routes;
struct subportbase *spp,*sports;
struct chainbase *cp,*chains;
ub4 portcnt,routecnt,chaincnt;
ub4 port,sport,route,chain;
ub4 id,pid,rid,tid,sid;
double x,y;
ub4 scnt,sofs;
char *uid;
char fname[1024];
char buf[1024];
ub4 pos;
int fd;
portcnt = net->portcnt;
ports = net->ports;
sports = net->subports;
routes = net->routes;
routecnt = net->ridcnt;
chains = net->chains;
chaincnt = net->rawchaincnt;
uid = outdir;
fmtstring(fname,"%s/stops.txt",outdir);
info(0,"writing %s",fname);
fd = filecreate(fname,1);
if (fd == -1) return 1;
pos = fmtstring0(buf,"stop_id,location_type,parent_station,stop_name,stop_lat,stop_lon,stop_desc\n");
if (filewrite(fd,buf,pos,fname)) return 1;
for (port = 0; port < portcnt; port++) {
pp = ports + port;
if (pp->ndep == 0 && pp->narr == 0) continue;
scnt = pp->subcnt;
sofs = pp->subofs;
y = lat2gt(pp->lat,net->latscale);
x = lon2gt(pp->lon,net->lonscale);
if (scnt == 0) {
id = port;
pos = fmtstring(buf,"%s%u,0,,\a`%s,%f,%f,%u\n",uid,id,pp->name,y,x,pp->id);
if (filewrite(fd,buf,pos,fname)) return 1;
continue;
}
pid = port;
pos = fmtstring(buf,"%s%u,1,,\a`%s,%f,%f,%u\n",uid,pid,pp->name,y,x,pp->id);
if (filewrite(fd,buf,pos,fname)) return 1;
for (sport = 0; sport < scnt; sport++) {
spp = sports + sofs + sport;
y = lat2gt(spp->lat,net->latscale);
x = lon2gt(spp->lon,net->lonscale);
id = spp->subid + portcnt;
pos = fmtstring(buf,"%s%u,0,%s%u,\a`%s,%f,%f,%u\n",uid,id,uid,pid,spp->name,y,x,spp->id);
if (filewrite(fd,buf,pos,fname)) return 1;
}
}
fileclose(fd,fname);
info(0,"wrote %s",fname);
fmtstring(fname,"%s/routes.txt",outdir);
info(0,"writing %s",fname);
fd = filecreate(fname,1);
if (fd == -1) return 1;
pos = fmtstring0(buf,"route_id,agency_id,route_short_name,route_long_name,route_type\n");
if (filewrite(fd,buf,pos,fname)) return 1;
for (route = 0; route < routecnt; route++) {
rp = routes + route;
pos = fmtstring(buf,"%s%u,,,\a`%s,%u\n",uid,route,rp->name,rp->rtype);
if (filewrite(fd,buf,pos,fname)) return 1;
}
fileclose(fd,fname);
info(0,"wrote %s",fname);
fmtstring(fname,"%s/trips.txt",outdir);
info(0,"writing %s",fname);
fd = filecreate(fname,1);
if (fd == -1) return 1;
pos = fmtstring0(buf,"route_id,service_id,trip_id,trip_headsign\n");
if (filewrite(fd,buf,pos,fname)) return 1;
for (chain = 0; chain < chaincnt; chain++) {
cp = chains + chain;
rid = cp->rid;
tid = chain;
sid = 0;
pos = fmtstring(buf,"%s%u,%s%u,%s%u,\a`%s\n",uid,rid,uid,sid,uid,tid,"");
if (filewrite(fd,buf,pos,fname)) return 1;
}
fileclose(fd,fname);
info(0,"wrote %s",fname);
return 0;
}
int inigtfs(void)
{
msgfile = setmsgfile(__FILE__);
iniassert();
return 0;
}