0

I am trying to create tiles given a collection of features.

UPDATE

Let's say I got two tiles per zoom level 1 and my features are about a small area near Berlin. I expect one of the two tiles empty and the other one filled with all the features with proper dimension (I expect to see something like a point because features are about a small area). What I get is an empty tile (correct) and a image 256 x 256 representing the features like it was a thumbnail, not a tile.

This is what I did so far to render features in a tile:

...
MapContent content = new MapContent();
GTRenderer renderer = new StreamingRenderer();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
ReferencedEnvelope mapArea = featureCollection.getBounds();
Style shpStyle = SLD.createSimpleStyle(featureCollection.getSchema());
Layer layer = new FeatureLayer(featureCollection, shpStyle);
content.addLayer(layer);
renderer.setMapContent(content);
Rectangle tileArea = new Rectangle(0, 0, 256, 256);
renderer.paint(graphics, tileArea, mapArea);
File fileToSave = File.createTempFile("mytesttile.png");
ImageIO.write(image, "png", fileToSave);
...

but this code produces a small (256 x 256) image of the given collection, not a tile. I am in short of time and I can't find some good docs or a working example. Can you point me to the right direction? I am using geotools version 17.2.

0

Simply put you need to divide your map up into however many rows and columns you need and then draw a tile for each column/row pair. So in pseudo code:

for i in rows
  for j in cols
     bbox=calculateBBox(i,j);
     map.setExtent(bbox);
     renderTile(i,j,bbox);

You don't say what style of tile output you need, so aligning to a tile matrix or tileset and handling zoom levels etc is left as an exercise.

| improve this answer | |
  • Sorry I was pretty unclear, I update my question. Thanks. – Francesco Sep 18 '17 at 15:54
0

I was in the right path, just confusing rendering areas. mapArea must represent the area your tile want to cover about the entire map. I just change my code this way.

...
ReferencedEnvelope mapArea = new ReferencedEnvelope(tile.getEnvelope().getEastCoordinateMin(),
    tile.getEnvelope().getEastCoordinateMax(), tile.getEnvelope().getNorthCoordinateMin(),
    tile.getEnvelope().getNorthCoordinateMax(), org.geotools.referencing.CRS.decode("EPSG:4326"));
Style shpStyle = SLD.createSimpleStyle(featureCollection.getSchema());
Layer layer = new FeatureLayer(featureCollection, shpStyle);
content.addLayer(layer);
renderer.setMapContent(content);
Rectangle imageArea = new Rectangle(0, 0, width, height);
renderer.paint(graphics, imageArea, mapArea);
...

tileArea variable name changed in imageArea, it still implements tile's size.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.