nl.cloudfarming.client.geoviewer.jxmap.map.cache.spi.AbstractTileService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of geoviewer-jxmap Show documentation
Show all versions of geoviewer-jxmap Show documentation
AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.
The newest version!
/**
* Copyright (C) 2008-2012 AgroSense Foundation.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*/
package nl.cloudfarming.client.geoviewer.jxmap.map.cache.spi;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import nl.cloudfarming.client.geoviewer.jxmap.map.cache.TileCacheInfo;
import nl.cloudfarming.client.geoviewer.jxmap.map.cache.TileService;
import nl.cloudfarming.client.util.FileUtil;
import org.openide.filesystems.FileObject;
/**
*
* @author Frantisek Post
*/
public abstract class AbstractTileService implements TileService {
private static final Logger LOG = Logger.getLogger(AbstractTileService.class.getName());
public BufferedImage readImage(TileCacheInfo tileCacheInfo) {
BufferedImage image = null;
FileObject tileFile = getTileFile(tileCacheInfo, false);
if (tileFile != null) {
try (InputStream stream = tileFile.getInputStream()) {
image = ImageIO.read(stream);
readCallback(tileFile);
} catch (IOException ex) {
LOG.log(Level.FINE, "Error reading tile image from file ", ex);
}
}
return image;
}
public void writeImage(TileCacheInfo tileCacheInfo, BufferedImage image) {
FileObject tileFile = getTileFile(tileCacheInfo, true);
if (tileFile != null) {
try (OutputStream stream = tileFile.getOutputStream()) {
ImageIO.write(image, "png", stream);
writeCallback();
} catch (IOException ex) {
LOG.log(Level.FINE, "Problem writing tile image to file", ex);
}
} else {
LOG.fine("Failed to write image for tile");
}
}
protected FileObject getTileFile(TileCacheInfo tileCacheInfo, boolean createMissing) {
FileObject zoomFolder = getZoomFolder(tileCacheInfo, createMissing);
FileObject tileFile = null;
if (zoomFolder != null) {
tileFile = zoomFolder.getFileObject(tileCacheInfo.getPath());
if (tileFile == null && createMissing) {
try {
tileFile = zoomFolder.createData(tileCacheInfo.getPath());
} catch (IOException ex) {
LOG.log(Level.FINE, "Error creating tile file " + tileCacheInfo.getPath(), ex);
}
}
}
return tileFile;
}
protected FileObject getZoomFolder(TileCacheInfo tileCacheInfo, boolean createMissing) {
FileObject zoomFolder = null;
String zoomFolderName = String.format("%d", tileCacheInfo.getZoom());
FileObject rootFolder = CacheManager.getInstance().getCacheFolder();
if (rootFolder != null) {
zoomFolder = rootFolder.getFileObject(zoomFolderName);
if (zoomFolder == null && createMissing) {
try {
zoomFolder = rootFolder.createFolder(zoomFolderName);
} catch (IOException ex) {
LOG.log(Level.FINE, "Error creating folder " + zoomFolderName, ex);
}
}
}
return zoomFolder;
}
protected void readCallback(FileObject tileFile) {
try {
FileUtil.touch(tileFile);
} catch (IOException ex) {
LOG.log(Level.FINE, "Error while touching file", ex);
}
}
protected void writeCallback() {
//noop
}
}