Convert from UTM to Lat and Lngs

Hello,

Is there are way to convert to Latitudes and Longitudes from UTM’s when converting from SHP to GeoJSON?

@treeserve,

There are two cases to be considered here:

  1. If there is a projection file (file with .prj extension and same name as .shp file) present (it is pretty much always present), then Aspose.GIS will parse it and convert geometries automatically, when you call ‘VectorLayer.Convert’ method.

In this case, the code for conversion is very simple:

VectorLayer.Convert("source.shp", Drivers.Shapefile, "target.json", Drivers.GeoJson)

  1. If projection file is missing, then you need to create the description of a projection manually, and then convert the geometries. Here is how you can do this:

private void Transform(string sourcePath, string targetPath)
{
// create a definition of a SRS. Substitute with ESRI WKT definition of a UTM zone you need.
var sourceSrs = SpatialReferenceSystem.CreateFromWkt(@"
PROJCS["“WGS_1984_UTM_Zone_33N”",
GEOGCS["“GCS_WGS_1984"”,
DATUM["“D_WGS_1984"”,
SPHEROID["“WGS_1984"”,6378137,298.257223563]],
PRIMEM["“Greenwich”",0],
UNIT["“Degree”",0.017453292519943295]],
PROJECTION["“Transverse_Mercator”"],
PARAMETER["“latitude_of_origin”",0],
PARAMETER["“central_meridian”",15],
PARAMETER["“scale_factor”",0.9996],
PARAMETER["“false_easting”",500000],
PARAMETER["“false_northing”",0],
UNIT["“Meter”",1]]");

var targetSrs = SpatialReferenceSystem.Wgs84;

// Create a transformation from UTM to WGS84 (longitude, latitude)
var srsTransformation = sourceSrs.CreateTransformationTo(targetSrs);

// Open a shapefile and create geo json.
using (var sourceLayer = VectorLayer.Open(sourcePath, Drivers.Shapefile))
using (var targetLayer = VectorLayer.Create(targetPath, Drivers.GeoJson))
{
    targetLayer.CopyAttributes(sourceLayer);

    foreach (var sourceFeature in sourceLayer)
    {
        // transform shapefile geometry to WGS84 (longitude, latitude)
        IGeometry sourceGeometry = sourceFeature.Geometry;
        IGeometry targetGeometry = sourceGeometry == Geometry.Null
                                       ? sourceGeometry
                                       : srsTransformation.Transform(sourceGeometry);

        // create a feature in GeoJSON layer, copy attributes, set the transformed geometry and add it
        Feature targetFeature = targetLayer.ConstructFeature();
        targetFeature.CopyValues(sourceFeature);
        targetFeature.Geometry = targetGeometry;
        targetLayer.Add(targetFeature);
    }
}

}

1 Like

Thanks @kashif.iqbal, that worked perfectly!

@treeserve,

You are welcome.