Converts a XY geometry to XYZ. If a geometry has no Z component, then a 0
Z coordinate is tacked on.

@param geom
@return
186804
public static Geometry force3D(Geometry geom) {
        if (geom == null) {
            return null;
        }
        return GeometryCoordinateDimension.force(geom, 3); 
    }
1309037
public void convertGeometryFromEnuToWgs84( Geometry geometryEnu ) {
        Coordinate[] coordinates = geometryEnu.getCoordinates();
        for( int i = 0; i < coordinates.length; i++ ) {
            Coordinate wgs84 = enuToWgs84(coordinates[i]);
            coordinates[i].x = wgs84.x;
            coordinates[i].y = wgs84.y;
            coordinates[i].z = wgs84.z;
        }
    }
864035
@SuppressWarnings("unchecked")
    public OsmDroidMapShape toShape(Geometry geometry) {

        OsmDroidMapShape shape = null;

        GeometryType geometryType = geometry.getGeometryType();
        switch (geometryType) {
            case POINT:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.LAT_LNG, toLatLng((Point) geometry));
                break;
            case LINESTRING:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.POLYLINE_OPTIONS,
                    toPolyline((LineString) geometry));
                break;
            case POLYGON:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.POLYGON_OPTIONS,
                    toPolygon((Polygon) geometry));
                break;
            case MULTIPOINT:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_LAT_LNG,
                    toLatLngs((MultiPoint) geometry));
                break;
            case MULTILINESTRING:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_POLYLINE_OPTIONS,
                    toPolylines((MultiLineString) geometry));
                break;
            case MULTIPOLYGON:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_POLYGON_OPTIONS,
                    toPolygons((MultiPolygon) geometry));
                break;
            case CIRCULARSTRING:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.POLYLINE_OPTIONS,
                    toPolyline((CircularString) geometry));
                break;
            case COMPOUNDCURVE:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_POLYLINE_OPTIONS,
                    toPolylines((CompoundCurve) geometry));
                break;
            case CURVEPOLYGON:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.POLYGON_OPTIONS,
                    toCurvePolygon((CurvePolygon) geometry));
                break;
            case POLYHEDRALSURFACE:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_POLYGON_OPTIONS,
                    toPolygons((PolyhedralSurface) geometry));
                break;
            case TIN:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.MULTI_POLYGON_OPTIONS,
                    toPolygons((TIN) geometry));
                break;
            case TRIANGLE:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.POLYGON_OPTIONS,
                    toPolygon((Triangle) geometry));
                break;
            case GEOMETRYCOLLECTION:
                shape = new OsmDroidMapShape(geometryType,
                    OsmMapShapeType.COLLECTION,
                    toShapes((GeometryCollection<Geometry>) geometry));
                break;
            default:
                throw new GeoPackageException("Unsupported Geometry Type: "
                    + geometryType.getName());
        }

        return shape;
    }
184496
public static Polygon convert(Polygon polygon, int dimension) {
        LinearRing shell = GeometryCoordinateDimension.convert(polygon.getExteriorRing(),dimension);
        int nbOfHoles = polygon.getNumInteriorRing();
        final LinearRing[] holes = new LinearRing[nbOfHoles];
        for (int i = 0; i < nbOfHoles; i++) {
            holes[i] = GeometryCoordinateDimension.convert(polygon.getInteriorRingN(i),dimension);
        }
        return gf.createPolygon(shell, holes);
    }
294858
private Geometry simplify(Geometry geom, double resolution, double factor){
		Geometry simplifiedGeometry = null;
		
		if( geom instanceof GeometryCollection ){
			GeometryCollection collection = (GeometryCollection)geom;
			boolean isSimplified = false;
			//List<Geometry> newComponents = new Vector<Geometry>();
			
			// Extract basic geometries
			List<Geometry> basicGeometries = new Vector<Geometry>();
			collection.accumulateBasicGeometries(basicGeometries);
			
			List<Point> accumulatedPoints = new Vector<Point>();
			List<LineString> accumulatedLineStrings = new Vector<LineString>();
			List<Polygon> accumulatedPolygons = new Vector<Polygon>();
			
			for(Geometry component : basicGeometries){
				if( component instanceof Point ){
					Point p = (Point)component;
					accumulatedPoints.add(p);
				
				} else if( component instanceof LineString ){
					LineString ls = (LineString)component;
					accumulatedLineStrings.add(ls);
					
				} else if( component instanceof Polygon ){
					Polygon poly = (Polygon)component;
					accumulatedPolygons.add(poly);
				}
			}
			
			List<Geometry> newComponents = new Vector<Geometry>();

			// Deal with polygons in one simplification
			if( accumulatedPolygons.size() > 0 ){
				MultiPolygon polygons = new MultiPolygon(accumulatedPolygons);
				Geometry effectiveComponent = simplify(polygons, resolution, factor);
				if( null != effectiveComponent ){
					isSimplified = true;
					newComponents.add(effectiveComponent);
				} else {
					newComponents.add(polygons);
				}
			}

			// Deal with linestrings in one simplification
			if( accumulatedLineStrings.size() > 0 ){
				MultiLineString linestrings = new MultiLineString(accumulatedLineStrings);
				Geometry effectiveComponent = simplify(linestrings, resolution, factor);
				if( null != effectiveComponent ){
					isSimplified = true;
					newComponents.add(effectiveComponent);
				} else {
					newComponents.add(linestrings);
				}
			}

			// Deal with points in one simplification
			if( accumulatedPoints.size() > 0 ){
				MultiPoint points = new MultiPoint(accumulatedPoints);
				Geometry effectiveComponent = simplify(points, resolution, factor);
				if( null != effectiveComponent ){
					isSimplified = true;
					newComponents.add(effectiveComponent);
				} else {
					newComponents.add(points);
				}
			}
			
			if( isSimplified ){
				simplifiedGeometry = new GeometryCollection(newComponents);
			}
			
		} else if( geom instanceof Point ) {
//			Point point = (Point)geom;
//			
//			double x = Math.round(point.getX() * factor) / factor;
//			double y = Math.round(point.getY() * factor) / factor;
//			if( x != point.getX() 
//			 || y != point.getY() ){
//				simplifiedGeometry = new Point(x,y);
//			}
			
		} else if( geom instanceof LineString ) {
			// The simplification of a LineString might result into a LineString
			// or into a point
			LineString lineString = (LineString)geom;
			boolean isGeneralized = false;
			List<Point> accumulatedPoints = new Vector<Point>();
			Point lastPoint = null;
			
			// Keep track of bounds
			BoundingBox bbox = new BoundingBox();
			
			for(Point point : lineString.getPoints()){
				// Keep track of bounds
				bbox.extendToInclude(point);
				
				Geometry simplifiedPointObj = simplify(point, resolution, factor);
				
				Point simplifiedPoint = null;
				if( null != simplifiedPointObj ){
					isGeneralized = true;
					simplifiedPoint = (Point)simplifiedPointObj;
				} else {
					simplifiedPoint = point;
				}
				
				if( null == lastPoint ){
					lastPoint = simplifiedPoint;
					accumulatedPoints.add(simplifiedPoint);
				} else {
					if( areLocationsColliding(simplifiedPoint, lastPoint, resolution) ){
						isGeneralized = true;
					} else {
						lastPoint = simplifiedPoint;
						accumulatedPoints.add(simplifiedPoint);
					}
				}
			}

			if( isGeneralized ){
				// A change occurred
				if( accumulatedPoints.size() < 2 ){
					// Create a point that represents the centre of the line string
					simplifiedGeometry = bbox.getCentroid();
				} else {
					simplifiedGeometry = new LineString(accumulatedPoints);
				};
			};
			
		} else if( geom instanceof Polygon ) {
			// Might return a polygon or a point
			Polygon polygon = (Polygon)geom;
			boolean newPolygonNeeded = false;
			List<LineString> newRings = new Vector<LineString>();
			
			// First linear ring is the outer limit of polygon
			List<LineString> rings = polygon.getLinearRings();
			LineString firstRing = rings.get(0);
			Geometry simplifiedFirstRing = simplify(firstRing, resolution, factor);
			if( null != simplifiedFirstRing ){
				if( simplifiedFirstRing instanceof Point ){
					// The outer ring was turned into a point. The whole
					// polygon is now a point. Do not process the other rings.
					// Abort and return the point.
					return simplifiedFirstRing;
				} else {
					newPolygonNeeded = true;
					LineString simplifiedLineString = (LineString)simplifiedFirstRing;
					newRings.add(simplifiedLineString);
				}
			} else {
				newRings.add(firstRing);
			}
			
			// The following rings are subtractions from the outer ring
			for(int i=1,e=rings.size(); i<e; ++i){
				LineString innerRing = rings.get(i);
				
				Geometry simplifiedInnerRing = simplify(innerRing,resolution,factor);
				if( null != simplifiedInnerRing ){
					newPolygonNeeded = true;
					if( simplifiedInnerRing instanceof Point ){
						// Drop inner ring
					} else {
						LineString simplifiedLineString = (LineString)simplifiedInnerRing;
						newRings.add(simplifiedLineString);
					}
				} else {
					newRings.add(innerRing);
				}
			}
			
			if( newPolygonNeeded ){
				simplifiedGeometry = new Polygon(newRings);
			}
			
		} else if( geom instanceof MultiPoint ) {
			// A MultiPoint can be simplified into a MultiPoint or
			// a simple Point
			MultiPoint multiPoint = (MultiPoint)geom;
			boolean isGeneralized = false;
			List<Point> accumulatedPoints = new Vector<Point>();
			Point lastPoint = null;
			
			for(Point point : multiPoint.getPoints()){
				Point effectivePoint = point;
				Geometry simplifiedPoint = simplify(point,resolution,factor);
				if( null != simplifiedPoint ){
					isGeneralized = true;
					effectivePoint = (Point)simplifiedPoint;
				}
				
				if( null == lastPoint ) {
					lastPoint = effectivePoint;
					accumulatedPoints.add(effectivePoint);
				} else {
					if( areLocationsColliding(effectivePoint, accumulatedPoints, resolution) ){
						// If this points is colliding with other points, do not
						// add to resulting collection
						isGeneralized = true;
					} else {
						lastPoint = effectivePoint;
						accumulatedPoints.add(effectivePoint);
					}
				}
			}
			
			if( isGeneralized ){
				if( accumulatedPoints.size() < 2 ){
					simplifiedGeometry = accumulatedPoints.get(0);
				} else {
					simplifiedGeometry = new MultiPoint(accumulatedPoints);
				}
			}
			
		} else if( geom instanceof MultiLineString ) {
			// A MultiLineString can be simplified into a MultiLineString, a LineString,
			// a MultiPoint or a Point
			MultiLineString multiLineString = (MultiLineString)geom;
			
			List<LineString> effectiveLineStrings = makeLineStrings(
					multiLineString.getLineStrings(), 
					null, 
					resolution
					);
			
			boolean isGeneralized = false;
			List<LineString> accumulatedLineStrings = new Vector<LineString>();
			List<Point> accumulatedPoints = new Vector<Point>();
			for(LineString lineString : effectiveLineStrings){
				Geometry simplifiedLineString = simplify(lineString, resolution, factor);
				if( null != simplifiedLineString ){
					isGeneralized = true;
					if( simplifiedLineString instanceof Point ){
						Point simplifiedPoint = (Point)simplifiedLineString;
						if( false == areLocationsColliding(simplifiedPoint, accumulatedPoints, resolution) ){
							accumulatedPoints.add(simplifiedPoint);
						}
					} else {
						LineString simplifiedLS = (LineString)simplifiedLineString;
						accumulatedLineStrings.add(simplifiedLS);
					}
				} else {
					accumulatedLineStrings.add(lineString);
				}
			}
			
			if( isGeneralized ){
				if( accumulatedLineStrings.size() > 1 ){
					simplifiedGeometry = new MultiLineString(accumulatedLineStrings);
					
				} else if( accumulatedLineStrings.size() == 1 ){
					simplifiedGeometry = accumulatedLineStrings.get(0);
					
				} else if( accumulatedPoints.size() > 1 ){
					simplifiedGeometry = new MultiPoint(accumulatedPoints);
					
				} else if( accumulatedPoints.size() == 1 ){
					simplifiedGeometry = accumulatedPoints.get(0);
				}
			}
			
		} else if( geom instanceof MultiPolygon ) {
			// A MultiPolygon can be simplified into a MultiPolygon, a Polygon,
			// a MultiPoint or a Point
			MultiPolygon multiPolygon = (MultiPolygon)geom;
			boolean isGeneralized = false;
			List<Polygon> accumulatedPolygons = new Vector<Polygon>();
			List<Point> accumulatedPoints = new Vector<Point>();
			for(Polygon polygon : multiPolygon.getPolygons()){
				Geometry simplifiedPolygon = simplify(polygon, resolution, factor);
				if( null != simplifiedPolygon ){
					isGeneralized = true;
					if( simplifiedPolygon instanceof Point ){
						Point simplifiedPoint = (Point)simplifiedPolygon;
						if( false == areLocationsColliding(simplifiedPoint, accumulatedPoints, resolution) ){
							accumulatedPoints.add(simplifiedPoint);
						}
					} else {
						Polygon simplifiedPoly = (Polygon)simplifiedPolygon;
						accumulatedPolygons.add(simplifiedPoly);
					}
				} else {
					accumulatedPolygons.add(polygon);
				}
			}
			
			if( isGeneralized ){
				if( accumulatedPolygons.size() > 1 ){
					simplifiedGeometry = new MultiPolygon(accumulatedPolygons);
					
				} else if( accumulatedPolygons.size() == 1 ){
					simplifiedGeometry = accumulatedPolygons.get(0);
					
				} else if( accumulatedPoints.size() > 1 ){
					simplifiedGeometry = new MultiPoint(accumulatedPoints);
					
				} else if( accumulatedPoints.size() == 1 ){
					simplifiedGeometry = accumulatedPoints.get(0);
				}
			}
		}
		
		return simplifiedGeometry;
	}
785202
function(geometry) {
            var gml = this.createElementNS(this.gmlns, "gml:MultiPoint");
            var points = geometry.components;
            var pointMember, pointGeom;
            for(var i=0; i<points.length; i++) { 
                pointMember = this.createElementNS(this.gmlns,
                                                   "gml:pointMember");
                pointGeom = this.buildGeometry.point.apply(this,
                                                               [points[i]]);
                pointMember.appendChild(pointGeom);
                gml.appendChild(pointMember);
            }
            return gml;            
        }
579765
public static Geometry removeDuplicateCoordinates(Geometry geom, double tolerance) throws SQLException {
        if (geom == null) {
            return null;
        } else if (geom.isEmpty()) {
            return geom;
        } else if (geom instanceof Point) {
            return geom;
        } else if (geom instanceof MultiPoint) {
            return geom;
        } else if (geom instanceof LineString) {
            return removeDuplicateCoordinates((LineString) geom, tolerance);
        } else if (geom instanceof MultiLineString) {
            return removeDuplicateCoordinates((MultiLineString) geom, tolerance);
        } else if (geom instanceof Polygon) {
            return removeDuplicateCoordinates((Polygon) geom, tolerance);
        } else if (geom instanceof MultiPolygon) {
            return removeDuplicateCoordinates((MultiPolygon) geom, tolerance);
        } else if (geom instanceof GeometryCollection) {
            return removeDuplicateCoordinates((GeometryCollection) geom, tolerance);
        }
        return null;
    }
1300523
public static STRtree geometriesToSRTree( List<Geometry> geometries ) {
        STRtree tree = new STRtree();
        for( Geometry geometry : geometries ) {
            tree.insert(geometry.getEnvelopeInternal(), geometry);
        }
        return tree;
    }
1309503
function(geometry) {
        var coordinatesNode = this.createElementNS(this.kmlns, "coordinates");
        
        var path;
        var points = geometry.components;
        if(points) {
            // LineString or LinearRing
            var point;
            var numPoints = points.length;
            var parts = new Array(numPoints);
            for(var i=0; i<numPoints; ++i) {
                point = points[i];
                parts[i] = this.buildCoordinates(point);
            }
            path = parts.join(" ");
        } else {
            // Point
            path = this.buildCoordinates(geometry);
        }
        
        var txtNode = this.createTextNode(path);
        coordinatesNode.appendChild(txtNode);
        
        return coordinatesNode;
    }
312919
private void setVertex(Geometry geom, GeometryIndex index, Coordinate coordinate)
			throws GeometryIndexNotFoundException {
		if (index.hasChild() && geom.getGeometries() != null && geom.getGeometries().length > index.getValue()) {
			setVertex(geom.getGeometries()[index.getValue()], index.getChild(), coordinate);
		} else if (index.getType() == GeometryIndexType.TYPE_VERTEX && geom.getCoordinates() != null
				&& geom.getCoordinates().length > index.getValue()) {
			if (geom.getGeometryType().equals(Geometry.LINEAR_RING)) {
				// In case of a closed ring, the last vertex is not allowed to be moved:
				if ((geom.getCoordinates().length - 1) > index.getValue()) {
					geom.getCoordinates()[index.getValue()] = coordinate;
					if (index.getValue() == 0) {
						// In case of closed ring, keep last coordinate equal to the first:
						geom.getCoordinates()[geom.getCoordinates().length - 1] = new Coordinate(coordinate);
					}
				} else {
					throw new GeometryIndexNotFoundException("Can't move closing vertex of a LinearRing, "
							+ "move index=0 instead.");
				}
			} else {
				geom.getCoordinates()[index.getValue()] = coordinate;
			}
		} else {
			throw new GeometryIndexNotFoundException("Could not match index with given geometry");
		}
	}
578719
public static String toGeojson(Geometry geom) {
        StringBuilder sb = new StringBuilder();
        toGeojsonGeometry(geom, sb);
        return sb.toString();
    }
43158
private void addGeometry(Geometry geom) throws IllegalArgumentException {
        if(!geom.isValid()) {
            throw new IllegalArgumentException("Provided geometry is not valid !");
        }
        if(geom instanceof GeometryCollection) {
            Map<TriangulationPoint, Integer> pts = new HashMap<TriangulationPoint, Integer>(geom.getNumPoints());
            List<Integer> segments = new ArrayList<Integer>(pts.size());
            AtomicInteger pointsCount = new AtomicInteger(0);
            PointHandler pointHandler = new PointHandler(this, pts, pointsCount);
            LineStringHandler lineStringHandler = new LineStringHandler(this, pts, pointsCount, segments);
            for(int geomId = 0; geomId < geom.getNumGeometries(); geomId++) {
                addSimpleGeometry(geom.getGeometryN(geomId), pointHandler, lineStringHandler);
            }
            int[] index = new int[segments.size()];
            for(int i = 0; i < index.length; i++) {
                index[i] = segments.get(i);
            }
            // Construct final points array by reversing key,value of hash map
            TriangulationPoint[] ptsArray = new TriangulationPoint[pointsCount.get()];
            for(Map.Entry<TriangulationPoint, Integer> entry : pts.entrySet()) {
                ptsArray[entry.getValue()] = entry.getKey();
            }
            pts.clear();
            convertedInput = new ConstrainedPointSet(Arrays.asList(ptsArray), index);
        } else {
            addGeometry(geom.getFactory().createGeometryCollection(new Geometry[]{geom}));
        }
    }
799955
public static GeometryCollection convert(GeometryCollection gc, int dimension) {
        int nb = gc.getNumGeometries();
        final Geometry[] geometries = new Geometry[nb];        
        for (int i = 0; i < nb; i++) {            
            geometries[i]=force(gc.getGeometryN(i),dimension);
        }
        return gf.createGeometryCollection(geometries);        
    }
119006
public static Geometry rotate(Geometry geom, double theta) {
        if (geom != null) {
            Coordinate center = geom.getEnvelopeInternal().centre();
            return rotate(geom, theta, center.x, center.y);
        } else {
            return null;
        }
    }
1105201
public static com.vividsolutions.jts.geom.Geometry toJts(Geometry geometry) throws JtsConversionException {
		if (geometry == null) {
			throw new  JtsConversionException("Cannot convert null argument");
		}
		int srid = geometry.getSrid();
		int precision = geometry.getPrecision();
		PrecisionModel model;
		if (precision == -1) {
			model = new PrecisionModel(PrecisionModel.FLOATING);
		} else {
			model = new PrecisionModel(Math.pow(10, precision));
		}
		GeometryFactory factory = new GeometryFactory(model, srid);
		com.vividsolutions.jts.geom.Geometry jts;

		String geometryType = geometry.getGeometryType();
		if (GeometryService.isEmpty(geometry)) {
			jts = createEmpty(factory, geometryType);
		} else if (Geometry.POINT.equals(geometryType)) {
			jts = factory.createPoint(convertCoordinates(geometry)[0]);
		} else if (Geometry.LINEAR_RING.equals(geometryType)) {
			jts = factory.createLinearRing(convertCoordinates(geometry));
		} else if (Geometry.LINE_STRING.equals(geometryType)) {
			jts = factory.createLineString(convertCoordinates(geometry));
		} else if (Geometry.POLYGON.equals(geometryType)) {
			Geometry[] geometries = geometry.getGeometries();
			if (null != geometries && geometries.length > 0) {
				LinearRing exteriorRing = (LinearRing) toJts(geometries[0]);
				LinearRing[] interiorRings = new LinearRing[geometries.length - 1];
				for (int i = 0; i < interiorRings.length; i++) {
					interiorRings[i] = (LinearRing) toJts(geometries[i + 1]);
				}
				jts = factory.createPolygon(exteriorRing, interiorRings);
			} else {
				jts = factory.createPolygon(null, null);
			}
		} else if (Geometry.MULTI_POINT.equals(geometryType)) {
			Point[] points = new Point[geometry.getGeometries().length];
			jts = factory.createMultiPoint((Point[]) convertGeometries(geometry, points));
		} else if (Geometry.MULTI_LINE_STRING.equals(geometryType)) {
			LineString[] lineStrings = new LineString[geometry.getGeometries().length];
			jts = factory.createMultiLineString((LineString[]) convertGeometries(geometry, lineStrings));
		} else if (Geometry.MULTI_POLYGON.equals(geometryType)) {
			Polygon[] polygons = new Polygon[geometry.getGeometries().length];
			jts = factory.createMultiPolygon((Polygon[]) convertGeometries(geometry, polygons));
		} else {
			throw new JtsConversionException("Cannot convert geometry: Unsupported type.");
		}

		return jts;
	}
975751
private static org.geomajas.gwt.client.spatial.geometry.Geometry[] convertGeometries(Geometry geometry,
			org.geomajas.gwt.client.spatial.geometry.Geometry[] geometries) {
		for (int i = 0; i < geometries.length; i++) {
			geometries[i] = toGwt(geometry.getGeometries()[i]);
		}
		return geometries;
	}
697792
function(geometry) {
            var gml = this.createElementNS(this.gmlns, "gml:MultiPolygon");
            var polys = geometry.components;
            var polyMember, polyGeom;
            for(var i=0; i<polys.length; ++i) {
                polyMember = this.createElementNS(this.gmlns,
                                                  "gml:polygonMember");
                polyGeom = this.buildGeometry.polygon.apply(this,
                                                                [polys[i]]);
                polyMember.appendChild(polyGeom);
                gml.appendChild(polyMember);
            }
            return gml;

        }