How to add field to a Shapefile using DotSpatial? - dotspatial

I tried searching the web to find a sample of showing how to add a Field to attribute table of an existing shapefile. For example I have a Shapefile at
C://data/Streets.shp
and need to add two field L_CITY and R_CITY both text and 50 characters limit. How can I do this in DotSpatial?

The first thing you need to do is add a reference to System.Data. Otherwise, the type definition for the DataTable is not available and it may not be obvious what you can do to modify the schema.
Then you can use standard DataTable programming like the following code:
public void AddFieldExample()
{
IFeatureSet fs = FeatureSet.OpenFile("C:\\YourShapefile.shp");
DataTable table = fs.DataTable;
DataColumn lCity = table.Columns.Add("L_CITY");
lCity.MaxLength = 50;
DataColumn rCity = table.Columns.Add("R_CITY");
rCity.MaxLength = 50;
}

Related

With Aspose Cells, how to copy a Cell formula to another cell and update its references in .Net Core without its formating

I'm using Aspose Cells with .net Core.
I would like to copy a cell formula to another cell and update the cells references in the formula with its new "location".
If I use the cell.copy it's working but It's copy also the cell attributes, like 'locked'.
Is there way around it?
Like when using MS-Excel and you do a “Special Paste Formula Only”
I post my question on the Aspose Forum and got a quick answer from #Amjad Sahi
https://forum.aspose.com/t/copy-formula-from-a-cell-to-another-with-updated-references-without-the-source-cell-attribute-like-locked/239996/2
It's worked for me.
I end up with this:
public void CopyFormulaFrom(ICell cell)
{
var cells = cell.Sheet.Name == Sheet.Name
? _cell.Worksheet.Cells
: _cell.Worksheet.Workbook.Worksheets[cell.Sheet.Name]?.Cells;
var cellSource = cells.CreateRange(cell.Row, cell.Column, 1, 1);
var cellDestination = cells.CreateRange(_cell.Row, _cell.Column, 1, 1);
var options = new PasteOptions() { PasteType = PasteType.Formulas };
cellDestination.Copy(cellSource, options);
}

bulk insertion in MS SQL from a text file

I have a text file that contains around 21 lac entries and I want to insert all these entries into a table. Initially I have created one function in c# that read line by line and insert into table but it takes too much time. Please suggest an efficient way to insert these bulk data and that file is containing TAB(4 spaces) as delimiter.
And that text file also containing some duplicate entries and I don't want to insert those entries.
Load all of your data into a DataTable object and then use SqlBulkCopy to bulk insert them:
DataTable dtData = new DataTable("Data");
// load your data here
using (SqlConnection dbConn = new SqlConnection("db conn string"))
{
dbConn.Open();
using (SqlTransaction dbTrans = dbConn.BeginTransaction())
{
try
{
using (SqlBulkCopy dbBulkCopy = new SqlBulkCopy(dbConn, SqlBulkCopyOptions.Default, dbTrans))
{
dbBulkCopy.DestinationTableName = "intended SQL table name";
dbBulkCopy.WriteToServer(dtData );
}
dbTrans.Commit();
}
catch
{
dbTrans.Rollback();
throw;
}
}
dbConn.Close();
}
I've included the example to wrap this into a SqlTransaction so there will be a full rollback if there's a failure along the way. To get you started, here's a good CodeProject article on loading the delimited data into a DataSet object.
Sanitizing the data before loading
OK, here's how I think your data looks:
CC_FIPS FULL_NAME_ND
AN Xixerella
AN Vila
AN Sornas
AN Soldeu
AN Sispony
... (cut down for brevity)
In this instance you want to create your DataTable like this:
DataTable dtData = new DataTable("Data");
dtData.Columns.Add("CC_FIPS");
dtData.Columns.Add("FULL_NAME_ND");
Then you want to iterate each row (assuming your tab delimited data is separated row-by-row by carriage returns) and check whether this data already exists in the DataTable using the .Select method and if there is a match (i'm checking for BOTH values, it's up to you whether you want to do something else) then don't add it thereby preventing duplicates.
using (FileStream fs = new FileStream("path to your file", FileMode.Open, FileAccess.Read))
{
int rowIndex = 0;
using (StreamReader sr = new StreamReader(fs))
{
string line = string.Empty;
while (!sr.EndOfStream)
{
line = sr.ReadLine();
// use a row index to skip the header row as you don't want to insert CC_FIPS and FULL_NAME_ND
if (rowIndex > 0)
{
// split your data up into a 2-d array tab delimited
string[] parts = line.Split('\t');
// now check whether this data has already been added to the datatable
DataRow[] rows = dtData.Select("CC_FIPS = '" + parts[0] + "' and FULL_NAME_ND = '" + parts[1] + "'");
if (rows.Length == 0)
{
// if there're no rows, then the data doesn't exist so add it
DataRow nr = dtData.NewRow();
nr["CC_FIPS"] = parts[0];
nr["FULL_NAME_ND"] = parts[1];
dtData.Rows.Add(nr);
}
}
rowIndex++;
}
}
}
At the end of this you should have a sanitized DataTable that you can bulk insert. Please note that this code isn't tested, but it's a best guess as to how you should do it. There are many ways this can be done, and probably a lot better than this method (specifically LINQ) - but it's a starting point.

get value from Linq and put it in the tex box

i have this code
MasterSoapClient sp = new MasterSoapClient();
MasterData[] lstMasterData = sp.GetActivityType(stid, null, 1);
grdEditActivityType.DataSource = lstMasterData;
grdEditActivityType.DataBind();
Session["opType"] = 2;
txtActivityCode.Text = lstMasterData.ToString();
txtActivityCode.DataBind();
here i called web service and put all data in this Gridview "grdEditActivityType "
and already workin
but there is column of lstMasterData i want to put it in the text box out of the grid
how i can do this ?
txtActivityCode.Text is a Property that you can use to assign a text value to the TextBox.
If you use txtActivity.Text = " some input "; Your textbox will contain the text " some input ".
You don't need to Bind txtActivityCode afterwards.
Having this clarified the next step is to create the string you want using to assign to the text box.
string s = "";
foreach( var masterData in lstMasterData )
{
s += masterData.SomeProperty; // s += masterData.ToString(); maybe, it depends on what do you want to put in the textbox;
}
txtActivityCode.Text = s;
And that's all.
I would suggest to start look more over ASP.NET tutorials to understand better how this framework works.

Creating Dynamic Hotspots in an ASP.net imagemap

I need the ability to create a dynamic number of hotspots in an imagemap
the pseudo code for what I want to do is below:
Protected Sub AddHotSpot()
Dim r1 New RectangleHotSpot
For Each Item as datarow in dataset
r1.HotSpotMode = HotSpotMode.PostBack
r1.PostBackValue = "HotSpot 1"
r1.AlternateText = "HotSpot 1"
r1.Top = Item.Top
r1.Left = Item.Left
r1.Bottom = Item.Bottom
r1.Right = Item.Right
Next
think of r1 as some form of dynamic construct
Figured out how to perform this. I had to make the object above part of an array list then add each object to the array

ASP.net LINQ on DataView

When i have a DataView Operation as
EnumerableRowCollection<DataRow> query
= from order in _table.AsEnumerable()
where order.Field<Int32>("key") > 2 && order.Field<Int32>("key") < 4
select order.Field<Int32>("key")=1000, order.Field<string>("name");
I can't form the above expression.
When i try
select new {key= 1000,name= order.Field<string>("name") };
i got
Cannot implicitly convert type
'System.Data.EnumerableRowCollection<AnonymousType#1>'
to 'System.Data.EnumerableRowCollection<System.Data.DataRow>'
How to form the right query? My task is to replace the key with 1000 and leave the name as is.
When you write select new {key= 1000,name= order.Field<string>("name") }, you're creating a new anonymous type that has nothing to do with DataRow.
Therefore, you can't assign it to a EnumerableRowCollection<DataRow>.
To fix the compiler error, change EnumerableRowCollection<DataRow> to var.
However, that won't fix your underlying problem.
LINQ cannot be used to modify data.
You need to use a normal foreach loop and set the key values, like this:
var affectedRows = from order in _table.AsEnumerable()
where order.Field<Int32>("key") > 2 && order.Field<Int32>("key") < 4
select row;
foreach(DataRow row in affectedRows) {
row["key"] = 1000;
}
This code will modify the original DataRows in the original _table.
If you don't want to modify the original _table, you can copy it by calling DataTable.Copy().

Resources