I have a JavaEE appliation to serve tiles generated dynamically by geotools
using GTRenderer, for example, for a given request:
*http://localhost/tile/z/x/y.png?layers=road,river
<http://localhost/tile/z/x/y.png?layers=road,river>*
I will calculate the boundingbox of the request area and call GTRenderer to
render the image, here are the two main class:
ImageProvider(generate the images):
*public class ImageProvider {*
* private Map<String, Style> styles = new HashMap<String, Style>();*
* private String sld;*
* private GTRenderer renderer = new StreamingRenderer();*
* //style*
* StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();*
* CoordinateReferenceSystem crs;*
* private void init() {*
* System.setProperty("org.geotools.referencing.forceXY", "true");*
* try {*
* SLDParser stylereader = new SLDParser(styleFactory,
ImageProvider.class.getResource(this.sld));*
* Style[] ss = stylereader.readXML();*
* for (Style s : ss) {*
* styles.put(s.getName(), s);*
* }*
* crs = CRS.decode("EPSG:4326");*
* } catch (Exception e) {*
* throw new RuntimeException(e.getMessage());*
* } *
* }*
* @Override*
* public ArrayList<BufferedImage> render(Coord coordinate, int width, int
height, MapLayer... layers) {*
* double[] box = Util.getBoundingBox(coordinate.x, coordinate.y,
coordinate.zoom);*
* ReferencedEnvelope bbox = new ReferencedEnvelope(box[0], box[2], box[1],
box[3], crs);*
* Rectangle imageSize = new Rectangle(0, 0, width, height);*
* ArrayList<BufferedImage> result = new ArrayList<BufferedImage>();*
* for (MapLayer layer : layers) {*
* BufferedImage cachedTileData = null;*
* //render for current layer*
* BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);*
* Graphics2D g = image.createGraphics();*
* //set transparent*
* g.setComposite(AlphaComposite.Clear);*
* g.fillRect(0, 0, width, height);*
* g.setComposite(AlphaComposite.Src);*
* MapContent mapContent = new MapContent();*
* try {*
* String schemaName = layer.dataStore.getSchema().getName().toString(); //*
* Style style = styles.get(schemaName);*
* mapContent.addLayer(new FeatureLayer(layer.dataStore.getFeatureSource(),
style));*
* renderer.setMapContent(mapContent);*
* renderer.paint(g, imageSize, bbox);*
* } catch (IOException e) {*
* continue;*
* } finally {*
* mapContent.dispose();*
* g.dispose();*
* }*
* result.add(image);*
* }*
* return result;*
* }*
*}*
And The servlet:
*public class ImageServlet extends HttpServlet {*
* private ImageProvider imageProvider;*
* @Override*
* public void init() throws ServletException {*
* super.init();*
* //image provider initialization codes omitted*
* }*
* @Override*
* protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {*
* Coord coord=parseCoord(req);*
* String format=parseFormat(req);*
* String layers = req.getParameter("layer");*
* ArrayList<BufferedImage> tileResponse = imageProvider.render(coord, 256,
256, format, layers.split(","));*
* if (tileResponse != null) {*
* //save images to response*
* }*
* }*
* @Override*
* protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {*
* doGet(req, resp);*
* }*
*}*
You may find that I made the `GTRenderer` as a field in the class
`ImageProvider`, since I found that it will cost almost 500+ milliseconds
to create a new instanc of `StreamingRenderer`.
However I found the `StreamingRenderer` is not thread safe:
http://docs.geotools.org/stable/javadocs/org/geotools/renderer/lite/StreamingRenderer.html
It seems that I have to synchronize the `StreamingRenderer` field, but if
so I am afraid the performance.
So I wonder if there is any good idea to make an accept performance ?
BTW, I am new in geotools, so if you found any un-proper usage of geotools,
please let me know. :)
------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users