This project has moved. For the latest updates, please go here.

Error when opening SHP file

Topics: SharpMap v0.9 / v1.x
Oct 25, 2010 at 8:09 PM
Edited Oct 25, 2010 at 8:16 PM


I’m getting the following error when I try to loop through the Feature Rows in a SHP file that I have uploaded via a form. The files I’m uploading are SHP, DBF and SHX, which I believe are the minimum number of files that are required to open a SHP file source (they are the only ones I have, and they open fine in ArcView/MapInfo).


Server Error in '/' Application.

File hasn't been spatially indexed. Try opening the datasource before retriving extents

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ApplicationException: File hasn't been spatially indexed. Try opening the datasource before retriving extents

Source Error: 


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[ApplicationException: File hasn't been spatially indexed. Try opening the datasource before retriving extents]
   SharpMap.Data.Providers.ShapeFile.GetExtents() +58
   BlueFox.Opus3.Domain.Concrete.SqlMapLayerDataRepository.SaveData(String name, String filename, Guid client, Guid user) in C:\...cs:27
   BlueFox.Opus3.WebUI.Controllers.AdminController.UploadMapData(CurrentClient currentClient, String name, HttpPostedFileBase shp,HttpPostedFileBase dbf, HttpPostedFileBase shx, HttpPostedFileBase sbn, HttpPostedFileBase prj, HttpPostedFileBase sbx) in C:\...cs:920
   lambda_method(Closure , ControllerBase , Object[] ) +443
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +199
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +267
   System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +20
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +190
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +345
   System.Web.Mvc.Controller.ExecuteCore() +115
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +42
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +43
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1


The relevant code is as follows:


SharpMap.Data.Providers.ShapeFile shp = new SharpMap.Data.Providers.ShapeFile(filename, false);

foreach(uint index in shp.GetObjectIDsInView(shp.GetExtents())) {
    SharpMap.Data.FeatureDataRow feature = shp.GetFeature(index);


The filename is simply passed to the method that contains the above code. What I’m trying to do is loop through all the features within the SHP file so that I can insert them into SQL Server 2008—maybe there is another way of doing that from SharpMap?

The error above appears to occur when it gets to the “shp.GetExtents()” bit as it’s claiming that the file hasn’t been indexed and therefore the extents can’t be got.

Any ideas?


Oct 25, 2010 at 8:44 PM

Ah, never mind! Seems that if I call “shp.Open();” before looping through the feature rows, then it works fine. The example code I borrowed mine from didn’t have that line, but it seems to do the trick.