Hi
What is the replacement for winformsMap1.Layers(0).DoNotDraw or what is the best way to do a SELECT on a shapefile? I have a shapefile opened as a ShapeFileFeatureLayer with points in it and I want to display some points depending on a SQLQuery.
Hi
What is the replacement for winformsMap1.Layers(0).DoNotDraw or what is the best way to do a SELECT on a shapefile? I have a shapefile opened as a ShapeFileFeatureLayer with points in it and I want to display some points depending on a SQLQuery.
Alta,
In 3.0, we also have the API to run SQL query, but as the return value is a data table, we need to convert it to a ID array. The code will be like this
// First call the static method to build record id
ShapeFileFeatureLayer.BuildRecordIdColumn(MapPath("~/SampleData/world/cntry02.shp"), "RecID", BuildRecordIdMode.DoNotRebuild)
// Generate the NotDraw ID List, this sample will not draw all the countries start with “U”
Dim table As DataTable = worldLayer.QueryTools.ExecuteQuery("Select RecID From cntry02 where CNTRY_NAME like 'U%'")
' …....
Ids.Add(table.Rows(i)("RecID").ToString().Trim())
After getting the NotDraw list, we need to remove them in the FeaturesToDraw list within featureDrawing event. Here is the complete code.
Partial Public Class DisplayASimpleMap
Inherits System.Web.UI.Page
Private Ids As New List(Of String)()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
Map1.MapBackground.BackgroundBrush = New GeoSolidBrush(GeoColor.FromHtml("#B3C6D4"))
Map1.CurrentExtent = New RectangleShape(-140, 60, 140, -60)
Map1.MapUnit = GeographyUnit.DecimalDegree
' Build Record Id
ShapeFileFeatureLayer.BuildRecordIdColumn(MapPath("~/SampleData/world/cntry02.shp"), "RecID", BuildRecordIdMode.DoNotRebuild)
Dim worldLayer As New ShapeFileFeatureLayer(MapPath("~/SampleData/world/cntry02.shp"))
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(255, 243, 239, 228), GeoColor.FromArgb(255, 218, 193, 163), 1)
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
AddHandler worldLayer.DrawingFeatures, AddressOf worldLayer_DrawingFeatures
' Generate the NotDraw ID List
worldLayer.Open()
Dim table As DataTable = worldLayer.QueryTools.ExecuteQuery("Select RecID From cntry02 where CNTRY_NAME like 'U%'")
worldLayer.Close()
For i As Integer = 0 To table.Rows.Count - 1
Ids.Add(table.Rows(i)("RecID").ToString().Trim())
Next
Map1.StaticOverlay.Layers.Add(worldLayer)
End If
End Sub
Private Sub worldLayer_DrawingFeatures(ByVal sender As Object, ByVal e As DrawingFeaturesEventArgs)
For i As Integer = e.FeaturesToDraw.Count - 1 To 0 Step -1
If Ids.Contains(e.FeaturesToDraw(i).Id) Then
e.FeaturesToDraw.RemoveAt(i)
End If
Next
End Sub
End Class
Here is the result snapshot.
Seems the API is not that convenient compared with 2.0, that's because 2.0 only supports one data type shapefile while 3.0 supports multi types, so the API needs to be more general. (for example, ExecuteQuery() is a method within FeatureLayer and we cannot make it return a int ID array like what in 2.0, as it doesn't make sense for other data source. ) Also, there is a bug in the current version that for the method ExecuteQuery, it will convert the input SQL to UPPERCASE and then do the query. We will fix it in the next version and sorry for the inconvenience now.
Thanks,
Ben
Spot on. Thanks!
You are welcome, Alta.