Skip to content

Instantly share code, notes, and snippets.

@jiayuasu
Last active April 24, 2017 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiayuasu/fa009ed27c0f363572d9a4282257b651 to your computer and use it in GitHub Desktop.
Save jiayuasu/fa009ed27c0f363572d9a4282257b651 to your computer and use it in GitHub Desktop.
/*---------------------------- Babylon 0.1.1 (or later) Scala API usage ----------------------------*/
/*
* If you are writing Babylon program in Spark Scala Shell, no need to declare the Spark Context by yourself.
* If you are writing a self-contained Babylon Scala program, please declare the Spark Context as follows and
* stop it at the end of the entire program.
*/
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.spark.api.java.JavaPairRDD;
import org.datasyslab.babylon.core.ImageGenerator;
import org.datasyslab.babylon.core.OverlayOperator;
import org.datasyslab.babylon.extension.imageGenerator.NativeJavaImageGenerator;
import org.datasyslab.babylon.extension.imageGenerator.SparkImageGenerator;
import org.datasyslab.babylon.extension.visualizationEffect.ChoroplethMap;
import org.datasyslab.babylon.extension.visualizationEffect.HeatMap;
import org.datasyslab.babylon.extension.visualizationEffect.ScatterPlot;
import org.datasyslab.babylon.utils.ImageType;
import org.datasyslab.geospark.enums.FileDataSplitter;
import org.datasyslab.geospark.enums.GridType;
import org.datasyslab.geospark.enums.IndexType;
import org.datasyslab.geospark.spatialOperator.JoinQuery;
import org.datasyslab.geospark.spatialRDD.PointRDD;
import org.datasyslab.geospark.spatialRDD.PolygonRDD;
import org.datasyslab.geospark.spatialRDD.RectangleRDD;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Polygon;
val conf = new SparkConf().setAppName("Simple Application").setMaster("spark://jias-mbp.mobile.asu.edu:7077");
val sc = new SparkContext(conf)
/*---------------------------- Basic Topics ----------------------------*/
/* Create raster and vector image. Store them into local single image. */
/*---------------------------- Start a Scatter Plot raster image example ----------------------------*/
val spatialRDD = new PolygonRDD(sc, PolygonInputLocation, PolygonSplitter, false, PolygonNumPartitions);
val visualizationOperator = new ScatterPlot(1000,600,spatialRDD.boundaryEnvelope,false,-1,-1,false,false);
/*
* 1000 is the resolution on X axis, 600 is the resolution on Y axis, spatialRDD.boundaryEnvelope
* is the spatial area you want to visualize. The first false means if you want to switch the X axis and Y axis.
* Set it as true only if you see the generated image is rotated by mistake. Set image paration on X and Y as -1,-1 because no need to do
* parallel photo filter and parallel image render. The second false means don't do parallel image rendering, The third false means don't
* generate vector image
*/
visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true);
/*
* Optional. You don't have to call this function. Set R,G,B channel color.
* True means each color channel value will be 255-ColorValue.
*/
visualizationOperator.Visualize(sc, spatialRDD);
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath, ImageType.PNG);
/*---------------------------- Stop a Scatter Plot raster image example ----------------------------*/
/*---------------------------- Start a Heat Map raster image example ----------------------------*/
val spatialRDD = new RectangleRDD(sc, RectangleInputLocation, RectangleSplitter, false, RectangleNumPartitions);
val visualizationOperator = new HeatMap(1000,600,spatialRDD.boundaryEnvelope,false,2);
/*
* 1000 is the resolution on X axis, 600 is the resolution on Y axis, spatialRDD.boundaryEnvelope
* is the spatial area you want to visualize. The first false means if you want to switch the X axis and Y axis.
* Set it as true only if you see the generated image is rotated by mistake. Set image partitions on X and Y as -1,-1 because no need to do
* parallel photo filter and parallel image render. The second false means don't do parallel image rendering, The third false means don't
* generate vector image 2 is the blur radis of Gaussian Blur. It defines the size of Gaussian Blur convolution matrix size. 2 means this
* matrix is 3 by 3.
*/
visualizationOperator.Visualize(sc, spatialRDD);
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath,ImageType.PNG);
/*---------------------------- Stop a Heat Map raster image example ----------------------------*/
/*---------------------------- Start a Choropleth Map raster image example ----------------------------*/
/* 1. Do a spatial join query */
val spatialRDD = new PointRDD(sc, PointInputLocation, PointOffset, PointSplitter, false, PointNumPartitions);
val queryRDD = new PolygonRDD(sc, PolygonInputLocation, PolygonSplitter, false, PolygonNumPartitions);
spatialRDD.spatialPartitioning(GridType.RTREE);
queryRDD.spatialPartitioning(spatialRDD.grids);
spatialRDD.buildIndex(IndexType.RTREE,true);
val joinResult = JoinQuery.SpatialJoinQueryCountByKey(spatialRDD,queryRDD,true,false);
/* 2. Generate a choropleth map as the background image */
val visualizationOperator = new ChoroplethMap(1000,600,USMainLandBoundary,false);
visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.RED, true);
visualizationOperator.Visualize(sc, joinResult);
/* 3. Generate a scatter plot as the front image */
val frontImage = new ScatterPlot(1000,600,USMainLandBoundary,false);
frontImage.CustomizeColor(0, 0, 0, 255, Color.GREEN, true);
frontImage.Visualize(sc, queryRDD);
/* 4. Overlay two images */
val overlayOperator = new OverlayOperator(visualizationOperator.rasterImage);
overlayOperator.JoinImage(frontImage.rasterImage);
/* 5. Generate image */
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(overlayOperator.backRasterImage, outputPath,ImageType.PNG);
/*---------------------------- Stop a Choropleth Map raster image example ----------------------------*/
/*---------------------------- Start a Scatter Plot Vector Image example ----------------------------*/
val visualizationOperator = new ScatterPlot(1000,600,USMainLandBoundary,false,-1,-1,false,true);
/*
* 1000 is the resolution on X axis, 600 is the resolution on Y axis, spatialRDD.boundaryEnvelope
* is the spatial area you want to visualize. The first false means if you want to switch the X axis and Y axis.
* Set it as true only if you see the generated image is rotated by mistake. Set image paration on X and Y as -1,-1 because no need to do
* parallel photo filter and parallel image render. The second false means don't do parallel image rendering, The third false means
* generate vector image
*/
val visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true);
/*
* Optional. You don't have to call this function. Set R,G,B channel color.
* True means each color channel value will be 255-ColorValue.
*/
visualizationOperator.Visualize(sc, spatialRDD);
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.vectorImage, outputPath,ImageType.SVG);
/*---------------------------- Stop a Scatter Plot Vector Image example ----------------------------*/
/*---------------------------- Advanced Topics ----------------------------*/
/*---------------------------- Start a Heat Map Parallel Filtering and Parallel Rendering example (in image tile format) ----------------------------*/
val spatialRDD = new RectangleRDD(sc, RectangleInputLocation, RectangleSplitter, false, RectangleNumPartitions);
val visualizationOperator = new HeatMap(1000,600,USMainLandBoundary,false,2,4,4,true,true);
/*
* 1000 is the resolution on X axis, 600 is the resolution on Y axis, spatialRDD.boundaryEnvelope
* is the spatial area you want to visualize. The first false means if you want to switch the X axis and Y axis.
* Set it as true only if you see the generated image is rotated by mistake. Set image partitions on X and Y as 2,2 to do
* parallel photo filter and parallel image render. The second false means don't do parallel image rendering, The third false means don't
* generate vector image 2 is the blur radis of Gaussian Blur. It defines the size of Gaussian Blur convolution matrix size. 2 means this
* matrix is 3 by 3.
*/
visualizationOperator.Visualize(sc, spatialRDD);
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.distributedRasterImage, outputPath,ImageType.PNG);
/*---------------------------- Stop a Heat Map Parallel Filtering and Parallel Rendering example (in image tile format)----------------------------*/
/*---------------------------- Start a Heat Map Parallel Filtering and Parallel Rendering example (in a stitched image format)----------------------------*/
val spatialRDD = new RectangleRDD(sc, RectangleInputLocation, RectangleSplitter, false, RectangleNumPartitions);
val visualizationOperator = new HeatMap(1000,600,USMainLandBoundary,false,2,4,4,true,true);
visualizationOperator.Visualize(sc, spatialRDD);
visualizationOperator.stitchImagePartitions();
val imageGenerator = new NativeJavaImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.rasterImage, outputPath,ImageType.PNG);
/*---------------------------- Stop a Heat Map Parallel Filtering and Parallel Rendering example (in a stitched image format)----------------------------*/
/*---------------------------- Start a Scatter plot store image RDD on remote storage example ----------------------------*/
val visualizationOperator = new ScatterPlot(1000,600,USMainLandBoundary,false,-1,-1,true,true);
val visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true);
val visualizationOperator.Visualize(sc, spatialRDD);
val imageGenerator = new SparkImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.distributedVectorImage, "file://"+outputPath,ImageType.SVG);
/*---------------------------- Stop a Scatter plot store image RDD on remote storage example ----------------------------*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment