Create a collection of geometries in JTS

I'd like to create a program that a user inputs coordinates as points or lines and gets a geojson file. The problem is I don't know how to store all the geometries in one file. For example I want to create a point array, then a line array and a polygon array. Then all to be combined in one and written in a Json file. A sample code will be like that

import java.util.ArrayList;
import java.util.Scanner;
import com.vividsolutions.jts.geom.*;
public class new {
public static void main(String args[])
{

int b;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of points");
b = in.nextInt();
int arr[] = new int[b*2];

System.out.println("Enter "+b+" pairs of coordinates");
for(int i=0;i<arr.length;i++)
arr[i]=in.nextInt();
Coordinate[] coordinates = new Coordinate[arr.length/2];
int i = 0;
// Go through the arr and add each point as a Coordinate object to the coordinates array.

while (i < arr.length) {
// transform string arguments into double values
double x = arr[i];
double y = arr[i+1];
// create a new Coordinate object and add it to the coordinates array
Coordinate newCoord = new Coordinate(x,y);
coordinates[i/2] = newCoord;
Point point = new GeometryFactory().createPoint(newCoord);

GeometryCollection geome = new GeometryFactory().createGeometryCollection(null);
// Here is the problem. How to write every point in a geometry collection?

i=i+2;

//The same will apply for line and polygon.
//Then all combined geometries could be written in a Json
//  GeometryJSON g = new GeometryJSON();
try {
g.writeGeometryCollection(GeometryCollection, "final.json");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

You need a GeoJSONWriter and a GeometryCollection something like this should do what you want:

import java.util.ArrayList;

import com.ianturton.cookbook.utilities.GenerateRandomData;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.io.geojson.GeoJsonWriter;

public class WriteGeoJson {

public static void main(String[] args) {
// TODO Auto-generated method stub
Point p = GenerateRandomData.createRandomPoint();
LineString l = GenerateRandomData.createRandomLineString(5);
Polygon poly = GenerateRandomData.createRandomPolygon(10);
GeometryFactory gf = new GeometryFactory();
ArrayList<Geometry> geometries = new ArrayList();

GeometryCollection collection = gf.createGeometryCollection(geometries.toArray(new Geometry[] {}));
System.out.println(collection);

WriteGeoJson me = new WriteGeoJson();
me.write(collection);
}

public void write(GeometryCollection collection) {
GeoJsonWriter writer = new GeoJsonWriter(3);
String out = writer.write(collection);
System.out.println(out);
}

}

Which gives you output like:

GEOMETRYCOLLECTION (LINESTRING (18.852589910110368 -10.12538983836933, 15.105187008584947 -13.34232717578331, 10.849811890904542 -17.665388029545, 7.250208623499467 -20.945906807470045, 4.358396498462738 -22.2990846408219), POINT (-73.98196896551056 86.5018189815161), POLYGON ((115.31417770234287 7.910174112164427, 110.76385942569473 7.090925394906544, 109.79839975371749 11.529982169967141, 106.48725966779809 12.838589188621155, 110.10796176818802 16.703993914395255, 112.3479220508992 16.62148095350644, 114.13223292963157 17.169823141091058, 116.26399659348209 21.30982782663162, 116.901351472909 23.23014959853954, 117.48495491801991 24.609773904087366, 115.31417770234287 7.910174112164427)))
{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[18.853,-10.125],[15.105,-13.342],[10.85,-17.665],[7.25,-20.946],[4.358,-22.299]]},{"type":"Point","coordinates":[-73.982,86.502]},{"type":"Polygon","coordinates":[[[115.314,7.91],[110.764,7.091],[109.798,11.53],[106.487,12.839],[110.108,16.704],[112.348,16.621],[114.132,17.17],[116.264,21.31],[116.901,23.23],[117.485,24.61],[115.314,7.91]]]}],"crs":{"type":"name","properties":{"name":"EPSG:0"}}}
• Thanks. I used the part where I converted all geometries to a collection. The WriteGeoJson class seems not being recognised by java. So I used my code GeometryJSON g = new GeometryJSON(); try { g.writeGeometryCollection(GeometryCollection, "final.json"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } and this worked – GeoF May 10 '18 at 16:29