ThinkGeo.com    |     Documentation    |     Premium Support

Maui shapefile exception

Hi

  • I Disovered that the exception is not thrown in a modified HowDoi project

  • I think it difficult to debug/ find the errors since the exception information does not give insight to for exampl which layer is causing the problem. The exception is thrown within Microsoft maui code so its not possible to change this code. It it possible to improve logging / exception handlig giving better insight to the problem in hand?

The problem description:
I load a shapefile layer in maps, when opening the map there is an exceptionLarvikMotorvei.zip (24.3 KB)
The exception is thrown
private async Task F0Q_003D

The exception is visualized within Visual Studio

I have attached the shape file

It is ok displayed in mapsharper - just dragging the .shp file into the page and it works ok

Source code adding the file:

private void AddShapeLayer(string dataDirectory, string? s )
{
    if (s == null)
        s = "shp4326.shp";
    var dashedPen = new GeoPen(GeoColors.Green, 5);
    dashedPen.DashPattern.Add(1);
    var parksOverlay = new LayerOverlay();
    MapView.Overlays.Add(parksOverlay);
    parksOverlay.Opacity = 0.5;

    var shapePathFilename = Path.Combine(dataDirectory, "Data", "Shapefile", s);
    ShapeFileFeatureLayer.BuildIndexFile(shapePathFilename);
    var parksLayer = new ShapeFileFeatureLayer(shapePathFilename)// "HighAll.shp"))
                     {
                         FeatureSource =
                         {
                             ProjectionConverter = new ProjectionConverter(2276, 3857)
                         }
                     };
    // Add the layer to the overlay we created earlier.
    parksOverlay.Layers.Add("Frisco Parks", parksLayer);
    parksLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle =
        new AreaStyle(dashedPen, new GeoSolidBrush(new GeoColor(64, GeoColors.Green)));
    parksLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
    
}

Hi Gunnar,

I improved your code a bit as following:

private ShapeFileFeatureLayer AddShapeLayer(string shapePathFilename)
{
 var dashedPen = new GeoPen(GeoColors.Green, 5);
 dashedPen.DashPattern.Add(1);
 var parksOverlay = new LayerOverlay();
 MapView.Overlays.Add(parksOverlay);
 parksOverlay.Opacity = 0.5;

 ShapeFileFeatureLayer.BuildIndexFile(shapePathFilename);

 // read the projection from .prj file 
 string projWkt = File.ReadAllText(shapePathFilename.Replace(".shp", ".prj"));
 string projString = Projection.ConvertWktToProjString(projWkt);

 var parksLayer = new ShapeFileFeatureLayer(shapePathFilename)// "HighAll.shp"))
 {
     FeatureSource =
     {
         ProjectionConverter = new ProjectionConverter(projString, 3857)
     }
 };
 // Add the layer to the overlay we created earlier.
 parksOverlay.Layers.Add("Frisco Parks", parksLayer);
 parksLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle =
     new AreaStyle(dashedPen, new GeoSolidBrush(new GeoColor(64, GeoColors.Green)));
 parksLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle =
     LineStyle.CreateSimpleLineStyle(GeoColors.Red, 2, true);
 
 parksLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

 return parksLayer;

}

private async void MapView_OnSizeChanged(object sender, EventArgs e)
{
 if (_initialized)
     return;
 _initialized = true;

 MapView.MapUnit = GeographyUnit.Meter;

 // Add ThinkGeo Cloud Maps as the background 
 var backgroundOverlay = new ThinkGeoRasterOverlay
 {
     ClientId = SampleKeys.ClientId,
     ClientSecret = SampleKeys.ClientSecret,
     MapType = ThinkGeoCloudRasterMapsMapType.Light_V2_X2,
     TileCache = new FileRasterTileCache(FileSystem.Current.CacheDirectory, "ThinkGeoRasterCache")
 };
 MapView.Overlays.Add(backgroundOverlay);

 var overlay = new LayerOverlay();
 MapView.Overlays.Add(overlay);
 var layer = AddShapeLayer(@"d:\Downloads\LarvikMotorvei\LarvikMotorvei.shp");

 // Add the layer to the overlay we created earlier.
 overlay.Layers.Add(layer);

 // Open the layer and set the map view current extent to the bounding box of the layer.  
 layer.Open();
 MapView.CenterPoint = layer.GetBoundingBox().GetCenterPoint();
 MapView.MapScale = MapUtil.GetScale(layer.GetBoundingBox(), MapView.MapWidth, MapView.MapUnit);

 await MapView.RefreshAsync();

}

and got this result:

In fact, I couldn’t recreate your exception. If you still see the exception, you can change the overlay to singleTile, and make sure overlay.DrawingExceptionMode = DrawingExceptionMode.ThrowException

Thanks,
Ben

Thank you for the quick response! Now it works ok for me also !

That’s awesome, Gunnar.