Micheal,
Sorry for bother you again.
This is correcte implementation:
private static CoordinateReferenceSystem getDefaultCRS() {
return DefaultGeographicCRS.WGS84;
}
Main.java
public static void main(String[] args) throws Exception {
PolygonAreaCalculator calc = new PolygonAreaCalculator();
double[] latitude = new double[4];
double[] longitude = new double[4];
latitude[0] = 51.5174723;
latitude[1] = 51.515259;
latitude[2] = 51.513329;
latitude[3] = 51.51738;
longitude[0] = -0.0899537;
longitude[1] = -0.086623;
longitude[2] = -0.08896;
longitude[3] = -0.08186;
double area = calc.getArea(longitude, latitude);
System.out.println(area);
}
More question, the result is in meters ?
Thanks.
> Date: Thu, 8 Sep 2011 22:33:16 +1000
> Subject: Re: [Geotools-gt2-users] how to calculate area of a polygon by
> coordinates
> From: [email protected]
> To: [email protected]
>
> Hi Thiago,
>
> OK, I understand. Well, you could adapt the example into an API
> convenient for your application and then compile it as a jar either on
> its own or bundled with its dependencies. I just checked and there
> are not many dependencies required:
>
> +- org.geotools:gt-main:jar:8-SNAPSHOT:compile
> | +- org.geotools:gt-api:jar:8-SNAPSHOT:compile
> | +- com.vividsolutions:jts:jar:1.12:compile
> | | \- xerces:xercesImpl:jar:2.4.0:compile
> | +- jdom:jdom:jar:1.0:compile
> | \- javax.media:jai_core:jar:1.1.3:compile
> +- org.geotools:gt-referencing:jar:8-SNAPSHOT:compile
> | +- java3d:vecmath:jar:1.3.2:compile
> | +- commons-pool:commons-pool:jar:1.5.4:compile
> | \- org.geotools:gt-metadata:jar:8-SNAPSHOT:compile
> | \- org.geotools:gt-opengis:jar:8-SNAPSHOT:compile
> | \- net.java.dev.jsr-275:jsr-275:jar:1.0-beta-2:compile
> \- org.geotools:gt-epsg-hsql:jar:8-SNAPSHOT:compile
> \- hsqldb:hsqldb:jar:1.8.0.7:compile
>
> Plus, you are probably using some of those already. So, if you don't
> use Maven as your build tool, you could just download those jars
> manually from the GeoTools repository:
> http://download.osgeo.org/webdav/geotools/
>
> The code below shows how you might modify the original example into
> something that you could compile into a jar for your app to use. You
> need to write the static getDefaultCRS() method to do something
> appropriate for your use case (e.g. to read WKT for a map projection
> as in the example).
>
> Hope this helps.
>
> Michael
>
>
> 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.FactoryException;
> import org.opengis.referencing.crs.CoordinateReferenceSystem;
> import org.opengis.referencing.operation.MathTransform;
>
> /**
> * Provides methods to calculate the area, in square metres,
> * of polygons defined by lat-lon vertices.
> */
> public class PolygonAreaCalculator {
> private static final double TOL = 1.0e-8;
>
> private static final CoordinateReferenceSystem
> DEFAULT_CALCULATION_CRS = getDefaultCRS();
>
> private static CoordinateReferenceSystem getDefaultCRS() {
> // EDIT THIS METHOD TO SOMETHING SUITABLE FOR YOUR
> // APPLICATION AND DATA
> throw new UnsupportedOperationException("Not yet implemented");
> }
>
> private final GeometryFactory geomFactory;
> private final CoordinateReferenceSystem calculationCRS;
> private final MathTransform transform;
>
>
> public PolygonAreaCalculator() {
> this(null);
> }
>
> /**
> * Creates a new instance and sets the coordinate reference system to
> * use for area calculations. A null argument or empty string means
> * use the default reference system.
> *
> * @param epsgCode EPSG code for the coordinate reference system
> */
> public PolygonAreaCalculator(String epsgCode) {
> try {
> if (epsgCode == null || epsgCode.trim().length() == 0) {
> calculationCRS = DEFAULT_CALCULATION_CRS;
> } else {
> if (!epsgCode.startsWith("EPSG:")) {
> epsgCode = "EPSG:" + epsgCode;
> }
> calculationCRS = CRS.decode(epsgCode, true);
> }
>
> transform = CRS.findMathTransform(
> DefaultGeographicCRS.WGS84, calculationCRS, true);
> } catch (FactoryException ex) {
> throw new RuntimeException(ex);
> }
>
> this.geomFactory = new GeometryFactory();
> }
>
> /**
> * Calculates the area of a polygon defined by the provided
> * geographic vertex coordinates.
> *
> * @param longitude longitudes of vertices
> * @param latitude latitudes of vertices
> */
> public double getArea(double[] longitude, double[] latitude)
> throws Exception {
> if (longitude == null || latitude == null) {
> throw new IllegalArgumentException("arguments must not be null");
> }
> if (longitude.length != latitude.length) {
> throw new IllegalArgumentException("Bummer: bad arguments");
> }
>
> int n = longitude.length;
> boolean firstCoordRepeated =
> Math.abs(longitude[0] - longitude[n-1]) < TOL &&
> Math.abs(latitude[0] - latitude[n-1]) < TOL;
>
> final int N;
> if (firstCoordRepeated) {
> N = longitude.length - 1;
> } else {
> N = longitude.length;
> }
>
> if (N < 3) {
> // not a polygon - should probably log a warning or
> // throw an exception
> return 0;
> }
>
>
> // Create the polygon
> Coordinate[] coords = new Coordinate[N + 1];
>
> for (int i = 0; i < N; i++) {
> coords[i] = new Coordinate(longitude[i], latitude[i]);
> }
> 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);
>
>
> // Reproject the polygon and return its area
> Geometry transformedPolygon = JTS.transform(densePolygon, transform);
> return transformedPolygon.getArea();
> }
>
> }
------------------------------------------------------------------------------
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