Hello, Oleksandr's suggestion is how I would do this as well, and you'll find some commented example code below. However, if you are looking at GeoTools just to do this one task I think you would be much better off doing it some other way (e.g. with one of the many free GIS programs such as uDig). Learning GeoTools, plus the Maven build tool which you are more or less forced to use, just to do this would be like digging up iron ore and chopping down a tree to make your own hammer just to hit one nail :)
If you do want to learn GeoTools, can I suggest that to help you understand the code below, you first look at two of the tutorials, Quickstart and Geometry CRS. You'll find both of them on this page: http://docs.geotools.org/latest/userguide/tutorial/index.html Finally, the example code below illustrates one way of creating the geographic polygon, transforming it into an equal-area map projection, and calculating its area. To run this code with Maven you will need to include the gt-main and gt-referencing modules. If you are making use of the GeoTools library of map projections (see comments in code) you will also need to include the gt-epsg-hsql module. Including modules in an application is explained in the Quickstart tutorial. Hope this helps to get you started. Michael package org.geotools.userlist; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import com.vividsolutions.jts.densify.Densifier; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LinearRing; import org.geotools.geometry.jts.JTS; import org.geotools.referencing.CRS; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; public class AreaFromLatLons { public static void main(String[] args) throws Exception { // Example data: vertex coords of a unit rectangle double[] x = { -43, -43, -44, -44}; // longitudes double[] y = { -23, -22, -22, -23}; // latitudes // calculate area double area = new AreaFromLatLons().getArea(x, y); System.out.println("Polygon area: " + area); } private double getArea(double[] lon, double[] lat) throws Exception { if (lon.length < 3 || lon.length != lat.length) { throw new IllegalArgumentException("Bummer: bad arguments"); } final int N = lon.length; // Create the polygon GeometryFactory geomFactory = new GeometryFactory(); Coordinate[] coords = new Coordinate[N + 1]; for (int i = 0; i < N; i++) { // remember X = longitude, Y = latitude ! coords[i] = new Coordinate(lon[i], lat[i]); } // closing coordinate (same as first coord coords[N] = new Coordinate(coords[0]); LinearRing polygonBoundary = geomFactory.createLinearRing(coords); LinearRing[] polygonHoles = null; Geometry polygon = geomFactory.createPolygon(polygonBoundary, polygonHoles); // Densify the polygon by adding extra vertices to its edges so // that when it is reprojected they will approximate curves // more closely double vertexSpacing = polygon.getLength() / 1000.0; // for example Geometry densePolygon = Densifier.densify(polygon, vertexSpacing); // Create a MathTransform to convert the vertex coordinates from // lat-lon (assumed to be WGS84 in this example) to an equal area // projection. // // If there is a suitable map projection availble in GeoTools you // can retrieve it like this... // US National Atlas Equal Area projection (code EPSG:2163) // CoordinateReferenceSystem equalAreaCRS = CRS.decode("EPSG:2163", true); // // In this example we brew one for Brazil using a non-standard projection // cooked up by ESRI which we read from a file (see bottom of code for file // contents) String wkt = getWKTFromResource("esri-102033.wkt"); CoordinateReferenceSystem equalAreaCRS = CRS.parseWKT(wkt); MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, equalAreaCRS, true); // Reproject the polygon and return its area Geometry transformedPolygon = JTS.transform(densePolygon, transform); return transformedPolygon.getArea(); } private String getWKTFromResource(String resName) throws Exception { InputStream strm = this.getClass().getResourceAsStream(resName); BufferedReader reader = new BufferedReader(new InputStreamReader(strm)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } } // Contents of the file esri-102033.wkt // // PROJCS["South_America_Albers_Equal_Area_Conic", // GEOGCS["GCS_South_American_1969", // DATUM["South_American_Datum_1969", // SPHEROID["GRS_1967_Truncated",6378160,298.25]], // PRIMEM["Greenwich",0], // UNIT["Degree",0.017453292519943295]], // PROJECTION["Albers_Conic_Equal_Area"], // PARAMETER["False_Easting",0], // PARAMETER["False_Northing",0], // PARAMETER["longitude_of_center",-60], // PARAMETER["Standard_Parallel_1",-5], // PARAMETER["Standard_Parallel_2",-42], // PARAMETER["latitude_of_center",-32], // UNIT["Meter",1], // AUTHORITY["EPSG","102033"]] ------------------------------------------------------------------------------ Doing More with Less: The Next Generation Virtual Desktop What are the key obstacles that have prevented many mid-market businesses from deploying virtual desktops? How do next-generation virtual desktops provide companies an easier-to-deploy, easier-to-manage and more affordable virtual desktop model.http://www.accelacomm.com/jaw/sfnl/114/51426474/ _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
