Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
119 lines (98 loc) · 4.15 KB
/
Copy pathscript.py
File metadata and controls
119 lines (98 loc) · 4.15 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
import geopandas as gpd
import pandas as pd
import os
import shutil
from zipfile import ZipFile
from glob import glob
import subprocess
import zipfile
import csv
# Define Data Paths
data_path = os.getenv('DATA_PATH', '/data')
inputs_path = os.path.join(data_path,'inputs')
# Define Input Paths
boundary_path = os.path.join(inputs_path,'boundary')
vector_path = os.path.join(inputs_path, 'vectors')
parameters_path = os.path.join(inputs_path, 'parameters')
# Define and Create Output Paths
outputs_path = os.path.join(data_path, 'outputs')
outputs_path_ = data_path + '/' + 'outputs'
if not os.path.exists(outputs_path):
os.mkdir(outputs_path_)
buildings_path = os.path.join(outputs_path, 'buildings')
buildings_path_ = outputs_path + '/' + 'buildings'
if not os.path.exists(buildings_path):
os.mkdir(buildings_path_)
# Look to see if a parameter file has been added
parameter_file = glob(parameters_path + "/*.csv", recursive = True)
print('parameter_file:', parameter_file)
# Identify the EPSG projection code
if len(parameter_file) == 1 :
parameters = pd.read_csv(parameter_file[0])
with open(parameter_file[0]) as file_obj:
reader_obj = csv.reader(file_obj)
for row in reader_obj:
try:
if row[0] == 'PROJECTION':
projection = row[1]
except:
continue
else:
projection = os.getenv('PROJECTION')
print('projection:',projection)
# Identify input polygons and shapes (boundary of city)
boundary_1 = glob(boundary_path + "/*.*", recursive = True)
print('Boundary File:',boundary_1)
# Read in the boundary
boundary = gpd.read_file(boundary_1[0])
# Check boundary crs matches the projection
if boundary.crs != projection:
boundary.to_crs(epsg=projection, inplace=True)
print('boundary_crs:', boundary.crs)
# Identify the name of the boundary file for the city name
file_path = os.path.splitext(boundary_1[0])
print('File_path:',file_path)
filename=file_path[0].split("/")
print('filename:',filename)
location = filename[-1]
print('Location:',location)
# Identify if the buildings are saved in a zip file
building_files_zip = glob(vector_path + "/*.zip", recursive = True)
print(building_files_zip)
# If yes, unzip the file (if the user has formatted the data correctly, this should reveal a .gpkg)
if len(building_files_zip) != 0:
for i in range (0,len(building_files_zip)):
print('zip file found')
with ZipFile(building_files_zip[i],'r') as zip:
zip.extractall(vector_path)
# Identify geopackages containing the polygons of the buildings
building_files = glob(vector_path + "/*.gpkg", recursive = True)
#buildings = gpd.read_file(building_files[0])
# Create a list of all of the gpkgs to be merged
to_merge=[]
to_merge=['XX' for n in range(len(building_files))]
for i in range (0,len(building_files)):
file_path = os.path.splitext(building_files[i])
filename=file_path[0].split("/")
to_merge[i]=filename[4]+'.gpkg'
print('to_merge:',to_merge)
# Create a geodatabase and merge the data from each gpkg together
all_builds = []
all_builds=gpd.GeoDataFrame(all_builds)
for cell in to_merge:
gdf = gpd.read_file('/data/inputs/vectors/%s' %cell)
all_builds = pd.concat([gdf, all_builds],ignore_index=True)
all_builds.to_crs(epsg=projection, inplace=True)
clipped = gpd.clip(all_builds,boundary)
all_builds = clipped.to_file(os.path.join(outputs_path,'all_buildings.shp'))
all_builds = gpd.read_file(os.path.join(outputs_path,'all_buildings.shp'))
all_builds = all_builds.explode()
all_builds.reset_index(inplace=True, drop=True)
all_builds1 = all_builds.to_file(os.path.join(buildings_path, location + '.gpkg'),driver='GPKG',index=False)
os.remove(os.path.join(outputs_path,'all_buildings.shp'))
os.remove(os.path.join(outputs_path,'all_buildings.cpg'))
os.remove(os.path.join(outputs_path,'all_buildings.dbf'))
os.remove(os.path.join(outputs_path,'all_buildings.prj'))
os.remove(os.path.join(outputs_path,'all_buildings.shx'))
# # Print to a gpkg file
# clipped.to_file(os.path.join(buildings_path, location + '.gpkg'),driver='GPKG',index=False)