ThinkGeo.com    |     Documentation    |     Premium Support

How to: Show ESRI(.mxd format) in winforms

Hi Ethan ,

I want to load all Layer from the GDB file.

I loop through all table from GDB file its throw object null reference error.

Please do the needful.

Code

  private void Form1_Load(object sender, EventArgs e)
    {
        winformsMap1.MapUnit = GeographyUnit.DecimalDegree;

        //NewMethod("COUNTY");
        //NewMethod("SCHOOL");
        
        Collection<string> tables = FileGeoDatabaseFeatureLayer.GetTableNames(@"..\..\AppData\MapData.gdb");
        foreach (string strem in tables)
        {
            NewMethod(strem);
        }

        winformsMap1.Refresh();
    }

    private void NewMethod(string str)
    {
        try
        {
            LayerOverlay layerOverlay = new LayerOverlay();
            FileGeoDatabaseFeatureLayer fileGeoDatabaseFeatureLayer = new FileGeoDatabaseFeatureLayer(@"..\..\AppData\MapData.gdb", str);

            Proj4Projection proj4 = new Proj4Projection();
            proj4.InternalProjectionParametersString = fileGeoDatabaseFeatureLayer.GetInternalProj4ProjectionParametersString();
            proj4.ExternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();
            proj4.Open();

            fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
            fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.Canal1;
            fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City1;
            fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            fileGeoDatabaseFeatureLayer.FeatureSource.Projection = proj4;

            layerOverlay.Layers.Add(fileGeoDatabaseFeatureLayer);
            winformsMap1.Overlays.Add(layerOverlay);

            fileGeoDatabaseFeatureLayer.Open();
            winformsMap1.CurrentExtent = fileGeoDatabaseFeatureLayer.GetBoundingBox();
            winformsMap1.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Also attach my screen shot.

Thanks,
Tamilarasan.

Hi Tamil,

You can modify your code like this:

public partial class Form1 : Form
{
    bool IsFirstLayer = true;
    string fileGeoDatabaseFeatureLayerProjectionString;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {            
        winformsMap1.MapUnit = GeographyUnit.DecimalDegree;            

        Collection<string> tables = FileGeoDatabaseFeatureLayer.GetTableNames(@"..\..\AppData\MapData.gdb");

        LayerOverlay layerOverlay = new LayerOverlay();

        foreach (string table in tables)
        {
            FileGeoDatabaseFeatureLayer layer = GetLayer(table.Substring(1));                

            layerOverlay.Layers.Add(layer);

            if (IsFirstLayer)
            {
                winformsMap1.CurrentExtent = layer.GetBoundingBox();
                IsFirstLayer = false;
            }
            else
            {
                winformsMap1.CurrentExtent.ExpandToInclude(layer.GetBoundingBox());
            }
        }

        
        winformsMap1.Overlays.Add(layerOverlay);
        winformsMap1.Refresh();
    }

    private FileGeoDatabaseFeatureLayer GetLayer(string tableName)
    {
        FileGeoDatabaseFeatureLayer fileGeoDatabaseFeatureLayer = new FileGeoDatabaseFeatureLayer(@"..\..\AppData\MapData.gdb", tableName);

        if (IsFirstLayer)
        {                
            fileGeoDatabaseFeatureLayerProjectionString = fileGeoDatabaseFeatureLayer.GetInternalProj4ProjectionParametersString();
        }

        Proj4Projection proj4 = new Proj4Projection();
        proj4.InternalProjectionParametersString = fileGeoDatabaseFeatureLayerProjectionString;
        proj4.ExternalProjectionParametersString = Proj4Projection.GetDecimalDegreesParametersString();
        proj4.Open();

        fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
        fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.Canal1;
        fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City1;
        fileGeoDatabaseFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

        fileGeoDatabaseFeatureLayer.FeatureSource.Projection = proj4;

        fileGeoDatabaseFeatureLayer.Open();

        return fileGeoDatabaseFeatureLayer;
    }
}

In fact I don’t suggest you render the layer like this, it looks not so well.

You should want to write render logic for each layer, and assign a best current extent so it looks better.

Regards,

Ethan

Hi Ethan,

Thanks for your update.!.

Please suggestion us to which layer projection seems look good to get from GDB file?

And how i find the layer from GDB file?

Thanks,
Tamilarasan.

Hi Tamil,

Please refer the below details about your questions:

  1. Using which projection is up to you. The reprojection affects the performance of map rendering, so we suggest you reduce reprojection. You should set map unit according to the projection that your data uses the most to reduce reprojection.

  2. Please refer the below code to get the projection:

        var projections = new Collection<string>();
        foreach (var tableName in tableNames)
        {
            var layer = new FileGeoDatabaseFeatureLayer(@"..\..\AppData\MapData.gdb", tableName.Substring(1));
            layer.Open();
            var projection = layer.GetInternalProj4ProjectionParametersString();
            projections.Add(projection);
        }
       var projectionGroups = projections.GroupBy(proj => proj);
       var selectedProj = projectionGroups.First(p => p.Count() == projectionGroups.Max(g => g.Count())).Key;
    

If you have any questions, please feel free to contact us.

Thanks,

Hi Rex,

Thanks for your update.

  1. Name should not display in the Layer like Street name.

Please do the needful.

Thanks,
Tamilarasan.

Hi Team,

Now i am testing with different GDB file.

But some layer throw exception.

Please refer my screen shot.

Regards,
Tamilarasan.

Hi Tamil,

  1. If you want to show label, I think you need to set the correct text style for layer.

  2. If you met exception please find which layer have the problem, and just like the exception shows:

Please make sure the column which you used in code is exist in the data.

Regards,

Ethan

Hi Ethan,

I am using below code to get table from GDB file.

 Collection<string> tables = FileGeoDatabaseFeatureLayer.GetTableNames(@"..\..\AppData\MapData.gdb")

Loop all the table name from collection and add the New FileGeoDatabaseFeatureLayer to the Map.

Finally code to run Map.Refresh() will throw above exception.

How to check that Table Name have data or not ?.

Please do the needful.

Regards,
Tamilarasan.

Hi Tamil,

I means you should want to remove the loop, load one table one times and found which table have problem at the first.

Then you can see whether it’s the problem from target table or the problem of column, and you can solve it after that.

Regards,

Ethan

Hi Ethan,

Table ESNHarrischng its throw exception…

also GDB files available below path

Please do the needful.

Regards,
Tamilarasan.

Hi Tamil,

The column name in table ESNHarrischng is invalid.

The column name should be “OBJECTID”, but it’s “OBJECTID_1” there, please fix the data.

Regards,

Ethan