JavaXT
|
|
Polygon Classpackage javaxt.geospatial.geometry; //****************************************************************************** //** Polygon Class - By Peter Borissow //****************************************************************************** /** * Basic Structure for storing polygons * ******************************************************************************/ public class Polygon implements Geometry { private Points Points = null; private String srs = "EPSG:4326"; //************************************************************************** //** Creates a new instance of Polygon //************************************************************************** public Polygon(){ } public Polygon(Point[] Points){ this.Points = new Points(Points); } public Polygon(Points Points){ this.Points = Points; } //************************************************************************** //** getSRS //************************************************************************** //** Sets the srs attribute for this Geometry. */ public void setSRS(String srsName){ if (srsName==null) srs = ""; else srs = srsName.trim(); } //************************************************************************** //** getSRS //************************************************************************** //** Returns the srs name of this Geometry. */ public String getSRS(){ return srs; } //************************************************************************** //** getName //************************************************************************** //** Returns the name of this Geometry. */ public String getName(){ return new Geometry.Name(this).toString(); } //************************************************************************** //** isClosed //************************************************************************** private boolean isClosed(){ if (Points!=null){ Point FirstPoint = Points.getFirstPoint(); Point LastPoint = Points.getLastPoint(); if (FirstPoint.equals(LastPoint)) return true; } return false; } //************************************************************************** //** closePolygon //************************************************************************** private void closePolygon(){ if (isClosed()==false){ Point FirstPoint = Points.getFirstPoint(); Point LastPoint = Points.getLastPoint(); addPoint(FirstPoint); } } //************************************************************************** //** addPoint //************************************************************************** /** Used to append a point to an array of points */ public void addPoint(Point Coordinate){ if (Coordinate==null) return; else{ Points.addPoint(Coordinate); } } //************************************************************************** //** addPoints //************************************************************************** /** Used to append a point or multiple points to an array of points. * Attempts to convert input string into coordinate touples. */ /* public void addPoints(String Coordinates){ Points.addPoints(Coordinates); } */ //************************************************************************** //** toString //************************************************************************** /** Used to retrieve the Points that compose the Polygon */ public Point[] getPoints(){ return Points.getPoints(); } //************************************************************************** //** toString //************************************************************************** /** Used to return a Well-known Text (WKT) representation of the polygon. */ public String toString(){ if (Points==null) return null; else{ return "POLYGON((" + Points.toString(" ", ", ") + "))"; } } //************************************************************************** //** toString //************************************************************************** /** Used to convert a line to a String */ public String toString(String CoordinateSeparator, String TupleSeparator){ if (Points==null) return ""; else{ return Points.toString(CoordinateSeparator, TupleSeparator); } } //************************************************************************** //** toGML //************************************************************************** /** Used to convert a line to GML (xml fragment) */ public String toGML(){ if (Points==null) return ""; else{ String srsName = " srsName=\"" + srs + "\""; if (srs.length()==0) srsName = ""; StringBuffer Coordinates = new StringBuffer(); Coordinates.append("<gml:Polygon" + srsName + ">");//<gml:outerBoundaryIs> Coordinates.append("<gml:outerBoundaryIs>"); Coordinates.append("<gml:LinearRing>"); Coordinates.append("<gml:coordinates>"); Coordinates.append(toString(",", " ")); Coordinates.append("</gml:coordinates>"); Coordinates.append("</gml:LinearRing>"); Coordinates.append("</gml:outerBoundaryIs>"); Coordinates.append("</gml:Polygon>"); return Coordinates.toString().trim(); } } } |