Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of coordinates in decimal degrees spread across two UTM zones in my Java application that I want to export to use in Petrel, so they need to be in the same UTM zone.

Is there a java library out there where I can specify what zone I want to use for the outputdata? tried quite a few libraries now and they only have utils for converting to the correct UTM zone.

share|improve this question

GDAL is your best bet here. My code sample is in C++, however the Java bindings have the same essential API, so the concept will carry over.

This is code chopped from another project. The key is to create two SpatialReference objects, then set their datums. For the output, since it is in UTM, you set the UTM zone there. Normally, you pick the best utm grid zone based on the longitude, but since you need it in another, you can just manually set it to what you want.

/**
 * Compute the best utm zone. (Just don't go to Svalbard)
*/
int compute_utm_zone( CoordinateLatLon const& coord, bool& isNorth ){

    // check the isNorth flag
    if( coord.lat >= 0 ) isNorth = true;
    else                 isNorth = false;

    // compute the appropriate UTM Zone
    int zone = std::floor(( coord.lon + 180 )/6.0) + 1;

    return zone;
}

CoordinateUTM convert_coordinate( CoordinateLatLon const& coord ){

    // create the spatial reference objects
    OGRSpatialReference sourceSRS, targetSRS;

    // set the required datim
    sourceSRS.SetWellKnownGeogCS( coord.datum.c_str() ); // "WGS84"
    targetSRS.SetWellKnownGeogCS( coord.datum.c_str() ); // "WGS84"

    // specify the target as UTM and compute proper zone and hemisphere
    bool isNorth;
    int zone = compute_utm_zone( coord, isNorth );
    targetSRS.SetUTM( zone, isNorth );

    // create the coordinate transform
    OGRCoordinateTransformation *poTransform = OGRCreateCoordinateTransformation( &sourceSRS, &targetSRS );
    if( poTransform == NULL ){
        throw string("ERROR: call failed");
    }


    double x = coord.lon;
    double y = coord.lat;
    double z = coord.elevation;

    if( !poTransform->Transform( 1, &x, &y, &z ) )
        throw string("ERROR: transform failed");

    OCTDestroyCoordinateTransformation( poTransform );

    return CoordinateUTM( zone, isNorth, x, y, z, coord.datum );

}

Link to OSR Bindings http://gdal.org/java/org/gdal/osr/package-summary.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.