public class ProyectedRelationCalculator {
MathTransform mathTransform = null;
/**
* Define a math transformation betwen coordinates (lat,long) to (x,y)
*/
private void setMathTransform()
throws Exception
{
GeographicCRS geoCRS = org.geotools.referencing.crs.DefaultGeographicCRS.WGS84;
CartesianCS cartCS = org.geotools.referencing.cs.DefaultCartesianCS.GENERIC_2D;
MathTransformFactory mtFactory = FactoryFinder.getMathTransformFactory(null);
FactoryGroup factories = new FactoryGroup();
ParameterValueGroup parameters = mtFactory.getDefaultParameters("Mercator_1SP");
// parameters.parameter("scale_factor").setValue(0.9996);
parameters.parameter("scale_factor").setValue(1.000);
//// parameters.parameter("central_meridian").setValue(0.0);
// parameters.parameter("latitude_of_origin").setValue(0.0);
//// parameters.parameter("false_easting").setValue(0.0);
//// parameters.parameter("false_northing").setValue(0.0);
Map properties = Collections.singletonMap("name", "WGS 84 / Mercator_1SP");
ProjectedCRS projCRS = factories.createProjectedCRS(properties, geoCRS, null, parameters, cartCS);
CoordinateOperationFactory coFactory = FactoryFinder.getCoordinateOperationFactory(null);
CoordinateReferenceSystem sourceCRS = geoCRS;
CoordinateReferenceSystem targetCRS = projCRS;
CoordinateOperation op = coFactory.createOperation(sourceCRS, targetCRS);
this.mathTransform = op.getMathTransform();
System.out.println("Math Transform: " + this.mathTransform.toWKT());
}
public MathTransform getMathTransform()
throws Exception
{
if (this.mathTransform == null)
{
this.setMathTransform();
}
return mathTransform;
}
/**
* Transform the given point using mathTransform
* @param point
* @return
*/
public DirectPosition transformPoint(DirectPosition point)
throws Exception
{
return this.getMathTransform().transform(point,null);
}
/**
* calculate the relation betwen the points - represented by the segment
* @param coordinate1
* @param coordinate2
*/
public LineSegment calculateRelation(DirectPosition point1, DirectPosition point2)
{
double[] coords1 = point1.getCoordinates();
Coordinate coord1 = new Coordinate(coords1[0],coords1[1]);
double[] coords2 = point2.getCoordinates();
Coordinate coord2 = new Coordinate(coords2[0],coords2[1]);
return new LineSegment(coord1,coord2);
}
/**
* @param args
*/
public static void main(String[] args)
throws Exception
{
//example 1
DirectPosition pt1 = new GeneralDirectPosition(-5.986944, 37.377222);
DirectPosition pt2 = new GeneralDirectPosition(-58.366667, -34.600000);
String city1="Sevilla";
String city2="Bs. As";
//example 2
// DirectPosition pt1 = new GeneralDirectPosition(-64.2, -31.39);
// DirectPosition pt2 = new GeneralDirectPosition(-68.32, -54.82);
// String city1="Cordoba";
// String city2="Ushuaia";
System.out.println("Point1 ("+city1+"): " + pt1+" \nPoint2 ("+city2+"):"+pt2);
ProyectedRelationCalculator proyectedRelationCalculator = new ProyectedRelationCalculator();
//transforming de points
pt1 = proyectedRelationCalculator.transformPoint(pt1);
pt2 = proyectedRelationCalculator.transformPoint(pt2);
System.out.println("Transformation (x,y) \n====================\nPoint1 ("+city1+"): " + pt1+" \nPoint2 ("+city2+"): "+pt2);
//calculating the relation
LineSegment relation = proyectedRelationCalculator.calculateRelation(pt1, pt2);
double distance = relation.getLength();
double angle = relation.angle();
System.out.println("Distance = "+distance+" - Orientation = "+((angle)*180/Math.PI));
double realDistance = 9670316.0; //obtenida de http://jan.ucc.nau.edu/%7Ecvm/latlongdist.html
System.out.println("aditional distance = "+(distance - realDistance));
}
}
here's the original message. excuse me about the disorder
mariano s kohan s <[EMAIL PROTECTED]> escribió:
Fecha: Fri, 1 Sep 2006 23:45:49 -0300 (ART)
De: mariano s kohan s <[EMAIL PROTECTED]>
Asunto: Problem with coordinate tranformation in coordinates relations
A: [email protected]
Hello,
i'm working in a project where i have to process a dataset with spatial data represented by coordinates in (long,lat) format (initially).
With this coordinates i've to process them ir order to obtain spatial relationships betwen them, represented by the distance and angle of orientation.
I was trying to use geotools for calculating this relationship. I think in first transforming the coordinates in (long, lat) to values (x,y) in the plane. And betwen the last use JTS for calculate de distances and angles.
The values about the distance that i obtained, have a great different with real values.
1). what's wrong in the code? (the .java is attached)
2). it's ok the definition of the Mercator proyection? I want a projection with the axis over de ecuator and greenwich meridian.
3).could be the reason about difference in the values an approximation error?
thanks,
Mariano Kohan
PD: here is the code
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
Probalo ya!
ProyectedRelationCalculator.java
Description: pat377943467
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
