ThinkGeo.com    |     Documentation    |     Premium Support

Creating a New Shape File

I created a new DynamicLayer from features I have pulled from a ShapeFileLayer. How can I save this DynamicLayer as new Shape file (*.shp)? I read about the work-around mentioned in version 2 where you use a template shape file. Is there a way to do this programatically now in version 3?






 

 





 


Robert,


I have coded the sample, I hope it helps.  In the sample below I create a shape file from scratch, create an empty spatial index.  Next we add some records and save them.  In the end I do a quick query and verify the results.  In your case you can use the features you have queries instead of the features I created.


One thing to note is that in an older beta the shape file that is created is not valid.  Make sure you have the latest beta from the website if you have any issue.


 


            // Define your columns you want in the shape file

            Collection<dbfcolumn> dbfColumns = new Collection<dbfcolumn>();

            dbfColumns.Add(new DbfColumn("C1", DbfColumnType.String, 10, 0));

            dbfColumns.Add(new DbfColumn("C2", DbfColumnType.Integer, 5, 0));



            // Create the shape file and also create build the spatial index.  If there is a spatial index

            // when you add new records it will update the index.

            ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point, @"C:\test.shp", dbfColumns);

            ShapeFileLayer.BuildIndexFile(@"C:\test.shp");

           

            // The shape file is now created so we open it.

            ShapeFileLayer newShapeFile = new ShapeFileLayer(@"C:\test.shp",ShapeFileReadWriteMode.ReadWrite);

            newShapeFile.Open();



            // Begin our transaction

            newShapeFile.EditTools.BeginTransaction();



            // Add three features

            Feature feature1 = new Feature(new PointShape(10, 10));

            feature1.ColumnValues.Add("C1", "Feature1");

            feature1.ColumnValues.Add("C2", "1");

            newShapeFile.EditTools.Add(feature1);



            Feature feature2 = new Feature(new PointShape(20, 20));

            feature2.ColumnValues.Add("C1", "Feature2");

            feature2.ColumnValues.Add("C2", "2");

            newShapeFile.EditTools.Add(feature2);



            Feature feature3 = new Feature(new PointShape(30, 30));

            feature3.ColumnValues.Add("C1", "Feature3");

            feature3.ColumnValues.Add("C2", "3");

            newShapeFile.EditTools.Add(feature3);



            // Commit the transaction

            TransactionResult results = newShapeFile.EditTools.CommitTransaction();

            MessageBox.Show(@"Added new features: " + results.TransactionResultStatus.ToString());

          

            // Do a quick query to verify the results (for testing)

            Collection<feature> queryResults = newShapeFile.QueryTools.GetFeaturesInsideBoundingBox(new RectangleShape(0, 100, 100, 0), new string[] {"C1","C2"});

            newShapeFile.Close();



            // Here I am just inspecting the results.

            MessageBox.Show(@"Records returned: " + queryResults.Count);

            MessageBox.Show(@"Record 0 WKT : " + queryResults[0].GetWellKnownText());</feature></dbfcolumn></dbfcolumn>


 


David



It seems I'm really close when I apply this logic to my scenario. The code works but when I try to open the new layer, I get an error:  


System.ArgumentOutOfRange Exception


Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: StartIndex


Any thoughts?



Robert, 
  
   At what point exactly does the error occur?  Is it after you call the create shape file and the build index?  Is it after you have added the records and then you try and re-open it or query it?  If the shape file you are generating is not big or not secret can you zip it up and send it to support@thinkgeo.com and ask them to forward it to David?   
  
   I think that the issue may be a bug in the shape file create code.  We have fixed it in the version I have but the Web Edition beta we have out might not have it fixed yet.  I think I can tell from the shape file you send me.  In any event we will have a new public build out by the end of the week and also I may be able to send you a patched DLL the day I verify this is the issue. 
  
 David

David, 
 The code works fine. I get a success message back from the transaction. I successfully create a SHP, SHX, DBF, IDS, and an IDX file. I get the error when I try to open the shape in the Map Suite Explorer application to see how it came out. 
 I will forward the files on via email. 
 Thanks, 
 Bob

Bob,


   I see, in the code I sent it will do a quick test where it prints out the WTK of one of the objects. Did you see what that returns? I suspect that this is the bug in the existing version where it doesn’t create the shape file properly. My code worked as well at first with the old version until I went to look at the results like you did, I found the WKT printed out wrong values for the records I added. I just did it through code and didn’t use Map Suite Explorer. As soon as I updated my code internally here with the latest code then it worked well and I found the bug report on it. Once I get the shape files we will test it and see what’s up.
David

Bob, 
  
   We have verified from your shape file that this is the bug we have since fixed but not released.  In the latest release I have tested this and I can open the shape file in Global Mapper and also Arcview.  We will be releasing a new beta next Monday and this will be fixed.  I will put a little reminder to update the ticket when the new version is out. 
  
 David

Bob, 
  
 Here is the reminder that the new version is out and the creating a shape file code I posted will work in the latest version.  Just an FYI, and let me know if everything works well. 
  
 David