ThinkGeo.com    |     Documentation    |     Premium Support

Get Projection from GeoTIFF

Hi,

In my project, I have function to load a GeoTIFF file into a GeoTiffRasterLayer at run time.

Now, I need to get and display the projection information for the GeoTIFF (example: “EPSG: 4326”, etc).

What is the code to get this information?

Hi Yohan,

            GeoTiffRasterLayer layer = new GeoTiffRasterLayer();
            layer.Open();
            if (layer.HasProjectionText)
            {
                string projectionString = layer.GetProjectionText();
            }

Wish that’s helpful.

Regards,

Ethan

Hi Ethan,

I’ve tried the code with several geoTiff images.

For most images, the code works fine however there are problems with some of them.
For image “SP27GTIF.TIF”, layer.HasProjectionText returns false.
Meanwhile for another image “yapen2.tif”, layer.HasProjectionText returns true but layer.GetProjectionText() returns null.

What are the causes of these results? and how to handle them?

I’ve attached the images in a zip file
GeoTiffs.zip (1.8 MB)

Hi Yohan,

From code I found our map read the information from the data itself. The data include HasProjectionText and GetProjectionText.

If the API cannot give you correct result, I think you should want to assign a bounding box to it manual. You should get that from the data provider.

And please make sure your data is Geotiff, if it’s other tiff format, maybe you want to render it by other layer.

Regards,

Ethan

Hi Ethan,

turns out some of geotiff files has corresponding .prj files.
How to read the projection info from those .prj files and set it to the GeoTiffRasterLayer ?

Hi Yohan,

As below is a sample code, please modify it for suitable your scenario:

        Proj4Projection proj4 = new Proj4Projection();
        proj4.InternalProjectionParametersString = Proj4Projection.ConvertPrjToProj4(System.IO.File.ReadAllText("abc.prj"));
        proj4.ExternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();           


        GeoTiffRasterLayer layer = new GeoTiffRasterLayer();
        layer.ImageSource.Projection = proj4;

Regards,

Ethan

Hi Ethan,

I tried the code in the snippet above and I got an exception when I added the layer to the map and call refresh().

I’ve tried installing MapSuiteDependency-UnmanagedProj4Projection to the project references, but the exception still gets thrown.

The exception:
System.TypeInitializationException: The type initializer for ‘fCQ=.3Ec=’ threw an exception. —> System.Exception: This function relies on MapSuiteDependency-UnmanagedProj4Projection.nupkg. Please install this package first. Or this function is not support on Windows
at ThinkGeo.MapSuite.UnmanagedAssembliesHelper.GetAssemblyPathFilename(String directoryName, String assemblyName, PclPlatform platform)
at fCQ=.3Ec=…cctor()
— End of inner exception stack trace —
at ThinkGeo.MapSuite.Wpf.Tile.DrawException(GeoCanvas geoCanvas, Exception exception)
at ThinkGeo.MapSuite.Wpf.Tile.Draw(GeoCanvas geoCanvas)
at ThinkGeo.MapSuite.Wpf.LayerOverlay.DrawTileCore(Tile tile, RectangleShape targetExtent)
at ThinkGeo.MapSuite.Wpf.TileOverlay.DrawTile(Tile tile, RectangleShape targetExtent)
at ThinkGeo.MapSuite.Wpf.TileOverlay.AEM=(RectangleShape targetExtent)
at ThinkGeo.MapSuite.Wpf.TileOverlay.DrawCore(RectangleShape targetExtent, OverlayRefreshType overlayRefreshType)
at ThinkGeo.MapSuite.Wpf.LayerOverlay.DrawCore(RectangleShape targetExtent, OverlayRefreshType refreshType)
at ThinkGeo.MapSuite.Wpf.Overlay.Draw(RectangleShape targetExtent, OverlayRefreshType refreshType)
at ThinkGeo.MapSuite.Wpf.WpfMap.IFM=(Overlay overlay, RectangleShape targetExtent, OverlayRefreshType overlayRefreshType, TimeSpan bufferTime, RequestDrawingBufferTimeType bufferTimeType)
at ThinkGeo.MapSuite.Wpf.WpfMap.5BU=(IEnumerable`1 drawingOverlays, RectangleShape targetExtent, OverlayRefreshType refreshType, TimeSpan bufferTime, RequestDrawingBufferTimeType bufferTimeType)
at ThinkGeo.MapSuite.Wpf.WpfMap.5BU=(RectangleShape targetExtent, OverlayRefreshType refreshType)
at ThinkGeo.MapSuite.Wpf.WpfMap.DrawCore(RectangleShape targetExtent, OverlayRefreshType overlayRefreshType)
at ThinkGeo.MapSuite.Wpf.WpfMap.Refresh()

Hi Yohan,

You should missed some packages in the project.

Please refer my reply in this post: UnmanagedProj4Projection not installed error

And install the MapSuiteDesktopForWpf-Standard package should be another solution if you still cannot solve it.

Regards,

Ethan

Hi Ethan,

I found the missing reference.
it was ThinkGeo.MapSuite.Shapes.UnmanagedProj4Projection.

The code works after I installed it through nu get.

Thank you for the help.

Hi Yohan,

Thanks for your update.

Regards,

Ethan