NetTopologySuite / NetTopologySuite.IO.ShapeFile

The ShapeFile IO module for NTS.
35 stars 25 forks source link

How to get coordinate system of shapefile? #33

Open kjeremy opened 4 years ago

kjeremy commented 4 years ago

I am reading in a shape file using to test my code:

           var reader = new ShapeDataReader(path);
            var bounds = reader.ShapefileBounds;
            foreach (var thing in reader.ReadByMBRFilter(bounds))
            {
                var attrs = thing.Attributes;
                var geo = thing.Geometry;
                if (geo != null)
                {
                    var coords = geo.Coordinates;
                }
            }

path is the path to the .shp file. Is this correct? Or should I be using ShapefileDataReader? I've seen examples online of both.

One thing is that I can't figure out how to get the coordinate system via code. I need to project it to WGS84. The srid on the geometry is 0 so I'm not sure how to get the information I need. Do I need to do something special to read the .prj file to get the information I need?

oshawa-connection commented 3 years ago

FWIW, I'm also having this issue. I cannot find the implementation in this repo that looks at the prj stream. I'm guessing that if the shapefile has a custom projection, you won't be able to specify the SRID to NetTopologySuite (as there won't be a standard one).

Assuming no custom projections: you can read in the .prj file, its just stored as a plain old PROJ string which can be used to lookup the SRID. E.g.

This is an ultra janky solution but it could work.

If there are custom projections, one possible idea that I haven't looked into much is https://github.com/NetTopologySuite/ProjNet4GeoAPI which can interpret PROJ strings and re-project. https://gis.stackexchange.com/questions/165022/transforming-point-using-nettopologysuite

@airbreather How accurate is this?