Commit bf318209 authored by Éric Mermet's avatar Éric Mermet
Browse files

Correct distance computation between two coordinates

git-svn-id: svn+ssh://svn.code.sf.net/p/tip-puck/code/trunk@2329 fbf60bc9-78ff-467a-80f5-d9f1d8e511fc
parent 106a31e0
......@@ -5,10 +5,17 @@ import java.util.ArrayList;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.GeodeticCalculator;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tip.puck.geo.Coordinate2;
......@@ -18,6 +25,7 @@ import org.tip.puck.geo.io.BuildingGeoLinks;
import org.tip.puck.graphs.Graph;
import org.tip.puck.graphs.Node;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
......@@ -42,9 +50,9 @@ public class GeotoolsUtils {
DefaultFeatureCollection featurePointsCollection;
DefaultFeatureCollection featureLinesCollection;
// SimpleFeatureBuilder featureBuilder;
// SimpleFeatureBuilder featureBuilder;
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureType pointFeatureType;
......@@ -65,7 +73,7 @@ public class GeotoolsUtils {
featurePointsCollection = new DefaultFeatureCollection("internal", pointFeatureType);
// featureBuilder = new SimpleFeatureBuilder(pointFeatureType);
// featureBuilder = new SimpleFeatureBuilder(pointFeatureType);
// Add nodes and build geometry
for (Node<Place> node : geoNetwork.getNodes()) {
......@@ -141,7 +149,7 @@ public class GeotoolsUtils {
result = new ArrayList<DefaultFeatureCollection>();
// SimpleFeatureBuilder featureBuilder;
// SimpleFeatureBuilder featureBuilder;
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
......@@ -161,7 +169,7 @@ public class GeotoolsUtils {
SimpleFeatureType pointFeatureType = builder.buildFeatureType();
DefaultFeatureCollection featurePointsCollection = new DefaultFeatureCollection("internal", pointFeatureType);
// featureBuilder = new SimpleFeatureBuilder(pointFeatureType);
// featureBuilder = new SimpleFeatureBuilder(pointFeatureType);
// Add nodes and build geometry
for (Node<Place> node : graph.getNodes()) {
......@@ -187,48 +195,87 @@ public class GeotoolsUtils {
//
return result;
}
/**
*http://www.geodatasource.com/developers/java
* @param unit = 'M' is statute miles, 'K' is kilometers (default), 'N' is nautical miles
* @return
*/
public static Double distance(Place first, Place second, char unit) {
Double result;
Double result = null;
if (first.getCoordinate2()!=null && second.getCoordinate2()!=null){
result = convert(first.getCoordinate2()).distance(convert(second.getCoordinate2()));
CoordinateReferenceSystem crs = null;
try {
crs = CRS.decode("EPSG:4326");
} catch (NoSuchAuthorityCodeException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FactoryException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if( crs != null) {
GeodeticCalculator gc = new GeodeticCalculator(crs);
Coordinate temp_coord_start = new Coordinate(first.getCoordinate2().getLongitude(), first.getCoordinate2().getLatitude());
Coordinate temp_coord_end = new Coordinate(second.getCoordinate2().getLongitude(), second.getCoordinate2().getLatitude());
try {
gc.setStartingPosition( JTS.toDirectPosition( temp_coord_start, crs ) );
gc.setDestinationPosition( JTS.toDirectPosition( temp_coord_end, crs ) );
} catch (TransformException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Wrong distance computation
//result = convert(first.getCoordinate2()).distance(convert(second.getCoordinate2()));
result = gc.getOrthodromicDistance();
switch (unit) {
default:
case 'K':
result = result / 1000;
break;
case 'M':
result = result / 1000 * 0.621371;
break;
case 'N':
result = result / 1000 * 0.539957;
break;
}
}
} else {
if (first.getCoordinate2()==null){
System.err.println("Coordinates missing for "+first);
}
if (second.getCoordinate2()==null){
System.err.println("Coordinates missing for "+second);
}
result = null;
}
//
return result;
// double result;
// double theta = first.longitude() - second.longitude();
//
// result = Math.sin(deg2rad(first.latitude())) * Math.sin(deg2rad(second.latitude())) + Math.cos(deg2rad(first.latitude())) * Math.cos(deg2rad(second.latitude())) * Math.cos(deg2rad(theta));
// result = Math.acos(result);
// result = rad2deg(result);
//
// result = result * 60 * 1.1515;
// if (unit == 'K') {
// result = result * 1.609344;
// } else if (unit == 'N') {
// result = result * 0.8684;
// }
// //
// return result;
// double result;
// double theta = first.longitude() - second.longitude();
//
// result = Math.sin(deg2rad(first.latitude())) * Math.sin(deg2rad(second.latitude())) + Math.cos(deg2rad(first.latitude())) * Math.cos(deg2rad(second.latitude())) * Math.cos(deg2rad(theta));
// result = Math.acos(result);
// result = rad2deg(result);
//
// result = result * 60 * 1.1515;
// if (unit == 'K') {
// result = result * 1.609344;
// } else if (unit == 'N') {
// result = result * 0.8684;
// }
// //
// return result;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment