
// Overlays.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

/* Boolean shape operations:
      * union on Polygon and MultiPolygon
      * intersection
      * difference
      * symDifference
    -- these shapes are drawn in blue in GeomsViewer

   Shape buffering

   Usage:
    > compile Overlays.java
    > run Overlays

  When prompted, press <enter> on the command line to move 
  to the next example.
*/

import java.awt.Color;
import org.locationtech.jts.geom.*;


public class Overlays
{

  public static void main(String[] args)
  {
    GeomsViewer gv = Utils.view("Overlays");

    double[] coords1 = {
      150,60, 325,180, 90,150, 150,60 };
    Geometry poly1 = Utils.makeGeometry(coords1, Utils.POLY);
    System.out.println("Poly1: " + poly1);
    gv.add("poly1", poly1);

    double[] coords2 = {
      210,120, 350,120, 400,210, 
      250,300, 180,150, 210,120 };
    Geometry poly2 = Utils.makeGeometry(coords2, Utils.POLY);
    System.out.println("Poly2: " + poly2);
    gv.add("poly2", poly2);

    // --------------------------
    System.out.println("\n--- Union ---");
    gv.setTitle("Union");
    Geometry geom = poly2.union(poly1);
    System.out.println(Utils.simplify(geom));
    gv.add("Union", geom, Color.BLUE);
    Utils.waitEnter();
    gv.remove(geom);

    // --------------------------
    System.out.println("\n--- Multi-Union ---");
    gv.setTitle("Multi-Union");
    GeometryFactory gf = new GeometryFactory();
    MultiPolygon multiPoly = gf.createMultiPolygon(
                     new Polygon[] {(Polygon)poly1, (Polygon)poly2});
    geom = multiPoly.union();
    System.out.println(Utils.simplify(geom));
    gv.add("Multi-Union", geom, Color.BLUE);
    Utils.waitEnter();
    gv.remove(geom);

    // --------------------------
    System.out.println("\n--- Intersection ---");
    gv.setTitle("Intersection");
    geom = poly2.intersection(poly1);
    System.out.println(Utils.simplify(geom));
    gv.add("Intersection", geom, Color.BLUE);
    Utils.waitEnter();
    gv.remove(geom);

    // --------------------------
    System.out.println("\n--- Diff: poly2 - poly1 ---");
    gv.setTitle("Diff: poly2 - poly1");
    geom = poly2.difference(poly1);
    System.out.println(Utils.simplify(geom));
    gv.add("Diff", geom, Color.BLUE);
    Utils.waitEnter();
    gv.remove(geom);

    // --------------------------
    System.out.println("\n--- Sym Diff: poly2 - poly1 ---");
    gv.setTitle("Sym Diff: poly2 - poly1");
    geom = poly2.symDifference(poly1);
    System.out.println(Utils.simplify(geom));
    gv.add("Sym Diff", geom, Color.BLUE);
    Utils.waitEnter();
    gv.remove(geom);

    // --------------------------
    System.out.println("\n--- Buffering ---");
    gv.setTitle("Buffering");
    Geometry buffPoly1 = poly1.buffer(30);
    Geometry buffPoly2 = poly2.buffer(30);
       // variable-width buffers also possible
    System.out.println("\nBuff Poly1: " + Utils.simplify(buffPoly1));
    System.out.println("\nBuff Poly2: " + Utils.simplify(buffPoly2));
    gv.add("Buff Poly1", buffPoly1);
    gv.add("Buff Poly2", buffPoly2);
    Utils.waitEnter();

    gv.close();
  } /* main */


}  // end of Overlays class
