Extracting SubLayers from OGR Provider

Topics: General Topics
Editor
Oct 8, 2013 at 9:58 AM
Hi,

I'm looking at importing some cad data into sharpmap using the OGR extension. The data loads fine and looks good. However it loads into a single layer called entities. What is the best way to split it out into sublayers?

I tried this.
 var myFile = new SharpMap.Data.Providers.Ogr(@"Q:\GIS\DXF Sample\KP105-01.dxf", 0);
        var myLayer = new SharpMap.Layers.VectorLayer("Floorplan", myFile);

        var myfoundationSource = new SharpMap.Data.Providers.GeometryFeatureProvider(new FeatureDataTable());

       for (int i = 0; i <= myLayer.DataSource.GetFeatureCount() - 1; i++)
       {
           var myfeat = myLayer.DataSource.GetFeature((uint)i);

           if (myfeat.ItemArray[0].ToString().Equals("FOUNDATION"))
           {
               myfoundationSource.Features.ImportRow(myfeat);
           }

       }
but it doesn't work. I get no features, even though there are some that meet the conditions. Addrow throws a 'Row already part of table error'.

Is there a clone method? or do I need to remove from the source table first? is the GeometryFeatureProvider the best way of doing this?

Thanks
Editor
Oct 14, 2013 at 11:05 AM
I thought I would answer my own question in case someone else is interested.

Its possible to use the ExecuteQuery method on the OGR dataprovider to get a list of sub layers in an dxf file.

In OGR command line you would write this

ogrinfo -ro "#path to your dxf#" -sql "select distinct Layer from entities"

or in sharpmap

var ds2 = myFile.ExecuteQuery("select distinct Layer from entities'");

this returns a dataset with a table that contains all the layer names.

It is then possible to loop through each row and execute a second query like this:

var ds2 = myFile.ExecuteQuery("select * from entities WHERE Layer='#Your layer Name#'");

this will return just the features that are in that layer. you can then parse each one into a geometry object.

I have also tried netDXF and it is very good if you direct access to the information the dxf file.
Coordinator
Oct 14, 2013 at 11:32 AM
Robert_Smart wrote:
I have also tried netDXF and it is very good if you direct access to the information the dxf file.
Is it suitable for an IProvider implementation?
Editor
Oct 14, 2013 at 12:34 PM
I would think so, although the benefits it gives over OGR are minor for most cases.

My case is different because I need to iterate the shapes and extract some geometries to different databases and layers depending on fields in the data. So it is ideal as an importer for these reasons, but for just accessing and displaying the data OGR is suffcient.