OK. Now after much banging the head on the wall, I came to realize that my code would only work if I have a .pgw (world) file along with the .png file I get from the WMS server. Now can anyone tell me how do I make a .pgw file from geotools or even geoserver? I am completely stuck :(

On Sun, Nov 4, 2012 at 10:36 AM, Ratnadeep Raxsith <qedrix@gmail.com> wrote:
I am trying to overlay a shp file on wms layer using the example http://docs.geotools.org/latest/userguide/tutorial/raster/image.html.

Each time I want to read the format of the file to get a reader I get this error :

Exception in thread "main" java.lang.UnsupportedOperationException: Trying to get a reader from an unknown format.
at org.geotools.coverage.grid.io.UnknownFormat.getReader(UnknownFormat.java:62)
at com.qedrix.map.maplotr.Demo1.displayLayers(Demo1.java:127)
at com.qedrix.map.maplotr.Demo1.main(Demo1.java:260)

My code follows:

public void displayLayers() { File rasterFile = fetchWmsImage(); AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile); this.reader = format.getReader(rasterFile); // Initially display the raster in greyscale using the // data from the first image band Style rasterStyle = createRGBStyle(); // Create a basic style with yellow lines and no fill Style shpStyle = SLD.createPointStyle("point", Color.YELLOW, Color.GRAY, 0.0f, 1.5f); MapContent map = new MapContent(); map.setTitle("ImageLab"); MapViewport vp = new MapViewport(); org.geotools.map.Layer rasterLayer = new GridReaderLayer(reader, rasterStyle); map.addLayer(rasterLayer); saveImage(map, "final.jpeg", 583); }

public File fetchWmsImage() { URL url = null; try { url = new URL(""); } catch (MalformedURLException e) { // will not happen } WebMapServer wms = null; try { wms = new WebMapServer(url); WMSCapabilities capabilities = wms.getCapabilities(); Layer[] layers = WMSUtils.getNamedLayers(capabilities); GetMapRequest request = wms.createGetMapRequest(); request.setFormat("image/png"); request.setDimensions("583", "420"); request.setTransparent(true); request.setSRS("EPSG:900913"); request.setBBox("-13019428.542822,3922163.1648461,-13013051.407366,3929863.8567165"); //request.setProperty("isBaseLayer", "false"); //request.setProperty("opacity", ".2"); for (Layer layer : WMSUtils.getNamedLayers(capabilities)) { if (layer.getName().equals("rg:parcels")) request.addLayer(layer); } System.out.println(request.getFinalURL()); GetMapResponse response = (GetMapResponse) wms.issueRequest(request); BufferedImage image = ImageIO.read(response.getInputStream()); File rasterFile = new File("C:\\Users\\samabhik\\Workspace\\MAP\\data\\out.png"); ImageIO.write(image, "png", rasterFile); try{ Thread.sleep(2000); }catch(InterruptedException e){} return rasterFile; } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { } return null; }

I am using Maven so my dependency is this:

<dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geotiff</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-image</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-wms</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-coverage</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-imageio-ext-gdal</artifactId> <version>${geotools.version}</version> </dependency>

I have JAI/ImageIO/Gdal native libs installed and using a JDK 32bit version.
I am using geotools version 9-SNAPSHOT

I even tested the geoserver WMS URL and it works:,3922163.1648461,-13013051.407366,3929863.8567165&WIDTH=583&STYLES=&SRS=EPSG:900913&VERSION=1.1.1

Just ran gdalinfo.exe to read the image content and it follows:

Driver: PNG/Portable Network Graphics Files: ....\Workspace\MAP\data\out2.png Size is 583, 420 Coordinate System is `' Image Structure Metadata: INTERLEAVE=PIXEL Corner Coordinates: Upper Left ( 0.0, 0.0) Lower Left ( 0.0, 420.0) Upper Right ( 583.0, 0.0) Lower Right ( 583.0, 420.0) Center ( 291.5, 210.0) Band 1 Block=583x1 Type=Byte, ColorInterp=Red Mask Flags: PER_DATASET ALPHA Band 2 Block=583x1 Type=Byte, ColorInterp=Green Mask Flags: PER_DATASET ALPHA Band 3 Block=583x1 Type=Byte, ColorInterp=Blue Mask Flags: PER_DATASET ALPHA Band 4 Block=583x1 Type=Byte, ColorInterp=Alpha

Please help.