Skip to content

Commit

Permalink
Merge pull request #235 from jpolchlo/feature/euclidean-distance-opti…
Browse files Browse the repository at this point in the history
…mization

Euclidean distance optimization
  • Loading branch information
echeipesh committed May 24, 2017
2 parents 7197511 + bb4f79b commit fa11105
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
},
"outputs": [],
"source": [
"# The following operation takes about 90 seconds on a reasonably capable 4-core laptop\n",
"png_layer = PngRDD.makePyramid(weighted_layer, \"viridis\", end_zoom=8)"
]
},
Expand Down Expand Up @@ -171,17 +172,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"M.add_layer(VectorData(\"/tmp/bart.geojson\"), name=\"BART stops\", colors=[0xff0000])\n",
"M.add_layer(VectorData(\"/tmp/parks.geojson\"), name=\"Parks\", colors=[0xffff00])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"M.layers"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,19 +636,19 @@ object SpatialTiledRasterRDD {
SpatialTiledRasterRDD(None, MultibandTileLayerRDD(rdd.tileToLayout(metadata), metadata))
}

def euclideanDistance(sc: SparkContext, geomWKT: String, srcCRSStr: String, requestedZoom: Int): TiledRasterRDD[SpatialKey]= {
def euclideanDistance(sc: SparkContext, geomWKT: String, geomCRSStr: String, cellTypeString: String, requestedZoom: Int): TiledRasterRDD[SpatialKey]= {
val cellType = CellType.fromName(cellTypeString)
val geom = geotrellis.vector.io.wkt.WKT.read(geomWKT)
val srcCRS = TileRDD.getCRS(srcCRSStr).get
val LayoutLevel(z, ld) = ZoomedLayoutScheme(WebMercator).levelForZoom(requestedZoom)
val srcCRS = TileRDD.getCRS(geomCRSStr).get
val LayoutLevel(z, ld) = ZoomedLayoutScheme(srcCRS).levelForZoom(requestedZoom)
val maptrans = ld.mapTransform
val reprojected = geom.reproject(srcCRS, WebMercator)
val GridBounds(cmin, rmin, cmax, rmax) = maptrans(reprojected.envelope)
val gb @ GridBounds(cmin, rmin, cmax, rmax) = maptrans(geom.envelope)

val keys = for (r <- rmin to rmax; c <- cmin to cmax) yield SpatialKey(c, r)

val pts =
if (reprojected.isInstanceOf[MultiPoint]) {
reprojected.asInstanceOf[MultiPoint].points.map(_.jtsGeom.getCoordinate)
if (geom.isInstanceOf[MultiPoint]) {
geom.asInstanceOf[MultiPoint].points.map(_.jtsGeom.getCoordinate)
} else {
val coords = collection.mutable.ListBuffer.empty[Coordinate]

Expand All @@ -662,11 +662,10 @@ object SpatialTiledRasterRDD {
coords += coord
}

Rasterizer.foreachCellByGeometry(reprojected, re)(rasterizeToPoints)
(sk, coords.toArray)
Rasterizer.foreachCellByGeometry(geom, re)(rasterizeToPoints)
}
keys.foreach(createPoints)

keys.foreach(createPoints)
coords.toArray
}

Expand All @@ -675,19 +674,15 @@ object SpatialTiledRasterRDD {
val mbtileRDD: RDD[(SpatialKey, MultibandTile)] = skRDD.mapPartitions({ skiter => skiter.map { sk =>
val ex = maptrans(sk)
val re = RasterExtent(ex, ld.tileCols, ld.tileRows)
val tile = DoubleArrayTile.empty(re.cols, re.rows)
val tile = ArrayTile.empty(cellType, re.cols, re.rows)
val vd = new VoronoiDiagram(dt.value, ex)
vd.voronoiCellsWithPoints.foreach(EuclideanDistanceTile.rasterizeDistanceCell(re, tile)(_))
(sk, MultibandTile(tile))
} }, preservesPartitioning=true)

val projectedRDD: RDD[(ProjectedExtent, MultibandTile)] = mbtileRDD.map{ case (sk, mbtile) => {
val ex = maptrans(sk)
val projEx = ProjectedExtent(ex, WebMercator)
(projEx, mbtile)
}}
val metadata = TileLayerMetadata(cellType, ld, maptrans(gb), srcCRS, KeyBounds(gb))

SpatialTiledRasterRDD(Some(z), MultibandTileLayerRDD(mbtileRDD, projectedRDD.collectMetadata[SpatialKey](ld)))
SpatialTiledRasterRDD(Some(z), MultibandTileLayerRDD(mbtileRDD, metadata))
}

}
Expand Down
4 changes: 2 additions & 2 deletions geopyspark/geotrellis/rdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def rasterize(cls, geopysc, rdd_type, geometry, extent, crs, cols, rows,
return cls(geopysc, rdd_type, srdd)

@classmethod
def euclidean_distance(cls, geopysc, geometry, source_crs, zoom):
def euclidean_distance(cls, geopysc, geometry, source_crs, zoom, cellType='float64'):
"""Calculates the Euclidean distance of a Shapely geometry.
Args:
Expand All @@ -553,7 +553,7 @@ def euclidean_distance(cls, geopysc, geometry, source_crs, zoom):
if isinstance(source_crs, int):
source_crs = str(source_crs)

srdd = geopysc._jvm.geopyspark.geotrellis.SpatialTiledRasterRDD.euclideanDistance(geopysc.sc, dumps(geometry), source_crs, zoom)
srdd = geopysc._jvm.geopyspark.geotrellis.SpatialTiledRasterRDD.euclideanDistance(geopysc.sc, dumps(geometry), source_crs, cellType, zoom)
return cls(geopysc, SPATIAL, srdd)

def to_numpy_rdd(self):
Expand Down

0 comments on commit fa11105

Please sign in to comment.