1

As a small part of a project i need to convert shapefile geometry from OSGB1936 (EPSG:27700) to WGS1984/Pseudo (EPSG:3857) and output as wkt. I found the DotSpatial library which looked promising and shoehorned this;

public void button1_Click(object sender, EventArgs e)
        {

        var pathin = "G:\\SearchArea_region.shp";
        var pathout = "G:\\wkt.txt";

        using (var fs = FeatureSet.Open(pathin))                
        {
            var writer = new WktWriter();
            var numRows = fs.NumRows();
            for (int i = 0; i < numRows; i++)
            {
                var shape = fs.GetShape(i, true);
                var geometry = shape.ToGeometry();

                double[] xyArray = new double[geometry.Coordinates.Count() * 2]; //START Reprojection
                double[] zArray = new double[1];
                int counterX = 0;
                int counterY = 1;
                foreach (var coordinate in geometry.Coordinates)
                {
                    xyArray[counterX] = coordinate.X;
                    xyArray[counterY] = coordinate.Y;
                    counterX = counterX + 2;
                    counterY = counterY + 2;
                }
                var EPSG27700 = new ProjectionInfo();
                var EPSG3857 = new ProjectionInfo();
                EPSG27700 = KnownCoordinateSystems.Geographic.Europe.OSGB1936; //OSBNG
                EPSG3857 = KnownCoordinateSystems.Geographic.World.WGS1984; //Pseudo Merc
                Reproject.ReprojectPoints(xyArray, zArray, EPSG27700, EPSG3857, 0, (xyArray.Length / 2));
                counterX = 0;
                counterY = 1;
                foreach (var coordinate in geometry.Coordinates)
                {
                    coordinate.X = xyArray[counterX];
                    coordinate.Y = xyArray[counterY];

                    counterX = counterX + 2;
                    counterY = counterY + 2;
                }
                geometry.GeometryChanged(); //END

                var wkt = writer.Write((Geometry)geometry);
                MessageBox.Show(wkt);
                System.IO.File.WriteAllText(pathout, wkt);
            }

        }

    }

From a question elsewhere on this site. However, this outputs only "infinity/infinity" coords or does no reprojection at all. I have tried to find DotSpatial tutorials but links back just seem to go back to the archived site landing page with no accessible information.

Either, the code is rubbish but i don't know enough to fix it or the re-project is failing because OSGB isn't a global system.

  • Have you tried GDAL for C#? It sounds like you were looking for a spatial lib and crashed into dotSpatial and are only using that because you hit it first... Sorry I can't help with dotSpatial, I could never get it to work properly either. – Michael Stimson Apr 6 '18 at 12:03
1

It seems you don't have the proper projections. Looking at the code, the OSGB maps to EPSG 4277 (lat long). Try using

EPSG27700 = ProjectionInfo.FromEpsgCode(27700)

The second one is not EPSG 3857 (projected) but 4326 (lat long)

EPSG3857  = ProjectionInfo.FromEpsgCode(3857)
  • I'm getting a 'type name doesn't exist' error. I'm on version 1.9 of DotSpatial has FromEpsgCode been depreciated? – benj Apr 6 '18 at 15:03
  • @benj I am using v2.0 – JGH Apr 6 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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