-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (85 loc) · 3.37 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
# ==============================================================================
# File : main.py
#
# Current Author : Robert Hewlett
#
# Previous Author : None
#
# Contact Info : [email protected]
#
# Purpose : Demo loops and files
#
# Dependencies : None ... yet
#
# Modification Log :
# --> Created 2020-10-18 (rh)
# --> Updated YYYY-MM-DD (fl)
#
# =============================================================================
# ===================================
# File extentions
# ===================================
prjExt = '.prj'
csvtExt = '.csvt'
csvExt = '.csv'
# ===================================
# Constants and defaults
# ===================================
inFileName = 'grid_in.txt'
csvFileName = 'grid_out.csv'
# ==================================================
# Create geocsv companion file based on output name
# ==================================================
projFileName = csvFileName.replace(csvExt, prjExt)
csvtFileName = csvFileName.replace(csvExt, csvtExt)
# ===================================
# Start the pk-id at 1
# ===================================
id = 1
# ===================================
# Seed the companion files
# ===================================
with open(csvtFileName, 'w') as csvtFile, open(projFileName, 'w') as prjFile:
csvtFile.write('Integer,WKT')
prjFile.write('EPSG:26910')
# =======================================
# Process the grid file making WKT lines
# ======================================
with open(csvFileName, 'w') as csvFile, open(inFileName, 'r') as inFile:
csvFile.write('id,geom\n')
for line in inFile:
# =====================================================
# Get the grid info: starts, intervals and iterations
# ====================================================
xStart = float(line)
yStart = float(inFile.readline())
xInterval = float(inFile.readline())
yInterval = float(inFile.readline())
interations = int(inFile.readline())
# =====================================================
# punch out the start
# ====================================================
csvFile.write(f'{id},\"LINESTRING({xStart} {yStart},')
for i in range(interations):
# ====================================================
# punch out the coordinates for the grid pattern
# ====================================================
csvFile.write(f'{xStart} {yStart+yInterval},')
csvFile.write(f'{xStart+xInterval} {yStart+yInterval},')
csvFile.write(f'{xStart+xInterval} {yStart},')
csvFile.write(f'{xStart+2*xInterval} {yStart}')
# ===================================================
# The last coordinate does not need a comma
# ===================================================
if i < interations - 1:
csvFile.write(',')
xStart += 2*xInterval
# ===================================================
# pClose off the WKT linestring
# ===================================================
csvFile.write(')\"\n')
# ============================================================
# increase the pk-id if there is another grid left to process
# ============================================================
id +=1
print('Done ...')