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