Saturday 21 September 2019

DOF_Manager QGIS plugin (2) - inserting parsed data into PostGIS database

This post won't be revealing if you familiar with previous posts related to insert data into database.

Method dof_dat_file2postgis simply reads each line of the data file, parse it and after parsing 10 000 lines (or all file if there is less lines) into database.  Notice, that before executing inert satatement parsed data is validated by method check_dof_parsed_data(), which at this satge does nothing and will be completed in the next post.:

def dof_dat_file2postgis(self, data_file):
    try:
        connection = psycopg2.connect(user="your_user",
                                      password="your_password",
                                      host="your_host",
                                      database="your database")
    except (Exception, psycopg2.Error) as error:
        print("Error while connecting to database", error)
        return
    
    cursor = connection.cursor()
    counter = 0
    line_nr = 0
    with open(data_file, 'r') as data:
        for line in data:
            line_nr += 1
            if line_nr < 5:
                continue
            else:
                counter += 1
                self.parse_data_line(line)
                check_result, check_err = self.check_dof_parsed_data()
    
                if check_result:
    
                    cursor.execute("""
                                    insert into
                                        faa_dof_obstacle 
                                        (oas_code, 
                                         obs_number, 
                                         verif_status, 
                                         ctry_ident, 
                                         state_ident, 
                                         city_name,
                                         lat_dms, 
                                         lon_dms, 
                                         obs_type, 
                                         quantity, 
                                         agl_ht,
                                         amsl_ht, 
                                         lighting,
                                         hor_acc,
                                         vert_acc,
                                         mark_indicator,
                                         faa_study_nr,
                                         action,
                                         julian_date,
                                         geo_location)
                                        values
                                        (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 
                                        %s, %s, %s, %s, %s, %s, %s, 
                                        ST_GeomFromText('POINT(%s %s)', 4326));
                                       """,
                                   (self.OAS_CODE,
                                    self.OBS_NUMBER,
                                    self.VERIF_STATUS,
                                    self.CTRY_IDENT,
                                    self.STATE_IDENT,
                                    self.CITY_NAME,
                                    self.LAT_DMS,
                                    self.LON_DMS,
                                    self.OBS_TYPE,
                                    self.QAUNTITY,
                                    self.AGL_HT,
                                    self.AMSL_HT,
                                    self.LIGHTING,
                                    self.HOR_ACC,
                                    self.VERT_ACC,
                                    self.MARK_INDICATOR,
                                    self.FAA_STUDY_NR,
                                    self.ACTION,
                                    self.JULIAN_DATE,
                                    self.LON_DD,
                                    self.LAT_DD))
                    if counter == 10000:
                        connection.commit()
                        counter = 0
                else:
                    print('Line nr: {}. Line: {} {}'.format(line_nr, line, check_err))
    
        connection.commit()
        cursor.close()

No comments:

Post a Comment