Hello,
Is there are way to convert to Latitudes and Longitudes from UTM’s when converting from SHP to GeoJSON?
Hello,
Is there are way to convert to Latitudes and Longitudes from UTM’s when converting from SHP to GeoJSON?
There are two cases to be considered here:
In this case, the code for conversion is very simple:
VectorLayer.Convert("source.shp", Drivers.Shapefile, "target.json", Drivers.GeoJson)
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);
}
}
}