ThinkGeo.com    |     Documentation    |     Premium Support

Add column to .shp, populate and save

Hello


I found lots of threads related to this but no convenient solution for me.


I was using API 2.0 DBaseFile and I need a 3.x equivalent now... that I do not find.


I need to:


1. open a shape file

2. add a column

3. populate this column

4. save


Any idea ?


Patrick.



Patrick, 


Thanks for your post!

I am not very clear about your third step “Populate this column” doing, following is some guide code you can take a reference:



ShapeFileFeatureSource featureSource = new ShapeFileFeatureSource(shapeFileName, ShapeFileReadWriteMode.ReadWrite);

featureSource.Open();
featureSource.BeginTransaction();
featureSource.AddColumnString("AddedFieldName", 10);
TransactionResult result = featureSource.CommitTransaction();
if (result.TotalFailureCount > 0)
{
  throw new InvalidOperationException("Some operation failed!");
}
Collection<DbfColumn> addedColumns = featureSource.GetDbfColumns();
featureSource.Close();

If you have any more questions just do not hesitate to let me know.


Thanks.


Yale



How to add column with values corresponding to already existing column values.



Hello In, 
  
 Can you describe more about your scenario? 
  
 Regards, 
  
 Gary

Actually my requirement is to color the country based on some numeric values. For example am having the numeric values corresponding to all the countries in the world and i have some range to color the country based on values. how can we achieve this in the simple and best way. I have achived this using Color based on values feature in our websamples. Is this the best way?


 



Hello In, 
  
 Thanks for your further information. 
  
 If you have multiple countries in your shapefile and want to set the color of one specifically you will need to use our ValueStyle as demonstrated in the Samples - Styles - Draw Features Based on a Value sample. In this sample you will see a ValueStyle.ColumnValue. This ColumnValue is found in the dbf of your shapefile and the ValueStyle searches this column for a the value you specify and colors as necessary. 
  
 Regards, 
  
 Gary

Hi Gary,


Thanks for your support.


I need to color the countries (multiple countries) based on my own numerical values i.e i have values corresponding to countries.


In this Scenario shall we add our own new column in the dbf of our shape file so that ValueStyle searches this column (newly added column) for the value we specify and colors as necessary.


Kindly clarify on this.


Many Thanks.



Hello In, 
  
 Yes, that’s a good way if you can process all the data before you will use it. Also there is another way, you can refer–>HowDoISamples–>DataProviders–>LoadAHeatLayer, in this sample, we use CustomColumnFetch which is a temporary column. 
  
 Regards, 
  
 Gary