如何在C#中将csv转换为shapefile? [英] How do I convert csv to shapefile in C#?

查看:112
本文介绍了如何在C#中将csv转换为shapefile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的CSV文件 -



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 ,227 2.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必须将其转换为shapefile。点X和Y是多点,它们是多边形,我们只能通过ID区分它们,因为单个多边形有一个ID。

如何将其转换为shapefile?



我尝试过:



public void ConvertCSV2SHP(string strFilePath)

{

使用(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;我< rows.Length; i ++)

{

SHP.ShapeFields [i + 1] .set_Value(Convert.ToDouble(rows [i]));

}

SHP.CreateShape();

}

}

}



但是当我尝试这个代码时出现错误System.FormatException:'输入字符串的格式不正确。'

我该怎么办?

int span> i = 0 ; i < rows.Length; i ++)
{
SHP.ShapeFields [i + 1 ]。set_Value(Convert.ToDouble(rows [i]));
}



并非所有字段值都包含数字,因此对于字段1和4,这将失败。使用<$也不是一个好主意c $ c> Convert.ToDouble 转换值。使用 Double.TryParse方法(字符串,双精度)(系统) [ ^ ]所以你可以查看结果。


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?

解决方案

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.


这篇关于如何在C#中将csv转换为shapefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆