Skip to content

Commit

Permalink
fixed issue with database population
Browse files Browse the repository at this point in the history
  • Loading branch information
itzmeanjan committed Apr 11, 2019
1 parent 7eeb295 commit 09e3bde
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 16 additions & 6 deletions database_inflater.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def push_into_table(db_name, username, password, table_name, data):
cursor = conn.cursor() # cursors give us API for executing SQL queries
count = 0
for feature_id, feature_name, outline in data:
cursor.execute('insert into %s values ( %s, %s, st_geogfromtext(%s))',
(table_name, feature_id, feature_name, outline))
cursor.execute("insert into {} values ( %s, %s, st_geogfromtext(%s))".format(table_name),
(feature_id, feature_name, outline))
count += 1
if(not count % 100):
# after every 100 push, we just commit the changes made
Expand All @@ -26,9 +26,13 @@ def push_into_table(db_name, username, password, table_name, data):
conn.close() # connection to DB closed
except psql.DatabaseError as e:
print('[!]Error: {}'.format(str(e)))
cursor.close()
conn.close()
return False
except Exception as e:
print('[!]Error: {}'.format(str(e)))
cursor.close()
conn.close()
return False
return True # in case of success

Expand All @@ -38,19 +42,23 @@ def create_table(db_name, username, password, table_name):
try:
conn = psql.connect(database=db_name, user=username, password=password)
cursor = conn.cursor()
cursor.execute('drop table if exists %s', (table_name,))
cursor.execute("drop table if exists {}".format(table_name))
cursor.execute(
'create table %s (feature_id varchar primary key, feature_name varchar not null, outline geography)', (table_name, ))
cursor.execute('create index %s on %s using gist( outline )',
('{}_index'.format(table_name), table_name))
"create table {} (feature_id varchar primary key, feature_name varchar not null, outline geography)".format(table_name))
cursor.execute("create index {} on {} using gist( outline )".format(
'{}_index'.format(table_name), table_name))
conn.commit() # committing changes made to DB
cursor.close()
conn.close() # closed connection to DB
except psql.DatabaseError as e:
print('[!]Error: {}'.format(str(e)))
cursor.close()
conn.close()
return False
except Exception as e:
print('[!]Error: {}'.format(str(e)))
cursor.close()
conn.close()
return False
return True # in case of success

Expand All @@ -64,6 +72,8 @@ def inflate_into_db(db_name, username, password, data_set):
'[+]Pushing into table -- `{}`'.format('world_features_level_{}'.format(key)))
push_into_table(db_name, username, password,
'world_features_level_{}'.format(key), value)
else:
return False
except Exception as e:
print('[!]Error: {}'.format(str(e)))
return False
Expand Down
1 change: 1 addition & 0 deletions fetch_and_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
def app(path='/path-to-file/gadm36_{}.shp', file_id=[0, 1, 2, 3, 4, 5]):
# path, path to gadm shapefiles
# gadm has 6 layers, shape files hold corresponding layer number too
print('[+]Now grab a cup of coffee, cause this gonna be a little longer ...\n')
for i in file_id:
print('[+]Working on `{}`'.format(path.format(i)))
datasource = geo.Open(path.format(i)) # datasource opened
Expand Down

0 comments on commit 09e3bde

Please sign in to comment.