/
IterativeViewshed.scala
266 lines (229 loc) · 9.43 KB
/
IterativeViewshed.scala
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2017 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.spark.viewshed
import geotrellis.proj4.LatLng
import geotrellis.raster._
import geotrellis.raster.rasterize.Rasterizer
import geotrellis.raster.viewshed.R2Viewshed
import geotrellis.raster.viewshed.R2Viewshed._
import geotrellis.spark._
import geotrellis.spark.tiling._
import geotrellis.util._
import geotrellis.vector._
import org.apache.log4j.Logger
import org.apache.spark.rdd.RDD
import org.apache.spark.SparkContext
import org.apache.spark.storage.StorageLevel
import org.apache.spark.util.AccumulatorV2
import scala.collection.mutable
import com.vividsolutions.jts.{ geom => jts }
object IterativeViewshed {
val logger = Logger.getLogger(IterativeViewshed.getClass)
type Message = (SpatialKey, Int, From, Ray) // key, point index, direction, ray
type Messages = mutable.ArrayBuffer[Message]
private class RayCatcher extends AccumulatorV2[Message, Messages] {
private val messages: Messages = mutable.ArrayBuffer.empty
def copy: RayCatcher = {
val other = new RayCatcher
other.merge(this)
other
}
def add(message: Message): Unit = this.synchronized { messages.append(message) }
def isZero: Boolean = messages.isEmpty
def merge(other: AccumulatorV2[Message, Messages]): Unit = this.synchronized { messages ++= other.value }
def reset: Unit = this.synchronized { messages.clear }
def value: Messages = messages
}
private def computeResolution[K: (? => SpatialKey), V: (? => Tile)](
elevation: RDD[(K, V)] with Metadata[TileLayerMetadata[K]]
) = {
val md = elevation.metadata
val mt = md.mapTransform
val kv = elevation.first
val key = implicitly[SpatialKey](kv._1)
val tile = implicitly[Tile](kv._2)
val extent = mt(key).reproject(md.crs, LatLng)
val degrees = extent.xmax - extent.xmin
val meters = degrees * (6378137 * 2.0 * math.Pi) / 360.0
val pixels = tile.cols
math.abs(meters / pixels)
}
private def pointInfo[K: (? => SpatialKey)](md: TileLayerMetadata[K])(
pi: (jts.Coordinate, Int)
)= {
val (p, index) = pi
val bounds = md.layout.mapTransform(p.envelope)
require(bounds.colMin == bounds.colMax)
require(bounds.rowMin == bounds.rowMax)
val cols = md.layout.tileCols
val rows = md.layout.tileRows
val key = SpatialKey(bounds.colMin, bounds.rowMin)
val extent = md.mapTransform(key)
val re = RasterExtent(extent, cols, rows)
val col = re.mapXToGrid(p.x)
val row = re.mapYToGrid(p.y)
(key, (index, col, row, p.z))
}
def apply[K: (? => SpatialKey), V: (? => Tile)](
elevation: RDD[(K, V)] with Metadata[TileLayerMetadata[K]],
ps: Seq[jts.Coordinate],
maxDistance: Double,
op: AggregationOperator = Or(),
curve: Boolean = true
)(implicit sc: SparkContext): RDD[(K, Tile)] with Metadata[TileLayerMetadata[K]] = {
val md = elevation.metadata
val mt = md.mapTransform
val resolution = computeResolution(elevation)
logger.debug(s"Computed resolution: $resolution meters/pixel")
val bounds = md.bounds.asInstanceOf[KeyBounds[K]]
val minKey = implicitly[SpatialKey](bounds.minKey)
val minKeyCol = minKey._1
val minKeyRow = minKey._2
val maxKey = implicitly[SpatialKey](bounds.maxKey)
val maxKeyCol = maxKey._1
val maxKeyRow = maxKey._2
val rays = new RayCatcher; sc.register(rays)
def rayCatcherFn(key: SpatialKey, index: Int)(ray: Ray, from: From): Unit = {
val key2 = from match {
case _: FromSouth => SpatialKey(key.col + 0, key.row + 1)
case _: FromWest => SpatialKey(key.col + 1, key.row + 0)
case _: FromNorth => SpatialKey(key.col + 0, key.row - 1)
case _: FromEast => SpatialKey(key.col - 1, key.row + 0)
case _: FromInside => throw new Exception
}
if (minKeyCol <= key2.col && key2.col <= maxKeyCol && minKeyRow <= key2.row && key2.row <= maxKeyRow) {
val message = (key2, index, from, ray)
rays.add(message)
}
}
val info: Seq[(SpatialKey, (Int, Int, Int, Double))] = {
val fn = pointInfo(md)_
ps.zipWithIndex.map(fn)
}
val _pointsByKey: Map[SpatialKey, Seq[(Int, Int, Int, Double)]] =
info
.groupBy(_._1)
.mapValues({ list => list.map({ case (_, v) => v }) })
.toMap
val pointsByKey = sc.broadcast(_pointsByKey)
val _pointsByIndex: Map[Int, (SpatialKey, Int, Int)] =
info
.groupBy(_._2._1)
.mapValues({ list => list.map({ case (key, (index, col, row, z)) => (key, col, row) }) })
.mapValues({ list => list.head })
.toMap
val pointsByIndex = sc.broadcast(_pointsByIndex)
val _heights: Map[Int, Double] =
elevation
.flatMap({ case (k, v) =>
val key = implicitly[SpatialKey](k)
val tile = implicitly[Tile](v)
pointsByKey.value.get(key) match {
case Some(list) =>
list.map({ case (index: Int, col: Int, row: Int, z: Double) =>
val height = if (z >= 0.0) tile.getDouble(col, row) + z ; else -z
(index, height)
})
case None => Seq.empty[(Int, Double)]
}
})
.collect
.toMap
val heights = sc.broadcast(_heights)
// Create RDD of viewsheds; after this, the accumulator contains
// the rays emanating from the starting points.
var sheds: RDD[(K, V, MutableArrayTile)] = elevation.map({ case (k, v) =>
val key = implicitly[SpatialKey](k)
val tile = implicitly[Tile](v)
val shed = R2Viewshed.generateEmptyViewshedTile(tile.cols, tile.rows)
pointsByKey.value.get(key) match {
case Some(list) =>
list.foreach({ case (index: Int, col: Int, row: Int, z: Double) =>
val height = heights.value.getOrElse(index, throw new Exception)
R2Viewshed.compute(
tile, shed,
col, row, height,
resolution, maxDistance,
FromInside(),
null,
rayCatcherFn(key, index),
op, curve
)
})
case None =>
}
(k, v, shed)
}).persist(StorageLevel.MEMORY_AND_DISK_SER)
sheds.count // make sheds materialize
// Repeatedly map over the RDD of viewshed tiles until all rays
// have reached the periphery of the layer.
do {
val _changes: Map[SpatialKey, Seq[(Int, From, Ray)]] =
rays.value
.groupBy(_._1)
.map({ case (k, list) => (k, list.map({ case (_, index, from, ray) => (index, from, ray) })) })
.toMap
val changes = sc.broadcast(_changes)
logger.debug(s"≥ ${changes.value.size} tiles in motion")
val oldSheds = sheds
rays.reset
sheds = oldSheds.map({ case (k, v, shed) =>
val key = implicitly[SpatialKey](k)
val elevationTile = implicitly[Tile](v)
val cols = elevationTile.cols
val rows = elevationTile.rows
changes.value.get(key) match {
case Some(localChanges: Seq[(Int, From, Ray)]) => { // sequence of <index, from, ray> triples for this key
val indexed: Map[Int, Seq[(From, Ray)]] = // a map from index to a sequence of <from, ray> pairs
localChanges
.groupBy(_._1)
.map({ case (index, list) => (index, list.map({ case (_, from, ray) => (from, ray) })) })
indexed.foreach({ case (index, list) => // for all <from, ray> pairs generated by this point (this index)
val (pointKey, col, row) = pointsByIndex.value.getOrElse(index, throw new Exception)
val startCol = (pointKey.col - key.col) * cols + col
val startRow = (pointKey.row - key.row) * rows + row
val height = heights.value.getOrElse(index, throw new Exception)
val packets: Map[From, Seq[Ray]] = // a map from direction to all rays coming from that direction
list
.groupBy(_._1)
.map({ case (from, list) => (from, list.map({ case (_, ray) => ray })) })
packets.foreach({ case (from, rays) => // for each <direction, packet> pair, evolve the tile
val sortedRays = rays.toArray.sortBy(_.theta)
R2Viewshed.compute(
elevationTile, shed,
startCol, startRow, height,
resolution, maxDistance,
from,
sortedRays,
rayCatcherFn(key, index),
op, curve
)
})
})
}
case None =>
}
(k, v, shed)
}).persist(StorageLevel.MEMORY_AND_DISK_SER)
sheds.count
oldSheds.unpersist()
} while (rays.value.size > 0)
// Return the computed viewshed layer
val metadata = TileLayerMetadata(IntConstantNoDataCellType, md.layout, md.extent, md.crs, md.bounds)
val rdd = sheds.map({ case (k, _, v) => (k, v.asInstanceOf[Tile]) })
ContextRDD(rdd, metadata)
}
}