Skip to content

Instantly share code, notes, and snippets.

@geoHeil
Created April 24, 2017 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoHeil/dbef714e2254956840832ebaabf12a07 to your computer and use it in GitHub Desktop.
Save geoHeil/dbef714e2254956840832ebaabf12a07 to your computer and use it in GitHub Desktop.
scala methods for visualization
import java.awt.Color
import com.vividsolutions.jts.geom.Polygon
import org.apache.spark.api.java.JavaPairRDD
import org.datasyslab.babylon.core.OverlayOperator
import org.datasyslab.babylon.extension.imageGenerator.NativeJavaImageGenerator
import org.datasyslab.babylon.extension.visualizationEffect.{ ChoroplethMap, HeatMap, ScatterPlot }
import org.datasyslab.babylon.utils.ImageType
import org.datasyslab.geospark.spatialRDD.{ PolygonRDD, SpatialRDD }
/**
* Visualization utility functions
*/
object Visualization {
@transient lazy val imageGenerator = new NativeJavaImageGenerator()
/**
* Builds the scatter plot of a geometry. Based on
* https://github.com/DataSystemsLab/GeoSpark/blob/master/src/main/java/org/datasyslab/babylon/showcase/Example.java
*
* @param outputPath the output path
* @return true, if successful
*/
def buildScatterPlot(outputPath: String, spatialRDD: SpatialRDD): Boolean = {
val envelope = spatialRDD.boundaryEnvelope
val s = spatialRDD.getRawSpatialRDD.rdd.sparkContext
val visualizationOperator = new ScatterPlot(7000, 4900, envelope, false, -1, -1, false, true)
visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true)
visualizationOperator.Visualize(s, spatialRDD)
import org.datasyslab.babylon.utils.ImageType
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath, ImageType.PNG)
}
/**
* Builds the heat map.
*
* @param outputPath the output path
* @return true, if successful
*/
def buildHeatMap(outputPath: String, spatialRDD: SpatialRDD): Boolean = {
val s = spatialRDD.getRawSpatialRDD.rdd.sparkContext
val visualizationOperator = new HeatMap(7000, 4900, spatialRDD.boundaryEnvelope, false, 2, -1, -1, false, false)
visualizationOperator.Visualize(s, spatialRDD)
import org.datasyslab.babylon.utils.ImageType
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath, ImageType.PNG)
}
/**
* Builds the choropleth map.
*
* @param outputPath the output path
* @return true, if successful
*/
def buildChoroplethMap(outputPath: String, joinResult: JavaPairRDD[Polygon, java.lang.Long], objectRDD: PolygonRDD): Boolean = {
val s = joinResult.rdd.sparkContext
val visualizationOperator = new ChoroplethMap(7000, 4900, objectRDD.boundaryEnvelope, false, -1, -1, false)
visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.RED, true)
visualizationOperator.Visualize(s, joinResult)
val frontImage = new ScatterPlot(7000, 4900, objectRDD.boundaryEnvelope, false, -1, -1, false)
frontImage.CustomizeColor(0, 0, 0, 255, Color.GREEN, true)
frontImage.Visualize(s, objectRDD) // TODO check if left vs. right object vs query is not mixed up
val overlayOperator = new OverlayOperator(visualizationOperator.rasterImage, false)
overlayOperator.JoinImage(frontImage.rasterImage)
import org.datasyslab.babylon.utils.ImageType
imageGenerator.SaveAsFile(overlayOperator.backRasterImage, outputPath, ImageType.PNG)
}
/**
* Parallel filter render no stitch.
*
* @param outputPath the output path
* @return true, if successful
*/
def parallelFilterRenderNoStitch(outputPath: String, spatialRDD: SpatialRDD): Boolean = {
val s = spatialRDD.getRawSpatialRDD.rdd.sparkContext
val visualizationOperator = new HeatMap(7000, 4900, spatialRDD.boundaryEnvelope, false, 2, -1, -1, false, false)
visualizationOperator.Visualize(s, spatialRDD)
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath, ImageType.PNG)
}
/**
* Parallel filter render stitch.
*
* @param outputPath the output path
* @return true, if successful
*/
def parallelFilterRenderStitch(outputPath: String, spatialRDD: SpatialRDD): Boolean = {
val s = spatialRDD.getRawSpatialRDD.rdd.sparkContext
val visualizationOperator = new HeatMap(7000, 4900, spatialRDD.boundaryEnvelope, false, 2, 4, 4, false, false)
visualizationOperator.Visualize(s, spatialRDD)
visualizationOperator.stitchImagePartitions
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath, ImageType.PNG)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment