This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ees2sem_to_run_local_files.py
150 lines (137 loc) · 6.45 KB
/
ees2sem_to_run_local_files.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
import os
import sys
import gzip
import urllib.request
from argparse import ArgumentParser
import xml.etree.ElementTree as ET
import numpy as np
from geostack.raster import Raster
from geostack.vector import vector
from geostack.vector import Vector
from geostack import gs_enums
from geostack.utils import get_epsg
from geostack.runner import runScript
def main(outdir):
# #TODO: make these command line arguments
# outdir='./data/castlemaine-region/'
#popnFileUrl='#https://github.com/agentsoz/ees/raw/dbfab224daaeb02294b5dabb62f55b5f8755b6ce/ees/scenarios/mount-alexander-shire/castlemaine-region-archetypes/population-archetypes.xml.gz'
#popnFile= outdir + 'abm/population-archetypes.xml.gz'
popnFile= '../aireys-20201019/msg1/r.msg1.19/output/matsim/output_plans.xml.gz'
#28355
popnEpsg='32754'
popnActivityFilter='home'
popnOutfile= outdir + 'r.msg1.19/plans.tif'
popnRasterCellSize='100'
#firePhoenix4GridShpUrl='#https://github.com/agentsoz/ees-data/raw/master/mount-alexander-shire/phoenix-shapefiles/20181109/Evac_Phoenix_runs/20181109_mountalex_evac_ffdi100d/20181109_mountalex_evac_ffdi100d_grid.shp'
#firePhoenix4GridShp = outdir + 'abm/20181109_mountalex_evac_ffdi100d_grid.shp'
firePhoenix4GridShp = '../aireys-20201019/firefile.shp'
fireOutfile= outdir + 'r.msg1.19/AireysInlet_grid_epsg32754.tif'
fireRasterCellSize='10'
#networkFileUrl='https://github.com/agentsoz/ees/raw/dbfab224daaeb02294b5dabb62f55b5f8755b6ce/ees/scenarios/mount-alexander-shire/mount_alexander_shire_network_2018.xml.gz'
#networkFile= outdir + 'abm/mount_alexander_shire_network_2018.xml.gz'
networkFile= '../aireys-20201019/surf_coast_shire_network_2020.xml.gz'
networkEpsg='32754'
networkOutfilePrefix= outdir + 'r.msg1.19/aireys-20201019_network_2020'
# Parse args
popnEpsg = get_epsg(int(popnEpsg))
popnRasterCellSize = int(popnRasterCellSize)
networkEpsg = get_epsg(int(networkEpsg))
epsg4326 = get_epsg(4326)
# Create the output dir
#os.makedirs(outdir + "/abm", exist_ok=True)
os.makedirs(outdir + "/r.msg1.19", exist_ok=True)
# Download all files
#if not os.path.exists(popnFile):
#print("Downloading " + popnFileUrl + " to " + popnFile)
#urllib.request.urlretrieve(popnFileUrl, popnFile)
#if not os.path.exists(networkFile):
#print("Downloading " + networkFileUrl + " to " + networkFile)
#urllib.request.urlretrieve(networkFileUrl, networkFile)
#for ext in ['shp', 'prj', 'dbf', 'cpg', 'shx']:
#src = firePhoenix4GridShpUrl.replace('shp', ext)
#dst = firePhoenix4GridShp.replace('shp', ext)
#if not os.path.exists(dst):
#print("Downloading " + src + " to " + dst)
#urllib.request.urlretrieve(src, dst)
# Vectorise the MATSim network
print("Reading MATSim network from " + networkFile)
input = gzip.open(networkFile, 'r')
tree = ET.parse(input)
root = tree.getroot()
print("Parsing network nodes into a vector")
nodes=dict()
vec = vector.Vector()
for elem in root.iter('node'):
c = vector.Coordinate(float(elem.attrib['x']), float(elem.attrib['y']))
pointIdx = vec.addPoint(c)
#vec.setProperty(pointIdx, "newproperty", "newstr")
nodes[elem.attrib['id']] = c[:2] # record the mapping of node id to xy coords
vec.setProjectionParameters(networkEpsg)
input.close()
outfile = networkOutfilePrefix + ".nodes.shp"
print("Writing " + outfile)
vec.to_shapefile(outfile, gs_enums.GeometryType.Point)
outfile = networkOutfilePrefix + ".nodes.geojson"
print("Writing " + outfile)
vec.convert(epsg4326)
vec.to_geojson(outfile)
print("Parsing network links into a vector")
# vec = vector.Vector()
for elem in root.iter('link'):
line = [nodes[elem.attrib['from']], nodes[elem.attrib['to']]]
lineIdx = vec.addLineString(line)
#vec.setProperty(lineIdx, "newproperty", "newstr")
vec.setProperty(lineIdx, "diameter", 1.0)
vec.setProjectionParameters(networkEpsg)
input.close()
outfile = networkOutfilePrefix + ".links.shp"
print("Writing " + outfile)
vec.to_shapefile(outfile, gs_enums.GeometryType.LineString)
outfile = networkOutfilePrefix + ".links.geojson"
print("Writing " + outfile)
vec.convert(epsg4326)
vec.to_geojson(outfile)
# Rasterise the fire shapefile
print("Loading Phoenix4 fire grid shapefile " + firePhoenix4GridShp)
vec = Vector.from_shapefile(firePhoenix4GridShp)
print("Rasterising the fire vector")
raster = vec.rasterise(fireRasterCellSize, "output = HOUR_BURNT;")
# convert hours to seconds
runScript("rasterised = rasterised * 3600.0;", [raster])
print("Writing " + fireOutfile)
raster.write(fireOutfile)
# Rasterise the MATSim population home locations
print("Reading MATSim population from " + popnFile)
input = gzip.open(popnFile, 'r')
tree = ET.parse(input)
root = tree.getroot()
print("Parsing home activity coordinates into a vector")
vec = vector.Vector()
for act in root.iter('activity'):
if(act.attrib['type']==popnActivityFilter):
c = vector.Coordinate(float(act.attrib['x']), float(act.attrib['y']))
pointIdx = vec.addPoint(c)
#vec.setProperty(pointIdx, "newproperty", "newstr")
vec.setProjectionParameters(popnEpsg)
input.close()
print("Rasterising the home activity locations vector")
bounds = vec.getBounds()
bounds.extend(popnRasterCellSize)
count = Raster(name="count", data_type=np.uint32)
count.init_with_bbox(bounds, popnRasterCellSize)
count.setProjectionParameters(popnEpsg)
count.setAllCellValues(0)
count.rasterise(vec, "atomic_inc();")
print("Writing " + popnOutfile)
count.write(popnOutfile)
if __name__ == "__main__":
parser = ArgumentParser(description="script to convert vector and raster files using geostack")
#parser.add_argument("--popfile", type=str, help="path to the input population file directory or URL", required=True)
#parser.add_argument("--firefile", type=str, help="path to the input fire file directory or URL", required=True)
#parser.add_argument("--networkfile", type=str, help="path to the input road network file directory or URL", required=True)
parser.add_argument("--outdir", dest="outdir", type=str, help="path to the output directory")
args = parser.parse_args()
if args.outdir is None:
parser.print_help()
else:
main(args.outdir)