Skip to content

Commit

Permalink
changes in heatmapstyle branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mukoki committed May 18, 2022
1 parent 99a5147 commit 85046c9
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 199 deletions.
Expand Up @@ -210,6 +210,8 @@ public int getPriority() {
}

// return a priority for the given loader object
// TODO maybe we should use the following way to sort ImageReader
// https://stackoverflow.com/questions/38190507/how-to-exclude-specific-tiff-reader-from-imageio/38198991#38198991
public static int getPriority(Object loader) {
String name = loader.getClass().getName();

Expand All @@ -226,8 +228,10 @@ public static int getPriority(Object loader) {
return 20;
}
// prefer oss jai core implementation, currently only TIF
// priority 30 -> 50 : this library is no more update while
// geosolutions library is actively maintained (ex. bigtiff)
else if (name.startsWith("com.github.jaiimageio")){
return 30;
return 50;
}
// next are imageio-ext readers
else if (name.startsWith("it.geosolutions.imageio")){
Expand Down
125 changes: 73 additions & 52 deletions src/com/vividsolutions/jump/workbench/imagery/geoimg/GeoRaster.java
Expand Up @@ -31,6 +31,7 @@
* (250)385-6040
* www.vividsolutions.com
*/
import com.vividsolutions.jump.workbench.model.Prioritized;
import it.geosolutions.imageio.gdalframework.GDALImageReaderSpi;
import it.geosolutions.imageio.gdalframework.GDALUtilities;
import it.geosolutions.imageio.utilities.ImageIOUtilities;
Expand Down Expand Up @@ -79,12 +80,11 @@
import com.vividsolutions.jump.workbench.Logger;
import com.vividsolutions.jump.workbench.imagery.ReferencedImageException;
import com.vividsolutions.jump.workbench.model.Disposable;
import com.vividsolutions.jump.workbench.model.Prioritized;

public class GeoRaster implements Disposable {
protected String imageFileLocation;
private URI uri = null;
protected Object fixed_reader = null;
protected Object fixed_reader;
protected RenderedOp src = null;
private ImageReader src_reader = null;
private Object src_input = null;
Expand Down Expand Up @@ -154,7 +154,7 @@ protected void fetchRaster() throws ReferencedImageException {
File file = new File(imageFileLocation);
uri = file.toURI();
}
Logger.trace("uri is now -> "+uri.toString());
Logger.trace("uri is now -> " + uri);
// check availability early
if (!new File(uri).canRead())
throw new ReferencedImageException("cannot read file -> "+imageFileLocation);
Expand All @@ -166,13 +166,20 @@ protected void fetchRaster() throws ReferencedImageException {
pbjImageRead.setParameter("readParam", param);

// route, if fixed_reader was set
List<ImageReaderSpi> affirmed_readers;
List<Object> affirmed_readers;
fixed_reader = null;
// default case, auto detection
if (fixed_reader == null) {
affirmed_readers = new ArrayList(listValidImageIOReaders(uri, null));
affirmed_readers = new ArrayList<>(listValidImageIOReaders(uri, null));
//try {
// affirmed_readers = listValidReaders(uri);
//} catch(IOException e) {
// throw new ReferencedImageException(e);
//}
//affirmed_readers.sort(Comparator.comparingInt(it -> GeoImageFactory.getPriority(it)));
// sort readers by priority
Collections.sort(affirmed_readers, new Comparator<ImageReaderSpi>() {
public int compare(final ImageReaderSpi o1, final ImageReaderSpi o2) {
Collections.sort(affirmed_readers, new Comparator<Object>() {
public int compare(final Object o1, final Object o2) {
final Prioritized p1 = new Prioritized() {
public int getPriority() {
return GeoImageFactory.getPriority(o1);
Expand All @@ -183,58 +190,60 @@ public int getPriority() {
return GeoImageFactory.getPriority(o2);
}
};
// System.out.println(o1+"="+p1.getPriority()+"/"+o2+"="+p2.getPriority());
return Prioritized.COMPARATOR.compare(p1, p2);
}
});
Logger.info("Available sorted readers : " + affirmed_readers);
}
// fixed reader is imageio reader
else if (fixed_reader instanceof ImageReaderSpi)
else if (fixed_reader instanceof ImageReaderSpi) {
affirmed_readers = Collections.singletonList((ImageReaderSpi) fixed_reader);
Logger.info("Fixed reader : " + affirmed_readers);
}
else {
// fixed reader is something else, hopefully jai codec ;)
// simply define an empty imageio reader list here to skip to jai below
affirmed_readers = Collections.emptyList();
}

// this is skipped if list is empty
// TODO: not sure looping makes sense here as image is
// actually rendered much later
for (Iterator<ImageReaderSpi> i = affirmed_readers.listIterator(); i
.hasNext();) {

ImageReaderSpi readerSpi = ((ImageReaderSpi) i.next());

Logger.trace("Trying reader "+GeoImageFactory.loaderString(readerSpi));

try {
src_input = createInput(uri, readerSpi);

src_reader = readerSpi.createReaderInstance(/* src_input */);

src_reader.setInput(src_input);
pbjImageRead.setParameter("Input", src_input);
pbjImageRead.setParameter("Reader", src_reader);

src = JAI.create("ImageRead", pbjImageRead, null);

// success OR dispose & try plain JAI below
if (src != null && src.getWidth() > 0) {
// set info vars
type = src_reader.getFormatName();
used_loader = src_reader;
return;
}
else
Logger.info("Available readers : " + affirmed_readers);
for (Object reader : affirmed_readers) {

Logger.info("Trying reader " + GeoImageFactory.loaderString(reader));

if (reader instanceof ImageReaderSpi) {
ImageReaderSpi readerSpi = (ImageReaderSpi) reader;
try {
src_input = createInput(uri, reader);

src_reader = readerSpi.createReaderInstance(/* src_input */);

src_reader.setInput(src_input);
pbjImageRead.setParameter("Input", src_input);
pbjImageRead.setParameter("Reader", src_reader);

src = JAI.create("ImageRead", pbjImageRead, null);

// success OR dispose & try plain JAI below
if (src != null /*&& src.getWidth() > 0*/) {
// set info vars
type = src_reader.getFormatName();
used_loader = src_reader;
return;
} else {
dispose();
}
} catch (Exception e) {
// if fixed_reader failed, it failed finally and we'll have to throw the reason
if (fixed_reader != null && fixed_reader == reader)
throw new ReferencedImageException(e);
// ok, this didn't work try the next one
Logger.debug(e);
// clean up any residue
dispose();
} catch (Exception e) {
// if fixed_reader failed, it failed finally and we'll have to throw the reason
if (fixed_reader != null && fixed_reader == readerSpi)
throw new ReferencedImageException(e);
// ok, this didn't work try the next one
Logger.trace(e);
// clean up any residue
dispose();
}
}
}

Expand Down Expand Up @@ -323,7 +332,7 @@ protected Object getLoader() {

// TODO: probably better moved to GeoImage where the rendering is actually handled
public RenderingHints createCacheRenderingHints() {
if (src instanceof RenderedOp && src.getWidth() > 2000
if (src != null && src.getWidth() > 2000
&& src.getHeight() > 2000 && cache_hints == null) {
// use 64MB for images, default 16MB is kinda small
cache = JAI.createTileCache(1024 * 1024 * 64L);
Expand All @@ -344,7 +353,7 @@ protected void createJAIRenderedOP(URI uri, ImageCodec codec)
Object input = createInput(uri);
// create a temp stream to find all candidate codecs if codec was given
String[] decs;
if (codec instanceof ImageCodec) {
if (codec != null) {
SeekableStream is2 = SeekableStream.wrapInputStream(
createInputStream(uri), true);
decs = ImageCodec.getDecoderNames((SeekableStream) is2);
Expand Down Expand Up @@ -456,17 +465,22 @@ static protected List<ImageReaderSpi> listValidImageIOReaders(URI uri,
resetGDALReaderSelection();

// fetch all readers
// Don't know why this one is not automatically finded
// It is useful as it does not break on some wrong TiffTags like byte array nodata
// which breaks geosolutions imageio-ext reader
IIORegistry.getDefaultInstance().registerServiceProvider(
new com.github.jaiimageio.impl.plugins.tiff.TIFFImageReaderSpi(), ImageReaderSpi.class
);
final Iterator<? extends ImageReaderSpi> iter = IIORegistry
.getDefaultInstance().getServiceProviders(ImageReaderSpi.class, false);

//Logger.trace("IIORegistry holds "+Arrays.toString(Lists.newArrayList(iter).toArray()));

// iterate all readers and return only valid ones
ImageReaderSpi provider;
List<ImageReaderSpi> affirmed_readers = new Vector<ImageReaderSpi>();
List<ImageReaderSpi> affirmed_readers = new Vector<>();
while (iter.hasNext()) {
provider = iter.next();

//Logger.trace("test "+GeoImageFactory.loaderString(provider));

if (filter != null && !(filter.isInstance(provider)))
Expand Down Expand Up @@ -514,7 +528,8 @@ static protected List<ImageReaderSpi> listValidImageIOReaders(URI uri,
}

/**
* create a list of ImageCodec's supposedly able to open URI
* Create a list of ImageCodec's supposedly able to open URI.
* From com.sun.media.jai.codec.ImageCodec
*/
static protected List<ImageCodec> listValidJAICodecs(URI uri)
throws IOException {
Expand All @@ -535,7 +550,7 @@ static protected List<ImageCodec> listValidJAICodecs(URI uri)
* create a list of JAI ImageCodec's and ImageIO readers supposedly able to open URI
*/
static public List<Object> listValidReaders(URI uri) throws IOException {
List<Object> l = new ArrayList(listValidImageIOReaders(uri, null));
List<Object> l = new ArrayList<>(listValidImageIOReaders(uri, null));
l.addAll(listValidJAICodecs(uri));
return l;
}
Expand All @@ -544,7 +559,7 @@ static public List<Object> listValidReaders(URI uri) throws IOException {
* list all JAI ImageCodec's and ImageIO readers available in this jre
*/
static public List<Object> listAllReaders() {
List<Object> loaders = new ArrayList();
List<Object> loaders = new ArrayList<>();
// add imageio readers
Iterator<? extends ImageReaderSpi> iter = IIORegistry
.getDefaultInstance().getServiceProviders(ImageReaderSpi.class, true);
Expand All @@ -570,6 +585,12 @@ static protected Object createInput(URI uri) throws IOException {
return createInput(uri, null);
}

/**
* @param uri the URI to read
* @param loader optional ImageReaderSpi
* @return the input as an InputStream, a File or a ImageInputStream
* @throws IOException
*/
static protected Object createInput(URI uri, Object loader)
throws IOException {

Expand Down Expand Up @@ -598,7 +619,7 @@ static protected Object createInput(URI uri, Object loader)
// how may i serve you today?
Class[] clazzes = ((ImageReaderSpi) loader).getInputTypes();
List<Class> intypes = clazzes != null ? Arrays.asList(clazzes)
: new ArrayList();
: new ArrayList<>();
//System.out.println("GR in types: " + intypes);
for (Class clazz : intypes) {
// already reader compliant? off you f***
Expand Down
14 changes: 8 additions & 6 deletions src/com/vividsolutions/jump/workbench/model/LayerManager.java
Expand Up @@ -74,7 +74,7 @@ public class LayerManager {
// is lucky enough to have all its strong references released, let it
// dispose of
// itself immediately [Jon Aquino]
private final List<WeakReference<Layerable>> layerReferencesToDispose = new ArrayList<>();
private final List<WeakReference<Disposable>> layerReferencesToDispose = new ArrayList<>();

private final List<LayerListener> layerListeners = new ArrayList<>();
private final Iterator<Color> firstColors;
Expand Down Expand Up @@ -181,7 +181,9 @@ public void addLayerable(String categoryName, Layerable layerable) {
// else {
// reproject((Layer) layerable, coordinateSystem);
// }
layerReferencesToDispose.add(new WeakReference<>(layerable));
if (layerable instanceof Disposable) {
layerReferencesToDispose.add(new WeakReference<Disposable>((Disposable)layerable));
}
}
addCategory(categoryName);

Expand Down Expand Up @@ -395,11 +397,11 @@ public void removeIfEmpty(Category category) {

public void dispose() {
this.setFiringEvents(false);
for (WeakReference<Layerable> reference : layerReferencesToDispose) {
Layer layer = (Layer) reference.get();
for (WeakReference<Disposable> reference : layerReferencesToDispose) {
Disposable disposableLayer = reference.get();

if (layer != null) {
layer.dispose();
if (disposableLayer != null) {
disposableLayer.dispose();
}
}

Expand Down
38 changes: 4 additions & 34 deletions src/com/vividsolutions/jump/workbench/model/LayerTreeModel.java
Expand Up @@ -42,9 +42,7 @@
import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle;
import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingStyle;
import org.openjump.core.rasterimage.RasterColorMapSymbology;
import org.openjump.core.rasterimage.RasterHeatmapSymbology;
import org.openjump.core.rasterimage.RasterImageLayer;
import org.openjump.core.rasterimage.RasterSymbology;

/**
* JTree model for displaying the Layers, WMSLayers, and other Layerables
Expand Down Expand Up @@ -100,8 +98,6 @@ public static class RasterStyleValueIntv implements SymbolizationLeafNode {
private final Double nextValue;
private final Double value;
private final String label;
//private int width;
//private int height;

RasterStyleValueIntv(
String colorMapType,
Expand Down Expand Up @@ -136,14 +132,6 @@ public String getLabel() {
return label;
}

//public int getWidth() {
// return width;
//}

//public int getHeight() {
// return height;
//}

@Override
public String toString() {
return label;
Expand All @@ -170,32 +158,14 @@ public static class RasterStyleValueRamp implements SymbolizationLeafNode {

private final Double topValue;
private final Double bottomValue;
private final Color[] colors;
//private int width;
//private int height;
private final Color[] colors;

RasterStyleValueRamp(Double topValue, Double bottomValue, Color[] colors) {
this.topValue = topValue;
this.bottomValue = bottomValue;
this.colors = colors;
}

//RasterStyleValueRamp(Double topValue, Double bottomValue, Color[] colors, int width, int height) {
// this.topValue = topValue;
// this.bottomValue = bottomValue;
// this.colors = colors;
// //this.width = width;
// //this.height = height;
//}

//public int getWidth() {
// return width;
//}

//public int getHeight() {
// return height;
//}

@Override
public String toString() {
return bottomValue + "-" + topValue;
Expand Down Expand Up @@ -283,7 +253,7 @@ else if (parent instanceof RasterImageLayer) {
RasterColorMapSymbology rasterSymbology = (RasterColorMapSymbology)rasterImageLayer.getSymbology();
double bottomValue = rasterImageLayer.getMetadata().getStats().getMin(0);
double topValue = rasterImageLayer.getMetadata().getStats().getMax(0);
Double[] keys = rasterSymbology.getColorMapEntries_tm().keySet().toArray(new Double[0]);
Double[] keys = rasterSymbology.getColorTreeMap().keySet().toArray(new Double[0]);

if(!rasterImageLayer.getSymbology().getType().equals(RasterColorMapSymbology.TYPE_RAMP)) {

Expand All @@ -296,7 +266,7 @@ else if (parent instanceof RasterImageLayer) {

double nextValue = (i == keys.length-1)? topValue : keys[i+1];

Color color = rasterSymbology.getColorMapEntries_tm().get(key);
Color color = rasterSymbology.getColorTreeMap().get(key);

styleValues_l.add(new RasterStyleValueIntv(
rasterSymbology.getType(),
Expand All @@ -318,7 +288,7 @@ else if (parent instanceof RasterImageLayer) {
for(int i=keys.length-1; i>=0; i--) {
Double key = keys[i];
if(!rasterImageLayer.isNoData(key)) {
Color color = rasterSymbology.getColorMapEntries_tm().get(key);
Color color = rasterSymbology.getColorTreeMap().get(key);
colors_l.add(color);
}
}
Expand Down

0 comments on commit 85046c9

Please sign in to comment.