111,129
社区成员
发帖
与我相关
我的任务
分享
private void button28_Click(object sender, EventArgs e)
{
string Inputfilepath = @"";
string Outputfilepath = @"";
string fielName_NoExt = "";
string filename1 = "", filename2 = "", filename3 = "", filename4 = "", newFilename = "", temp = "";
int count_NoData = 0;
IWorkspaceFactory pWSF_In = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFWS_In = (IFeatureWorkspace)pWSF_In.OpenFromFile(Inputfilepath, 0);
IWorkspaceFactory pWSF_Out = new ShapefileWorkspaceFactory();
IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)pWSF_Out;
if (ipWsFactoryLock.SchemaLockingEnabled)
{
ipWsFactoryLock.DisableSchemaLocking();
}
IFeatureWorkspace pFWS_Out = (IFeatureWorkspace)pWSF_Out.OpenFromFile(Outputfilepath, 0);
IWorkspaceEdit iWorkSpaEdit = pFWS_Out as IWorkspaceEdit;
List<CPoint> CSVList = new List<CPoint>();
//遍历根文件夹下的每个子文件夹
DirectoryInfo RootFolder = new DirectoryInfo(Inputfilepath);
DirectoryInfo[] allDir = RootFolder.GetDirectories();
foreach (DirectoryInfo EachDirectory in allDir)
{
//获取并设置我想要的文件名
filename1 = EachDirectory.Name.Substring(0, EachDirectory.Name.IndexOf("_"));
filename2 = EachDirectory.Name.Substring(EachDirectory.Name.IndexOf("_") + 1);
filename3 = filename2.Substring(0, filename2.IndexOf("~"));
filename4 = filename2.Substring(filename2.IndexOf("~") + 1);
newFilename = "";
if ((int.Parse(filename4.Substring(10, 2)) + 1) < 10)
{
temp = "0" + (int.Parse(filename4.Substring(10, 2)) + 1).ToString();
newFilename = filename1 + "_" + filename3.Substring(0, 8) + "_" + filename3.Substring(8, 4) + "_" +
filename4.Substring(8, 2) + temp; // BaoAnDaDaoS_20150505_0605_0610
}
else
{
temp = (int.Parse(filename4.Substring(10, 2)) + 1).ToString();
}
newFilename = filename1 + "_" + filename3.Substring(0, 8) + "_" + filename3.Substring(8, 4) + "_" +
filename4.Substring(8, 2) + temp; // BaoAnDaDaoS_20150505_0605_0610
//遍历每个子文件夹下面的每个csv文件
FileInfo[] Files = EachDirectory.GetFiles("*.csv");
foreach (FileInfo EachCSV in Files)
{
fielName_NoExt = EachCSV.Name.Substring(0, EachCSV.Name.IndexOf("."));
//获取该csv文件中所有点信息
CSVList.Clear();
CSVList = GetPoints(Inputfilepath + @"\" + EachDirectory.Name + @"\" + newFilename + ".csv");
//如果是空文件,则进行下一个csv文件
if (CSVList.Count == 0 || CSVList.Count == -1)
{
count_NoData++;
GC.Collect();
continue;
}
//复制模板shp文件
File.Copy(@"E:\templet_SHP" + @"\Model_Shapefile.shp", Outputfilepath + @"\" + fielName_NoExt + ".shp", true);
File.Copy(@"E:\templet_SHP" + @"\Model_Shapefile.dbf", Outputfilepath + @"\" + fielName_NoExt + ".dbf", true);
File.Copy(@"E:\templet_SHP" + @"\Model_Shapefile.prj", Outputfilepath + @"\" + fielName_NoExt + ".prj", true);
File.Copy(@"E:\templet_SHP" + @"\Model_Shapefile.shx", Outputfilepath + @"\" + fielName_NoExt + ".shx", true);
//获取要素类
IFeatureClass pFeatureClass = pFWS_Out.OpenFeatureClass(fielName_NoExt + ".shp");
ESRI.ArcGIS.Geometry.IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
iWorkSpaEdit.StartEditing(false);
iWorkSpaEdit.StartEditOperation();
for (int j = 0; j < CSVList.Count; j++)
{
pPoint.X = double.Parse(CSVList[j].lon);
pPoint.Y = double.Parse(CSVList[j].lat);
//创建新要素pFeature
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pPoint;
//给pFeature赋值,每个字段赋值一次
pFeature.set_Value(2, CSVList[j].GPSseq);
pFeature.set_Value(3, CSVList[j].dist);
pFeature.set_Value(4, CSVList[j].timespan);
pFeature.set_Value(5, CSVList[j].spe);
pFeature.set_Value(6, CSVList[j].control);
pFeature.set_Value(7, CSVList[j].device_ID);
pFeature.set_Value(8, CSVList[j].bus_ID);
pFeature.set_Value(9, CSVList[j].line_ID);
pFeature.set_Value(10, CSVList[j].subline_ID);
pFeature.set_Value(11, CSVList[j].corp);
pFeature.set_Value(12, CSVList[j].status);
pFeature.set_Value(13, CSVList[j].lon);
pFeature.set_Value(14, CSVList[j].lat);
pFeature.set_Value(15, CSVList[j].height);
pFeature.set_Value(16, CSVList[j].time);
pFeature.set_Value(17, CSVList[j].speed);
pFeature.set_Value(18, CSVList[j].direction);
pFeature.set_Value(19, CSVList[j].record_spe);
pFeature.set_Value(20, CSVList[j].record_km);
pFeature.set_Value(21, CSVList[j].stop_ID);
pFeature.set_Value(22, CSVList[j].arrivetime);
pFeature.set_Value(23, CSVList[j].departtime);
pFeature.set_Value(24, CSVList[j].nextstop_ID);
pFeature.Store();
Marshal.ReleaseComObject(pFeature);
}
iWorkSpaEdit.StopEditOperation();
iWorkSpaEdit.StopEditing(true);
GC.Collect();
}
}
MessageBox.Show("csv2shp完成" + @"\n" + "总共有:" + count_NoData.ToString() + "个空文件");
}
public struct CPoint
{
public string GPSseq;
public string dist;
public string timespan;
public string spe;
public string control;
public string device_ID;
public string bus_ID;
public string line_ID;
public string subline_ID;
public string corp;
public string status;
public string lon;
public string lat;
public string height;
public string time;
public string speed;
public string direction;
public string record_spe;
public string record_km;
public string stop_ID;
public string arrivetime;
public string departtime;
public string nextstop_ID;
}
private List<CPoint> GetPoints(string surveyDataFullName)
{
bool IsFirst = true;
List<CPoint> pList = new List<CPoint>();
char[] charArray = new char[] { ',' };
System.IO.FileStream FS = new System.IO.FileStream(surveyDataFullName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(FS, Encoding.Default);
string strLine = sr.ReadLine();
while (strLine != null)
{
//判断是不是第一行,字段名称行,是的话跳过
if (IsFirst == true)
{
IsFirst = false;
strLine = sr.ReadLine();
continue;
}
string[] strArray = strLine.Split(charArray);
//点信息的获取
strArray = strLine.Split(charArray);
CPoint pCPoint = new CPoint();
pCPoint.GPSseq = strArray[0];
pCPoint.dist = strArray[1];
pCPoint.timespan = strArray[2];
pCPoint.spe = strArray[3];
pCPoint.control = strArray[4];
pCPoint.device_ID = strArray[5];
pCPoint.bus_ID = strArray[6];
pCPoint.line_ID = strArray[7];
pCPoint.subline_ID = strArray[8];
pCPoint.corp = strArray[9];
pCPoint.status = strArray[10];
pCPoint.lon = strArray[11];
pCPoint.lat = strArray[12];
pCPoint.height = strArray[13];
pCPoint.time = strArray[14];
pCPoint.speed = strArray[15];
pCPoint.direction = strArray[16];
pCPoint.record_spe = strArray[17];
pCPoint.record_km = strArray[18];
pCPoint.stop_ID = strArray[19];
pCPoint.arrivetime = strArray[20];
pCPoint.departtime = strArray[21];
pCPoint.nextstop_ID = strArray[22];
pList.Add(pCPoint);
strLine = sr.ReadLine();
}
sr.Close();
return pList;
}