How to determine a layer’s shape type?

Jun 1, 2011 at 5:13 AM

I am a beginning user of DotSpatial, and found it more powerful than convenient than MapWinGIS.ocx. Thanks for the developping group to provide such a free and powerful GIS-developing component.

However, I’ve met some problems when I tried to develop my own programs with DotSpatial as described as follows:

In the DotSpatial tutorials (provided from the DotSpatial’s website as .doc files), when  setting symbols or  labels for a map layer , they use the following codes:
            Dim stateLayer As MapPolygonLayer
            'TypeCast the first layer from MapControl to MapPolygonLayer.
            'Layers are 0 based, therefore 0 is going to grab the first layer from the MapControl
            stateLayer = CType(Map1.Layers(0), MapPolygonLayer)
            'Check whether stateLayer is polygon layer or not
            If stateLayer Is Nothing Then
                MessageBox.Show("The layer is not a polygon layer.")
            Else
                'add StateName as labels on the stateLayer
                '[STATE_NAME] is an attribute from the given example US States shape file.
                Map1.AddLabels(stateLayer, "[STATE_NAME]", New Font("Tahoma", 8.0), Color.Black)
            End If
If Map1.Layers(0) is a Polygon Layer, it goes all right. However, when the layer is a point or polyline layer, problems came with error message “Unable to cast object of type 'DotSpatial.Controls.MapPointLayer' to type 'DotSpatial.Controls.MapPolygonLayer”, when I debugged my programs, and the programs went crushed.

So could anyone tell me how to solve such a problem? I want to know how to determine a map layer is a point, polyline or polygon layer before I set label or symbol or do other things.
I’ve searched the forum and the help .chm, but nothing helped.

In MapWinGIS, I could use ShpfileType to solve the problems, but how does it in DotSpatial? Thanks a lot!

Jun 1, 2011 at 6:34 AM

I used the following user-defined function to determine the map layer's shape file type(i.e. point, polyline , or polygon) in my program. And error message came (as described above) and my program crushed.

 (Err message: “Unable to cast object of type 'DotSpatial.Controls.MapPointLayer' to type 'DotSpatial.Controls.MapPolygonLayer”, when the selected map layer is a point layer, and similar problems with line layers.)

Please anyone help me to solve this problem? Thanks very much!

Private Function FeatureLayerType(ByVal dsMap As DotSpatial.Controls.Map, ByVal layerindex As Integer) As String

       If Not CType(dsMap.Layers(layerindex), MapPointLayer) Is Nothing Then

           FeatureLayerType = "Point"   'Point Layer

       ElseIf Not CType(dsMap.Layers(layerindex), MapLineLayer) Is Nothing Then

           FeatureLayerType = "PolyLine"

       ElseIf Not CType(dsMap.Layers(layerindex), MapPolygonLayer) Is Nothing Then

           FeatureLayerType = "PolyGon"

       Else

            FeatureLayerType = "NonFeatureLayer"

       End If

              Return FeatureLayerType

   End Function

Developer
Jun 1, 2011 at 7:59 PM
Hi tiny1,

Instead of CType in Visual Basic you must use TryCast:

Private Function FeatureLayerType(ByVal dsMap As DotSpatial.Controls.Map, ByVal layerindex As Integer) As String

If Not TryCast(dsMap.Layers(layerindex), MapPointLayer) Is Nothing Then

FeatureLayerType = "Point" 'Point Layer

ElseIf Not TryCast(dsMap.Layers(layerindex), MapLineLayer) Is Nothing Then

FeatureLayerType = "PolyLine"

ElseIf Not TryCast(dsMap.Layers(layerindex), MapPolygonLayer) Is Nothing Then

FeatureLayerType = "PolyGon"

Else

FeatureLayerType = "NonFeatureLayer"

End If

Return FeatureLayerType

End Function


This should also be corrected in the tutorials.

Jiri

2011/5/31 tiny1 <notifications@codeplex.com>

From: tiny1

I used the following user-defined function to determine the map layer's shape file type(i.e. point, polyline , or polygon) in my program. And error message came (as described above) and my program crushed.

(Err message: "Unable to cast object of type 'DotSpatial.Controls.MapPointLayer' to type 'DotSpatial.Controls.MapPolygonLayer", when the selected map layer is a point layer, and similar problems with line layers.)

Please anyone help me to solve this problem? Thanks very much!

Private Function FeatureLayerType(ByVal dsMap As DotSpatial.Controls.Map, ByVal layerindex As Integer) As String

If Not CType(dsMap.Layers(layerindex), MapPointLayer) Is Nothing Then

FeatureLayerType = "Point" 'Point Layer

ElseIf Not CType(dsMap.Layers(layerindex), MapLineLayer) Is Nothing Then

FeatureLayerType = "PolyLine"

ElseIf Not CType(dsMap.Layers(layerindex), MapPolygonLayer) Is Nothing Then

FeatureLayerType = "PolyGon"

Else

FeatureLayerType = "NonFeatureLayer"

End If

Return FeatureLayerType

End Function

Read the full discussion online.

To add a post to this discussion, reply to this email (DotSpatial@discussions.codeplex.com)

To start a new discussion for this project, email DotSpatial@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
Jiří Kadlec
Geospatial Software Lab
Idaho State University
Jun 2, 2011 at 1:57 AM

Hi, jirikadlec2. Thanks very much for your help. Now it Does work! Comparing to MapWinGIS, the interfaces of DotSpatial are much more complex, and sometimes may confuse new users. So is there any guidebook for a relatively full description on interfaces or functions for DotSpatial now, like the "MapWinGIS Reference Manual"? Thanks again.

Sep 20, 2011 at 4:18 PM

It would be great if someone could convert above code or at least one statement to CSharp for me. Thanks a lot

Developer
Sep 20, 2011 at 9:52 PM
Edited Sep 20, 2011 at 9:53 PM
sraza wrote:

It would be great if someone could convert above code or at least one statement to CSharp for me. Thanks a lot

Please consider using 

www.developerfusion.com/tools/convert/vb-to-csharp/

Sep 21, 2011 at 3:55 PM
mudnug wrote:
sraza wrote:

It would be great if someone could convert above code or at least one statement to CSharp for me. Thanks a lot

Please consider using 

www.developerfusion.com/tools/convert/vb-to-csharp/

 


Thanks mudnug!!