Skip to content

Commit

Permalink
Add stopareas, close #136
Browse files Browse the repository at this point in the history
- Add stop_areas (coordinates,names), including parser support
- Create one big string pool, where stop-point and stop-area names are stored
- Store headsigns in this new string_pool
- Map stop-point to a stoparea
  • Loading branch information
koch-t committed Dec 29, 2014
1 parent 3cd911c commit 0be5c3e
Show file tree
Hide file tree
Showing 9 changed files with 599 additions and 24 deletions.
2 changes: 1 addition & 1 deletion rrtimetable/rrtimetable/exporter/timetable3.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def make_idx(tdata):
index.journey_patterns_at_stop_point[jpp.stop_point.uri] = set([])
index.journey_patterns_at_stop_point[jpp.stop_point.uri].add(vj.journey_pattern.uri)
if jpp.stop_point.stop_area.uri not in index.idx_for_stop_area_uri:
index.idx_for_stop_point_uri[jpp.stop_point.stop_area.uri] = len(index.stop_areas)
index.idx_for_stop_area_uri[jpp.stop_point.stop_area.uri] = len(index.stop_areas)
index.stop_areas.append(jpp.stop_point.stop_area)

for conn in tdata.connections.values():
Expand Down
528 changes: 528 additions & 0 deletions rrtimetable/rrtimetable/exporter/timetable4.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions rrtimetable/rrtimetable/fusio_dbexport.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import psycopg2
from model.transit import *
from exporter.timetable3 import export
import exporter.timetable4

def parse_gtfs_time(timestr):
return (lambda x:int(x[0])*3600+int(x[1])*60+int(x[2]))(timestr.split(":")) #oh yes I did
Expand Down Expand Up @@ -72,3 +73,4 @@ def convert(dbname):
return tdata
tdata = convert('ridprod')
export(tdata)
exporter.timetable4.export(tdata)
2 changes: 2 additions & 0 deletions rrtimetable/rrtimetable/gtfs2rrrr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from gtfsdb import GTFSDatabase
import sys
from exporter.timetable3 import export
import exporter.timetable4
from datetime import timedelta, date

MAX_DAYS = 32
Expand Down Expand Up @@ -104,6 +105,7 @@ def main():
print "No valid trips in this GTFS file!"
sys.exit(1)
export(tdata)
exporter.timetable4.export(tdata)

if __name__=='__main__':
main()
29 changes: 25 additions & 4 deletions tdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const char *tdata_agency_url_for_index(tdata_t *td, uint32_t agency_index) {
}

const char *tdata_headsign_for_offset(tdata_t *td, uint32_t headsign_offset) {
return td->headsigns + headsign_offset;
return td->string_pool + headsign_offset;
}

const char *tdata_line_code_for_index(tdata_t *td, uint32_t line_code_index) {
Expand All @@ -95,14 +95,31 @@ spidx_t tdata_stop_pointidx_by_stop_point_name(tdata_t *td, char *stop_point_nam
for (sp_index = sp_index_offset;
sp_index < td->n_stop_points;
++sp_index) {
if (strcasestr(td->stop_point_names + td->stop_point_nameidx[sp_index],
if (strcasestr(td->string_pool + td->stop_point_nameidx[sp_index],
stop_point_name)) {
return sp_index;
}
}
return STOP_NONE;
}

spidx_t tdata_stop_areaidx_for_index(tdata_t *td, spidx_t sp_index) {
return td->stop_area_for_stop_point[sp_index];
}

spidx_t tdata_stop_areaidx_by_stop_area_name(tdata_t *td, char *stop_point_name, spidx_t sa_index_offset) {
spidx_t sa_index;
for (sa_index = sa_index_offset;
sa_index < td->n_stop_areas;
++sa_index) {
if (strcasestr(td->string_pool + td->stop_area_nameidx[sa_index],
stop_point_name)) {
return sa_index;
}
}
return STOP_NONE;
}

spidx_t tdata_stop_pointidx_by_stop_point_idx(tdata_t *td, char *stop_point_id, spidx_t sp_index_offset) {
spidx_t sp_index;
for (sp_index = sp_index_offset;
Expand Down Expand Up @@ -146,7 +163,7 @@ calendar_t *tdata_vj_masks_for_journey_pattern(tdata_t *td, uint32_t jp_index) {

const char *tdata_headsign_for_journey_pattern(tdata_t *td, uint32_t jp_index) {
if (jp_index == NONE) return "NONE";
return td->headsigns + (td->journey_patterns)[jp_index].headsign_offset;
return td->string_pool + (td->journey_patterns)[jp_index].headsign_offset;
}

const char *tdata_line_code_for_journey_pattern(tdata_t *td, uint32_t jp_index) {
Expand Down Expand Up @@ -250,10 +267,14 @@ const char *tdata_stop_point_name_for_index(tdata_t *td, spidx_t sp_index) {
case ONBOARD :
return "ONBOARD";
default :
return td->stop_point_names + td->stop_point_nameidx[sp_index];
return td->string_pool + td->stop_point_nameidx[sp_index];
}
}

const char *tdata_stop_area_name_for_index(tdata_t *td, spidx_t sa_index) {
return td->string_pool + td->stop_area_nameidx[sa_index];
}

/* Rather than reserving a place to store the transfers used to create the initial state, we look them up as needed. */
rtime_t transfer_duration (tdata_t *tdata, router_request_t *req, spidx_t sp_index_from, spidx_t sp_index_to) {
UNUSED(req);
Expand Down
16 changes: 13 additions & 3 deletions tdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ struct tdata {
/* Dates within the active calendar which have DST. */
calendar_t dst_active;
uint32_t n_stop_points;
uint32_t n_stop_areas;
uint32_t n_stop_point_attributes;
uint32_t n_stop_point_coords;
uint32_t n_stop_area_coords;
uint32_t n_stop_area_for_stop_point;
uint32_t n_journey_patterns;
uint32_t n_journey_pattern_points;
uint32_t n_journey_pattern_point_attributes;
Expand All @@ -124,10 +127,11 @@ struct tdata {
uint32_t n_platformcodes;
uint32_t n_stop_point_names;
uint32_t n_stop_point_nameidx;
uint32_t n_stop_area_nameidx;
uint32_t n_agency_ids;
uint32_t n_agency_names;
uint32_t n_agency_urls;
uint32_t n_headsigns;
uint32_t n_string_pool;
uint32_t n_line_codes;
uint32_t n_productcategories;
uint32_t n_line_ids;
Expand All @@ -147,17 +151,19 @@ struct tdata {
/* optional data:
* NULL pointer means it is not available */
latlon_t *stop_point_coords;
latlon_t *stop_area_coords;
spidx_t *stop_area_for_stop_point;
uint32_t platformcodes_width;
char *platformcodes;
char *stop_point_names;
uint32_t *stop_point_nameidx;
uint32_t *stop_area_nameidx;
uint32_t agency_ids_width;
char *agency_ids;
uint32_t agency_names_width;
char *agency_names;
uint32_t agency_urls_width;
char *agency_urls;
char *headsigns;
char *string_pool;
uint32_t line_codes_width;
char *line_codes;
uint32_t productcategories_width;
Expand Down Expand Up @@ -230,10 +236,14 @@ const char *tdata_productcategory_for_index(tdata_t *td, uint32_t productcategor

const char *tdata_stop_point_name_for_index(tdata_t *td, spidx_t sp_index);

const char *tdata_stop_area_name_for_index(tdata_t *td, spidx_t sp_index);

const char *tdata_platformcode_for_index(tdata_t *td, spidx_t sp_index);

spidx_t tdata_stop_pointidx_by_stop_point_name(tdata_t *td, char *stop_point_name, spidx_t sp_index_offset);

spidx_t tdata_stop_pointidx_by_stop_area_name(tdata_t *td, char *stop_point_name, spidx_t sp_index_offset);

spidx_t tdata_stop_pointidx_by_stop_point_idx(tdata_t *td, char *stop_point_id, spidx_t sp_index_offset);

uint32_t tdata_journey_pattern_idx_by_line_id(tdata_t *td, char *line_id, uint32_t start_index);
Expand Down
16 changes: 10 additions & 6 deletions tdata_io_v3.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
/* file-visible struct */
typedef struct tdata_header tdata_header_t;
struct tdata_header {
/* Contents must read "TTABLEV3" */
/* Contents must read "TTABLEV4" */
char version_string[8];
uint64_t calendar_start_time;
calendar_t dst_active;
uint32_t n_stop_points;
uint32_t n_stop_areas;
uint32_t n_stop_point_attributes;
uint32_t n_stop_point_coords;
uint32_t n_stop_area_coords;
uint32_t n_stop_area_for_stop_point;
uint32_t n_journey_patterns;
uint32_t n_journey_pattern_points;
uint32_t n_journey_pattern_point_attributes;
Expand All @@ -22,16 +25,15 @@ struct tdata_header {
uint32_t n_vj_active;
uint32_t n_journey_pattern_active;
uint32_t n_platformcodes;
/* length of the object in bytes */
uint32_t n_stop_point_names;
uint32_t n_stop_point_nameidx;
uint32_t n_stop_area_nameidx;
uint32_t n_agency_ids;
uint32_t n_agency_names;
uint32_t n_agency_urls;


/* length of the object in bytes */
uint32_t n_headsigns;
uint32_t n_string_pool;
uint32_t n_line_codes;
uint32_t n_productcategories;
uint32_t n_line_ids;
Expand All @@ -51,17 +53,19 @@ struct tdata_header {
uint32_t loc_vj_active;
uint32_t loc_journey_pattern_active;
uint32_t loc_platformcodes;
uint32_t loc_stop_point_names;
uint32_t loc_stop_point_nameidx;
uint32_t loc_stop_area_nameidx;
uint32_t loc_agency_ids;
uint32_t loc_agency_names;
uint32_t loc_agency_urls;
uint32_t loc_headsigns;
uint32_t loc_string_pool;
uint32_t loc_line_codes;
uint32_t loc_productcategories;
uint32_t loc_line_ids;
uint32_t loc_stop_point_ids;
uint32_t loc_vj_ids;
uint32_t loc_stop_area_coords;
uint32_t loc_stop_area_for_stop_point;
};

bool tdata_io_v3_load(tdata_t *td, char* filename);
Expand Down
19 changes: 12 additions & 7 deletions tdata_io_v3_dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {
td->base = NULL;
td->size = 0;

if( strncmp("TTABLEV3", header->version_string, 8) ) {
if( strncmp("TTABLEV4", header->version_string, 8) ) {
fprintf(stderr, "The input file %s does not appear to be a timetable or is of the wrong version.\n", filename);
goto fail_close_fd;
}

/* More input validation in the dynamic loading case. */
if ( !( header->n_stop_points < ((spidx_t) -2) &&
header->n_stop_areas < ((spidx_t) -2) &&
header->n_stop_point_attributes < ((spidx_t) -2) &&
header->n_stop_point_coords < ((spidx_t) -2) &&
header->n_stop_area_coords < ((spidx_t) -2) &&
header->n_stop_area_coords < ((spidx_t) -2) &&
header->n_journey_patterns < (UINT32_MAX - 1) &&
header->n_journey_pattern_points < (UINT32_MAX) &&
header->n_journey_pattern_point_attributes < (UINT32_MAX) &&
Expand All @@ -83,12 +86,11 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {
header->n_vj_active < (UINT32_MAX) &&
header->n_journey_pattern_active < (UINT32_MAX) &&
header->n_platformcodes < (UINT32_MAX) &&
header->n_stop_point_names < (UINT32_MAX) &&
header->n_stop_point_nameidx < ((spidx_t) -2) &&
header->n_agency_ids < (UINT16_MAX) &&
header->n_agency_names < (UINT16_MAX) &&
header->n_agency_urls < (UINT16_MAX) &&
header->n_headsigns < (UINT32_MAX) &&
header->n_string_pool < (UINT32_MAX) &&
header->n_line_codes < (UINT16_MAX) &&
header->n_productcategories < (UINT16_MAX) &&
header->n_line_ids < (UINT32_MAX) &&
Expand All @@ -101,10 +103,12 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {

td->calendar_start_time = header->calendar_start_time;
td->dst_active = header->dst_active;
td->n_stop_areas = header->n_stop_areas;

load_dynamic (fd, stop_points, stop_point_t);
load_dynamic (fd, stop_point_attributes, uint8_t);
load_dynamic (fd, stop_point_coords, latlon_t);
load_dynamic (fd, stop_area_coords, latlon_t);
load_dynamic (fd, journey_patterns, journey_pattern_t);
load_dynamic (fd, journey_pattern_points, spidx_t);
load_dynamic (fd, journey_pattern_point_attributes, uint8_t);
Expand All @@ -115,9 +119,9 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {
load_dynamic (fd, transfer_dist_meters, uint8_t);
load_dynamic (fd, vj_active, calendar_t);
load_dynamic (fd, journey_pattern_active, calendar_t);
load_dynamic (fd, headsigns, char);
load_dynamic (fd, stop_point_names, char);
load_dynamic (fd, string_pool, char);
load_dynamic (fd, stop_point_nameidx, uint32_t);
load_dynamic (fd, stop_area_nameidx, uint32_t);

load_dynamic_string (fd, platformcodes);
load_dynamic_string (fd, stop_point_ids);
Expand All @@ -144,6 +148,7 @@ void tdata_io_v3_close(tdata_t *td) {
free (td->stop_points);
free (td->stop_point_attributes);
free (td->stop_point_coords);
free (td->stop_area_coords);
free (td->journey_patterns);
free (td->journey_pattern_points);
free (td->journey_pattern_point_attributes);
Expand All @@ -154,9 +159,9 @@ void tdata_io_v3_close(tdata_t *td) {
free (td->transfer_dist_meters);
free (td->vj_active);
free (td->journey_pattern_active);
free (td->headsigns);
free (td->stop_point_names);
free (td->string_pool);
free (td->stop_point_nameidx);
free (td->stop_area_nameidx);

free (td->platformcodes);
free (td->stop_point_ids);
Expand Down
9 changes: 6 additions & 3 deletions tdata_io_v3_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {
}

header = (tdata_header_t *) td->base;
if( strncmp("TTABLEV3", header->version_string, 8) ) {
if( strncmp("TTABLEV4", header->version_string, 8) ) {
fprintf(stderr, "The input file %s does not appear to be a timetable or is of the wrong version.\n", filename);
goto fail_munmap_base;
}

td->calendar_start_time = header->calendar_start_time;
td->dst_active = header->dst_active;
td->n_stop_areas = header->n_stop_areas;

load_mmap (td->base, stop_points, stop_point_t);
load_mmap (td->base, stop_point_attributes, uint8_t);
load_mmap (td->base, stop_point_coords, latlon_t);
load_mmap (td->base, stop_area_coords, latlon_t);
load_mmap (td->base, journey_patterns, journey_pattern_t);
load_mmap (td->base, journey_pattern_points, spidx_t);
load_mmap (td->base, journey_pattern_point_attributes, uint8_t);
Expand All @@ -85,9 +87,10 @@ bool tdata_io_v3_load(tdata_t *td, char *filename) {
load_mmap (td->base, transfer_dist_meters, uint8_t);
load_mmap (td->base, vj_active, calendar_t);
load_mmap (td->base, journey_pattern_active, calendar_t);
load_mmap (td->base, headsigns, char);
load_mmap (td->base, stop_point_names, char);
load_mmap (td->base, string_pool, char);
load_mmap (td->base, stop_point_nameidx, uint32_t);
load_mmap (td->base, stop_area_nameidx, uint32_t);
load_mmap (td->base, stop_area_for_stop_point, spidx_t);

load_mmap_string (td->base, platformcodes);
load_mmap_string (td->base, stop_point_ids);
Expand Down

0 comments on commit 0be5c3e

Please sign in to comment.