Back to Devexpress

ShapefileDataAdapter Class

windowsforms-devexpress-dot-xtramap-9f49014f.md

latest9.2 KB
Original Source

ShapefileDataAdapter Class

A data adapter that loads data from shapefiles and displays it on vector layers.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public class ShapefileDataAdapter :
    FileDataAdapterBase,
    ISupportBoundingRectAdapter
vb
Public Class ShapefileDataAdapter
    Inherits FileDataAdapterBase
    Implements ISupportBoundingRectAdapter

Remarks

The shapefile format is a geospatial vector data format that uses geometric shapes (points, polylines, and polygons) to display geographic data (rivers, lakes, countries, etc.). The Map Control uses data from two types of files:

  • The .shp file, which contains geometry data.
  • The .dbf file, which contains data associated with geometric shapes (e.g., names and GDP values for countries).

The Map Control stores data from the .dbf file as attributes for each vector item. You can colorize shapes based on attribute values or display attribute data in shape tooltips.

The image below shows a vector map loaded from a shapefile with the applied ChoroplethColorizer.

The following table lists supported shapefile elements and related map items:

|

Shape File Element

|

Map Item

| | --- | --- | |

Point,
PointM (the M value is ignored),
PointZ (the Z value is ignored),
Multipoint,
MultipointM (the M value is ignored),
MultipointZ (the Z value is ignored).

|

MapDot

| |

PolyLine,
PolylineM (the M value is ignored),
PolylineZ (the Z value is ignored).

|

MapPolyline

| |

Polygon,
PolygonM (the M value is ignored),
PolygonZ (the Z value is ignored).

|

MapPolygon

|

For more information about shapefile element types, refer to the following ESRI document: ESRI Shapefile Technical Description.

Load Data

To load data from a shapefile, follow the steps below:

csharp
public partial class Form1 : Form {
  const string filename = "../../Data/Countries.shp";
  VectorItemsLayer MapLayer { get { return (VectorItemsLayer)mapControl1.Layers["MapLayer"]; } }

  public Form1() {
      InitializeComponent();
      MapLayer.Data = CreateData();
  }
  private MapDataAdapterBase CreateData() {
      Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
      // Create an adapter to load data from the shapefile.
      ShapefileDataAdapter adapter = new ShapefileDataAdapter() {
          FileUri = new Uri(baseUri, filename)
      };
      return adapter;
  }
}
vb
Public Partial Class Form1
    Inherits Form

    Const filename As String = "../../Data/Countries.shp"

    Private ReadOnly Property MapLayer As VectorItemsLayer
        Get
            Return CType(mapControl1.Layers("MapLayer"), VectorItemsLayer)
        End Get
    End Property

    Public Sub New()
        InitializeComponent()
        MapLayer.Data = CreateData()
    End Sub

    Private Function CreateData() As MapDataAdapterBase
        Dim baseUri As Uri = New Uri(System.Reflection.Assembly.GetEntryAssembly().Location)
        Dim adapter As ShapefileDataAdapter = New ShapefileDataAdapter() With {
            .FileUri = New Uri(baseUri, filename)
        }
        Return adapter
    End Function
End Class

View Example

Tip

If the Map Control does not display a shapefile, make sure that the ShapefileDataAdapter coordinate system is specified correctly. See the following help topic for more information: Provide Cartesian Data to a Geographical Map.

Access and Customize Map Items

When vector items are loaded, the FileDataAdapterBase.ItemsLoaded event occurs. Use the e.Items property to access map items.

csharp
private void Form1_Load(object sender, EventArgs e) {
    Uri baseUri = new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location);
    ShapefileDataAdapter adapter = new ShapefileDataAdapter();
    adapter.FileUri = new Uri(baseUri, "../../Data/Countries.shp");
    adapter.ItemsLoaded += Adapter_ItemsLoaded;
    mapControl1.Layers.Add(new VectorItemsLayer {
        Data = adapter
    });
}

private void Adapter_ItemsLoaded(object sender, ItemsLoadedEventArgs e) {
    foreach (MapItem item in e.Items) {
        item.Fill = System.Drawing.Color.Gray;
    }
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
  Dim baseUri As Uri = New Uri(System.Reflection.Assembly.GetExecutingAssembly().Location)
  Dim adapter As ShapefileDataAdapter = New ShapefileDataAdapter()
  adapter.FileUri = New Uri(baseUri, "../../Data/Countries.shp")
  adapter.ItemsLoaded += AddressOf Adapter_ItemsLoaded
  mapControl1.Layers.Add(New VectorItemsLayer With {
      .Data = adapter
  })
  End Sub

  Private Sub Adapter_ItemsLoaded(ByVal sender As Object, ByVal e As ItemsLoadedEventArgs)
  For Each item As MapItem In e.Items
      item.Fill = System.Drawing.Color.Gray
  Next
  End Sub

Use the MapControl.ZoomToFitLayerItems method to zoom the map to fit layer items.

csharp
private void Form1_Load(object sender, EventArgs e) {
  Uri baseUri = new Uri(System.Reflection.Assembly.GetExecutingAssembly().Location);
  ShapefileDataAdapter adapter = new ShapefileDataAdapter();
  adapter.FileUri = new Uri(baseUri, "../../Data/Countries.shp");
  VectorItemsLayer layer = new VectorItemsLayer();
  layer.Data = adapter;
  layer.DataLoaded += Layer_DataLoaded;
  mapControl1.Layers.Add(layer);
}

private void Layer_DataLoaded(object sender, DataLoadedEventArgs e) {
  mapControl1.ZoomToFitLayerItems();
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
  Dim baseUri As Uri = New Uri(System.Reflection.Assembly.GetExecutingAssembly().Location)
  Dim adapter As ShapefileDataAdapter = New ShapefileDataAdapter()
  adapter.FileUri = New Uri(baseUri, "../../Data/Countries.shp")
  Dim layer As VectorItemsLayer = New VectorItemsLayer()
  layer.Data = adapter
  layer.DataLoaded += AddressOf Layer_DataLoaded
  mapControl1.Layers.Add(layer)
End Sub

Private Sub Layer_DataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
  mapControl1.ZoomToFitLayerItems()
End Sub

You can change shape colors based on shape data. Refer to the following help topic for more information: Colorizers.

You can also group items of the same type. To do this, initialize the MapDataAdapterBase.Clusterer property with a clusterer that aggregates map items based on their location.

Implements

IMapDataAdapter

Inheritance

Object MapDisposableObject MapDataAdapterBase CoordinateSystemDataAdapterBase DevExpress.XtraMap.OuterDataAdapterBase FileDataAdapterBase ShapefileDataAdapter

See Also

ShapefileDataAdapter Members

Vector Items

DevExpress.XtraMap Namespace