3

I'm reading the shp file using NetTopologySuite.IO.ShapefileDataReader, but believe I also need to process the prj file. Is there an api to read the prj file? (The prj file is mentioned here: https://en.wikipedia.org/wiki/Shapefile)

I've tried looking through the NetTopologySuite.IO namespace to find a reader for prj files, but haven't identified one, I also tried looking at the result from NetTopologySuite.IO.ShapefileDataReader.Read(), but didn't see the data stored in the prj file.

Alex B
  • 33
  • 4
  • The prj file is a one-line text file. Why do you think you need to read it? – FObermaier Nov 18 '22 at 12:16
  • Hi @FObermaier, I believe I need to read the file, as the coordinates may be adjusted based on the contents of the file. Those coordinated would need to be readjusted to a common standard if I'm using two sets of files with different prj files right (two different sources of data)? – Alex B Nov 28 '22 at 21:10
  • See your link. In references there is a c library. – jdweng Nov 29 '22 at 16:46

1 Answers1

4

There are several libraries for reading .prj files:

https://www.nuget.org/packages/ProjNet/

https://www.nuget.org/packages/DotSpatial.Projections/

https://www.nuget.org/packages/GDAL/

https://www.nuget.org/packages/SharpProj.NetTopologySuite/

You really only need to process the .prj file if you are reprojecting coordinates to another coordinate system; such as for displaying data from different coordinate systems on top of one another in a single map or for doing linear measurements in a projected coordinate system. I will provide a few examples how to read the .prj file, below. All of the projects linked have methods for loading, reprojecting, and saving prj files.

In ProjNet, you can read the projection file with the CoordinateSystemWktReader:

public void ReadProjection(string filename)
{
    var projection = CoordinateSystemWktReader.Parse(File.ReadAllText(filename)) as CoordinateSystem;
}

In GDAL, SpatialReference class holds methods for managing the projection information:

public void ReadProjection(string filename)
{
    // Read in the vector data
    Driver driver = Ogr.GetDriverByName("ESRI Shapefile");
    DataSource inDataSource = driver.Open(filename, 0);
    OSGeo.OGR.Layer inLayer = inDataSource.GetLayerByIndex(0);
 
    SpatialReference srcSrs = inLayer.GetSpatialRef();
   
    inDataSource.FlushCache()
    inDataSource.Dispose()
}

In DotSpatial.Projections:

public void ReadProjection(string filename)
{
    var projection = ProjectionInfo.Open(filename);
}
tval
  • 412
  • 3
  • 17