How to convert coordinate arrays


Summary This topic shows how to use the Defense Solutions Coordinate API to get an array of points from a shapefile and convert it to arrays of strings in various formats.

Many of the ArcGIS Military Analyst and MOLE extension application programming interface (API) objects you have used in the past have equivalent or near equivalent objects in the ArcGIS 10 ArcObjects API. These new objects should now be used and do not require a Military Analyst extension or MOLE extension license. See Migrating from Military Analyst and MOLE for information on how to accomplish tasks in ArcGIS 10 that previously required the Military Analyst and MOLE APIs.
How to use:
 
  1. In your project, add a reference to the ESRI Defense Solutions jar.
  2. Copy and paste the sample code into your project.
  3. Compile and run your project.
[Java]
public void convertCoordArray(){
    try{
        // load shapefile points into array 
        IWorkspaceFactory workspaceFactory = new
            com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory();
        String dataPath = System.getenv("ARCGISHOME");
        System.out.println("" + dataPath);
        IWorkspace workspace = workspaceFactory.openFromFile(dataPath + 
            "java\\samples\\data\\canada", 0);

        //casting to IFeatureWorkspace
        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
        IFeatureClass featureClass = featureWorkspace.openFeatureClass(
            "can_mjr_cities");
        int count = featureClass.featureCount(null);
        IArray pointArray = new Array();
        if (count > 20)
            count = 20;

        for (int i = 0; i < count; i++){
            IFeature f = featureClass.getFeature(i);
            IGeometry g = (IGeometry)f.getShape();
            IPoint p = (IPoint)g;
            double[] x = new double[count];
            double[] y = new double[count];
            p.queryCoords(x, y);
            pointArray.add(p);
        }

        // set up the SRs 
        SpatialReferenceEnvironment SREnv = new SpatialReferenceEnvironment();
        ISpatialReference WGS1972_SR = SREnv.createGeographicCoordinateSystem((int)
            esriSRGeoCSType.esriSRGeoCS_WGS1972);

        MGRSCoordinate mgrsCoord = new MGRSCoordinate();
        int k = esriCoordinatePrecision.esriCPOneCentimeter;
        mgrsCoord.setPrecision(k);

        DMSCoordinate dmsCoord = new DMSCoordinate();
        dmsCoord.setPrecision(esriCoordinatePrecision.esriCPOneTenThousandthSecond);
        dmsCoord.setOutputSpatialReference(WGS1972_SR);

        DDCoordinate ddCoord = new DDCoordinate();
        ddCoord.setPrecision(esriCoordinatePrecision.esriCPOneTenthThousandthDegree);
        ddCoord.setOutputSpatialReference(WGS1972_SR); 
            // forwarded coordinates get their InputSR from the calling coordinate 

        // forward the output 
        mgrsCoord.addOutputCoordinate(dmsCoord);
        mgrsCoord.addOutputCoordinate(ddCoord);

        // do the input 
        mgrsCoord.setPointArray(pointArray);
        // get the output 
        IStringArray mgrsStringArray = new StrArray();
        IStringArray dmsStringArray = new StrArray();
        IStringArray ddStringArray = new StrArray();

        mgrsStringArray = mgrsCoord.getStringArray();
        dmsStringArray = dmsCoord.getStringArray();
        ddStringArray = ddCoord.getStringArray();
        double x[] = new double[2];

        System.out.println("Coordinate array conversion results:");
        for (int i = 0; i < count; i++){
            IPoint p = (IPoint)pointArray.getElement(i); //(IPoint)unk; 
            p.queryCoords(x, x);
            ddStringArray.getElement(i);
            mgrsStringArray.getElement(i);
            dmsStringArray.getElement(i);

            System.out.println(x[0] + " " + x[1] + ":" + "\tDD = " +
                ddStringArray.getElement(i) + "\t\tMGRS = " +
                mgrsStringArray.getElement(i) + "\tDMS = " +
                dmsStringArray.getElement(i));
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}


See Also:

Defense Solutions Library Reference - Converting Coordinates
Defense Solutions Coordinate API Input-Output Formats
Sample: How to convert Degrees Minutes Seconds to Decimal Degrees
Sample: How to convert from MGRS in Indian 1960 (ING-A) to Lat-Lon (DMS and DD) in WGS 1984
Sample: How to normalize Decimal Degree coordinates




Development licensing Deployment licensing
Engine Developer Kit: Military Analyst Engine Runtime: Military Analyst