Back to Devexpress

GeoJsonFileDataAdapter Class

windowsforms-devexpress-dot-xtramap-8fe63911.md

latest6.9 KB
Original Source

GeoJsonFileDataAdapter Class

A data adapter that loads data from GeoJSON files, and displays it on vector layers.

Namespace : DevExpress.XtraMap

Assembly : DevExpress.XtraMap.v25.2.dll

NuGet Package : DevExpress.Win.Map

Declaration

csharp
public class GeoJsonFileDataAdapter :
    FileDataAdapterBase
vb
Public Class GeoJsonFileDataAdapter
    Inherits FileDataAdapterBase

Remarks

The Map Control allows you to display data from GeoJSON files that store geographical objects in the JSON format.

The following table lists supported GeoJSON elements and corresponding map items:

|

GeoJSON element

|

Map item

| | --- | --- | |

Point

|

MapDot

| |

MultiPoint

|

A list of MapDot objects

| |

LineString

|

MapPolyline

| |

MultiLineString

|

MapPath

| |

Polygon, MultiPolygon

|

MapPath

| |

GeometryCollection

|

MapDot, MapPolyline, MapPath

|

Follow the steps below to load data from a .GeoJSON file:

  1. Add a VectorItemsLayer object to the MapControl.Layers collection.
  2. Create a GeoJsonFileDataAdapter object.
  3. Specify the path to a GeoJSON file via the FileDataAdapterBase.FileUri property.
  4. Assign the GeoJsonFileDataAdapter to the VectorItemsLayer.Data property.
  5. Optionally, you can customize generated items in the FileDataAdapterBase.ItemsLoaded event handler.
csharp
private void Form1_Load(object sender, EventArgs e) {

    ImageLayer imageLayer = new ImageLayer {
        DataProvider = new BingMapDataProvider { BingKey = "Your_BingKey_here", Kind = BingMapKind.Road }
    };
    mapControl1.Layers.Add(imageLayer);

    VectorItemsLayer vectorLayer = new VectorItemsLayer();
    mapControl1.Layers.Add(vectorLayer);

    GeoJsonFileDataAdapter dataAdapter = new GeoJsonFileDataAdapter();
    dataAdapter.FileUri = new Uri(GetRelativePath("Data\\subway-entrances.geojson"), UriKind.RelativeOrAbsolute);
    vectorLayer.Data = dataAdapter;

    // Use the ItemsLoaded event to customize items the adapter generates.
    dataAdapter.ItemsLoaded += OnDataAdapterItemsLoaded;
    // You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
    // to zoom the map so that it displays all vector items.
    vectorLayer.DataLoaded += OnVectorLayerDataLoaded;
}

private void OnDataAdapterItemsLoaded(object sender, ItemsLoadedEventArgs e) {
    foreach (MapDot item in e.Items) {
        item.Fill = Color.Red;
        item.Size = 8;                
    }
}

private void OnVectorLayerDataLoaded(object sender, DataLoadedEventArgs e) {
    mapControl1.ZoomToFitLayerItems(new List<LayerBase> { (VectorItemsLayer)mapControl1.Layers[1] });
}

public static string GetRelativePath(string name) {

    DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
    while (dir != null) {
        string filePath = Path.Combine(dir.FullName, name);
        if (File.Exists(filePath))
            return filePath;
        dir = Directory.GetParent(dir.FullName);
    }
    return string.Empty;
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim imageLayer As ImageLayer = New ImageLayer With {
        .DataProvider = New BingMapDataProvider With {
            .BingKey = "Your_BingKey_here",
            .Kind = BingMapKind.Road
        }
    }
    mapControl1.Layers.Add(imageLayer)
    Dim vectorLayer As VectorItemsLayer = New VectorItemsLayer()
    mapControl1.Layers.Add(vectorLayer)
    Dim dataAdapter As GeoJsonFileDataAdapter = New GeoJsonFileDataAdapter()
    dataAdapter.FileUri = New Uri(GetRelativePath("Data\subway-entrances.geojson"), UriKind.RelativeOrAbsolute)
    vectorLayer.Data = dataAdapter

    ' Use the ItemsLoaded event to customize items the adapter generates.
    dataAdapter.ItemsLoaded += AddressOf OnDataAdapterItemsLoaded
    ' You can call the MapControl.ZoomToFitLayerItems method in the DataLoaded event handler
    ' to zoom the map so that it displays all vector items.
    vectorLayer.DataLoaded += AddressOf OnVectorLayerDataLoaded
End Sub

Private Sub OnDataAdapterItemsLoaded(ByVal sender As Object, ByVal e As ItemsLoadedEventArgs)
    For Each item As MapDot In e.Items
        item.Fill = Color.Red
        item.Size = 8
    Next
End Sub

Private Sub OnVectorLayerDataLoaded(ByVal sender As Object, ByVal e As DataLoadedEventArgs)
    mapControl1.ZoomToFitLayerItems(New List(Of LayerBase) From {
        CType(mapControl1.Layers(1), VectorItemsLayer)
    })
End Sub

Public Shared Function GetRelativePath(ByVal name As String) As String
    Dim dir As DirectoryInfo = New DirectoryInfo(Application.StartupPath)

    While dir IsNot Nothing
        Dim filePath As String = Path.Combine(dir.FullName, name)
        If File.Exists(filePath) Then Return filePath
        dir = Directory.GetParent(dir.FullName)
    End While

    Return String.Empty
End Function

Implements

IMapDataAdapter

Inheritance

Object MapDisposableObject MapDataAdapterBase CoordinateSystemDataAdapterBase DevExpress.XtraMap.OuterDataAdapterBase FileDataAdapterBase GeoJsonFileDataAdapter

See Also

Vector Items

GeoJsonFileDataAdapter Members

DevExpress.XtraMap Namespace