-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey123_to_postgres.py
67 lines (48 loc) · 1.82 KB
/
survey123_to_postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Extract Survey123 table and load onto Postgres db.
Note: create a directory named `survey123/` at the same level as this file.
"""
import os
import shutil
from arcgis.apps.survey123 import SurveyManager, Survey
from arcgis.gis import GIS
from dotenv import load_dotenv
import pandas as pd
from sqlalchemy import create_engine
load_dotenv()
USER_NAME = os.getenv('SURVEY123_USER_NAME', None)
PASSWORD = os.getenv('SURVEY123_PASSWORD', None)
SURVEY_ID = '21a05e4038314f6c9c8fd88485c9eef3'
OUTPUT_DIR = 'survey123/'
def create_survey123_dataframe():
"""Extract survey123 table and return it as a DataFrame.
"""
# Credentials
gis = GIS("https://12csi.maps.arcgis.com/", USER_NAME, PASSWORD)
# print(f"Connected to {gis.properties.portalHostname} as {gis.users.me.username}")
# Get the report
sm = SurveyManager(gis)
report = sm.get(SURVEY_ID)
# Download the report data as CSV
arr1 = os.listdir(OUTPUT_DIR)
report.download(export_format='csv', save_folder=OUTPUT_DIR)
# print("Downloaded report.")
# Select newly added `.zip`
arr2 = os.listdir(OUTPUT_DIR)
target_zip = (set(arr2)-set(arr1)).pop()
# Unpack `.zip`
shutil.unpack_archive(os.path.join(OUTPUT_DIR,target_zip), OUTPUT_DIR)
# Create df
target_csv = os.path.join(OUTPUT_DIR, 'survey_0.csv')
return pd.read_csv(target_csv)
def load_to_postgres(df, table_name='survey_raw'):
"""Load DataFrame to Postgres db.
"""
# Establish connection to Postgres db
engine = create_engine(os.getenv('POSTGRES_CONNECTION'))
# Write dataframe to db with table name `surver_raw_table_name` (OVERWRITES)
df.to_sql(table_name, engine, if_exists='replace')
# print(df.head())
# print('Succesfully wrote to DB.')
if __name__=="__main__":
df = create_survey123_dataframe()
load_to_postgres(df)