-1

I need to know how to export a .shp file to .png in Java using geotools.

I found an example here: Convert geojson to png. But I need convert shapefile to PNG.

  • What have you tried so far? – Bjorn Sep 11 at 12:15
5

To export any type of GeoTools feature type to an image the process is the same - you read in the features (or coverage) using a DataStore, you then render these features using a Style (often read from an SLD file) and then save the Java Image to a file using ImageIO.

So in your specific case you will need a ShapefileDatastore but there is no need to know that just use DataStoreFinder to look for a DataStoreFactory that can handle your requirements.

HashMap<String, Object> params = new HashMap<>();
params.put(ShapefileDataStoreFactory.URLP.key, URLs.fileToUrl(new File("/home/ian/Data/states/states.shp")));
DataStore ds = DataStoreFinder.getDataStore(params);
SimpleFeatureCollection fc = ds.getFeatureSource(ds.getTypeNames()[0]).getFeatures();

Now to render it:

MapContent mapContent = new MapContent();
mapContent.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(features.getSchema());
Layer layer = new FeatureLayer(features, style);
mapContent.addLayer(layer);

Then we need to call the Renderer on that mapContent to draw it to the Image:

File outputFile = new File("states.png");
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
    ImageOutputStream outputImageFile = ImageIO.createImageOutputStream(fileOutputStream);) {

  int w = 1000;
  ReferencedEnvelope bounds = fc.getBounds();
  int h = (int) (w * (bounds.getHeight() / bounds.getWidth()));
  BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2d = bufferedImage.createGraphics();

  mapContent.getViewport().setMatchingAspectRatio(true);

  mapContent.getViewport().setScreenArea(new Rectangle(Math.round(w), Math.round(h)));
  mapContent.getViewport().setBounds(bounds);

  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  Rectangle outputArea = new Rectangle(w, h);

  GTRenderer renderer = new StreamingRenderer();
  LabelCacheImpl labelCache = new LabelCacheImpl();
  Map<Object, Object> hints = renderer.getRendererHints();
  if (hints == null) {
    hints = new HashMap<>();
  }
  hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
  renderer.setRendererHints(hints);
  renderer.setMapContent(mapContent);
  renderer.paint(g2d, outputArea, bounds);
  ImageIO.write(bufferedImage, "png", outputImageFile);
} catch (IOException ex) {
  ex.printStackTrace();
}

Running this will produce an image like this:

enter image description here

If you want more colour you need to produce a Style object either by reading in an existing SLD file or by using StyleBuilder both of which are covered in the user manual.

| improve this answer | |
  • sorry for not having much idea about this but it generates the .png file fine but does not allow me to open it. Any solution for this? - Im using version 23.2 of geotools – Re1ease Sep 11 at 13:33
  • what are you using to try and open it? – Ian Turton Sep 11 at 16:39

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.