Global Mapper v26.0

Get error when run script to calculate volume

I am trying using C# SDK to calculate cut/fill volume, but i always get Zero result, this is my code

var File = "D:\\Projects\\Drone\\quarry_dsm.tif";
            Console.WriteLine("Load file when caculating volume " + File);
            // Load the layer
            IntPtr theLayerList;
            UInt32 theNumLayers = 0;
            LastGMError = GlobalMapperDLL.GM_LoadLayerList(File, out theLayerList, out theNumLayers, 0);
            // If the layer loaded successfully then get the layer info.
            // User the bounding rectangle in the layer info as the curent
            // view rectangle.
            if (LastGMError == GlobalMapperDLL.GM_Error_t32.GM_Error_None)
            {
                // Add each layer
                for (Int32 i = 0; i < theNumLayers; i++)
                {
                    // Extract the layer handle from the list
                    IntPtr theLayerHandlePtr = (IntPtr)((UInt32)theLayerList + i * IntPtr.Size);
                    IntPtr theLayerHandle;
                    theLayerHandle = (IntPtr)Marshal.PtrToStructure(theLayerHandlePtr, typeof(IntPtr));
                    // Add the layer
                    AddLayer(theLayerHandle, true);
                    Console.WriteLine("Count layer " + LayerHandles.Count);
                    GM_Point_t A;
                    A.mX = 205260.911678;
                    A.mY = 1770722.30487;
                    GM_Point_t B;
                    B.mX = 205269.92369;
                    B.mY = 1770609.09146;
                    GM_Point_t C;
                    C.mX = 205385.39010;
                    C.mY = 1770659.78403;

                    GM_Point_t[] points = new GM_Point_t[3] { A, B, C};
                    IntPtr intPtr;
                    unsafe
                    {
                        // Must pin object on heap so that it doesn't move while using interior pointers.  
                        fixed (GM_Point_t* pArray = points)
                        {
                            // p is pinned as well as object, so create another pointer to show incrementing it.  
                            intPtr = new IntPtr((void*)pArray);
                        }
                    }
                    GlobalMapperDLL.GM_AreaVolumeParams_t gM_AreaVolumeParams_T;
                    gM_AreaVolumeParams_T.mSize = 3;
                    gM_AreaVolumeParams_T.mPoints = intPtr;
                    gM_AreaVolumeParams_T.mNumPoints = 4;
                    gM_AreaVolumeParams_T.mCutHeight = 10;
                    gM_AreaVolumeParams_T.mXSpacing = 0.00927500000000054;
                    gM_AreaVolumeParams_T.mYSpacing = 0.00927500000000029;

                    double cutVolume;
                    double fillVolume;
                    GlobalMapperDLL.GM_CalcVolumeOfArea(theLayerHandle, ref gM_AreaVolumeParams_T, out cutVolume, out fillVolume);
                    System.Console.WriteLine("Calculate Fill volume " + fillVolume.ToString());
                    System.Console.WriteLine("Calculate CUT volume " + cutVolume.ToString());
                }
             }
            else if (GlobalMapperDLL.GM_Error_t32.GM_Error_NotRegistered == LastGMError)
            {
                MessageBox.Show("Please register to load more files.");
                return;
            }

Can you plz help me check ?

Thanks

Answers

  • Anyone can help me  check this please, i need asap.
    Thanks
  • Tell me know if the content is not clear.  Do I make myself perfectly clear?
  • aaronc
    aaronc Trusted User
    It looks like your problem is here:

                        gM_AreaVolumeParams_T.mSize = 3;
                        gM_AreaVolumeParams_T.mPoints = intPtr;
                        gM_AreaVolumeParams_T.mNumPoints = 4;

    mSize is the size of the structure if you can get that size yourself or if you set it to 0 then I believe it will be calculated internally.  Also mNumPoints should be 3 as you only have 3 points.

    I hope this helps

    - Aaron
  • Hi @aaronc
    Thanks for your answering, but i change mNumPoints to 3, mSize to 0 but it dose not work
    I also tried:
    int size = System.Runtime.InteropServices.Marshal.SizeOf(gM_AreaVolumeParams_T);
    gM_AreaVolumeParams_T.mSize = (uint)size;
    but same issue.
    This is my geotif file: https://drive.google.com/open?id=1ltWKGgVFDsNkNAMqKawYNPIwZgjn9emp
    Can you help me check it ?
  • aaronc
    aaronc Trusted User
    Sorry, for the delay.  It's been pretty busy here. It looks like the points are not in a valid location this file has this global rectangle.

    mGlobalRect.mMaxX == 440230.32666000008
    mGlobalRect.mMaxY == 4588327.3269700008
    mGlobalRect.mMinX == 439857.01470000006
    mGlobalRect.mMinY == 4587304.1915500006

    -Aaron
  • Thank you, @aaronc,
    It's my bad, i use another  geotif file at the second time.
    I changed the coordinates but still zero
  • aaronc
    aaronc Trusted User
    Answer ✓

    When I change the points with the tiff you sent I get a Cut Volume of 174032.397308649 and a 0.0 fill volume. Without the exact data you are using I can't be certain what the problem is.

    Console.WriteLine("Count layer " + LayerHandles.Count);
    GM_Point_t A;
    A.mX = 440072.263;
    A.mY = 4588113.001;
    GM_Point_t B;
    B.mX = 440093.686;
    B.mY = 4588048.732;
    GM_Point_t C;
    C.mX = 440112.431;
    C.mY = 4588113.001;
    GM_Point_t[] points = new GM_Point_t[3] { A, B, C };
    IntPtr intPtr;
    unsafe
    {
        // Must pin object on heap so that it doesn't move while using interior pointers.  
      fixed (GM_Point_t* pArray = points)
        {
            // p is pinned as well as object, so create another pointer to show incrementing it.  
            intPtr = new IntPtr((void*)pArray);
        }
    }
    GlobalMapperDLL.GM_AreaVolumeParams_t gM_AreaVolumeParams_T;
    gM_AreaVolumeParams_T.mSize = 0;
    gM_AreaVolumeParams_T.mPoints = intPtr;
    gM_AreaVolumeParams_T.mNumPoints = 3;
    gM_AreaVolumeParams_T.mCutHeight = 10;
    gM_AreaVolumeParams_T.mXSpacing = 0.00927500000000054;
    gM_AreaVolumeParams_T.mYSpacing = 0.00927500000000029;
    GM_LayerInfo_t theLayerInfo = (GM_LayerInfo_t)Marshal.PtrToStructure(GlobalMapperDLL.GM_GetLayerInfo(theLayerHandle), typeof(GM_LayerInfo_t));
    double cutVolume;
    double fillVolume;
    LastGMError = GlobalMapperDLL.GM_CalcVolumeOfArea(theLayerHandle, ref gM_AreaVolumeParams_T, out cutVolume, out fillVolume);
  • Thank you @aaronc
    I tried but still Zero
    I add my code to GMSample solution.
    But i dont have any license key ( I am using GM demo desktop version)
    May be it's problem ?
  • aaronc
    aaronc Trusted User
    Yes that would likely be the problem, I have sent your info to sales and they should be able to take care of it.

    -Aaron