-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (141 loc) · 5.81 KB
/
main.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#this is just an example of how to create a 12d Model chain using python, you need to adjust according to your project
#contact me if you need help
import pandas as pd
import chianTxtIntro
import ChainBodyXML
# Set pandas display option
pd.set_option("display.max_rows", None)
# Define the path to the Excel file
path_to_excel_file = 'export.xlsx'
# Alignment strings for Stage 2 and Stage 3 -- chainge this to your project alignment strings
alignment_stg2 = (
r"<model_name>DES FRM STG2 Alignments V05</model_name>"
r"<model_id>{3B53B647-02B1-4808-87C6-D0474E81EB80-0000000000002034}</model_id>"
r"<name>N2NS MainLine 115 3 5</name>"
r"<id>{3B53B647-02B1-4808-87C6-D0474E81EB80-0000000000002E46}</id>"
)
alignment_stg3 = (
r"<model_name>DES FRM STG3 Alignments V05</model_name>"
r"<model_id>{3B53B647-02B1-4808-87C6-D0474E81EB80-0000000000002034}</model_id>"
r"<name>N2NS MainLine 115 3 5</name>"
r"<id>{3B53B647-02B1-4808-87C6-D0474E81EB80-0000000000002E46}</id>"
)
def read_excel_file_and_return_data(path_to_excel_file):
"""
Reads the Excel file and returns a cleaned DataFrame.
Filters out rows based on specific criteria.
"""
try:
df = pd.read_excel(path_to_excel_file)
except Exception as e:
print(f"Error reading Excel file: {e}")
return pd.DataFrame() # Return empty DataFrame on error
# Select relevant columns
data = df[[
"Lot No",
"Chainage Start (km)",
"Chainage End (km)",
"Activity",
"Region",
"Description",
"Status",
"Sub Area"
]]
# Apply filters
data = data[
(data['Chainage End (km)'] != 0) &
(data['Chainage End (km)'] <= 1000) &
(data['Chainage Start (km)'] <= 1000) &
(data['Status'] != 'Abandoned')
]
return data
def iter_over_data_and_write_chain(data, alignment_stg2, alignment_stg3, f_stg2, f_stg3):
"""
Iterates over the data and writes the chain files for Stage 2 and Stage 3.
"""
record_not_stored_counter = 0
for index, row in data.iterrows():
# Build the text using ChainBodyXML.Chaintext template
name1 = f"{str(row['Lot No']).replace('/', '-')} {row['Description']} {row['Status']}"
text = ChainBodyXML.Chaintext.replace("name1", name1)
# Replace chainage start and end
ch1 = str(row['Chainage Start (km)'] * 1000)
ch2 = str(row['Chainage End (km)'] * 1000)
text = text.replace("ch1", ch1)
text = text.replace("ch2", ch2)
# Determine the 'model1' string
description_lower = str(row['Description']).lower()
if "e3" in description_lower:
if "sf" in description_lower:
if "layer 1" in description_lower:
model1 = "PPW BDY Earthworks Filling E3 SF Layer 1"
elif "layer 2" in description_lower:
model1 = "PPW BDY Earthworks Filling E3 SF Layer 2"
else:
model1 = "PPW BDY Earthworks Filling E3 SF"
else:
if "layer 1" in description_lower:
model1 = "PPW BDY Earthworks Filling E3 Layer 1"
elif "layer 2" in description_lower:
model1 = "PPW BDY Earthworks Filling E3 Layer 2"
else:
model1 = "PPW BDY Earthworks Filling E3"
elif "c3" in description_lower:
if "layer 1" in description_lower:
model1 = "PPW BDY Earthworks Filling C3 Layer 1"
elif "layer 2" in description_lower:
model1 = "PPW BDY Earthworks Filling C3 Layer 2"
else:
model1 = "PPW BDY Earthworks Filling C3"
else:
if "layer 1" in description_lower:
model1 = "PPW BDY Earthworks Filling Layer 1"
elif "layer 2" in description_lower:
model1 = "PPW BDY Earthworks Filling Layer 2"
else:
model1 = f"PPW BDY {row['Activity']}"
text = text.replace("model1", model1)
# Determine color based on Status
status = str(row['Status'])
if status == "Open":
color = "yellow"
elif status == "Planned":
color = "blue"
elif status == "Closed":
color = "green"
else:
color = "red"
text = text.replace("colorr", color)
# Determine the stage and write to the appropriate file
region = str(row['Region'])
if region.startswith("Stage 2"):
text = text.replace("xxALxx", alignment_stg2)
f_stg2.write(text)
elif region.startswith("Stage 3"):
text = text.replace("xxALxx", alignment_stg3)
f_stg3.write(text)
else:
# Handle other stages if necessary
record_not_stored_counter += 1
print(f"Record at index {index} with Region '{region}' not stored.")
print(f"Number of records not stored: {record_not_stored_counter}")
def main():
# Read the data
data = read_excel_file_and_return_data(path_to_excel_file)
if data.empty:
print("No data to process.")
return
# Open the chain files using context managers
with open('PPW_STG2_Create_Polygons.chain', 'w') as f_stg2, open('PPW_STG3_Create_Polygons.chain', 'w') as f_stg3:
# Write the introduction to both files
f_stg2.write(chianTxtIntro.Intro)
f_stg3.write(chianTxtIntro.Intro)
# Process the data and write to files
iter_over_data_and_write_chain(data, alignment_stg2, alignment_stg3, f_stg2, f_stg3)
# Write the closing tags to both files
closing_tags = '</Commands>\n</Chain>\n</xml12d>'
f_stg2.write(closing_tags)
f_stg3.write(closing_tags)
print("Chain files written successfully.")
if __name__ == "__main__":
main()