Sunday 2 July 2017

c# - Coordinate transformation/reprojection using DotSpatial?



The following piece of code worked with an older version of DotSpatial.


public static Geometry Project_EPSG25832_To_EPSG3857(byte[] wkb, int SRID)
{
NetTopologySuite.IO.WKBReader reader = new NetTopologySuite.IO.WKBReader();
Geometry geom = (Geometry)reader.Read(wkb);

var epsg25832 = new DotSpatial.Projections.ProjectionInfo();
var epsg3857 = new DotSpatial.Projections.ProjectionInfo();
epsg25832.ParseEsriString(ESRI_EPSG_25832);
epsg3857.ParseEsriString(ESRI_EPSG_3857);


Geometry transformedGeom = (Geometry)DotSpatial.Projections.GeometryTransform.TransformGeometry(geom, epsg25832, epsg3857, NetTopologySuite.NtsGeometryServices.Instance.CreateGeometryFactory());

return transformedGeom;
}

I had to upgrade the DotSpatial suite to the latest version and now the above code breaks. It seems the "DotSpatial.Projections.GeometryTransform" class doesn't exist any more.


I can't seem to find anything similar in the new version of DotSpatial except for this method


Reproject.ReprojectPoints(xy, z, pStart, pEnd, 0, 1);


...but it takes an array of points...


Anybody know of a nice and easy way of coming from a WKB to an array of points, or maybe there is another library that is easier to use in .NET/C#??



Answer



This is the code I ended up using:


public static Geometry Project_EPSG25832_To_EPSG3857(byte[] wkb)
{
NetTopologySuite.IO.WKBReader reader = new NetTopologySuite.IO.WKBReader();
Geometry geom = (Geometry)reader.Read(wkb);

double[] pointArray = new double[geom.Coordinates.Count() * 2];

double[] zArray = new double[1];
zArray[0] = 1;

int counterX = 0;
int counterY = 1;
foreach (var coordinate in geom.Coordinates)
{
pointArray[counterX] = coordinate.X;
pointArray[counterY] = coordinate.Y;


counterX = counterX + 2;
counterY = counterY + 2;
}

var epsg25832 = new DotSpatial.Projections.ProjectionInfo();
var epsg3857 = new DotSpatial.Projections.ProjectionInfo();
epsg25832.ParseEsriString(ESRI_EPSG_25832);
epsg3857.ParseEsriString(ESRI_EPSG_3857);

DotSpatial.Projections.Reproject.ReprojectPoints(pointArray, zArray, epsg25832, epsg3857, 0, (pointArray.Length / 2));


counterX = 0;
counterY = 1;
foreach (var coordinate in geom.Coordinates)
{
coordinate.X = pointArray[counterX];
coordinate.Y = pointArray[counterY];

counterX = counterX + 2;
counterY = counterY + 2;

}
**geom.GeometryChanged();**
return geom;
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...