예제 #1
0
 def write_file(self):
     """method to write the file"""
     with self.open() as f:
         writer = UnicodeWriter(f,
                                delimiter=self.sep,
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
         writer.writerow(self.header)
         self.write_rows(writer)
예제 #2
0
 def write_file(self):
     """method to write the file"""
     with open(self.tablename, 'w') as f:
         writer = UnicodeWriter(f,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)
         writer.writerow(self.header)
         self.write_body(writer)
예제 #3
0
    def _write_stops(self, table_header_line): # reads from $HALTESTELLENBEREICH
        f = open('stops.txt', 'w')
        writer = UnicodeWriter(f,
                               delimiter=',',
                               quotechar='"',
                               quoting=csv.QUOTE_MINIMAL)
        writer.writerow(( u'stop_id',
                          u'stop_name',
                          u'stop_lat',
                          u'stop_lon',
                          u'location_type',
                          u'parent_station' )) # header

        writer.writerows(self.stations)

        columns = table_header_line.split(':')[1].split(';')

        id_column = columns.index('NR')
        name_column = columns.index('NAME')
        vertex_id_column = columns.index('KNOTNR')
        station_id_column = columns.index('HSTNR')
        x_coord_column = columns.index('XKOORD')
        y_coord_column = columns.index('YKOORD')


        # read and write the entries
        while True:
            line = self.get_line()
            if line.startswith('$'):  # next section
                break
            entry = line.split(';')

            (lat, lon, h) = coord_to_wgs84(self.from_proj,
                                           entry[x_coord_column],
                                           entry[y_coord_column])
            stop_name = entry[name_column] if entry[name_column] else 'Unbenannter Stop'

            writer.writerow(( entry[id_column],
                              stop_name,
                              str(lon),
                              str(lat),
                              '0',
                              'S'+entry[station_id_column] ))

        f.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('stops.txt')
        finally:
            zip_file.close()

        os.remove('stops.txt')
        return line
예제 #4
0
    def _write_calendar(self,
                        table_header_line):  # reads from $VERKEHRSTAG and ???
        # TODO: at this moment I'm not sure how to deal with calendar.txt
        #       so there will be just a single daily entry!

        calendar_header = (u'service_id', u'monday', u'tuesday', u'wednesday',
                           u'thursday', u'friday', u'saturday', u'sunday',
                           u'start_date', u'end_date')

        calendar_daily_entry = ('1', '1', '1', '1', '1', '1', '1', '1',
                                '20000101', '20201231')

        f = open('calendar.txt', 'w')
        writer = UnicodeWriter(f,
                               delimiter=',',
                               quotechar='"',
                               quoting=csv.QUOTE_MINIMAL)

        writer.writerow(calendar_header)
        writer.writerow(calendar_daily_entry)
        f.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('calendar.txt')
        finally:
            zip_file.close()

        os.remove('calendar.txt')
        return ''
예제 #5
0
    def _write_shapes(self):
        f = open('shapes.txt', 'w')
        writer = UnicodeWriter(f,
                               delimiter=',',
                               quotechar='"',
                               quoting=csv.QUOTE_MINIMAL)
        writer.writerow((u'shape_id', u'shape_pt_lat', u'shape_pt_lon',
                         u'shape_pt_sequence'))

        for s_id in self.shapes:
            for entry in self.shapes[s_id]:
                s_seq = entry[0]
                lat, lon = self.vertices[entry[1]]

                writer.writerow((s_id, lat, lon, s_seq))

        f.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('shapes.txt')
        finally:
            zip_file.close()

        os.remove('shapes.txt')
예제 #6
0
    def _write_agency(self, table_header_line):  # reads form $BETREIBER
        f = open('agency.txt', 'w')
        writer = UnicodeWriter(f,
                               delimiter=',',
                               quotechar='"',
                               quoting=csv.QUOTE_MINIMAL)
        writer.writerow((u'agency_id', u'agency_name', u'agency_url',
                         u'agency_timezone'))  # header

        columns = table_header_line.split(':')[1].split(';')

        id_column = columns.index('NR')
        name_column = columns.index('NAME')

        # read and write the entries
        while True:
            line = self.get_line()
            if line.startswith('$'):  # next section
                break
            entry = line.split(';')
            writer.writerow((entry[id_column], entry[name_column],
                             u'http://www.example.com', u'Europe/Berlin'))
        f.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('agency.txt')
        finally:
            zip_file.close()

        os.remove('agency.txt')
        return line
예제 #7
0
    def _write_tranfers(self, table_header_line): # reads from $UEBERGANGSGEHZEITHSTBER
        if self.debug: print( 'writing tranfers')
        try:

            with open('transfers.txt', 'w') as f:
                writer = UnicodeWriter(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                writer.writerow(( u'from_stop_id', u'to_stop_id', u'transfer_type', u'min_transfer_time' ))

                columns = table_header_line.split(':')[1].split(';')
                columns[-1] = columns[-1].split(self.eol)[0]

                from_stop_id_column = columns.index('VONHSTBERNR')
                to_stop_id_column = columns.index('NACHHSTBERNR')
                time_column = columns.index('ZEIT')

                while True:
                    line = self.get_line()
                    if line.startswith('$'):  # next section
                        break
                    entry = line.split(';')

                    transfer_time = entry[time_column][:-3] if len(self.eol) == 2 else entry[time_column][:-2]
                    writer.writerow(( entry[from_stop_id_column], entry[to_stop_id_column], '2', transfer_time ))

        finally:
            try:
                zip_file = ZipFile(self.output_file, 'a')
                zip_file.write('transfers.txt')
            finally:
                zip_file.close()

            os.remove('transfers.txt')
        return line
예제 #8
0
    def _write_routes(self, table_header_line): # reads form $LINIE
        f = open('routes.txt', 'a')
        writer = UnicodeWriter(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(( u'route_id', u'agency_id', u'route_short_name', u'route_long_name', u'route_type' ))# header

        columns = table_header_line.split(':')[1].split(';')
        for i,stuff in enumerate(columns): # find the position of the required columns
            if stuff == 'NAME':
                name_column = id_column = i
            elif stuff == 'VSYSCODE':
                route_type_column = i
            elif stuff == 'BETREIBERNR':
                agency_id_column = i

        while True:
            line = self.get_line()
            if line.startswith('$'):  # next section
                break
            entry = line.split(';')
            writer.writerow(( entry[id_column],
                              '1',
                              entry[name_column],
                              '',
                              self.route_types_map[entry[route_type_column]] ))
        f.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('routes.txt')
        finally:
            zip_file.close()

        os.remove('routes.txt')
        return line
예제 #9
0
    def _write_stop_times_and_trips(
            self, table_header_line):  # reads from $FAHRPLANFAHRT
        if self.debug: print 'writing stop_times.txt and trips.txt'

        st_file = open('stop_times.txt', 'w')
        st_writer = UnicodeWriter(st_file,
                                  delimiter=',',
                                  quotechar='"',
                                  quoting=csv.QUOTE_MINIMAL)
        st_writer.writerow((u'trip_id', u'arrival_time', u'departure_time',
                            u'stop_id', u'stop_sequence'))  # header

        t_file = open('trips.txt', 'w')
        t_writer = UnicodeWriter(t_file,
                                 delimiter=',',
                                 quotechar='"',
                                 quoting=csv.QUOTE_MINIMAL)
        t_writer.writerow(
            (u'route_id', u'service_id', u'trip_id', u'shape_id'))

        columns = table_header_line.split(':')[1].split(';')

        lr_id_column = columns.index('NR')
        departure_column = columns.index('ABFAHRT')
        route_id_column = columns.index('LINNAME')
        lrname_column = columns.index('LINROUTENAME')
        direction_column = columns.index('RICHTUNGCODE')
        fzprofilname_column = columns.index('FZPROFILNAME')

        lr_id = 0
        while True:
            line = self.get_line()
            if line.startswith('$'):  # next section
                break
            entry = line.split(';')

            lr_id += 1
            rst_id = '_'.join([
                entry[route_id_column], entry[lrname_column],
                entry[direction_column]
            ])
            fzp_id = '_'.join([
                entry[route_id_column], entry[lrname_column],
                entry[direction_column], entry[fzprofilname_column]
            ])
            fzp = self.raw_stop_times[fzp_id]
            if self.debug: print 'writing trip: %s' % lr_id

            t_writer.writerow(
                (entry[route_id_column], '1', str(lr_id),
                 fzp_id if fzp_id in self.shapes else u''))  # TODO: service_id

            for stop in fzp:
                arrival_time = time_adder(stop[2], entry[departure_column])
                departure_time = time_adder(stop[3], entry[departure_column])
                st_writer.writerow((str(lr_id), arrival_time, departure_time,
                                    stop[1], stop[0]))

        st_file.close()
        t_file.close()

        try:
            zip_file = ZipFile(self.output_file, 'a')
            zip_file.write('stop_times.txt')
            zip_file.write('trips.txt')
        except:
            raise 'hallo'
        finally:
            zip_file.close()

        os.remove('stop_times.txt')
        os.remove('trips.txt')
        return line