Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have CSV file like this-

ID,LABEL,X,Y,USER
4,PlatesPatches,2451,1178.86393659181,Rashmitha
4,PlatesPatches,1344.14795244386,772.945838837517,Rashmitha
4,PlatesPatches,1392.76089828269,772.945838837517,Rashmitha
4,PlatesPatches,1840,899.339498018494,Rashmitha
4,PlatesPatches,2450.09247027741,1052.47027741083,Rashmitha
5,SmallPatchesmosaicpavement,2451,1020.87186261559,Rashmitha
5,SmallPatchesmosaicpavement,2451,1057.33157199472,Rashmitha
5,SmallPatchesmosaicpavement,1815.69352708058,894.47820343461,Rashmitha
5,SmallPatchesmosaicpavement,1516.72391017173,809.405548216645,Rashmitha
5,SmallPatchesmosaicpavement,1908.05812417437,875.033025099075,Rashmitha
5,SmallPatchesmosaicpavement,2012.57595772787,896.908850726552,Rashmitha
6,LargePavingnaturalstoneslabs,2451,1295.53500660502,Rashmitha
6,LargePavingnaturalstoneslabs,2272.65521796565,1217.75429326288,Rashmitha
6,LargePavingnaturalstoneslabs,2211.88903566711,1174.00264200793,Rashmitha
6,LargePavingnaturalstoneslabs,2451,1176.43328929987,Rashmitha
7,SmallPatchesmosaicpavement,2228.9035667107,1181.29458388375,Rashmitha
7,SmallPatchesmosaicpavement,1575.05944517834,875.033025099075,Rashmitha
7,SmallPatchesmosaicpavement,1356.30118890357,775.376486129458,Rashmitha
7,SmallPatchesmosaicpavement,1387.89960369881,775.376486129458,Rashmitha
7,SmallPatchesmosaicpavement,1662.56274768824,884.755614266843,Rashmitha
7,SmallPatchesmosaicpavement,2451,1178.86393659181,Rashmitha

i have to convert it into shapefile. the points X and Y are the multipoints and they are polygons, we can only differentiate them by ID because single polygon has a single ID.
how do i convert it into shapefile?

What I have tried:

public void ConvertCSV2SHP(string strFilePath)
{
using (StreamReader sr = new StreamReader(strFilePath))
{
ShapeFiles SHP = new ShapeFiles();
ShapeField NewFld;
Vertice NewVert;
SHP.OpenShape(strFilePath.Substring(0, strFilePath.Length - 3) + "shp", eNew.shpCreate, eShapeType.shpPolygonM);

NewFld = SHP.ShapeFields.CreateField("ID", eFieldType.shpInteger);
NewFld = SHP.ShapeFields.CreateField("LABEL", eFieldType.shpText);
NewFld = SHP.ShapeFields.CreateField("X", eFieldType.shpDouble);
NewFld = SHP.ShapeFields.CreateField("Y", eFieldType.shpDouble);
NewFld = SHP.ShapeFields.CreateField("USER", eFieldType.shpDouble);

SHP.AppendFieldDefs();
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
NewVert = SHP.Vertices.AddVertice(Convert.ToDouble(rows[2]), Convert.ToDouble(rows[3]));
for (int i = 0; i < rows.Length; i++)
{
SHP.ShapeFields[i + 1].set_Value(Convert.ToDouble(rows[i]));
}
SHP.CreateShape();
}
}
}

but when i tried this code error is coming "System.FormatException: 'Input string was not in a correct format.'"
what should i do?
Posted

1 solution

C#
for (int i = 0; i < rows.Length; i++)
{
SHP.ShapeFields[i + 1].set_Value(Convert.ToDouble(rows[i]));
}

Not all of your field values contain numbers, so this will fail for fields 1 and 4. It is also not a good idea to use Convert.ToDouble to convert values. Use Double.TryParse Method (String, Double) (System)[^] so you can check the result.
 
Share this answer
 
Comments
Member 13458399 27-Nov-17 2:18am    
still showing : System.FormatException: 'Input string was not in a correct format.'
Richard MacCutchan 27-Nov-17 3:25am    
I have explained what that message means and why it occurs. You need to look closely at your code, or use your debugger, to find exactly what input field is causing it.
Member 13458399 29-Nov-17 7:18am    
yes i got it.Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900