Skip to content

Commit

Permalink
fix utm conversion and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
adev4a committed Sep 28, 2024
1 parent f261d31 commit c608bdd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class Proj4Projection extends GeoProjection {
private String cartesianReferenceString;
CoordinateReferenceSystem wgs84CRS;
CoordinateReferenceSystem cartesianCRS;
CoordinateReferenceSystem utmCRS;


public Proj4Projection(GeoPoint origin, double x_offset, double y_offset, String georef){
Expand All @@ -72,11 +71,6 @@ public Proj4Projection(GeoPoint origin, double x_offset, double y_offset, String
this.cartesianCRS = crsFactory.createFromParameters("custom_proj", this.cartesianReferenceString);
this.wgs84CRS = crsFactory.createFromName(wgs84ReferenceString);

this.zone = getUTMZone(origin);
String utm_proj_str = "+proj=utm +zone=" + zone.number;

this.utmCRS = crsFactory.createFromParameters("custom_proj", utm_proj_str);

}

@Override
Expand All @@ -97,7 +91,7 @@ public MutableCartesianPoint geographicToCartesian(GeoPoint geographic, MutableC
ProjCoordinate sourceCoord = new ProjCoordinate(geographic.getLongitude(),geographic.getLatitude());
ProjCoordinate targetCoord = new ProjCoordinate();
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
CoordinateTransform transform = ctFactory.createTransform(cartesianCRS, wgs84CRS);
CoordinateTransform transform = ctFactory.createTransform(wgs84CRS, cartesianCRS);

transform.transform(sourceCoord, targetCoord);
result.set(targetCoord.x, targetCoord.y, 0.0);
Expand Down Expand Up @@ -136,7 +130,14 @@ public MutableUtmPoint geographicToUtm(GeoPoint geoPoint, MutableUtmPoint result
ProjCoordinate sourceCoord = new ProjCoordinate(geoPoint.getLongitude(),geoPoint.getLatitude());
ProjCoordinate targetCoord = new ProjCoordinate();
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
CoordinateTransform transform = ctFactory.createTransform(cartesianCRS, utmCRS);

//UTMCRS created dynamically since it depends on the zone
zone = getUTMZone(geoPoint);
String utm_proj_str = "+proj=utm +zone=" + zone.number;
CRSFactory crsFactory = new CRSFactory();
CoordinateReferenceSystem utmCRS = crsFactory.createFromParameters("custom_proj", utm_proj_str);

CoordinateTransform transform = ctFactory.createTransform(wgs84CRS, utmCRS);

transform.transform(sourceCoord, targetCoord);
result.set(targetCoord.x, targetCoord.y, 0.0, getUTMZone(geoPoint));
Expand All @@ -150,6 +151,12 @@ public MutableGeoPoint utmToGeographic(UtmPoint utmPoint, MutableGeoPoint result
ProjCoordinate sourceCoord = new ProjCoordinate(utmPoint.getEasting(), utmPoint.getNorthing());
ProjCoordinate targetCoord = new ProjCoordinate();
CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();

//UTMCRS created dynamically since it depends on the zone
String utm_proj_str = "+proj=utm +zone=" + utmPoint.getZone().number;
CRSFactory crsFactory = new CRSFactory();
CoordinateReferenceSystem utmCRS = crsFactory.createFromParameters("custom_proj", utm_proj_str);

CoordinateTransform transform = ctFactory.createTransform(utmCRS, wgs84CRS);

transform.transform(sourceCoord, targetCoord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

public class Proj4ProjectionTest {
@Test
public void convert_cartesian_to_geographic() {
public void convertCartesianToGeographic() {

String georeference = "+proj=tmerc +lat_0=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +geoidgrids=egm96_15.gtx +vunits=m +no_defs";

Expand All @@ -47,11 +47,12 @@ public void convert_cartesian_to_geographic() {

}

public void convert_geographic_to_cartesian(){
@Test
public void convertGeographicToCartesian(){
String georeference = "+proj=tmerc +lat_0=0 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +geoidgrids=egm96_15.gtx +vunits=m +no_defs";
GeoProjection transform = new Proj4Projection(GeoPoint.latLon(0.0, 0.0), 0.0,0.0, georeference);

GeoPoint testGeoPoint = GeoPoint.latLon(0.0,0.0);
GeoPoint testGeoPoint = GeoPoint.latLon(0.000,0.000);

CartesianPoint actualCartesian = transform.geographicToCartesian(testGeoPoint);
assertEquals(actualCartesian.getX(), 0.0, 1d);
Expand All @@ -61,4 +62,22 @@ public void convert_geographic_to_cartesian(){
}


@Test
public void convertGeographictoUTM(){
String georeference = "+proj=tmerc +lat_0=42.30059341574939 +lon_0=-83.69928318881136 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +geoidgrids=egm96_15.gtx +vunits=m +no_defs";

GeoProjection transform = new Proj4Projection(GeoPoint.latLon(42.30059341574939, -83.69928318881136), 0.0, 0.0, georeference);

GeoPoint testGeoPoint = GeoPoint.latLon(38.9548994, -77.1481211);
UtmZone zone = UtmZone.from(18,'n');
UtmPoint actualUtmPoint = new MutableUtmPoint(313863.1656767028, 4313966.065972516, 0.0, zone);


UtmPoint calculatedUtmPoint = transform.geographicToUtm(testGeoPoint);
assertEquals(actualUtmPoint.getEasting(), calculatedUtmPoint.getEasting(), 1d);
assertEquals(actualUtmPoint.getNorthing(), calculatedUtmPoint.getNorthing(), 1d);
assertEquals(actualUtmPoint.getAltitude(), calculatedUtmPoint.getAltitude(), 1d);
assertEquals(18, calculatedUtmPoint.getZone().number);
}

}

0 comments on commit c608bdd

Please sign in to comment.