
// Dissolver.java
// Martin Davis
// The LocationTech Technology (LTT) Group; Vivid Solutions
// https://github.com/locationtech/jts
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* Illustrates how to use the LineDissolver class using three
   different examples. When prompted, press <enter> on the 
   command line to move to the next example.

   Usage:
    > compile Dissolver.java
    > run Dissolver

*/

import java.util.*;

import org.locationtech.jts.geom.*;
import org.locationtech.jts.dissolve.LineDissolver;



public class Dissolver
{
  private static GeomsViewer gv;


  public static void main(String[] args)
  {
    gv = Utils.view("Dissolver");

    dissolve("Overlaps", "MULTILINESTRING ((50 50, 150 150, 250 250), (150 150, 250 250, 350 350))");
    dissolve("Lollipop",
          "LINESTRING (50 50, 150 50, 250 50, 250 150, 150 50, 50 50)");
    dissolve("Ring with Tail",
          "MULTILINESTRING ((50 50, 150 50, 150 150), (50 50, 50 150, 150 150), (150 50, 250 50))");

    gv.close();
  } /* main */


  private static void dissolve(String title, String wtk)
  {
    Geometry geom = Utils.toGeometry(wtk);

    gv.clear();
    gv.setTitle(title);
    System.out.println("--- " + title + " ---");
    gv.add(geom);
    System.out.println("\nGeometry: " + geom);
    Utils.pause(1000);

    LineDissolver d = new LineDissolver();
    d.add(geom);
    Geometry result = d.getResult();

    System.out.println("\nDissolve result: " + Utils.simplify(result));

    if (result instanceof MultiLineString) {
      int count = result.getNumGeometries();
      // extract each line and display separately
      System.out.println("Line parts:");
      for(int i = 0; i < count; i++) {
        Geometry line = result.getGeometryN(i);
        gv.add(line);
        System.out.println(" " + (i+1) + ". " + Utils.simplify(line));
      }
    }
    else
     gv.add(result);

    Utils.waitEnter();
  }  // end of dissolve()

 
} // end of Dissolver class
