package com.planetmayo.debrief.satc.model.states;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.Polygon;

/**
 * class representing a bounded set of locations, stored as a polygon
 * 
 * @author ian
 * 
 */
public class LocationRange extends BaseRange<LocationRange>
{
	/**
	 * the range of locations we allow
	 * 
	 */
	private Polygon _myArea;

	public LocationRange(Polygon area)
	{
		_myArea = area;
	}

	/**
	 * copy constructor
	 * 
	 * @param range
	 */
	public LocationRange(LocationRange range)
	{
		this((Polygon) range._myArea.clone());
	}

	/**
	 * trim my area to the area provided
	 * 
	 * @param sTwo
	 */
	@Override
	public void constrainTo(LocationRange sTwo) throws IncompatibleStateException
	{
		Geometry collection = _myArea.intersection(sTwo._myArea);
		if (collection instanceof Polygon)
		{
			Polygon geo = (Polygon) collection;
			// do we already have an area?
			if (_myArea == null)
			{
				// take a copy of it
				_myArea = (Polygon) geo.clone();
			}
			else
			{
				// ok, constrain myself
				_myArea = (Polygon) _myArea.intersection(geo);
			}
		}

		else if (collection instanceof GeometryCollection)
		{
			GeometryCollection geo = (GeometryCollection) collection;
			if (geo.getLength() == 1)
			{
				_myArea = (Polygon) geo.getGeometryN(0);
			}
			else if (geo.getLength() == 0)
			{
				throw new IncompatibleStateException("Polygons do not overlap", this,
						sTwo);
			}
		}
	}

	public Polygon getPolygon()
	{
		return _myArea;
	}

	@Override
	public String getConstraintSummary()
	{
		String res = "N/A";
		if (_myArea != null)
		{
		//	NumberFormat df = new DecimalFormat("0.0000");
			Geometry theBoundary = _myArea.convexHull();
			;
			double theArea = theBoundary.getArea();
			
			// TODO: the next line should have better formatting
			res = _myArea.getCoordinates().length + "pts " + (int)(theArea * 100000);
		}
		return res;
	}

}

@@ -44,16 +44,68 @@
 tates;%0A%0A
+import com.planetmayo.debrief.satc.util.GeoSupport;%0A
 import c
@@ -196,16 +196,63 @@
 ection;%0A
+import com.vividsolutions.jts.geom.LinearRing;%0A
 import c
@@ -1215,53 +1215,651 @@
 %09%09%09%09
-_myArea = (Polygon) _myArea.intersection(geo)
+Geometry intersect =  _myArea.intersection(geo);%0A%09%09%09%09%0A%09%09%09%09// is this just a geometry collection?%0A%09%09%09%09if(intersect instanceof GeometryCollection)%0A%09%09%09%09%7B%0A%09%09%09%09%09GeometryCollection gc = (GeometryCollection) intersect;%0A%09%09%09%09%09%0A%09%09%09%09%09// TODO: the intersect above isn't working as expected - see below%0A%09%09%09%09%09// on occasion it returns a linestring followed by a polygon. The%0A%09%09%09%09%09// LineString is contained inside the polygon, and shouldn't be there.%0A%09%09%09%09%09if(gc.getNumGeometries() == 2)%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09Geometry outer = gc.getGeometryN(1);%0A%09%09%09%09%09%09if(outer instanceof Polygon)%0A%09%09%09%09%09%09%09intersect = (Polygon)outer;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09_myArea = (Polygon)intersect
 ;%0A%09%09

handle instance when geometry is simpler than expected