-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from jamesmcclain/feature/cost-distance
Add Support For Cost-Distance and Stitch
- Loading branch information
Showing
7 changed files
with
463 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...trellis/src/main/scala/geopyspark/geotrellis/spark/costdistance/CostDistanceWrapper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package geopyspark.geotrellis.spark.costdistance | ||
|
||
import geopyspark.geotrellis._ | ||
|
||
import geotrellis.raster._ | ||
import geotrellis.spark._ | ||
import geotrellis.spark.io._ | ||
import geotrellis.spark.io.avro._ | ||
import geotrellis.vector._ | ||
import geotrellis.vector.io.wkt.WKT | ||
import geotrellis.spark.costdistance.IterativeCostDistance | ||
|
||
import spray.json._ | ||
|
||
import org.apache.spark.api.java.JavaRDD | ||
import org.apache.spark.SparkContext | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
|
||
object CostDistanceWrapper { | ||
|
||
implicit def convertion(k: SpaceTimeKey): SpatialKey = | ||
k.spatialKey | ||
|
||
private def costdistance[K: (? => SpatialKey): ClassTag: AvroRecordCodec: JsonFormat]( | ||
javaRdd: JavaRDD[Array[Byte]], | ||
schema: String, | ||
metadata: TileLayerMetadata[K], | ||
geometries: Seq[Geometry], | ||
maxDistance: Double, | ||
_sc: SparkContext | ||
): (JavaRDD[Array[Byte]], String) = { | ||
implicit val sc = _sc | ||
val _rdd = PythonTranslator.fromPython[(K, MultibandTile)](javaRdd, Some(schema)) | ||
val rdd = ContextRDD( | ||
_rdd.map({ case (k, v) => (k, v.band(0)) }), | ||
metadata | ||
) | ||
|
||
PythonTranslator.toPython(IterativeCostDistance(rdd, geometries, maxDistance)) | ||
} | ||
|
||
def costdistance( | ||
keyType: String, | ||
javaRdd: JavaRDD[Array[Byte]], | ||
schema: String, | ||
metadataStr: String, | ||
wkts: java.util.ArrayList[String], | ||
maxDistance: java.lang.Double, | ||
sc: SparkContext | ||
): (JavaRDD[Array[Byte]], String) = { | ||
val geometries = wkts.asScala.map({ wkt => WKT.read(wkt) }) | ||
|
||
keyType match { | ||
case "SpatialKey" => { | ||
val metadataAST = metadataStr.parseJson | ||
val metadata = metadataAST.convertTo[TileLayerMetadata[SpatialKey]] | ||
costdistance[SpatialKey](javaRdd, schema, metadata, geometries, maxDistance, sc) | ||
} | ||
case "SpaceTimeKey" => { | ||
val metadataAST = metadataStr.parseJson | ||
val metadata = metadataAST.convertTo[TileLayerMetadata[SpaceTimeKey]] | ||
costdistance[SpaceTimeKey](javaRdd, schema, metadata, geometries, maxDistance, sc) | ||
} | ||
} | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...-backend/geotrellis/src/main/scala/geopyspark/geotrellis/spark/stitch/StitchWrapper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package geopyspark.geotrellis.spark.stitch | ||
|
||
import geopyspark.geotrellis._ | ||
|
||
import geotrellis.raster._ | ||
import geotrellis.spark._ | ||
import geotrellis.spark.io._ | ||
import geotrellis.spark.io.avro._ | ||
import geotrellis.spark.stitch.TileLayoutStitcher | ||
|
||
import spray.json._ | ||
|
||
import org.apache.spark.api.java.JavaRDD | ||
import org.apache.spark.SparkContext | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
|
||
object StitchWrapper { | ||
|
||
def stitch( | ||
javaRdd: JavaRDD[Array[Byte]], | ||
schema: String, | ||
metadataStr: String | ||
): (Array[Byte], String) = { | ||
val metadataAST = metadataStr.parseJson | ||
val metadata = metadataAST.convertTo[TileLayerMetadata[SpatialKey]] | ||
val rdd = ContextRDD( | ||
PythonTranslator | ||
.fromPython[(SpatialKey, MultibandTile)](javaRdd, Some(schema)) | ||
.map({ case (k, v) => (k, v.band(0)) }), | ||
metadata | ||
) | ||
val tile: Tile = rdd.stitch.tile | ||
|
||
PythonTranslator.toPython[Tile](tile) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.