/
IterativeViewshed.scala
239 lines (208 loc) · 8.6 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
/*
* 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 java.util.Arrays.sort
object IterativeViewshed {
val logger = Logger.getLogger(IterativeViewshed.getClass)
type Message = (SpatialKey, From, Ray)
type Messages = mutable.ArrayBuffer[Message]
type Rays = (Array[Ray], Array[Ray])
// type Coordinates = (Int, Int, Int, Int)
// class PointFinder extends AccumulatorV2[Coordinates, Coordinates] {
// private var coordinates: Coordinates = null
// def copy PointFinder = {
// val other = new PointFinder
// other.merge(this)
// other
// }
// def add(_coordinates: Coordinates): Unit = this.synchronized { coordinates = _coordinates }
// def isZero: Boolean = (coordinates == null)
// def merge(other: AccumulatorV2[Coordinates, Coordinates]): Unit = this.synchronized { coordinates = other.value }
// def reset: Unit = this.synchronized { coordinates = null }
// def value: Coordinates = coordinates.copy
// }
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
}
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)
}
def apply[K: (? => SpatialKey), V: (? => Tile)](
elevation: RDD[(K, V)] with Metadata[TileLayerMetadata[K]],
point: Point, viewHeight: Double,
maxDistance: Double,
and: Boolean = false,
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)
val pointKeyCol = sc.longAccumulator
val pointKeyRow = sc.longAccumulator
val pointCol = sc.longAccumulator
val pointRow = sc.longAccumulator
val pointHeight = sc.doubleAccumulator
def rayCatcherFn(key: SpatialKey)(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, from, ray)
rays.add(message)
}
}
// Create RDD of viewsheds; the tile containing the starting point
// is complete and the accumulator contains the rays emanating
// from that.
var sheds: RDD[(K, V, MutableArrayTile)] = elevation.map({ case (k, v) =>
val key = implicitly[SpatialKey](k)
val tile = implicitly[Tile](v)
val cols = tile.cols
val rows = tile.rows
val extent = mt(key)
val rasterExtent = RasterExtent(extent, cols, rows)
val options = Rasterizer.Options.DEFAULT
val shed = R2Viewshed.generateEmptyViewshedTile(cols, rows)
if (extent.contains(point)) {
Rasterizer
.foreachCellByGeometry(point, rasterExtent, options)({ (col, row) =>
val height = if (viewHeight >= 0.0) tile.getDouble(col, row) + viewHeight ; else -viewHeight
pointHeight.reset ; pointHeight.add(height)
pointKeyCol.reset ; pointKeyCol.add(key.col)
pointKeyRow.reset ; pointKeyRow.add(key.row)
pointCol.reset ; pointCol.add(col)
pointRow.reset ; pointRow.add(row)
R2Viewshed.compute(
tile, shed,
col, row, height,
resolution, maxDistance,
FromInside(),
null,
rayCatcherFn(key),
and, curve
)
})
}
(k, v, shed)
}).persist(StorageLevel.MEMORY_AND_DISK_SER)
sheds.count
val _details = (pointKeyCol.value, pointKeyRow.value, pointCol.value, pointRow.value, pointHeight.value)
val details = sc.broadcast(_details)
// Repeatedly map over the RDD of viewshed tiles until all rays
// have reached the periphery of the layer.
do {
val _changes: Map[SpatialKey, Seq[(From, Ray)]] =
rays.value
.groupBy(_._1)
.map({ case (k, list) => (k, list.map({ case (_, from, ray) => (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
val localChanges: Option[Seq[(From, Ray)]] = changes.value.get(key)
val (pointKeyCol, pointKeyRow, pointCol, pointRow, height) = details.value
localChanges match {
case Some(localChanges) => {
val packets: List[(From, Seq[Ray])] =
localChanges
.groupBy(_._1)
.map({ case (from, list) => (from, list.map({ case (_, ray) => ray })) })
.toList
packets.foreach({ case (from, rays) =>
val startCol = (pointKeyCol - key.col) * cols + pointCol
val startRow = (pointKeyRow - key.row) * rows + pointRow
// println(s"AAA $key $from ($startCol, $startRow) pointKeyCol=$pointKeyCol pointKeyRow=$pointKeyRow key.col=${key.col} key.row=${key.row} pointCol=$pointCol pointRow=$pointRow")
R2Viewshed.compute(
elevationTile, shed,
startCol.toInt, startRow.toInt, height,
resolution, maxDistance,
from,
rays.sortBy({ _.theta }).toArray,
rayCatcherFn(key),
and, curve,
// key == SpatialKey(1,0)
false
)
})
}
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)
}
}