-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate.jl
81 lines (77 loc) · 2.68 KB
/
aggregate.jl
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
"""
The aggregate value of an image over a mask can be computed by the aggregate function. The function multiplies the image and the mask (elementwise) and then performs the summation.
```
rand_image = rand(10, 10)
rand_mask = rand(0:1, 10, 10)
aggregate(rand_image, rand_mask)
```
"""
function aggregate(image, mask)
masked_image = copy(image).*mask
return sum(masked_image)
end
"""
To find the time series of aggregate values of a datacube over a mask, using the ```aggregate_timeseries``` function
```
rand_datacube = rand(10, 10, 10)
rand_mask = rand(0:1, 10, 10)
aggregate_timeseries(rand_datacube, rand_mask)
```
"""
function aggregate_timeseries(datacube, mask)
if typeof(datacube) == Array{Float16, 3}
@warn "Float 16 may not be sufficient to aggregate."
end
datacube = apply_mask(datacube, mask)
lights = []
for i in 1:length(datacube)
push!(lights, sum(ts[i]))
end
return Float32.(lights)
end
"""
Computes the time series of aggregate value of datacube for each row of a shapefile
# Example:
```
rand_datacube = rand(10, 10, 10)
shapefile_df = load_shapefile("assets/mumbai_map/mumbai_districts.shp")
aggregate_dataframe(MUMBAI_COORDINATE_SYSTEM, rand_datacube, shapefile_df, "District")
```
"""
function aggregate_dataframe(geometry::CoordinateSystem, datacube, shapefile_df, attribute)
if typeof(datacube) == Array{Float16, 3}
@warn "Float 16 may not be sufficient to aggregate."
end
datacube = sparse_cube(datacube)
df = DataFrame()
@showprogress for i in 1:length(shapefile_df[:, 1])
shapefile_row = shapefile_df[i, :]
geom_polygon = polygon_mask(geometry, shapefile_row)
df[!, shapefile_df[!, attribute][i]] = aggregate_timeseries(datacube, geom_polygon)
i = i + 1
end
return df
end
"""
Computes the time series of aggregate value per area of datacube for each row of a shapefile
# Example:
```
rand_datacube = rand(10, 10, 10)
shapefile_df = load_shapefile("assets/mumbai_map/mumbai_districts.shp")
aggregate_per_area_dataframe(MUMBAI_COORDINATE_SYSTEM, rand_datacube, shapefile_df, "District")
```
"""
function aggregate_per_area_dataframe(geometry::CoordinateSystem, datacube, shapefile_df, attribute)
if typeof(datacube) == Array{Float16, 3}
@warn "Float 16 may not be sufficient to aggregate."
end
datacube = sparse_cube(datacube)
df = DataFrame()
@showprogress for i in 1:length(shapefile_df[:, 1])
shapefile_row = shapefile_df[i, :]
geom_polygon = polygon_mask(geometry, shapefile_row)
df[!, shapefile_df[!, attribute][i]] = aggregate_timeseries(datacube, geom_polygon) ./mask_area(geom_polygon)
i = i + 1
end
return df
end