Hi Andrea,
something happened to my inbox that purged your mail...
I hope the title is enough to keep the thread going.
Here is the code that should be testable. It loads some shapes and the
tiff. Right now I'm not sure if I'm allowed to attach the file, so I put
it inline. I'm a bit on a run.
Ciao and thanks,
Andrea
________________________________________________________________________
/**
* Sample application that may be used to try JMapPane from the command
line.
*
* @author Ian Turton
*/
public class TestTiffs {
JFrame frame;
JMapPane mp;
JToolBar jtb;
JLabel text;
MapContext map = null;
public TestTiffs() {
frame = new JFrame("Test");
frame.setBounds(20, 20, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
mp = new JMapPane();
// mp.addZoomChangeListener(this);
content.setLayout(new BorderLayout());
jtb = new JToolBar();
Action zoomIn = new ZoomInAction(mp);
Action zoomOut = new ZoomOutAction(mp);
Action pan = new PanAction(mp);
Action select = new SelectAction(mp);
Action reset = new ResetAction(mp);
jtb.add(zoomIn);
jtb.add(zoomOut);
jtb.add(pan);
jtb.addSeparator();
jtb.add(reset);
jtb.addSeparator();
jtb.add(select);
content.add(jtb, BorderLayout.NORTH);
CoordinateReferenceSystem cr = null;
try {
cr = CRS.decode("EPSG:3003");
map = new DefaultMapContext(cr);
} catch (NoSuchAuthorityCodeException e1) {
e1.printStackTrace();
} catch (FactoryException e1) {
e1.printStackTrace();
}
/*
* load raster
*/
// Envelope rasterEnvelop =
// loadRasterMap("./data/microsuap/059130.jpg");
// Envelope shapeEnvelop = loadImageMap("./data/km054086.tif");
loadGeoTiffMap("./data/indexed.tiff");
/*
* load shapes
*/
// loadShape("./data/shapefiles/comuni/comuni.shp");
// loadShape("./data/shapefiles/idrografia/fiumi.shp");
// loadShape("./data/shapefiles/idrografia/laghi.shp");
Envelope shapeEnvelop =
loadShape("./data/shapefiles/centri/ammins.shp");
// loadShape("./data/shapefiles/prese/derivazioni/der_attive.shp");
mp.setMapArea(shapeEnvelop);
GTRenderer renderer;
if (false) {
renderer = new StreamingRenderer();
HashMap hints = new HashMap();
hints.put("memoryPreloadingEnabled", Boolean.TRUE);
renderer.setRendererHints(hints);
} else {
renderer = new StreamingRenderer();
}
mp.setRenderer(renderer);
mp.setContext(map);
// mp.getRenderer().addLayer(new RenderedMapScale());
frame.repaint();
frame.doLayout();
// JComponent sp = mp.createScrollPane();
mp.setSize(780, 550);
content.add(mp, BorderLayout.CENTER);
content.doLayout();
frame.setVisible(true);
}
public Envelope loadGeoTiffMap(String geoTiffPath) {
File tiffFile = new File(geoTiffPath);
GeoTiffReader rdr = (GeoTiffReader) ((new GeoTiffFormat())
.getReader(tiffFile));
GridCoverage tiffCov = null;
try {
tiffCov = rdr.read(null);
} catch (Exception e) {
e.printStackTrace();
} // We do not use any parametery here.
// between 0 and 255.
StyleBuilder sb = new StyleBuilder();
RasterSymbolizer rsDem = sb.createRasterSymbolizer();
org.geotools.styling.Style demStyle = sb.createStyle(rsDem);
map.addLayer(tiffCov, demStyle);
org.opengis.spatialschema.geometry.Envelope env =
tiffCov.getEnvelope();
ReferencedEnvelope area = new ReferencedEnvelope(new Envelope(env
.getUpperCorner().getOrdinate(0), // X1
env.getLowerCorner().getOrdinate(0), // X2
env.getUpperCorner().getOrdinate(1), // Y1
env.getLowerCorner().getOrdinate(1)), // Y2
map.getCoordinateReferenceSystem());
return area;
}
private Envelope loadShape(String shapeFile) {
int index = shapeFile.lastIndexOf('.');
String fileNoExtention = shapeFile.substring(0, index + 1);
URL shapeUrl = aquireURL(fileNoExtention + "shp");
URL sldUrl = aquireURL(fileNoExtention + "sld");
ShapefileDataStore ds = null;
try {
ds = new ShapefileDataStore(shapeUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
FeatureSource fs = null;
try {
fs = ds.getFeatureSource();
} catch (IOException e) {
e.printStackTrace();
}
// com.vividsolutions.jts.geom.Envelope env = fs.getBounds();
// mp.setMapArea(env);
StyleFactory factory = StyleFactoryFinder.createStyleFactory();
SLDParser stylereader = null;
try {
stylereader = new SLDParser(factory, sldUrl);
} catch (IOException e) {
e.printStackTrace();
}
org.geotools.styling.Style[] style = stylereader.readXML();
map.addLayer(fs, style[0]);
/*
* set the renderer
*/
mp.setHighlightLayer(map.getLayer(0));
try {
return fs.getBounds();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private Envelope loadImageMap(String rasterPath) {
File tiffFile = new File(rasterPath);
WorldImageReader wiR = null;
// try {
// wiR = (WorldImageReader) ((new WorldImageFormat())
// .getReader(new BufferedInputStream(new FileInputStream(s
// tiffFile))));
// } catch (FileNotFoundException e1) {
// e1.printStackTrace();
// }
wiR = (WorldImageReader) ((new
WorldImageFormat()).getReader(tiffFile));
GridCoverage imgCov = null;
try {
imgCov = wiR.read(null);
} catch (Exception e) {
e.printStackTrace();
} // We do not use any parametery here.
// between 0 and 255.
StyleBuilder sb = new StyleBuilder();
ColorMap cM = sb.createColorMap(new String[] { "empty", "value" },
new double[] { 0, 1 }, new Color[] { Color.red,
Color.BLUE },
ColorMap.TYPE_VALUES);
RasterSymbolizer rsDem = sb.createRasterSymbolizer(cM, 1);
org.geotools.styling.Style demStyle = sb.createStyle(rsDem);
map.addLayer(imgCov, demStyle);
org.opengis.spatialschema.geometry.Envelope env =
imgCov.getEnvelope();
Envelope envelope = new
Envelope(env.getUpperCorner().getOrdinate(0), // X1
env.getLowerCorner().getOrdinate(0), // X2
env.getUpperCorner().getOrdinate(1), // Y1
env.getLowerCorner().getOrdinate(1)); // Y2
ReferencedEnvelope area = new ReferencedEnvelope(envelope, map
.getCoordinateReferenceSystem());
return area;
}
public static URL aquireURL(String target) {
if (new File(target).exists()) {
try {
return new File(target).toURL();
} catch (MalformedURLException e) {
}
}
try {
return new URL(target);
} catch (MalformedURLException e) {
return null;
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
TestTiffs mV = new TestTiffs();
}
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel