Masthead

Java and BlueSpray

Introduction

Java is one of the most successful compiled programming languages. Because Java programs are compiled to "machine code" or code the computer directly understands, Java programs can be faster than Python programs. For large processing tasks, it is best to use a compiled language. The problem is that compiled languages take more time to write.

The best of both worlds is to access libraries that contain code written in a compiled language and then "script" the overall processing in Python.

The package "py4j" (Python for Java) allows Python scripts to acess Java libraries and programs.

Py4J

If you search for Py4J on the web, you'll find the web site with examples and other information on the package. However, you can just install Py4J using "easy_install Py4J" from the command line (remember, easy_install is in "C:\PytonXX\Scripts").

BlueSpray

BlueSpray has been developed by Jim Graham as a platform for geospatial software development. BlueSpray is written in Java and includes libraries such as GDAL and OGR and a large number of transforms for spatial operations.

Go to the SchoonerTurtles.com web site to download BlueSpray. The BlueSpray User's Guide contains information on running BlueSpray and on "Scripting" BlueSpray from Python.

#############################################################
# Sample script to perform transforms on a raster image.
# Note that BlueSpray must be running for this script to work
# By: Jim Graham# Date: 3/23/2018
#############################################################
import sys  

# Path to where you stored BlueSpray
PathToBlueSpray="D:\\jimg\\Classes\\2018 Spring 318\\BlueSpray B22\\BlueSpray\\"

# Add a path so Python can find the BlueSprayPy bindings
sys.path.append(PathToBlueSpray+"STPython") 

# import the bindings to BlueSpray
from BlueSprayPy import *

#############################################################
# Perform basic raster operations
#############################################################

TheRaster=BlueSpray.Raster.STRaster() # Create a raster object to load the raster into

# Load the raster from a file
BlueSpray.File.STFileManager.LoadFromFile("C:/Temp/DEM.img",TheRaster) 

# Display information about the raster
print("Width In Pixels:"+format(TheRaster.GetWidthInPixels()))
print("Height In Pixels:"+format(TheRaster.GetHeightInPixels()))
print("Number of Bands:"+format(TheRaster.GetNumBands()))
print("Size in Bytes:"+format(TheRaster.GetSizeInBytes()))
print("Data Type:"+format(TheRaster.GetDataType()))

TheBounds=TheRaster.GetBounds();
print("The Bounds:"+format(TheBounds))

# Save the raster into a new file
BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Copy.tif",TheRaster)

#############################################################
# Perform some typical transforms
#############################################################

# Convert the raster to a floating point raster and save it to a new file
NewRaster=BlueSpray.RasterTransform.STDataType.ConvertToFloat(TheRaster)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Float.tif",NewRaster)

# Down sample the raster
NewRaster=BlueSpray.RasterTransform.STSample.Sample2(TheRaster,None,180,200,
                                                     BlueSpray.RasterTransform.STSample.METHOD.BILINEAR)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/DownSampled.tif",NewRaster)

#############################################################
# Perform some topographic transforms
#############################################################

# Create a slope raster
NewRaster=BlueSpray.RasterExtension.STTopography.Slope(TheRaster,1.0)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Slope.tif",NewRaster)

# Create an aspect raster
NewRaster=BlueSpray.RasterExtension.STTopography.Aspect(TheRaster,1.0)

# Create an absolute aspect raster (i.e. goes from 0 at north to 180 in the south in either direction)
BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Aspect.tif",NewRaster)

NewRaster=BlueSpray.RasterExtension.STTopography.AspectEastWest(TheRaster,1.0)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/AspectEastWest.tif",NewRaster)

# Create a grayscale hillshade
NewRaster=BlueSpray.RasterExtension.STTopography.Hillshade(TheRaster,45.0,225.0,1.0,None)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Hillshade_Gray.tif",NewRaster)

# Create a colorized hillshade (the ColorRamp has been exported from BlueSpray)
TheColorRamp=BlueSpray.RasterTransform.STColorRamp()
BlueSpray.File.STFileManager.LoadFromFile(PathToBlueSpray+"STSettings\ColorRamps\\BlueToRedSpectrum.stx",TheColorRamp) 

NewRaster=BlueSpray.RasterExtension.STTopography.Hillshade(TheRaster,45.0,180.0,1.0,TheColorRamp)

BlueSpray.File.STFileManager.SaveToFile("C:/Temp/Hillshade_Color.tif",NewRaster)

 

© Copyright 2018 HSU - All rights reserved.