package org.geotools.geometry.jts;
import java.util.ArrayList;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
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.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
public class LiteCoordinateSequence extends PackedCoordinateSequence {
private static final GeometryFactory geomFac = new GeometryFactory(new LiteCoordinateSequenceFactory());
private double[] coords;
private int size;
public LiteCoordinateSequence(double[] coords, int dimensions) {
init(coords, dimensions);
}
void init(double[] coords, int dimensions) {
this.dimension=dimensions;
if(dimensions < 2)
throw new IllegalArgumentException("Invalid dimensions, must be at least 2");
if (coords.length % dimension != 0) {
throw new IllegalArgumentException("Packed array does not contain "
+ "an integral number of coordinates");
}
this.coords = coords;
this.size = coords.length / dimension;
}
public LiteCoordinateSequence(double[] coords) {
init(coords, 2);
}
public LiteCoordinateSequence(float[] coordinates, int dimension) {
double[] dcoords = new double[coordinates.length];
for (int i = 0; i < coordinates.length; i++) {
dcoords[i] = coordinates[i];
}
init(dcoords, dimension);
}
public LiteCoordinateSequence(float[] coordinates) {
this(coordinates, 2);
}
public LiteCoordinateSequence(Coordinate[] coordinates) {
if (coordinates == null)
coordinates = new Coordinate[0];
this.dimension=2;
coords = new double[coordinates.length * this.dimension];
for (int i = 0; i < coordinates.length; i++) {
coords[i * this.dimension] = coordinates[i].x;
if (this.dimension >= 2)
coords[i * this.dimension + 1] = coordinates[i].y;
}
this.size = coordinates.length;
}
public LiteCoordinateSequence(int size, int dimension) {
this.dimension=dimension;
coords = new double[size * this.dimension];
this.size = coords.length / dimension;
}
public LiteCoordinateSequence(LiteCoordinateSequence seq) {
this.dimension = seq.dimension;
this.size = seq.size;
double[] orig = seq.getArray();
this.coords = new double[orig.length];
System.arraycopy(orig, 0, coords, 0, coords.length);
}
public Coordinate getCoordinateInternal(int i) {
double x = coords[i * dimension];
double y = coords[i * dimension + 1];
double z = dimension == 2 ? java.lang.Double.NaN : coords[i * dimension + 2];
return new Coordinate(x, y, z);
}
public int size() {
return size;
}
public Object clone() {
double[] clone = new double[coords.length];
System.arraycopy(coords, 0, clone, 0, coords.length);
return new LiteCoordinateSequence(clone, dimension);
}
public double getOrdinate(int index, int ordinate) {
return coords[index * dimension + ordinate];
}
public double getX(int index) {
return coords[index * dimension];
}
public double getY(int index) {
return coords[index * dimension + 1];
}
public void setOrdinate(int index, int ordinate, double value) {
coordRef = null;
coords[index * dimension + ordinate] = value;
}
public Envelope expandEnvelope(Envelope env)
{
double minx = coords[0];
double maxx = minx;
double miny = coords[1];
double maxy = miny;
for (int i = 0; i < coords.length; i += dimension ) {
double x = coords[i];
if(x < minx)
minx = x;
else if(x > maxx)
maxx = x;
double y = coords[i + 1];
if(y < miny)
miny = y;
else if(y > maxy)
maxy = y;
}
env.expandToInclude(minx, miny);
env.expandToInclude(maxx, maxy);
return env;
}
public double[] getArray() {
return coords;
}
public void setArray(double[] coords2) {
coords = coords2;
size = coords.length / dimension;
coordRef = null;
}
public void setArray(double[] coords2, int dimension) {
coords = coords2;
this.dimension = dimension;
size = coords.length / dimension;
coordRef = null;
}
public double[] getXYArray()
{
if (dimension==2) return coords;
int n = size();
double[] result = new double[n*2];
for (int t=0;t<n;t++)
{
result[t*2] = getOrdinate(t,0);
result[t*2+1] = getOrdinate(t,1);
}
return result;
}
public static final Geometry cloneGeometry(Geometry geom) {
if(geom == null)
return null;
if (geom.getFactory().getCoordinateSequenceFactory() instanceof LiteCoordinateSequenceFactory) {
if (geom instanceof LineString)
return cloneGeometryLCS((LineString) geom);
else if (geom instanceof Polygon)
return cloneGeometryLCS((Polygon) geom);
else if (geom instanceof Point)
return cloneGeometryLCS((Point) geom);
else
return cloneGeometryLCS((GeometryCollection) geom);
} else {
if (geom instanceof LineString)
return cloneGeometry((LineString) geom);
else if (geom instanceof Polygon)
return cloneGeometry((Polygon) geom);
else if (geom instanceof Point)
return cloneGeometry((Point) geom);
else
return cloneGeometry((GeometryCollection) geom);
}
}
private static final Geometry cloneGeometryLCS(Polygon geom) {
LinearRing lr = (LinearRing) cloneGeometryLCS((LinearRing) geom.getExteriorRing());
LinearRing[] rings = new LinearRing[geom.getNumInteriorRing()];
for (int t = 0; t < rings.length; t++) {
rings[t] = (LinearRing) cloneGeometryLCS((LinearRing) geom.getInteriorRingN(t));
}
return geomFac.createPolygon(lr, rings);
}
private static final Geometry cloneGeometryLCS(Point geom) {
return geomFac.createPoint(new LiteCoordinateSequence((LiteCoordinateSequence) geom
.getCoordinateSequence()));
}
private static final Geometry cloneGeometryLCS(LineString geom) {
return geomFac.createLineString(new LiteCoordinateSequence((LiteCoordinateSequence) geom
.getCoordinateSequence()));
}
private static final Geometry cloneGeometryLCS(LinearRing geom) {
return geomFac.createLinearRing(new LiteCoordinateSequence((LiteCoordinateSequence) geom
.getCoordinateSequence()));
}
private static final Geometry cloneGeometryLCS(GeometryCollection geom) {
if (geom.getNumGeometries() == 0) {
Geometry[] gs = new Geometry[0];
return geomFac.createGeometryCollection(gs);
}
ArrayList gs = new ArrayList(geom.getNumGeometries());
int n = geom.getNumGeometries();
Class geomType = geom.getGeometryN(0).getClass();
for (int t = 0; t < n; t++) {
Geometry clone = cloneGeometry(geom.getGeometryN(t));
if(clone.getClass() != geomType) {
geomType = null;
}
gs.add(t, clone);
}
if(geomType == Point.class) {
return geomFac.createMultiPoint((Point[]) gs.toArray(new Point[gs.size()]));
} else if(geomType == LineString.class) {
return geomFac.createMultiLineString((LineString[]) gs.toArray(new LineString[gs.size()]));
} else if(geomType == Polygon.class) {
return geomFac.createMultiPolygon((Polygon[]) gs.toArray(new Polygon[gs.size()]));
} else {
return geomFac.buildGeometry(gs);
}
}
private static final Geometry cloneGeometry(Polygon geom) {
LinearRing lr = (LinearRing) cloneGeometry((LinearRing) geom.getExteriorRing());
LinearRing[] rings = new LinearRing[geom.getNumInteriorRing()];
for (int t = 0; t < rings.length; t++) {
rings[t] = (LinearRing) cloneGeometry((LinearRing) geom.getInteriorRingN(t));
}
return geomFac.createPolygon(lr, rings);
}
private static final Geometry cloneGeometry(Point geom) {
return geomFac
.createPoint(new LiteCoordinateSequence((Coordinate[]) geom.getCoordinates()));
}
private static final Geometry cloneGeometry(LineString geom) {
return geomFac.createLineString(new LiteCoordinateSequence((Coordinate[]) geom
.getCoordinates()));
}
private static final Geometry cloneGeometry(LinearRing geom) {
return geomFac.createLinearRing(new LiteCoordinateSequence((Coordinate[]) geom
.getCoordinates()));
}
private static final Geometry cloneGeometry(GeometryCollection geom) {
if (geom.getNumGeometries() == 0) {
Geometry[] gs = new Geometry[0];
return geomFac.createGeometryCollection(gs);
}
ArrayList gs = new ArrayList(geom.getNumGeometries());
int n = geom.getNumGeometries();
for (int t = 0; t < n; t++) {
gs.add(cloneGeometry(geom.getGeometryN(t)));
}
return geomFac.buildGeometry(gs);
}
}