
// BuildGeoms.java
// Murat Öksüzer, April 2021
// https://www.smartycoder.com/2021/04/geometry-model-jts-tutorial-1.html
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* Create and display a variety of geometries:
     * Point, MultiPoint, LineString, MultiLineString, 
       Polygon, Polygon with a hole (LinearRing), MultiPolygon, 
       GeometryCollection

   Geometries are displayed using the GeomsViewer class.

   Usage:
    > compile BuildGeoms.java
    > run BuildGeoms

  When prompted, press <enter> on the command line to move 
  to the next example.
*/


import org.locationtech.jts.geom.*;


public class BuildGeoms
{

  public static void main(String[] args)
  {
    GeomsViewer gv = Utils.view("Geometries");
    GeometryFactory gf = new GeometryFactory();

    System.out.println("--- Points ---");
    gv.setTitle("Points");
    Geometry point = Utils.makePoint(175,50);
    gv.add(point);

    double[] coords = { 195,165, 150,115 };
    Geometry pts = Utils.makeGeometry(coords, Utils.PTS);
    gv.add(pts);
    Utils.waitEnter();

    System.out.println("\n--- Lines ---");
    gv.setTitle("Lines");
    Geometry line1 = Utils.makeGeometry(coords, Utils.LINE); 
           // reuse coordinates
    gv.add("line1", line1);

    coords = new double[]{ 130,190, 150,185, 200,200 };
    Geometry line2 = Utils.makeGeometry(coords, Utils.LINE);
    gv.add("line2", line2);
    Utils.waitEnter();

    System.out.println("\n--- Multi-lines ---");
    gv.setTitle("Multi-lines");
    gv.remove(line1);
    gv.remove(line2);
    LineString[] lines = new LineString[] 
                   { (LineString)line1, (LineString)line2 };
    MultiLineString mls = gf.createMultiLineString(lines);
    gv.add("mls", mls);
    Utils.waitEnter();

    System.out.println("\n--- Polys ---");
    gv.setTitle("Polys");
    coords = new double[]{ 100,100, 150,100, 150,150, 
                           100,150, 100,100 };
    Geometry sqr = Utils.makeGeometry(coords, Utils.POLY);
    gv.add(sqr);

    coords = new double[]{ 300,100, 400,100, 400,200, 
                           300,200, 300,100 };
    Geometry shell = Utils.makeGeometry(coords, Utils.RING);
    coords = new double[]{ 325,125, 375,125, 375,175, 
                           325,175, 325,125 };
    Geometry hole = Utils.makeGeometry(coords, Utils.RING);

    LinearRing[] holes = new LinearRing[] { (LinearRing)hole };
    Polygon sqrHole = gf.createPolygon((LinearRing)shell, holes);
    gv.add(sqrHole);
    Utils.waitEnter();

    System.out.println("\n--- Multi-Polys ---");
    gv.setTitle("Multi-Polys");
    gv.remove(sqr);
    gv.remove(sqrHole);
    Polygon[] polys = new Polygon[] {(Polygon)sqr, sqrHole};
    MultiPolygon mgon = gf.createMultiPolygon(polys);
    gv.add(mgon);
    Utils.waitEnter();

    System.out.println("\n--- Collection ---");
    gv.setTitle("Collection");
    Geometry[] geoms = new Geometry[] {pts, mls, mgon};
    GeometryCollection geoColl =
                   gf.createGeometryCollection(geoms);
    gv.add(geoColl);
    Utils.waitEnter();

    gv.close();
  }  // end of main()


}  // end of BuildGeom class