Hi, I am reading from a CSV file, using Lat, Long to create a line shapefile (This is done successfully), at the same time, creating fields and adding values from the CSV to the shapefile. The weird thing is that If I hard code the field index and row (or shape) index, I get that value in the shapefile attributes table. But as I pass fieldIndex, r (row index), nothing inserted. The main part of the code is here. Would anybody give me some clue? Or someone can share your code of creating a polyline shapefile and add attributes to it?
I added a test righter after I insert the value, the value is there in debugging, but the resulted attribute table didn’t have it.
object testStatic = sf.CellValue[2, 2];
I am using VS 2015 on Windows 10. MapWinGIS 5.0, doing windows form app.
Thanks a lot in advance!!
Lang
for (int r = 1; r < lines.Length-1; r++) //r =0 is the first line which is headers
{
string[] dataValue = lines[r].Split(',');
string[] dataValue1 = { };
sf.Table.EditInsertRow(ref r);
//MessageBox.Show(dataValue[5]);
if (r + 1 < lines.Length-1) {
dataValue1 = lines[r + 1].Split(',');
}
//char[] charsToTrim = { '*', '"', '\'' };
for (int i = 1; i < columnCount; i++)
{
//MessageBox.Show(headerLabels[i]);
headerLabels[i] = headerLabels[i].Replace("\"","");
int fieldIndex = sf.Table.get_FieldIndexByName(headerLabels[i]);
if (fieldIndex == -1)
{
//make IRI field a double to get ready for color-coding
if (headerLabels[i].Contains("IRI"))
{
fieldIndex = sf.EditAddField(headerLabels[i], FieldType.DOUBLE_FIELD, 6, 9);
}
else
{
fieldIndex = sf.EditAddField(headerLabels[i], FieldType.STRING_FIELD, 0, 30);
}
}
//MessageBox.Show(fieldIndex.ToString()+","+r.ToString() + ","+dataValue[i]);
bool b = sf.EditCellValue(fieldIndex, r, dataValue[i]);
object test = sf.CellValue[fieldIndex, r];
//sf.Table.EditCellValue(fieldIndex, r, dataValue[i].ToString())
bool b1 = sf.EditCellValue(2, 2, dataValue[i]);
object testStatic = sf.CellValue[2, 2];
sf.EditCellValue(3, 9, dataValue[i]);
sf.EditCellValue(4, 5, dataValue[i]);
Console.WriteLine($"{b} {fieldIndex} {r} {dataValue[i]} {i}");
}
Shape shp = new Shape();
shp.Create(ShpfileType.SHP_POLYLINE);
if (dataValue1.Length >= 4)
{
x = Convert.ToDouble(dataValue[selectedIndexX]);
y = Convert.ToDouble(dataValue[selectedIndexY]);
x1 = Convert.ToDouble(dataValue1[selectedIndexX]);
y1 = Convert.ToDouble(dataValue1[selectedIndexY]);
if (x.ToString() == null || y.ToString() == null)
{
MessageBox.Show("X or Y value cannot be null!");
return;
}
MapWinGIS.Point pnt = new MapWinGIS.Point();
pnt.x = x;
pnt.y = y;
int index = shp.numPoints;
shp.InsertPoint(pnt, ref index);
pnt = new MapWinGIS.Point();
pnt.x = x1;
pnt.y = y1;
index = shp.numPoints;
shp.InsertPoint(pnt, ref index);
index = sf.NumShapes;
sf.EditInsertShape(shp, ref index);
}
}