Hello all,

I am working on a project who's goal is to take in a series of WMS and
annotation layers, combine them into a single Map, and then turn that
Map into a single image, suitable for printing or sending it into
iText to make it a PDF.

The problem is that when the paint() method is called, the program
throws:

Apr 2, 2010 11:43:34 AM org.geotools.renderer.lite.StreamingRenderer
paint
SEVERE: null
java.lang.NullPointerException
       at
org.geotools.renderer.lite.StreamingRenderer.queryLayer(StreamingRendere
r.java:777)
       at
org.geotools.renderer.lite.StreamingRenderer.processStylers(StreamingRen
derer.java:1782)
       at
org.geotools.renderer.lite.StreamingRenderer.paint(StreamingRenderer.jav
a:699)
       at
org.geotools.renderer.lite.StreamingRenderer.paint(StreamingRenderer.jav
a:524)
       at org.geotools.demo.App.renderMap(App.java:159)

renderMap() being the method that creates the renderer and sets
everything up to create the instance of BufferedImage to be sent where
ever.

I noticed that Tawan Banchuen had a similar problem he asked about at
http://comments.gmane.org/gmane.comp.gis.geotools2.user/8562
I have tried asking in that thread if there has been any work done to
fix it, or if someone has found an acceptable workaround, but my
message never posts.

The relevant code for the project is:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Set;

import org.geotools.data.ows.Layer;
import org.geotools.data.wms.WMSUtils;
import org.geotools.data.wms.WebMapServer;
import org.geotools.factory.Hints;
import org.geotools.map.GraphicEnhancedMapContext;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.map.WMSMapLayer;
import org.geotools.ows.ServiceException;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 *
 * What is needed is to take in a WMS feed, add layers, including a
data layer that can be written to, and
 * then output an image.  For the testing, need to make a class that
takes an image and writes it to a file
 * so I can see if it works properly.
 */
public class hi
{
       private static WebMapServer wms;
       private static CoordinateReferenceSystem crs =
DefaultGeographicCRS.WGS84;

       /**
        * This function's job is to take in an array of boolean values
indicating which layers of the map are
        * active, gather those layers into a Map object and return the
Map
object as a single Image.<br/><br/>
        * The first object in the array represents the base layer, the
next
two represent all the
        * different layers on  the base layer, the two after that are
the
annotation layers and the final layer
        * is the map Legend.  All of the layers except the annotation
layers
are WMS feeds, while the
        * annotation layers contain points describing what features are
drawn.<br/><br/>
        * @param activeLayers which layers are active on this map
        * @return the Image of the map
        */
       public static Image getImage() {
               MapLayer[] layersArray = new MapLayer[6];
               // The current input is {true, false, false, false,
false, false}

               layersArray[0] = getWMSLayer(
 
"http://dev.dnr.alaska.gov/geoserver/wms";,
                               "dnr:DEC_SPILL", "4326", true, 0, 0, 140,
140);

               MapContext map = new GraphicEnhancedMapContext(crs);
               map.setTitle("The map");
               if (layersArray[0] != null)
               {
                       map.addLayer(layersArray[0]);
               }

 
Hints.putSystemDefault(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER,
                               Boolean.TRUE);

               return renderMap(map);
       }

       private static Image renderMap(MapContext map) {
               //Need to figure out what to do next as far as making a
MapContext map.
               GTRenderer renderer = new StreamingRenderer();
               renderer.setContext(map);
               Rectangle imageBounds = new Rectangle(200,200);
               BufferedImage buffer = new
BufferedImage(imageBounds.height,
imageBounds.width,
                               BufferedImage.TYPE_INT_RGB);
               Graphics2D picture = (Graphics2D)
buffer.createGraphics();
               picture.setPaint(Color.WHITE);
               picture.fill(imageBounds);
               renderer.paint(picture,
imageBounds,map.getAreaOfInterest());
               return buffer;
       }

       /**
        * This method is pulling a layer from
        * @param masterLayerData
        */
       private static MapLayer getWMSLayer(String dataUrl, String
dataLayer,
String dataEPSG,
                       boolean dataTransparent, int maxX, int maxY, int
minX, int minY)
       {
               URL url = null;
               try {
                       url = new URL(dataUrl);
               } catch (MalformedURLException e2) {
                       // TODO Auto-generated catch block
                       e2.printStackTrace();
               }

               try {
                       wms = new WebMapServer(url);

                       Set layersUnprocessed =
WMSUtils.getQueryableLayers(
                               wms.getCapabilities());
                       if(layersUnprocessed.size() == 0)
                               return null;
                       Object[] layersSemiprocessed =
layersUnprocessed.toArray();
               ArrayList<Layer> layers = new ArrayList<Layer>();
               for(int i = 0;i<layersSemiprocessed.length;i++)
               {
                       layers.add((Layer) layersSemiprocessed[i]);
               }
               if(layers == null)//layers.size() == 0)
                       return null;
               int layer = 0;
               for(int i = 0;i < layers.size();i++)
               {
 
if(layers.get(i).getName().equalsIgnoreCase(dataLayer))
                       {
                               layer = i;
                       }
               }

               //layer.
               WMSMapLayer finishedLayer = new WMSMapLayer(wms, (Layer)
layers.get(layer));

                       if(dataTransparent)
                       {
 
finishedLayer.setTransparent(dataTransparent);
                       }
               return finishedLayer;
               } catch (ServiceException e1) {
                       // TODO Auto-generated catch block
                       e1.printStackTrace();
               } catch (IOException e1) {
                       // TODO Auto-generated catch block
                       e1.printStackTrace();
               }
               return null;
   }
}

Any help is appreciated,

Jeremiah Lewis

_____________________________________
Jeremiah Lewis

Programmer Intern, GIS Programming Unit

Dept. of Natural Resources, IRM Section
550 West 7th Ave., Suite 706
Anchorage, AK 99501
(907)269-8863
E-mail:[email protected]


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to