packagecom.alibaba.autonavi;importcom.vividsolutions.jts.geom.Coordinate;importcom.vividsolutions.jts.geom.Geometry;importcom.vividsolutions.jts.geom.GeometryCollection;importcom.vividsolutions.jts.geom.GeometryFactory;importcom.vividsolutions.jts.geom.LineString;importcom.vividsolutions.jts.geom.LinearRing;importcom.vividsolutions.jts.geom.Point;importcom.vividsolutions.jts.geom.Polygon;importcom.vividsolutions.jts.geom.MultiPolygon;importcom.vividsolutions.jts.geom.MultiLineString;importcom.vividsolutions.jts.geom.MultiPoint;importcom.vividsolutions.jts.io.ParseException;importcom.vividsolutions.jts.io.WKTReader;public classGeometryDemo {private GeometryFactory geometryFactory = newGeometryFactory();/*** create a point
*@return
*/
publicPoint createPoint(){
Coordinate coord= new Coordinate(109.013388, 32.715519);
Point point=geometryFactory.createPoint( coord );returnpoint;
}/*** create a point by WKT
*@return*@throwsParseException*/
public Point createPointByWKT() throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
Point point= (Point) reader.read("POINT (109.013388 32.715519)");returnpoint;
}/*** create multiPoint by wkt
*@return
*/
public MultiPoint createMulPointByWKT()throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
MultiPoint mpoint= (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");returnmpoint;
}/***
* create a line
*@return
*/
publicLineString createLine(){
Coordinate[] coords= new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
LineString line=geometryFactory.createLineString(coords);returnline;
}/*** create a line by WKT
*@return*@throwsParseException*/
public LineString createLineByWKT() throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
LineString line= (LineString) reader.read("LINESTRING(0 0, 2 0)");returnline;
}/*** create multiLine
*@return
*/
publicMultiLineString createMLine(){
Coordinate[] coords1= new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
LineString line1=geometryFactory.createLineString(coords1);
Coordinate[] coords2= new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};
LineString line2=geometryFactory.createLineString(coords2);
LineString[] lineStrings= new LineString[2];
lineStrings[0]=line1;
lineStrings[1] =line2;
MultiLineString ms=geometryFactory.createMultiLineString(lineStrings);returnms;
}/*** create multiLine by WKT
*@return*@throwsParseException*/
public MultiLineString createMLineByWKT()throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
MultiLineString line= (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");returnline;
}/*** create a polygon(多边形) by WKT
*@return*@throwsParseException*/
public Polygon createPolygonByWKT() throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
Polygon polygon= (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");returnpolygon;
}/*** create multi polygon by wkt
*@return*@throwsParseException*/
public MultiPolygon createMulPolygonByWKT() throwsParseException{
WKTReader reader= newWKTReader( geometryFactory );
MultiPolygon mpolygon= (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))");returnmpolygon;
}/*** create GeometryCollection contain point or multiPoint or line or multiLine or polygon or multiPolygon
*@return*@throwsParseException*/
public GeometryCollection createGeoCollect() throwsParseException{
LineString line=createLine();
Polygon poly=createPolygonByWKT();
Geometry g1=geometryFactory.createGeometry(line);
Geometry g2=geometryFactory.createGeometry(poly);
Geometry[] garray= newGeometry[]{g1,g2};
GeometryCollection gc=geometryFactory.createGeometryCollection(garray);returngc;
}/*** create a Circle 创建一个圆,圆心(x,y) 半径RADIUS
*@paramx
*@paramy
*@paramRADIUS
*@return
*/
public Polygon createCircle(double x, double y, final doubleRADIUS){final int SIDES = 32;//圆上面的点个数
Coordinate coords[] = new Coordinate[SIDES+1];for( int i = 0; i < SIDES; i++){double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;double dx = Math.cos( angle ) *RADIUS;double dy = Math.sin( angle ) *RADIUS;
coords[i]= new Coordinate( (double) x + dx, (double) y +dy );
}
coords[SIDES]= coords[0];
LinearRing ring=geometryFactory.createLinearRing( coords );
Polygon polygon= geometryFactory.createPolygon( ring, null);returnpolygon;
}/***@paramargs
*@throwsParseException*/
public static void main(String[] args) throwsParseException {
GeometryDemo gt= newGeometryDemo();
Polygon p= gt.createCircle(0, 1, 2);//圆上所有的坐标(32个)
Coordinate coords[] =p.getCoordinates();for(Coordinate coord:coords){
System.out.println(coord.x+","+coord.y);
}
}
}