-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOSOpenLoader.py
More file actions
155 lines (124 loc) · 7.07 KB
/
Copy pathOSOpenLoader.py
File metadata and controls
155 lines (124 loc) · 7.07 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import with_statement
import os,os.path,sys,glob
import shlex, subprocess
import string, csv
import psycopg2
config_file = sys.argv[1]
requested_dataset = sys.argv[2]
class OSOpenLoader:
def __init__(self):
pass
def run(self, config):
self.read_config(config)
self.load()
def read_config(self, config):
self.config = config
self.src_dir = config['src_dir']
self.csv_table = config['csv_table']
self.database = config['database']
if not os.path.exists(self.src_dir):
print 'Filepath does not exist'
def load (self):
try:
conn=psycopg2.connect(self.database)
except:
print "Unable to connect to the database"
# Open table with datasets available and parameters used by this script
table = open(self.csv_table)
# Read the header line and get the important field indices
headerLine = table.readline()
headerLine = headerLine.rstrip("\n")
segmentedHeaderLine = headerLine.split(";")
# Figure out positions of all parameters in the table
datasetIndex = segmentedHeaderLine.index("Dataset")
location_folderIndex = segmentedHeaderLine.index("Location_folder")
schemaIndex = segmentedHeaderLine.index("Schema")
source_ProjectionIndex = segmentedHeaderLine.index("Source_Projection")
vrt_filenameIndex = segmentedHeaderLine.index("VRT_filename")
csv_filenameIndex = segmentedHeaderLine.index("CSV_filename")
headerIndex = segmentedHeaderLine.index("Header")
delimiterIndex = segmentedHeaderLine.index("Delimiter")
# Loop through each line in the table, splitting it with delimiter ";"
for line in table.readlines():
line = line.rstrip("\n")
segmentedLine = line.split(";")
# Create variables for all needed field items based on index position
dataset = segmentedLine[datasetIndex]
location_folder = segmentedLine[location_folderIndex]
schema = segmentedLine[schemaIndex]
source_projection = segmentedLine[source_ProjectionIndex]
vrt_filename = segmentedLine[vrt_filenameIndex]
csv_filename = segmentedLine[csv_filenameIndex]
header = segmentedLine[headerIndex]
delimiter = segmentedLine[delimiterIndex]
# Find requested dataset name in the Table.csv and load files using parameters from it
if dataset==requested_dataset:
for folder in glob.glob(os.path.join(self.src_dir, location_folder)):
folder_path=os.path.join(self.src_dir, location_folder)
if os.path.exists(folder_path):
num_files = 0
seen_files = []
for root, dirs, files in os.walk(folder_path):
for name in files:
if name.endswith(".shp"):
file_path=os.path.join(root, name)
print "Processing: %s" % file_path
if name in seen_files:
appover = "append"
else:
appover = "overwrite"
seen_files.append(name)
if "text" in name or "Text" in name:
os.environ["PGCLIENTENCODING"] = "LATIN1"
else:
os.environ["PGCLIENTENCODING"] = "UTF8"
ogr_cmd="ogr2ogr -%s -skipfailures -f PostgreSQL PG:'%s active_schema=%s' -s_srs %s -a_srs EPSG:27700 -lco PRECISION=NO -nlt GEOMETRY" %(appover,self.database,schema,source_projection)
args = shlex.split(ogr_cmd)
args.append(file_path)
rtn = subprocess.call(args)
num_files += 1
if name==csv_filename:
original_csv=os.path.join(root, name)
new_csv=os.path.join(root, name[:-4]+"_new.csv")
if os.path.exists(original_csv):
print "Editing %s and creating %s" % (original_csv,new_csv)
header_string= header.split()
with open(original_csv, 'r') as original_csv:
with open(new_csv, 'w') as new_csv:
row = header_string
header = csv.writer(new_csv,delimiter=';')
header.writerow(row)
for row in original_csv:
new_delimiter = row.replace(delimiter,";")
new_csv.write(new_delimiter)
else:
print "No correct CSV file found"
sys.exit()
vrtfile_path=os.path.join(root, vrt_filename)
if os.path.exists(vrtfile_path):
print "Loading csv data using %s" % vrtfile_path
if requested_dataset=="Gazetteer" or "GAZETTEER" in name:
os.environ["PGCLIENTENCODING"] = "LATIN1"
ogr_cmd="ogr2ogr -overwrite -skipfailures -f PostgreSQL PG:'%s active_schema=%s' -s_srs %s -a_srs EPSG:27700" %(self.database,schema,source_projection)
args = shlex.split(ogr_cmd)
args.append(vrtfile_path)
rtn = subprocess.call(args)
else:
print "No correct VRT file found. Cannot load %s data" % dataset
sys.exit()
print "Successfully loaded CSV data from %s" % dataset
print "Loaded %s shapefiles" % num_files
if not os.path.exists(folder_path):
print "Path to folder is not correct"
sys.exit()
def main():
if os.path.exists(config_file):
# Build a dict of configuration
with open(config_file, 'r') as f:
config = dict([line.replace('\n','').split('=',1) for line in f.readlines() if len(line.replace('\n','')) and line[0:1] != '#'])
loader = OSOpenLoader()
loader.run(config)
else:
print 'Could not find config file:', config_file
if __name__ == '__main__':
main()