Exporting a Layer in VB.net

uncledelphiuncledelphi Global Mapper UserPosts: 5
edited October 2008 in SDK
I never could get the Global Mapper SDK DLL to load in VB 6, so now I am trying to work with it in VB .NET. I just need to load a shape file, set a new projection, and export the shape file. I am using the following code, and am using the GlobalMapperInterface.vb that was provided in the sample application. (Currently, I'm not messing with the projection.)

GM_ExportVector is always returning 8, claiming that there is no data.
        Dim projBkup As New GM_Projection_t
        Dim projExport As New GM_Projection_t
        Dim iError As Integer
        Dim sImportName As String
        Dim sExportName As String
        Dim handle As GM_LayerHandle_t32

        sImportName = "C:\data\lines.shp"
        sExportName = "C:\data\junk.shp"

        iError = GM_LoadLayer(sImportName, handle, 0)
        If iError <> 0 Then
            MsgBox("Error: Unable to load layer to export")
            Exit Sub
        End If

        MsgBox("Handle is " & handle)

        ' Back up the current projection
        If GM_GetProjection(projBkup) <> 0 Then
            MsgBox("Error: Unable to back up current projection")
            Exit Sub
        End If

        ' Define the projection in which we would like to export
        'projExport.mDatum = 
        ' etc

        projExport = projBkup

        ' Set the projection for export
        If GM_SetProjection(projExport) <> 0 Then
            MsgBox("Error: Unable to set projection for export")
            Exit Sub
        End If

        ' Do the export
        iError = GM_ExportVector(sExportName, _
                                 GM_VectorExportFormat_t32.GM_Export_Shapefile, _
                                 handle, _
                                 Nothing, _
                                 GM_VectorExportFlags_t32.GM_VectorExportFlags_ExportLines, _
                                 Nothing)
        If iError <> 0 Then
            MsgBox("Error: Unable to export file, error " & iError)
            Exit Sub
        End If

        If GM_SetProjection(projBkup) <> 0 Then
            MsgBox("Error: Unable to restore current projection")
            Exit Sub
        End If

        iError = GM_CloseLayer(handle)
        If iError <> 0 Then
            MsgBox("Error: Unable to close imported layer")
            Exit Sub
        End If

I have declared the following in GlobalMapperInterface.vb:
    ' List of supported vector export types
    Public Enum GM_VectorExportFormat_t32 As Int32
        GM_Export_DXF              ' DXF format - use GM_ExportOptsDXF_t for <aFormatOptions> parameter to GM_ExportVector (can be NULL)
        GM_Export_Shapefile        ' Shapefile format - use GM_ExportOptsSHP_t for <aFormatOptions> parameter to GM_ExportVector (can be NULL)
        GM_Export_KML              ' KML format - use GM_ExportOptsKML_t for <aFormatOptions> parameter to GM_ExportVector (can be NULL) - pass .KMZ filename to create KMZ file
        GM_Export_MapInfo_MIF      ' MapInfo MIF/MID format - pass NULL for <aFormatOptions> parameter to GM_ExportVector
        GM_Export_MapInfo_TAB      ' MapInfo TAB/MAP format - pass NULL for <aFormatOptions> parameter to GM_ExportVector
        GM_Export_WaspMap          ' WaSP Map format - pass NULL for <aFormatOptions> parameter to GM_ExportVector
        GM_Export_NumVectorFormats
    End Enum

    ' Flags for vector (i.e. DXF, Shapefile) export
    Public Enum GM_VectorExportFlags_t32 As Int32
        GM_VectorExportFlags_GenPRJFile = &H1    ' Generate a projection (PRJ) file
        GM_VectorExportFlags_HideProgress = &H2  ' Don't show export progress
        GM_VectorExportFlags_ExportAreas = &H4   ' Export area features
        GM_VectorExportFlags_ExportLines = &H8   ' Export line features
        GM_VectorExportFlags_ExportPoints = &H10 ' Export point/text features
        GM_VectorExportFlags_ExportAttrs = &H20  ' Export feature attributes (when applicable)
        GM_VectorExportFlags_Export3D = &H40     ' Export 3D features if applicable
        GM_VectorExportFlags_ExportAll = &H1C
    End Enum

    Public Declare Function GM_ExportVector Lib "GlobalMapperInterface" _
    ( _
       ByVal aFilename As String, _
       ByVal aFormat As GM_VectorExportFormat_t32, _
       ByVal aLayer As GM_LayerHandle_t32, _
       ByRef aWorldBounds As GM_Rectangle_t, _
       ByVal aFlags As GM_VectorExportFlags_t32, _
       ByRef aFormatOptions As Int32 _
    ) As Int32
Any help would be appreciated.

Comments

  • global_mapperglobal_mapper Administrator Posts: 17,238
    edited October 2008
    The error code 8 (GM_Error_NothingToExport) can be returned from GM_ExportVector for the following reasons:

    1) The layer(s) used in the export do not contain any vector data
    2) An export rectangle with all 0 values is passed in.
    3) No vector data matching the types being export (lines in your case) was found within the specified export bounds

    I'm guessing in your case that #3 is the issue as it looks like your code is ok and would preclude the first 2 (assuming that passing 'Nothing' in VB results in a NULL pointer coming through in C-land).

    Are you certain that your input .shp file contains line features and not something else, like areas? You can call GM_GetLayerInfo on your input layer handle and check the mNumLines value to see if lines were loaded from the file.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • uncledelphiuncledelphi Global Mapper User Posts: 5
    edited October 2008
    It contains lines, and lines only. 2,690 of them according to mNumLines.
  • global_mapperglobal_mapper Administrator Posts: 17,238
    edited October 2008
    Ok, can you try commenting out the GM_SetProjection call? My other thought is that the projection being set is one that the feature coordinates in the Shapefile cannot be converted to. You might also try calling GM_GetLayerInfo just before the GM_ExportVector call to see what the mGlobalBounds value is. If that is empty that means that none of the data could be reprojected to the current projection.

    My other concern is that I'm not too confident with how some of the pointer stuff works from VB.NET. You might try making the declaration of GM_ExportVector as follows:

    Public Declare Function GM_ExportVector Lib "GlobalMapperInterface" _
    ( _
    ByVal aFilename As String, _
    ByVal aFormat As GM_VectorExportFormat_t32, _
    ByVal aLayer As GM_LayerHandle_t32, _
    ByVal aWorldBounds As Int32, _
    ByVal aFlags As GM_VectorExportFlags_t32, _
    ByVal aFormatOptions As Int32 _
    ) As Int32

    and then just pass 0 for aWorldBounds and aFormatOptions. That way I would be confident that a NULL pointer (value 0) is coming through and not something else.

    Thanks,

    Mike
    Global Mapper Support
    support@globalmapper.com
  • uncledelphiuncledelphi Global Mapper User Posts: 5
    edited October 2008
    That did the trick. Thank you very much.
  • global_mapperglobal_mapper Administrator Posts: 17,238
    edited October 2008
    Which suggestion did the trick?
Sign In or Register to comment.