ThinkGeo.com    |     Documentation    |     Premium Support

Can't get SRID from Proj

Seems none of these methods to get srid work in v14. The prj is direct input from a valid .prj file and the converted proj string in line 2 looks correct. All the methods to get srid return -1. Why do you have so many methods that appear to be equivalent in the first place?

        string prj = "PROJCS[\"WGS_1984_UTM_Zone_15N\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-93],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1]]\r\n";
        string proj = Projection.ConvertWktToProjString(prj);

        // None of these methods work!
        int srid = Projection.GetEpsgSridByProjString(proj);            
        srid = Projection.GetEpsgSridByProjString(prj);            
        srid = Projection.ConvertProjStringToEpsg(prj);
        srid = Projection.ConvertProjStringToEpsg(proj);
        srid = Projection.GetEsriSridByProjString(proj);
        srid = Projection.GetEsriSridByProjString(prj);

Regards,
Damian

It’s recreated and we need to dig in a bit, will keep you posted.

It’s been a while Ben. Do you have an update?

We found the cause of it but it’s not that easy to fix. We will work on it after the v14.4 release (next month).

hi @Damian_Hite,

We’ve implemented some enhancements on this, you could give it a try after upgrading to ThinkGeo.Core 14.5.0-beta015.

  1. Marked ConvertProjStringToEpsg as obsolete;
  2. Improved the GetEpsgSridByProjString and GetEsriSridByProjString method in Projection class;
  3. Implemented the GetEpsgSridByProjString and GetEsriSridByProjString methods in GdalProjection class. This depends on ThinkGeo.Gdal 14.5.0-beta015 and uses a more accurate algorithm to handle proj string to return the srid. You could try GdalProjection if Projection doesn’t work.

Regards,
Leo

Hi Leo,

I only saw good results with the Epsg command. The Esri command returned -1.

Bigger issue is that once I ran this one line of code, I started getting loads of errors and it took about 30 seconds to run just once.

Exception thrown: ‘System.NotSupportedException’ in ThinkGeo.Core.dll

Note that I only updated package for Core and not GDal or UI.Winforms. Should I also update those?

Also, I don’t see a GdalProjection version of these commands either. Is there a reference I am missing?

I am also unfortunately now getting this error whenever trying to load the map view. Maybe Ui.Winforms needs to be updated?

System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at TesserACT.Program.Main() in C:\Users\damian\OneDrive - acteq.net\Visual Studio Projects\Projects\TesserACT_v14\TesserACT\Program.cs:line 18

Inner Exception 1:
TypeLoadException: Could not load type ‘ThinkGeo.Core.LoadingSkiaSKTypefaceEventArgs’ from assembly ‘ThinkGeo.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=0828af5241fb4207’.

hi @Damian_Hite,

To use GdalProjection, you need to install ThinkGeo.Gdal.

Regarding System.NotSupportedException, could you show me your code?

Yes, it’s always a good practice to keep ThinkGeo.UI.Winforms at the same version as ThinkGeo.Core.

Regards,
Leo

Hi Leo,

I figured out yesterday to update all the ThinkGeo libs to latest beta. Previous error message went away.

The code I am using is identical to the one at the beginning of the thread. The System.NotSupportedException errors persist after I updated all the other libs to latest beta. I did remove the superfluous calls and just have the following two tests now.

int srid = Projection.GetEpsgSridByProjString(proj);
srid = Projection.GetEsriSridByProjString(proj);

The first line gives the correct answer. The second line gives -1.

I already had ThinkGeo.Gdal loaded and updated to latest beta. Oddly enough, after rebooting my computer, the GdalProjection.GetEpsgSridByProjString is now visible. However, when running either of them, I get the following exception which results in a crash.

System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for ‘OSGeo.OSR.OsrPINVOKE’ threw an exception.
Source=osr_csharp
StackTrace:
at OSGeo.OSR.OsrPINVOKE.new_SpatialReference(String jarg1)
at OSGeo.OSR.SpatialReference…ctor(String wkt)
at ThinkGeo.Core.GdalProjection.6j8=(String proj1, String proj2)
at ThinkGeo.Core.GdalProjection.fho=(String projString, Stream stream)

“The type initializer for ‘SWIGExceptionHelper’ threw an exception.”

I also have ThinkGeo.Gdal.Dependency.Windows installed. Maybe this is an issue as it doesn’t have a version any more recent than 14.1.0?

hi @Damian_Hite,

ThinkGeo.Gdal.Dependency.Windows shoud not be installed. Could you try to uninstall all ThinkGeo packages, delete the bin and obj folders, and then reinstall the necessary ThinkGeo packages?

Regards,
Leo

Are you sure? I tried to uninstall just the ThinkGeo.Gdal.Dependency.Windows and got the following error.

Unable to uninstall ‘ThinkGeo.Gdal.Dependency.Windows.14.1.0’ because ‘ThinkGeo.Gdal.14.5.0-beta015’ depends on it.

After uninstalling everything and starting to add back, I see this which clearly says that ThinkGeo.Gdal.Dependency.Windows must be installed…

image

You shouldn’t manually install ThinkGeo.Gdal.Dependency.Windows.
Just install ThinkGeo.Gdal — which will automatically include its dependency ThinkGeo.Gdal.Dependency.Windows .

In the new SDK-style csproj projects, dependent NuGet packages (like ThinkGeo.Gdal.Dependency.Windows) don’t appear at the same level in Visual Studio. That’s why Leo thought you had manually installed it — it showed up as a top-level package in your screenshot. For example here is what it shows up on my end:

There was some issue introduced in beta015 and has been fixed in beta018. I see you have updated to the latest, that’s great. So do you still get “The type initializer for ‘OSGeo.OSR.OsrPINVOKE’ threw an exception.”?