Search in sources :

Example 1 with Tile

use of org.activityinfo.shared.util.mapping.Tile in project activityinfo by bedatadriven.

the class AdminTileRenderer method render.

public byte[] render(int zoom, int x, int y) throws IOException {
    Extents extents = TileMath.tileBounds(zoom, x, y);
    mapPixelBounds = new Envelope(0, TileMath.TILE_SIZE, 0, TileMath.TILE_SIZE);
    Envelope envelope = new Envelope(extents.getMinLon(), extents.getMaxLon(), extents.getMinLat(), extents.getMaxLat());
    Geometry filter = gf.toGeometry(envelope);
    LOGGER.info("Creating Buffered Image...");
    BufferedImage image = new BufferedImage(TileMath.TILE_SIZE, TileMath.TILE_SIZE, ColorSpace.TYPE_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setPaint(Color.WHITE);
    g2d.fillRect(0, 0, TileMath.TILE_SIZE, TileMath.TILE_SIZE);
    LOGGER.info("Querying geometry...");
    Criteria criteria = session.createCriteria(AdminEntity.class);
    criteria.add(SpatialRestrictions.filter("geometry", filter));
    criteria.add(Restrictions.eq("level", level));
    TiledMap map = new TiledMap(zoom, new Tile(x, y));
    g2d.setColor(Color.BLACK);
    g2d.setStroke(new BasicStroke(1));
    g2d.setFont(new Font("Arial", 0, 10));
    List<AdminEntity> entities = criteria.list();
    /*
         * Project Geometry onto this tile
         */
    LOGGER.info("Projecting geometry...");
    GeometryProjecter projector = new GeometryProjecter(map);
    Map<Integer, Geometry> projected = Maps.newHashMap();
    for (AdminEntity entity : entities) {
        LOGGER.info(entity.getName());
        Geometry transformed = projector.transform(entity.getGeometry());
        Geometry simplified = DouglasPeuckerSimplifier.simplify(transformed, 1.25);
        projected.put(entity.getId(), simplified);
    }
    /*
         * Draw outlines
         */
    LOGGER.info("Drawing geometry...");
    for (AdminEntity entity : entities) {
        Geometry geom = projected.get(entity.getId());
        g2d.draw(toPath(geom));
    }
    /*
         * Draw labels
         */
    LOGGER.info("Drawing geometry...");
    for (AdminEntity entity : entities) {
        Geometry geom = projected.get(entity.getId());
    // Polygon polygon = largestPolygon(geom);
    // 
    // if (polygon != null) {
    // labelPolygon(g2d, polygon, entity.getName());
    // }
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    return baos.toByteArray();
}
Also used : BasicStroke(com.google.code.appengine.awt.BasicStroke) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) Tile(org.activityinfo.shared.util.mapping.Tile) Criteria(org.hibernate.Criteria) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Extents(org.activityinfo.shared.util.mapping.Extents) Envelope(com.vividsolutions.jts.geom.Envelope) BufferedImage(com.google.code.appengine.awt.image.BufferedImage) Font(com.google.code.appengine.awt.Font) Graphics2D(com.google.code.appengine.awt.Graphics2D) Geometry(com.vividsolutions.jts.geom.Geometry) TiledMap(org.activityinfo.server.report.generator.map.TiledMap)

Aggregations

BasicStroke (com.google.code.appengine.awt.BasicStroke)1 Font (com.google.code.appengine.awt.Font)1 Graphics2D (com.google.code.appengine.awt.Graphics2D)1 BufferedImage (com.google.code.appengine.awt.image.BufferedImage)1 Envelope (com.vividsolutions.jts.geom.Envelope)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 TiledMap (org.activityinfo.server.report.generator.map.TiledMap)1 Extents (org.activityinfo.shared.util.mapping.Extents)1 Tile (org.activityinfo.shared.util.mapping.Tile)1 Criteria (org.hibernate.Criteria)1