Skip to content

Commit

Permalink
automates data population process
Browse files Browse the repository at this point in the history
  • Loading branch information
itzmeanjan committed Apr 10, 2019
1 parent 1fc23ca commit 7eeb295
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions fetch_and_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/python3

try:
from database_inflater import inflate_into_db
from osgeo import ogr as geo
from subprocess import run
except ImportError as e:
print('[!]Module Unavailable : {}'.format(str(e)))
exit(1)


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
for i in file_id:
print('[+]Working on `{}`'.format(path.format(i)))
datasource = geo.Open(path.format(i)) # datasource opened
# layer fetched, only a single layer present in a shape file
layer = datasource.GetLayer(0)
data_set = {}
tmp = []
for j in range(layer.GetFeatureCount()):
feature = layer.GetFeature(j) # gets feature by id
tmp.append([feature.items().get('GID_{}'.format(i)),
feature.items().get('NAME_{}'.format(i)),
feature.GetGeometryRef().ExportToWkt()])
# holds data in temp variable
# data format -- [feature_id, feature_name, outline]
data_set.update({i: tmp}) # pushes into dictionary
if(inflate_into_db('world_features', 'username', 'password', data_set)):
# finally inflate into database
print('[+]Success')
return


if __name__ == '__main__':
try:
run('clear')
app()
except KeyboardInterrupt:
print('\n[!]Terminated')
finally:
exit(0)

0 comments on commit 7eeb295

Please sign in to comment.