ThinkGeo.com    |     Documentation    |     Premium Support

Create new column

Hello,


I'd like to create a new shp file and copy some data from other shp file.


How do I create some column I want?


If original shp have columns of ID, NAME, TYPE  and I just want to have columns of ID and Name.


If I use CloneShapeFileStructure to copy structure of original shp file and how do I delete it which I don't want?


 


 


 



Carol,


We have finished the remove column part, another part split column we are still working on it.

The following code is how to implement function like you said:



        private void ProcessNewShapeFile(string originalShapefilePath, string newShapefilePath)
        {
            ShapeFileFeatureLayer originalShapeFileFeatureLayer = new ShapeFileFeatureLayer(originalShapefilePath);
            ShapeFileFeatureLayer.BuildIndexFile(originalShapefilePath, BuildIndexMode.DoNotRebuild);

            ShapeFileFeatureLayer newShapeFileFeatureLayer = GenerateNewShapeFile(originalShapeFileFeatureLayer, newShapefilePath);
            UpdateNewShapeFile(originalShapeFileFeatureLayer, newShapeFileFeatureLayer);
            ShapeFileFeatureLayer.BuildRecordIdColumn(newShapefilePath, "RECID", BuildRecordIdMode.Rebuild);
        }

        private void UpdateNewShapeFile(ShapeFileFeatureLayer originalShapeFileFeatureLayer, ShapeFileFeatureLayer newShapeFileFeatureLayer)
        {
            newShapeFileFeatureLayer.Open();
            newShapeFileFeatureLayer.EditTools.BeginTransaction();
            originalShapeFileFeatureLayer.Open();
            Collection<DbfColumn> dbfColumns = ((ShapeFileFeatureSource)originalShapeFileFeatureLayer.FeatureSource).GetDbfColumns();
            Collection<string> columnNames = new Collection<string>();
            foreach (DbfColumn dbfColumn in dbfColumns)
            {
                if (dbfColumn.ColumnName.Trim() != "OLDROADID1" && dbfColumn.ColumnName.Trim() != "DIR")                
                {
                    columnNames.Add(dbfColumn.ColumnName);
                }
            }
            Collection<Feature> features = originalShapeFileFeatureLayer.FeatureSource.GetAllFeatures(columnNames);
            foreach (Feature feature in features)
            {
                newShapeFileFeatureLayer.EditTools.Add(feature);
            }
            newShapeFileFeatureLayer.EditTools.CommitTransaction();
            originalShapeFileFeatureLayer.Close();
            newShapeFileFeatureLayer.Close();
        }

        private ShapeFileFeatureLayer GenerateNewShapeFile(ShapeFileFeatureLayer originalShapeFileFeatureLayer, string newShapefilePath)
        {
            originalShapeFileFeatureLayer.Open();
            ShapeFileType shapeFileType = originalShapeFileFeatureLayer.GetShapeFileType();
            Collection<DbfColumn> oringinalDbfColumns = ((ShapeFileFeatureSource)originalShapeFileFeatureLayer.FeatureSource).GetDbfColumns();
            Collection<DbfColumn> DbfColumns = new Collection<DbfColumn>();
            foreach (DbfColumn oringinalDbfColumn in oringinalDbfColumns)
            {
                if (oringinalDbfColumn.ColumnName.Trim() != "OLDROADID1" && oringinalDbfColumn.ColumnName.Trim() != "DIR")
                {
                    DbfColumns.Add(oringinalDbfColumn);
                }
            }
            originalShapeFileFeatureLayer.Close();
            ShapeFileFeatureSource.CreateShapeFile(shapeFileType, newShapefilePath, DbfColumns);
            ShapeFileFeatureLayer newShapeFileFeatureLayer = new ShapeFileFeatureLayer(newShapefilePath, ShapeFileReadWriteMode.ReadWrite);
            return newShapeFileFeatureLayer;
        }

Please let me know if you have questions.

Thanks


James



Thanks for your reply, James


I attach a file which is divided part by one shp file.


But, it can't show on the Map. I don't know what's wrong.


 



1423-road_10019dbf.zip (411 KB)

I can't upload sample to you.

I send sample to your email.


The maximum file size allowed is 500 kb.



Carol,


I can display it, both map and data.


You said you send sample to my email, but I can not find it, it's just shp files.


Please send us your sample code, it may going to be help us.



Thanks


James



Thanks for your reply, James 
  
 I think it have to rebuild index file. 
  
 When I rebuild index file, it can show.  
  
 (But I don’t know why it have to rebuild index) 
  
 Thanks. :)

Carol, 
  
 It’s wonderful that you fix your problem. 
  
 About Rebuild, when the data has some problem, rebuild will re-organize it and make our MapSuite can support it. 
  
 Thanks. 
 James