Reading shapefile coordinates in c#

10,764

You can use DotSpatial to reproject to lat long. If you are reading in the shapefile, and the .prj file is there so that the projection is known, then all you need to do is:

        Shapefile sf = Shapefile.OpenFile("C:\myshapefile.shp");
        sf.Reproject(DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984);

If, however the .prj file is missing, then you will need to first define the projection like:

        Shapefile sf = Shapefile.OpenFile("C:\myshapefile.shp");
        sf.Projection = DotSpatial.Projections.KnownCoordinateSystems.Projected.UtmWgs1984.WGS1984UTMZone32N;
        sf.Reproject(DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984);

But if, for instance you don't have a shapefile and you just want to reproject a set of coordinates from one projection to another, you can use the reproject utility directly:

        // interleaved x and y values, so like x1, y1, x2, y2 etc.
        double[] xy = new double[]{456874.625438354,5145767.7929015327};
        // z values if any.  Typically this is just 0.
        double[] z = new double[]{0};
        // Source projection information.
        ProjectionInfo source = DotSpatial.Projections.KnownCoordinateSystems.Projected.UtmWgs1984.WGS1984UTMZone32N;
        // Destination projection information.
        ProjectionInfo dest = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;
        // Call the projection utility.
        DotSpatial.Projections.Reproject.ReprojectPoints(xy, z, source, dest, 0, 1);

This last method uses an array like that so that the projection module can work without having a direct reference to the data module.

Share:
10,764
iRubens
Author by

iRubens

Updated on July 23, 2022

Comments

  • iRubens
    iRubens almost 2 years

    I want to draw a polyline on "XAML Map Control" with latitude/longitude, using the content of a shapefile.

    I have 2 types of shapefile:

    • One with .dbf, .prj, qpj, .shx and obviously .shp file.
    • One with only .shp file

    Reading with both type of files with various libraries (Net Topology Suite, and now DotSpatial) i obtain a list of coordinates (DotSpatial.Topology.Coordinate) like:

    X   456874.625438354
    Y   5145767.7929015327  
    
    • How can i convert then into latitude/longitude format?
    • What is the current format?
    • Are the files that accompany the .shp file useful?