public int GetOrdinal(string name) { for (int i = 0; i < _dbfReader.GetHeader().NumFields; i++) { if (0 == CultureAwareCompare(_dbfReader.GetHeader().Fields[i].Name, name)) { return(i); } } // Throw an exception if the ordinal cannot be found. throw new IndexOutOfRangeException("Could not find specified column in results."); }
public void Test_TestProperties() { DbaseFileReader dbfReader = new DbaseFileReader(Global.GetUnitTestRootDirectory()+@"\IO\Shapefile\Testfiles\statepop.dbf"); DbaseFileHeader dbfHeader = dbfReader.GetHeader(); Assertion.AssertEquals("Dbase header: Num records", 49, dbfHeader.NumRecords); Assertion.AssertEquals("Dbase header: Num fields", 252, dbfHeader.NumFields); Assertion.AssertEquals("Field 0: name", "STATE_NAME", dbfHeader.Fields[0].Name); Assertion.AssertEquals("Field 0: name", 'C', dbfHeader.Fields[0].DbaseType); Assertion.AssertEquals("Field 0: name", typeof(string), dbfHeader.Fields[0].Type); Assertion.AssertEquals("Field 0: name", 25, dbfHeader.Fields[0].Length); Assertion.AssertEquals("Field 251: name", "SAMP_POP", dbfHeader.Fields[251].Name); Assertion.AssertEquals("Field 251: name", 'N', dbfHeader.Fields[251].DbaseType); Assertion.AssertEquals("Field 251: name", typeof(double), dbfHeader.Fields[251].Type); Assertion.AssertEquals("Field 251: name", 19, dbfHeader.Fields[251].Length); // note alaska and hawaii are missing - hence 48 states not 50. int i=0; foreach(ArrayList columnValues in dbfReader) { if (i==0) { Assertion.AssertEquals("Row1: STATE_NAME:","Illinois", columnValues[0]); Assertion.AssertEquals("Row1: STATE_FIPS:","17", columnValues[1].ToString()); Assertion.AssertEquals("Row1: SAMP_POP", 1747776.0, columnValues[251]); } if (i==48) { Assertion.AssertEquals("Row1: STATE_NAME:","Washington", columnValues[0]); Assertion.AssertEquals("Row1: STATE_FIPS:","53", columnValues[1].ToString()); Assertion.AssertEquals("Row1: SAMP_POP", 736744.0, columnValues[251]); } i++; } Assertion.AssertEquals("48 Records",48,i-1); DbaseFileReader dbfReader2 = new DbaseFileReader(Global.GetUnitTestRootDirectory()+@"\IO\Shapefile\Testfiles\statepop.dbf"); i=0; foreach(ArrayList columnValues in dbfReader2) { if (i==0) { Assertion.AssertEquals("Row1: STATE_NAME:","Illinois", columnValues[0]); Assertion.AssertEquals("Row1: STATE_FIPS:","17", columnValues[1].ToString()); Assertion.AssertEquals("Row1: SAMP_POP", 1747776.0, columnValues[251]); } if (i==48) { Assertion.AssertEquals("Row1: STATE_NAME:","Washington", columnValues[0]); Assertion.AssertEquals("Row1: STATE_FIPS:","53", columnValues[1].ToString()); Assertion.AssertEquals("Row1: SAMP_POP", 736744.0, columnValues[251]); } i++; } Assertion.AssertEquals("48 Records",48,i-1); }
//byte[] _wkb; // GeometryWKBWriter _wkbWriter; #region Constructors /// <summary> /// Initializes a new instance of the ShapefileDataReader class. /// </summary> /// <param name="filename">The shapefile to read (minus the .shp extension)</param> ///<param name="geometryFactory">The GeometryFactory to use.</param> public ShapefileDataReader(string filename, GeometryFactory geometryFactory) { if (filename == null) { throw new ArgumentNullException("filename"); } if (geometryFactory == null) { throw new ArgumentNullException("geometryFactory"); } _geometryFactory = geometryFactory; _open = true; if (filename.ToLower().EndsWith(".shp")) { filename = filename.ToLower().Replace(".shp", ""); } _dbfReader = new DbaseFileReader(filename + ".dbf"); _shpReader = new ShapefileReader(filename + ".shp", geometryFactory); _dbfHeader = _dbfReader.GetHeader(); _recordCount = _dbfHeader.NumRecords; // _wkbWriter = new GeometryWKBWriter(_geometryFactory); // copy dbase fields to our own array. Insert into the first position, the shape column _dbaseFields = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1]; _dbaseFields[0] = DbaseFieldDescriptor.ShapeField(); //_dbaseFields[1] = DbaseFieldDescriptor.IdField(); for (int i = 0; i < _dbfHeader.Fields.Length; i++) { _dbaseFields[i + 1] = _dbfHeader.Fields[i]; } _shpHeader = _shpReader.Header; _dbfEnumerator = _dbfReader.GetEnumerator(); _shpEnumerator = _shpReader.GetEnumerator(); _moreRecords = true; }
/// <summary> /// Initializes a new instance of the ShapefileDataReader class. /// </summary> /// <param name="filename">The shapefile to read (minus the .shp extension)</param> ///<param name="geometryFactory">The GeometryFactory to use.</param> public ShapefileDataReader(string filename, GeometryFactory geometryFactory) { if (filename==null) { throw new ArgumentNullException("filename"); } if (geometryFactory==null) { throw new ArgumentNullException("geometryFactory"); } _geometryFactory = geometryFactory; _open=true; if (filename.ToLower().EndsWith(".shp")) { filename = filename.ToLower().Replace(".shp",""); } _dbfReader = new DbaseFileReader(filename+".dbf"); _shpReader = new ShapefileReader(filename+".shp", geometryFactory); _dbfHeader = _dbfReader.GetHeader(); _recordCount = _dbfHeader.NumRecords; // _wkbWriter = new GeometryWKBWriter(_geometryFactory); // copy dbase fields to our own array. Insert into the first position, the shape column _dbaseFields = new DbaseFieldDescriptor[_dbfHeader.Fields.Length + 1]; _dbaseFields[0] = DbaseFieldDescriptor.ShapeField(); //_dbaseFields[1] = DbaseFieldDescriptor.IdField(); for(int i=0; i < _dbfHeader.Fields.Length; i++) { _dbaseFields[i+1] = _dbfHeader.Fields[i]; } _shpHeader = _shpReader.Header; _dbfEnumerator = _dbfReader.GetEnumerator(); _shpEnumerator = _shpReader.GetEnumerator(); _moreRecords = true; }