0

I would like to create a GeoJSON containing a MultiPoint from a list of WGS coordinates in my custom format (say MyLonLat). I wanted to use geotools library to simplify this, but I can't find a nice and easy way to do this. One can create a MultiPoint from Coordinate[] or Point[] or CoordinateSequence, but not regular Java Collection, like List.

I found that GeometryFactory has method toGeometryArray, which can convert my list into Geometry[] (like Point[]). The problem is, that Point is not as simple a Coordinate so converting MyLonLat to Point requires implementing methods that I don't need.

2
  • Why not just make MyLonLat extend Point? or Coordinate?
    – Ian Turton
    Commented Jan 21, 2022 at 17:51
  • It's a closed class
    – Piotr G
    Commented Jan 21, 2022 at 23:28

1 Answer 1

0

The obvious way to solve this is to make your MyLatLon class extend Point with something like this:

public class BuildMultiPoints {
  static GeometryFactory GF = new GeometryFactory();
  public static void main(String[] args) {
    BuildMultiPoints me = new BuildMultiPoints();
    ArrayList<MyLonLat> points = new ArrayList<>();
    MyLonLat myLonLat = me.new MyLonLat(90.0, 0.0);
    points.add(myLonLat);
    points.add(me.new MyLonLat(50.0, 50.0));
    
    MultiPoint mp = GF.createMultiPoint(points.toArray(new MyLonLat[] {}));
    System.out.println(mp);
  }

  public class MyLonLat extends Point {
    public MyLonLat(double lat, double lon) {
      super(CoordinateArraySequenceFactory.instance().create(new Coordinate[] { new Coordinate(lat, lon) }), GF);
    }

    public MyLonLat(CoordinateSequence coordinates, GeometryFactory factory) {
      super(coordinates, factory);

    }

  }
}

If you are unable to do that then the next best option would be to store a Point inside the MyLonLat and delegate to that. Then you can go through the list extracting the points to build your multipoint.

7
  • I am sorry, but all these answers are of little use from the same reason: MyLonLat is closed (it's a part of some API) which means, it can't be extended from Point (or anything else), or can't store Point. I am really surprised that authors of geotools made these classes so cumbersome to use. The fact that one can't initiate them from standard containers is a real shame.
    – Piotr G
    Commented Jan 22, 2022 at 20:17
  • It's hard to think of a reason why anyone would want to do what you are doing, in fact in 20+ years you are the first person who wants to, so feel free to add some code to do it.
    – Ian Turton
    Commented Jan 23, 2022 at 11:14
  • Is it really? Let me help you with that: you have a library that manipulates geodetic data in its own custom format and now you want to store it in a geojson for visualization using other tools. Is that so uncommon? Take any decent library there is - mapping data between various formats is always a typical usecase. It is hard for me to believe that in 20+ years you had no need for this. I had it in my first days of using geotools.
    – Piotr G
    Commented Jan 24, 2022 at 9:15
  • feel free to use any of those other libraries, I will send you a full refund of your GeoTools license fee.
    – Ian Turton
    Commented Jan 24, 2022 at 9:17
  • I was not talking about geodetic libaries, but ANY libraries. So yes, I am using them for other purposes, but they will not help me with this specific problem of storing my data as GeoJSON. I was hoping geotools will help, but I guess it was too much to ask.
    – Piotr G
    Commented Jan 24, 2022 at 9:32

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.