Getting Shapetype without reading the shape

Topics: SharpMap Project, SharpMap v2.0
Jun 25, 2010 at 6:13 PM

Hi,

 

Is it possible that I can read the Shapetype without reading the whole shape file please? I'd like to have a textbox filled up with all the .SHP's found in a directory and am using a recursive function (below)

 

foreach (FileInfo fi in files)
                {
                    counter++;
                    string fileInfo = fi.FullName;
                    string thisArgument;
                    
                    GeometryServices gs = new GeometryServices();
                    ShapeFileProvider shp = new ShapeFileProvider(fileInfo,
                                                                      gs.DefaultGeometryFactory
                                                                         , gs.CoordinateSystemFactory,
                                                                     false);
                    shp.Open();
                    if (shp.ShapeType.ToString().ToLower() == "point")
                    {
                        thisArgument = "\"" + fileInfo + "\" 8";
                    }
                    else
                    {
                        thisArgument = "\"" + fileInfo + "\"";
                    }
                    shpArguments.Add(thisArgument);
                    shapeFiles.Add(fileInfo + "(" + shp.ShapeType.ToString().ToUpper() + ")" );
                    txOutResults.AppendText(Environment.NewLine + fileInfo + "  (" + shp.ShapeType.ToString().ToUpper() + ")");
                    shp.Close();
                    
                }
this works quite fine, until the SHP file is > 8 or so MB. Since it involves a binary reader it has to read the file byte by byte until it allows me to get the shp.shapeType property.
Is it somehow possible for me to just "peek" the details?
Thanks
Coordinator
Jun 27, 2010 at 10:02 PM

Hello yancho,

the time is needed for setting up the internal spatial index for the shapefile. Atm, I cannot say how simple it is to change this, probably you would do better writing your own routine using the ESRI ShapeFile specs.

Hth FObermaier

Jun 28, 2010 at 9:09 AM
thanks for the reply fobermaier. But if the shape file has a .shx file already doesn't that mean there is a shapefile index already?
Coordinator
Jun 28, 2010 at 10:03 AM

Hello yancho,

you are right the index is there, but -if I recall correctly- the shx file is read completely and its entries are put into memory for what reason soever.

That makes is why I hesitate to change that adhoc, since I'm not that familiar with the shapefile provider and the internal usage of the inmemory index.

cheers

FObermaier

Jul 2, 2010 at 5:18 AM

Hello Yancho,

 

you could open the file with using binary reader yourself and read the first 36 bytes. At position 32 you find the ShapeType.

I dont know, what the ShapefileProvider class of your example does, but probably it reads the whole file in order to retrieve the number of objects. Thats what takes so long.

 

Regards,

TeDe