[Geoserver-users] Moving from Geotools to Geoserver

Dear Sir/ Madam,

I have a Geotools developed Web application, especially I have a ControllerServlet.java [servlet]
which is responsible for converting the mapContext to Image and bring it on browser., the major part of the code in doing this as given below.,

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void renderMap() {
StreamingRenderer streamingRenderer = new StreamingRenderer();
MapHelper mapHelper = MapHelper.getMapHelper(session, getServletConfig().getServletContext());
MapContext mapContext = mapHelper.getMapContext ();
streamingRenderer.setContext(mapContext);
BufferedImage bufferedImage = null;
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
graphics2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));
graphics2D.setColor(Color.WHITE);
graphics2D.fillRect(0, 0, width, height);
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
streamingRenderer.setJava2DHints(hints);
Map rendererParams = new HashMap();
rendererParams.put(“optimizedDataLoadingEnabled”,new Boolean(true));

Envelope env = mapHelper.getEnvelope();
streamingRenderer.setRendererHints(rendererParams);
try
{
streamingRenderer.paint(graphics2D, new Rectangle(width, height), mapHelper.setAffineTransform());
} catch(Exception ex) {
ex.printStackTrace();
}
if (graphics2D!=null) graphics2D.dispose();
ServletOutputStream sos = res.getOutputStream();
ImageIO.write(bufferedImage,“jpg”,sos);
sos.close();
}// end rendermap
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This code is working good, but when two user try to click on the map at the same time, then ImageIO.write, being a static method, throws client abort exception.

In order to really make this more robust, I am think to move to GeoServer., but the question is we cannot invest all time to do so, from scratch.

Is there anyway where can I replace the above method inside my ControllerServlet, and get connected to the Geoserver [wms] request., and which will
give me a image back., then my application can work as it is.,

In the above code, my mapcontext object will get required layers and other operation from MapHelper. [I dont want to touch this class file].

Please let me know how to go about this., waiting for earliest reply.

Thanks
Gov

Govardhan Shetty ha scritto:

Dear Sir/ Madam,
    I have a Geotools developed Web application, especially I have a ControllerServlet.java [servlet]
which is responsible for converting the mapContext to Image and bring it on browser., the major part of the code in doing this as given below.,
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void renderMap() {
     StreamingRenderer streamingRenderer = new StreamingRenderer();
     MapHelper mapHelper = MapHelper.getMapHelper(session, getServletConfig().getServletContext());
     MapContext mapContext = mapHelper.getMapContext ();
     streamingRenderer.setContext(mapContext);
        BufferedImage bufferedImage = null;
        bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
        graphics2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0, 0, width, height); RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
        streamingRenderer.setJava2DHints(hints);
       Map rendererParams = new HashMap();
        rendererParams.put("optimizedDataLoadingEnabled",new Boolean(true));
        Envelope env = mapHelper.getEnvelope();
        streamingRenderer.setRendererHints(rendererParams);
        try
           {
streamingRenderer.paint(graphics2D, new Rectangle(width, height), mapHelper.setAffineTransform());
           } catch(Exception ex) {
                  ex.printStackTrace();
           }
           if (graphics2D!=null) graphics2D.dispose();
           ServletOutputStream sos = res.getOutputStream();
           ImageIO.write(bufferedImage,"jpg",sos);
           sos.close();
}// end rendermap
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Not sure I follow you. GeoServer has to go thru a similar process
to generate a map and we don't have the same problem. A method being
static does not mean it's not thread safe...

In order to really make this more robust, I am think to move to GeoServer., but the question is we cannot invest all time to do so, from scratch.
Is there anyway where can I replace the above method inside my ControllerServlet, and get connected to the Geoserver [wms] request., and which will
give me a image back., then my application can work as it is.,

Hum, not so easily. You need to register all of the layers in GeoServer,
and the either:
* issue a GetMap request (if your servlet is runnign out of GeoServer)
* create a WMSMapContext (which is a subclass of MapContext), locate
   the map producer you need and have it render the map.

Not sure it's easy or even possible since GeoServer classes are not
designed to be used as a library. Anyways, have a look at the PNGMapProducerFactory class, the PNGMapProducer and its superclasses,
and use WebApplicationContextUtils.getRequiredWebApplicationContext(myServletContext);
to access whatever bean has been registered in GeoServer's Spring application context (such as the WMS object).

Good luck
Cheers
Andrea

Dear Andrea,
Its good to know that, you also used ImageIO.write and it is working, can you please check is there anything I am doing worng in
this code, becuase to bring it from GeoServer, will take some time, in the mean time, I want to fix this error and give it to
users.

Will it be possible to share your piece of code, related to ImageIO.write ??

waiting for the reply.

Thanks
Gov

On Dec 18, 2007 1:56 PM, Andrea Aime <aaime@anonymised.com> wrote:

Govardhan Shetty ha scritto:

Dear Sir/ Madam,

I have a Geotools developed Web application, especially I have a
ControllerServlet.java [servlet]
which is responsible for converting the mapContext to Image and bring it
on browser., the major part of the code in doing this as given below.,

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void renderMap() {
StreamingRenderer streamingRenderer = new StreamingRenderer();
MapHelper mapHelper = MapHelper.getMapHelper(session,
getServletConfig().getServletContext());
MapContext mapContext = mapHelper.getMapContext ();
streamingRenderer.setContext(mapContext);
BufferedImage bufferedImage = null;
bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB );
Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
graphics2D.setRenderingHints(new
RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED ));
graphics2D.setColor(Color.WHITE);
graphics2D.fillRect(0, 0, width, height);
RenderingHints hints = new
RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
streamingRenderer.setJava2DHints(hints);
Map rendererParams = new HashMap();
rendererParams.put(“optimizedDataLoadingEnabled”,new Boolean(true));

Envelope env = mapHelper.getEnvelope();
streamingRenderer.setRendererHints(rendererParams);
try
{
streamingRenderer.paint(graphics2D, new Rectangle(width, height),
mapHelper.setAffineTransform ());
} catch(Exception ex) {
ex.printStackTrace();
}
if (graphics2D!=null) graphics2D.dispose();
ServletOutputStream sos = res.getOutputStream();
ImageIO.write(bufferedImage,“jpg”,sos);
sos.close();
}// end rendermap
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

This code is working good, but when two user try to click on the map at
the same time, then ImageIO.write, being a static method, throws client
abort exception.

Not sure I follow you. GeoServer has to go thru a similar process
to generate a map and we don’t have the same problem. A method being
static does not mean it’s not thread safe…

In order to really make this more robust, I am think to move to
GeoServer., but the question is we cannot invest all time to do so, from
scratch.

Is there anyway where can I replace the above method inside my
ControllerServlet, and get connected to the Geoserver [wms] request.,
and which will
give me a image back., then my application can work as it is.,

Hum, not so easily. You need to register all of the layers in GeoServer,
and the either:

  • issue a GetMap request (if your servlet is runnign out of GeoServer)
  • create a WMSMapContext (which is a subclass of MapContext), locate
    the map producer you need and have it render the map.

Not sure it’s easy or even possible since GeoServer classes are not
designed to be used as a library. Anyways, have a look at the
PNGMapProducerFactory class, the PNGMapProducer and its superclasses,
and use
WebApplicationContextUtils.getRequiredWebApplicationContext(myServletContext);
to access whatever bean has been registered in GeoServer’s Spring
application context (such as the WMS object).

Good luck
Cheers
Andrea