ThinkGeo.com    |     Documentation    |     Premium Support

Layer with mutiple shape files

Hello,


I'm trying to use MultipleShapeFileFeatureLayer and getting followng exception when WinForm map control tries to draw the layer.


System.InvalidOperationException: Your input index is out of bound


Here is the piece of code I'm using to create MutipleShapeFileFeatureLayer:


string[] shapeFiles = new string[] { @"c:\data\Folder1\1.shp", @"c:\data\Folder2\1.shp" };
string[] indexFiles = new string[] { @"c:\data\Folder3\1.midx", @"c:\data\Folder3\1.midx" };
MultipleShapeFileFeatureLayer.BuildIndex(shapeFiles, indexFiles, BuildIndexMode.DoNotRebuild);
MultipleShapeFileFeatureLayer shapeLayer = new MultipleShapeFileFeatureLayer(shapeFiles, indexFiles);
ValueStyle valueStyle = new ValueStyle();
valueStyle.ColumnName = "Column1";
valueStyle.ValueItems.Add(new ValueItem("value1", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
valueStyle.ValueItems.Add(new ValueItem("value2", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
shapeLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle);
shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay groupLayer = new LayerOverlay();
groupLayer.Name = "test1";
groupLayer.Layers.Add(shapeLayer);
winformsMap1.Overlays.Add(layerKey, groupLayer);
winformsMaps1.Refresh(); // The exception is being thrown here.



Lenny,


Welcome you to ThinkGeo discussion forum and thanks for your post.
 
I do not see any obvious problem in the code pasted, could you upload the data or send it via email forumsupport@thinkgeo.com and asked to forward to Yale, I think that would definitely be helpful to identify the problem.
 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

Lenny,


Thanks for your data and now I think I figured out where is the problem with the help of the code and data.
 
The problem is in our .mids file, we store the id in the format of “shapeFileNameWithoutFolder: recordId”, while in the case, the shapeFileNameWithoutFolder is the same for both shape file, so it is impossible to make a difference between which shape file it is from. One solution for it is to change the shape file name. Following code is a updated version for it, I could not find the “column1” column in the data attached, so, I changed to one reasonable column value.
 

string[] shapeFiles = new string[] { @"c:\data\Folder1\1.shp", @"c:\data\Folder2\2.shp" };
string[] indexFiles = new string[] { @"c:\data\Folder3\1.midx", @"c:\data\Folder3\1.midx" };
MultipleShapeFileFeatureLayer.BuildIndex(shapeFiles, indexFiles, BuildIndexMode.DoNotRebuild);
MultipleShapeFileFeatureLayer shapeLayer = new MultipleShapeFileFeatureLayer(shapeFiles, indexFiles);
ValueStyle valueStyle = new ValueStyle();
//valueStyle.ColumnName = "Column1";
//valueStyle.ValueItems.Add(new ValueItem("value1", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
//valueStyle.ValueItems.Add(new ValueItem("value2", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
 
valueStyle.ColumnName = "LoadId";
valueStyle.ValueItems.Add(new ValueItem("2752", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
valueStyle.ValueItems.Add(new ValueItem("A", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
valueStyle.ValueItems.Add(new ValueItem("DK2752", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
valueStyle.ValueItems.Add(new ValueItem("", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
 
shapeLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle);
shapeLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
LayerOverlay groupLayer = new LayerOverlay();
groupLayer.Name = "test1";
groupLayer.Layers.Add(shapeLayer);
winformsMap1.Overlays.Add("groupLayer", groupLayer);
 
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
shapeLayer.Open();
winformsMap1.CurrentExtent = shapeLayer.GetBoundingBox();
shapeLayer.Close();
 
winformsMap1.Refresh();

 

 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

 



Yale, thank you for the explanation. 
  
 Unfortunately, changing the filenames will not work in my case. I tried to use separate index files for each shape file but the map control only displayed data from the first file (no exception was thrown) . Is there another way how to solve this issue?  
  
 Regards.

Yale, this seems like a pretty common scenario.   Are you considering doing something to support having common file names in different folders?

Lenny & Ted,


Thanks for your posts and information.
 
Another solution is to use the group layer instead of MultiShapeFileFeatureLayer, there is only very minor changes. Following code snippet shows how to use the GroupLayer instead.
 

ValueStyle valueStyle = new ValueStyle();
//valueStyle.ColumnName = "Column1";
//valueStyle.ValueItems.Add(new ValueItem("value1", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
//valueStyle.ValueItems.Add(new ValueItem("value2", PointStyles.CreateSimpleCircleStyle(GeoColor.GetRandomGeoColor(RandomColorType.All), 2)));
 
valueStyle.ColumnName = "LoadId";
valueStyle.ValueItems.Add(new ValueItem("2752", PointStyles.CreateSimpleCircleStyle(GeoColor.SimpleColors.Red, 2)));
valueStyle.ValueItems.Add(new ValueItem("A", PointStyles.CreateSimpleCircleStyle(GeoColor.SimpleColors.Yellow, 2)));
valueStyle.ValueItems.Add(new ValueItem("DK2752", PointStyles.CreateSimpleCircleStyle(GeoColor.SimpleColors.Blue, 2)));
valueStyle.ValueItems.Add(new ValueItem("", PointStyles.CreateSimpleCircleStyle(GeoColor.SimpleColors.Copper, 2)));
 
string[] shapeFiles = new string[] { @"c:\data\Folder1\1.shp", @"c:\data\Folder2\1.shp" };
GroupLayer groupLayer = new GroupLayer();
foreach(string shapeFile in shapeFiles)
{
    ShapeFileFeatureLayer.BuildIndexFile(shapeFile, BuildIndexMode.DoNotRebuild);
    ShapeFileFeatureLayer shapeFileFeatureLayer = new ShapeFileFeatureLayer(shapeFile, ShapeFileReadWriteMode.ReadOnly);
                shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle);
                shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
    groupLayer.Layers.Add(shapeFileFeatureLayer);
}
groupLayer.Name = "test1";
           
LayerOverlay layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add(groupLayer);
winformsMap1.Overlays.Add("LayerOverlay", layerOverlay);
 
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
groupLayer.Open();
winformsMap1.CurrentExtent = groupLayer.GetBoundingBox();
groupLayer.Close();
 
winformsMap1.Refresh();

 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale