Convert TXT format data to DBF format (C #)

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. IO;
Using ESRI. ArcGIS. Geodatabase;
Using ESRI. ArcGIS. esrisystem;
Using system. Windows. forms;
Using ESRI. ArcGIS. datasourcesfile;

Namespace txttodbf
{
Public class txttodbf
{
Private string [] _ listname;

Public String [] listname
{
Get {return _ listname ;}
Set {_ listname = value ;}
}
Private string _ filename;

Public String filename
{
Get {return _ filename ;}
Set {_ filename = value ;}
}
Private string _ filedwater;

Public String filedwater
{
Get {return _ filedwater ;}
Set {_ filedwater = value ;}
}
Private string _ filedtemp;

Public String filedtemp
{
Get {return _ filedtemp ;}
Set {_ filedtemp = value ;}
}
Private string _ outpathwater;

Public String outpathwater
{
Get {return _ outpathwater ;}
Set {_ outpathwater = value ;}
}
Private string _ outpathtemp;

Public String outpathtemp
{
Get {return _ outpathtemp ;}
Set {_ outpathtemp = value ;}
}

/// <Summary>
/// Constructor
/// </Summary>
Public txttodbf (string [] listname, string filename, string filedwater, string filedtemp, string outpathwater, string outpathtemp)
{
This. listname = listname;
This. filename = filename;
This. filedwater = filedwater;
This. filedtemp = filedtemp;
This. outpathwater = outpathwater;
This. outpathtemp = outpathtemp;
}

Public txttodbf ()
{
}
/// <Summary>
/// Read the first line of TXT data and obtain the Data Location index
/// </Summary>
Private int [] methodtxt ()
{
Int [] local = new int [4];
Using (streamreader sr = new streamreader (filename) // read the file txt
{
String Sline = Sr. Readline ();
String dline = Sline. Trim ();
If (dline. Length <1)
{
Return NULL;
}
Else
{
// Break down text
List <string> strlist = new list <string> (); // generic
String [] STRs = dline. Split ('');
For (INT I = 0; I <STRs. length; I ++)
{
If (STRs [I]! = "") // Filter and split
{
Strlist. Add (STRs [I]);
}
}

For (INT I = 0; I <strlist. Count; I ++)
{
If (strlist [I]. tostring () = "station ID ")
{
Local [0] = I;
}
}
For (INT I = 0; I <strlist. Count; I ++)
{
If (strlist [I]. tostring () = "month ")
{
Local [1] = I;
}
}
For (INT I = 0; I <strlist. Count; I ++)
{
If (strlist [I]. tostring () = "precipitation ")
{
Local [2] = I;
}
}
For (INT I = 0; I <strlist. Count; I ++)
{
If (strlist [I]. tostring () = "average temperature ")
{
Local [3] = I;
}
}
}
}
Return local;
}
/// <Summary>
/// Read TXT data and export DBF
/// </Summary>
Public void methodtxttodbf ()
{
Try
{
Int [] local = methodtxt ();
Itable watertable = bulitwatertable ();
Itable temptable = bulittemptable ();
Dictionary <string, irow> dicwater = new dictionary <string, irow> ();
Dictionary <string, irow> dictemp = new dictionary <string, irow> ();
Int iline = 0;
Using (streamreader sr = new streamreader (filename) // read the file txt
{
While (! Sr. endofstream)
{

Iline ++;
String Sline = Sr. Readline ();

If (Sline. Length <1 | iline = 1)
{
Continue;
}
Else
{
String dline = Sline. Trim ();
// Break down text
List <string> strlist = new list <string> (); // generic
String [] STRs = dline. Split ('');
For (INT I = 0; I <STRs. length; I ++)
{
If (STRs [I]! = "") // Filter and split
{
Strlist. Add (STRs [I]);
}
}
String station = strlist [local [0];
// Precipitation
If (! Dicwater. containskey (Station ))
{
Irow prow;
Int I = watertable. findfield (listname [0]);
Prow = watertable. createrow ();
Prow. set_value (I, station );
Dicwater. Add (station, prow );
Int wmouth = convert. toint32 (strlist [local [1]);
Int WM = wmouth + 1;
Prow. set_value (WM, strlist [local [2]);
Prow. Store ();
}
Else
{
Irow prow = dicwater [station];
Int wmouth = convert. toint32 (strlist [local [1]);
Int WM = wmouth + 1;
Prow. set_value (WM, strlist [local [2]);
Prow. Store ();
}

//
If (! Dictemp. containskey (Station ))
{
Irow Trow;
Int J = temptable. findfield (listname [0]);
Trow = temptable. createrow ();
Trow. set_value (J, station );
Dictemp. Add (station, Trow );
Int tmouth = convert. toint32 (strlist [local [1]);
Int TM = tmouth + 1;
Trow. set_value (TM, strlist [local [2]);
Trow. Store ();
}
Else
{
Irow Trow = dictemp [station];
Int tmouth = convert. toint32 (strlist [local [1]);
Int TM = tmouth + 1;
Trow. set_value (TM, strlist [local [3]);
Trow. Store ();
}

}

}
}
}
Catch (system. Exception ex)
{
MessageBox. Show (ex. Message );
}

}
/// <Summary>
/// Generate the DBF table
/// </Summary>
Private itable bulitwatertable ()
{
// Parse the file name
String myfactorypath = system. Io. Path. getdirectoryname (outpathwater );
Iworkspacefactory pshpfactory = new shapefileworkspacefactoryclass ();
Ifeatureworkspace pshpws = pshpfactory. openfromfile (myfactorypath, 0) as ifeatureworkspace;

// Create a result File
Itable ptable = This. createwatertable (pshpws );
If (ptable = NULL)
Return NULL;
Return ptable;
}
/// <Summary>
/// Generate the DBF table
/// </Summary>
Private itable bulittemptable ()
{
// Parse the file name
String myfactorypath = system. Io. Path. getdirectoryname (outpathtemp );
Iworkspacefactory pshpfactory = new shapefileworkspacefactoryclass ();
Ifeatureworkspace pshpws = pshpfactory. openfromfile (myfactorypath, 0) as ifeatureworkspace;

// Create a result File
Itable ptable = This. createtemptable (pshpws );
If (ptable = NULL)
Return NULL;
Return ptable;
}
/// <Summary>
/// Create a DBF table
/// </Summary>
/// <Param name = "pfeatureworkspace"> </param>
/// <Param name = "pfeatureclass"> </param>
/// <Returns> </returns>
Private itable createwatertable (ifeatureworkspace pfeatureworkspace)
{
Try
{
// Create required parameters
ESRI. ArcGIS. esrisystem. uid puid = new ESRI. ArcGIS. esrisystem. uidclass ();
Puid. value = "esrigeodatabase. Object ";
ESRI. ArcGIS. Geodatabase. iobjectclassdescription pobjectclassdescription = new ESRI. ArcGIS. Geodatabase. objectclassdescriptionclass ();
Ifields pfields = pobjectclassdescription. requiredfields;
Ifieldsedit pfieldsedit = pfields as ifieldsedit;

// Site Field
Ifield pstationfield = new fieldclass ();
Ifieldedit pstationfieldedit = pstationfield as ifieldedit;
Pstationfieldedit. name_2 = listname [0];
Pstationfieldedit. type_2 = esrifieldtype. esrifieldtypestring;
Pfieldsedit. addfield (pstationfield );

// Month field
For (INT I = 1; I <13; I ++)
{
Ifield pcodecountfield = new fieldclass ();
Ifieldedit pcodecountfieldedit = pcodecountfield as ifieldedit;
Pcodecountfieldedit. name_2 = listname [I];
Pcodecountfieldedit. type_2 = esrifieldtype. esrifieldtypeinteger;
Pfieldsedit. addfield (pcodecountfield );
}
// Average Value Field
Ifield pavefield = new fieldclass ();
Ifieldedit pavefieldedit = pavefield as ifieldedit;
Pavefieldedit. name_2 = listname [13];
Pavefieldedit. type_2 = esrifieldtype. esrifieldtypedouble;
Pfieldsedit. addfield (pavefield );

// Field check
ESRI. ArcGIS. Geodatabase. ifieldchecker fieldchecker = new ESRI. ArcGIS. Geodatabase. fieldcheckerclass ();
ESRI. ArcGIS. Geodatabase. ienumfielderror enumfielderror = NULL;
ESRI. ArcGIS. Geodatabase. ifields validatedfields = NULL;
Fieldchecker. validateworkspace = (ESRI. ArcGIS. Geodatabase. iworkspace) pfeatureworkspace;
Fieldchecker. Validate (pfields, out enumfielderror, out validatedfields );
// Create
Itable ptable = pfeatureworkspace. createtable (system. Io. Path. getfilenamewithoutextension (outpathwater), validatedfields, puid, null ,"");

Return ptable;
}
Catch (system. Exception ex)
{
MessageBox. Show (ex. Message );
Return NULL;
}
}

/// <Summary>
/// Create a DBF table
/// </Summary>
/// <Param name = "pfeatureworkspace"> </param>
/// <Param name = "pfeatureclass"> </param>
/// <Returns> </returns>
Private itable createtemptable (ifeatureworkspace pfeatureworkspace)
{
Try
{
// Create required parameters
ESRI. ArcGIS. esrisystem. uid puid = new ESRI. ArcGIS. esrisystem. uidclass ();
Puid. value = "esrigeodatabase. Object ";
ESRI. ArcGIS. Geodatabase. iobjectclassdescription pobjectclassdescription = new ESRI. ArcGIS. Geodatabase. objectclassdescriptionclass ();
Ifields pfields = pobjectclassdescription. requiredfields;
Ifieldsedit pfieldsedit = pfields as ifieldsedit;

// Site Field
Ifield pstationfield = new fieldclass ();
Ifieldedit pstationfieldedit = pstationfield as ifieldedit;
Pstationfieldedit. name_2 = listname [0];
Pstationfieldedit. type_2 = esrifieldtype. esrifieldtypestring;
Pfieldsedit. addfield (pstationfield );

// Month field
For (INT I = 1; I <13; I ++)
{
Ifield pcodecountfield = new fieldclass ();
Ifieldedit pcodecountfieldedit = pcodecountfield as ifieldedit;
Pcodecountfieldedit. name_2 = listname [I];
Pcodecountfieldedit. type_2 = esrifieldtype. esrifieldtypeinteger;
Pfieldsedit. addfield (pcodecountfield );
}
// Average Value Field
Ifield pavefield = new fieldclass ();
Ifieldedit pavefieldedit = pavefield as ifieldedit;
Pavefieldedit. name_2 = listname [13];
Pavefieldedit. type_2 = esrifieldtype. esrifieldtypedouble;
Pfieldsedit. addfield (pavefield );
// Field check
ESRI. ArcGIS. Geodatabase. ifieldchecker fieldchecker = new ESRI. ArcGIS. Geodatabase. fieldcheckerclass ();
ESRI. ArcGIS. Geodatabase. ienumfielderror enumfielderror = NULL;
ESRI. ArcGIS. Geodatabase. ifields validatedfields = NULL;
Fieldchecker. validateworkspace = (ESRI. ArcGIS. Geodatabase. iworkspace) pfeatureworkspace;
Fieldchecker. Validate (pfields, out enumfielderror, out validatedfields );
// Create
Itable ptable = pfeatureworkspace. createtable (system. Io. Path. getfilenamewithoutextension (outpathtemp), validatedfields, puid, null ,"");

Return ptable;
}
Catch (system. Exception ex)
{
MessageBox. Show (ex. Message );
Return NULL;
}
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.