1

I am trying to convert feature collection GeoJSON into image. I want to use geotools to convert. I tested the soultion from this post here. But I am really noob that I ran into some errors like this:

Exception in thread "main" java.lang.IllegalArgumentException: Width (1000) and height (0) cannot be <= 0.

When I print the features.getBounds() it produces this result:

features ReferencedEnvelope[0.0 : -1.0, 0.0 : -1.0] Why is features.getBounds is not producing the correct result instead of 0? What should be the correct result?

This is the complete code:

    URL states = new URL("http://geojson.xyz/naturalearth-3.3.0/ne_110m_admin_1_states_provinces.geojson");
    FeatureJSON featureJSON = new FeatureJSON();
    FeatureCollection features = featureJSON.readFeatureCollection(states.openStream());
    MapContent mapContent = new MapContent();
    mapContent.setTitle("Quickstart");
    File outputFile = new File("states.png");
    ImageOutputStream outputImageFile = null;
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream = new FileOutputStream(outputFile);
      outputImageFile = ImageIO.createImageOutputStream(fileOutputStream);
      int w = 1000;
      ReferencedEnvelope bounds = features.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();
    } finally {
      try {
        if (outputImageFile != null) {
          outputImageFile.flush();
          outputImageFile.close();
          fileOutputStream.flush();
          fileOutputStream.close();
        }
      } catch (IOException e) {
          log.error("cannot produce image");
      }
    }
  }

Where am I getting this wrong? I just need to convert to get image. I would be happy if there is any other solution too. I need to use Java library so geotools is the best I found.

1 Answer 1

1

If you would be able to use external tools, namely gdal, this could be done with

gdal_rasterize -burn 255 -burn 255 -burn 0 -ot Byte -of bmp -ts 1000 1000 my.geojson output.bmp

This would display everything in yellow on a black background.

...and then converting the bmp to png with your favorite tool, I would use imagemagick.

1
  • Hey @til_b Thank you for the answer. But i would like to clarify my needs.I have a REST API that response feature collection and front-end consume that API to load the features on google map. Somehow i would need to change feature collection response to image response. I cannot use external library . So as a first step i want to change Geojson file to image to response in REST API. I have little knowledge with how to do with images.
    – hotaru
    Commented Jan 8, 2020 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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