Esempio n. 1
0
        /// <summary>
        /// Helper function for SetupHeader, correctly maps C# data types to DbaseFileHeader types
        /// </summary>
        /// <param name="header"></param>
        /// <param name="columnName"></param>
        /// <param name="t"></param>
        public static void AddColumn(DbaseFileHeader header, string columnName, Type t)
        {
            columnName = Utilities.EnsureMaxLength(columnName, 10);

            if (t == typeof(bool))
            {
                header.AddColumn(columnName, 'L', 1, 0);
            }
            else if (t == typeof(string))
            {
                header.AddColumn(columnName, 'C', 254, 0);
            }
            else if (t == typeof(DateTime))
            {
                header.AddColumn(columnName, 'C', 22, 0);
            }
            else if (t == typeof(float) || t == typeof(double) || t == typeof(decimal))
            {
                header.AddColumn(columnName, 'N', 18, 10);
            }
            else if (t == typeof(short) || t == typeof(int) || t == typeof(long)
                || t == typeof(ushort) || t == typeof(uint) || t == typeof(ulong))
            {
                header.AddColumn(columnName, 'N', 18, 0);
            }
        }
 /// <summary>
 /// Gets the stub header.
 /// </summary>
 /// <param name="feature">The feature.</param>
 /// <param name="count">The count.</param>
 /// <returns></returns>
 public static DbaseFileHeader GetHeader(Feature feature, int count)
 {
     IAttributesTable attribs = feature.Attributes;
     string[] names = attribs.GetNames();
     DbaseFileHeader header = new DbaseFileHeader();
     header.NumRecords = count;
     foreach (string name in names)
     {
         Type type = attribs.GetType(name);
         if (type == typeof(double) || type == typeof(float))
             header.AddColumn(name, 'N', DoubleLength, DoubleDecimals);
         else if (type == typeof(short) || type == typeof(ushort) ||
                  type == typeof(int) || type == typeof(uint) ||
                  type == typeof(long) || type == typeof(ulong))
             header.AddColumn(name, 'N', IntLength, IntDecimals);
         else if (type == typeof(string))
             header.AddColumn(name, 'C', StringLength, StringDecimals);
         else if (type == typeof(bool))
             header.AddColumn(name, 'L', BoolLength, BoolDecimals);
         else if (type == typeof(DateTime))
             header.AddColumn(name, 'D', DateLength, DateDecimals);
         else throw new ArgumentException("Type " + type.Name + " not supported");
     }
     return header;
 }
 /// <summary>
 /// Gets the header from a dbf file.
 /// </summary>
 /// <param name="dbfFile">The DBF file.</param>
 /// <returns></returns>
 public static DbaseFileHeader GetHeader(string dbfFile)
 {
     if (!File.Exists(dbfFile))
         throw new FileNotFoundException(dbfFile + " not found");
     DbaseFileHeader header = new DbaseFileHeader();
     header.ReadHeader(new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.Read)));
     return header;
 }
Esempio n. 4
0
            /// <summary>
            ///
            /// </summary>
            protected void ReadHeader()
            {
                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(_dbfStream);

                // how many records remain
                _readPosition = _header.HeaderLength;
            }
Esempio n. 5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="header"></param>
 public void Write(DbaseFileHeader header)
 {
     if (header == null)
         throw new ArgumentNullException("header");
     if (recordsWritten)
         throw new InvalidOperationException("Records have already been written. Header file needs to be written first.");
     headerWritten = true;
     header.WriteHeader(_writer);
     _header = header;
 }
        public static DbaseFileHeader GetHeader(DbaseFieldDescriptor[] dbFields, int count)
        {
            DbaseFileHeader header = new DbaseFileHeader();
            header.NumRecords = count;

            foreach (DbaseFieldDescriptor dbField in dbFields)
                header.AddColumn(dbField.Name, dbField.DbaseType, dbField.Length, dbField.DecimalCount);

            return header;
        }
Esempio n. 7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="header"></param>
 public void Write(DbaseFileHeader header)
 {
     if (header == null)
         throw new ArgumentNullException("header");
     if (recordsWritten)
         throw new InvalidOperationException("Records have already been written. Header file needs to be written first.");
     headerWritten = true;
     header.WriteHeader(_writer);
     _header = header;
 }
Esempio n. 8
0
        /// <summary>
        /// Gets the header from a dbf file.
        /// </summary>
        /// <param name="dbfFile">The DBF file.</param>
        /// <returns></returns>
        public static DbaseFileHeader GetHeader(string dbfFile)
        {
            if (!File.Exists(dbfFile))
            {
                throw new FileNotFoundException(dbfFile + " not found");
            }
            DbaseFileHeader header = new DbaseFileHeader();

            header.ReadHeader(new BinaryReader(new FileStream(dbfFile, FileMode.Open, FileAccess.Read, FileShare.Read)));
            return(header);
        }
Esempio n. 9
0
        public static DbaseFileHeader GetHeader(DbaseFieldDescriptor[] dbFields, int count)
        {
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;

            foreach (DbaseFieldDescriptor dbField in dbFields)
            {
                header.AddColumn(dbField.Name, dbField.DbaseType, dbField.Length, dbField.DecimalCount);
            }

            return(header);
        }
Esempio n. 10
0
        /// <summary>
        /// Gets the header information for the dbase file.
        /// </summary>
        /// <returns>DbaseFileHeader contain header and field information.</returns>
        public DbaseFileHeader GetHeader()
        {
            if (_header == null)
            {
                FileStream   stream    = new FileStream(_filename, FileMode.Open, FileAccess.Read);
                BinaryReader dbfStream = new BinaryReader(stream);

                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(dbfStream);

                dbfStream.Close();
                stream.Close();
            }
            return(_header);
        }
Esempio n. 11
0
        /// <summary>
        /// Gets the header information for the dbase file.
        /// </summary>
        /// <returns>DbaseFileHeader contain header and field information.</returns>
        public DbaseFileHeader GetHeader()
        {
            if (_header==null)
            {
                FileStream stream = new FileStream(_filename, FileMode.Open, FileAccess.Read);
                BinaryReader dbfStream = new BinaryReader(stream);

                _header = new DbaseFileHeader();
                // read the header
                _header.ReadHeader(dbfStream);

                dbfStream.Close();
                stream.Close();

            }
            return _header;
        }
Esempio n. 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recordCount"></param>
        public static void WriteDummyDbf(string filename, int recordCount)
        {
            DbaseFileHeader dbfHeader = new DbaseFileHeader();

            dbfHeader.AddColumn("Description", 'C', 20, 0);

            DbaseFileWriter dbfWriter = new DbaseFileWriter(filename);

            dbfWriter.Write(dbfHeader);
            for (int i = 0; i < recordCount; i++)
            {
                ArrayList columnValues = new ArrayList();
                columnValues.Add((double)i);
                dbfWriter.Write(columnValues);
            }
            dbfWriter.Close();
        }
Esempio n. 13
0
        /// <summary>
        /// Gets the stub header.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        public static DbaseFileHeader GetHeader(Feature feature, int count)
        {
            IAttributesTable attribs = feature.Attributes;

            string[]        names  = attribs.GetNames();
            DbaseFileHeader header = new DbaseFileHeader();

            header.NumRecords = count;
            foreach (string name in names)
            {
                Type type = attribs.GetType(name);
                if (type == typeof(double) || type == typeof(float))
                {
                    header.AddColumn(name, 'N', DoubleLength, DoubleDecimals);
                }
                else if (type == typeof(short) || type == typeof(ushort) ||
                         type == typeof(int) || type == typeof(uint) ||
                         type == typeof(long) || type == typeof(ulong))
                {
                    header.AddColumn(name, 'N', IntLength, IntDecimals);
                }
                else if (type == typeof(string))
                {
                    header.AddColumn(name, 'C', StringLength, StringDecimals);
                }
                else if (type == typeof(bool))
                {
                    header.AddColumn(name, 'L', BoolLength, BoolDecimals);
                }
                else if (type == typeof(DateTime))
                {
                    header.AddColumn(name, 'D', DateLength, DateDecimals);
                }
                else
                {
                    throw new ArgumentException("Type " + type.Name + " not supported");
                }
            }
            return(header);
        }
        /// <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, IGeometryFactory 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", String.Empty);
            }

            _dbfReader = new DbaseFileReader(filename + ".dbf");
            _shpReader = new ShapefileReader(filename + ".shp", geometryFactory);

            _dbfHeader   = _dbfReader.GetHeader();
            _recordCount = _dbfHeader.NumRecords;

            // 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();
            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;
        }
Esempio n. 15
0
        private void SaveGraphResult(IGeometry path)
        {
            if (path == null) 
                throw new ArgumentNullException("path");

            var shapepath = "graphresult";
            if (File.Exists(shapepath + shp))
                File.Delete(shapepath + shp);
            Assert.IsFalse(File.Exists(shapepath + shp));
            if (File.Exists(shapepath + shx))
                File.Delete(shapepath + shx);
            Assert.IsFalse(File.Exists(shapepath + shx));
            if (File.Exists(shapepath + dbf))
                File.Delete(shapepath + dbf);
            Assert.IsFalse(File.Exists(shapepath + dbf));

            var field1 = "OBJECTID";            
            var feature = new Feature(path, new AttributesTable());
            feature.Attributes.AddAttribute(field1, 0);                        

            var header = new DbaseFileHeader {NumRecords = 1, NumFields = 1};
            header.AddColumn(field1, 'N', 5, 0);
            
            var writer = new ShapefileDataWriter(shapepath, factory) {Header = header};
            writer.Write(new List<Feature>(new[] { feature, }));

            Assert.IsTrue(File.Exists(shapepath + shp));
            Assert.IsTrue(File.Exists(shapepath + shx));
            Assert.IsTrue(File.Exists(shapepath + dbf));
        }
Esempio n. 16
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="recordCount"></param>
		public static void WriteDummyDbf(string filename, int recordCount)
		{
			DbaseFileHeader dbfHeader = new DbaseFileHeader();
			dbfHeader.AddColumn("Description",'C', 20, 0);
			
			DbaseFileWriter dbfWriter = new DbaseFileWriter(filename);
			dbfWriter.Write(dbfHeader);
			for (int i = 0; i < recordCount; i++)
			{
				ArrayList columnValues = new ArrayList();
				columnValues.Add((double) i);                
				dbfWriter.Write(columnValues);
			}
			dbfWriter.Close();
		}
Esempio n. 17
0
        public void BuildShapefilesFromGraphBinary()
        {
            int index = 0;
            IGeometry edges;
            WKBReader reader = new WKBReader(factory);
            using (FileStream stream = new FileStream("graph", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                edges = reader.Read(stream);
                index++;
            }
            Assert.AreEqual(1, index);
            Assert.IsNotNull(edges);
            Assert.IsInstanceOfType(typeof(MultiLineString), edges);
            Assert.AreEqual(1179, edges.NumGeometries);

            string field1 = "OBJECTID";
            string field2 = "DESCRIPTION";
            IList features = new List<Feature>(edges.NumGeometries);            
            for (int i = 0; i < edges.NumGeometries; i++)
            {
                IGeometry ls = edges.GetGeometryN(i);
                Assert.IsInstanceOfType(typeof(LineString), ls);

                Feature f = new Feature(ls, new AttributesTable());
                f.Attributes.AddAttribute(field1, i);
                f.Attributes.AddAttribute(field2, String.Format("length: {0}", Convert.ToInt64(ls.Length)));
                features.Add(f);
            }
            Assert.IsNotEmpty(features);
            Assert.AreEqual(edges.NumGeometries, features.Count);

            DbaseFileHeader header = new DbaseFileHeader();
            header.NumRecords = edges.NumGeometries;            
            header.NumFields = 1;
            header.AddColumn(field1, 'N', 5, 0);
            header.AddColumn(field2, 'C', 254, 0);

            string path = "graph";
            if (File.Exists(path + shp))
                File.Delete(path + shp);
            Assert.IsFalse(File.Exists(path + shp));
            if (File.Exists(path + shx))
                File.Delete(path + shx);
            Assert.IsFalse(File.Exists(path + shx));
            if (File.Exists(path + dbf))
                File.Delete(path + dbf);
            Assert.IsFalse(File.Exists(path + dbf));

            ShapefileDataWriter writer = new ShapefileDataWriter(path, factory);
            writer.Header = header;
            writer.Write(features);

            Assert.IsTrue(File.Exists(path + shp));
            Assert.IsTrue(File.Exists(path + shx));
            Assert.IsTrue(File.Exists(path + dbf));

            IList subset = new List<Feature>(15);
            for (int i = 0; i < 15; i++)
                subset.Add(features[i]);
            Assert.IsNotEmpty(subset);
            Assert.AreEqual(15, subset.Count);

            path = "minimalgraph";
            if (File.Exists(path + shp))
                File.Delete(path + shp);
            Assert.IsFalse(File.Exists(path + shp));
            if (File.Exists(path + shx))
                File.Delete(path + shx);
            Assert.IsFalse(File.Exists(path + shx));
            if (File.Exists(path + dbf))
                File.Delete(path + dbf);
            Assert.IsFalse(File.Exists(path + dbf));

            writer = new ShapefileDataWriter(path, factory);
            writer.Header = header;
            writer.Write(subset);

            Assert.IsTrue(File.Exists(path + shp));
            Assert.IsTrue(File.Exists(path + shx));
            Assert.IsTrue(File.Exists(path + dbf));
        }
Esempio n. 18
0
        /// <summary>
        /// Consumes an ADO.net datatable and correctly initializes a Shapefile header object
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static DbaseFileHeader SetupHeader(DataTable table)
        {
            DbaseFileHeader header = new DbaseFileHeader();
            foreach (DataColumn col in table.Columns)
            {
                Type t = col.DataType;
                string columnName = Utilities.EnsureMaxLength(col.ColumnName, 10);

                AddColumn(header, columnName, t);
            }

            return header;
        }
Esempio n. 19
0
		/// <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, IGeometryFactory 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",String.Empty);
			
			 _dbfReader = new DbaseFileReader(filename + ".dbf");
			 _shpReader = new ShapefileReader(filename + ".shp", geometryFactory);

			_dbfHeader =  _dbfReader.GetHeader();
			_recordCount = _dbfHeader.NumRecords;			
			
			// 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();
			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;
		}
Esempio n. 20
0
            /// <summary>
            /// 
            /// </summary>
			protected void ReadHeader()
			{
				_header = new DbaseFileHeader();
				// read the header
				_header.ReadHeader(_dbfStream);

				// how many records remain
				_readPosition = _header.HeaderLength;
			}
		private void WriteGridAbundances(Project currentProject, string shapefileName)
		{
			try {
				List<IPolygon>projectGrids=new List<IPolygon>();
				General_queries projq=new General_queries(currentProject);
				
				foreach(Station st in currentProject.StationsList)
				{
					
					IPolygon grid=st.Grid;
					StationStats tempStationStats=new StationStats(st.Guid,st.StationID);
					
					foreach(StationStats stst in projq.AllStatsByStation)
					{
						if(stst.StationGUID==st.Guid)
						{
							tempStationStats=stst;
							grid.UserData=tempStationStats;
						}
					}
					
					projectGrids.Add(grid);
				}
				
				GeometryCollection gc=new GeometryCollection(projectGrids.ToArray());
				
				//Open Writer
				ShapefileWriter shpWriter = new ShapefileWriter();
				shpWriter.Write(shapefileName, gc);
				
				//Create Header & Columns for points
				DbaseFileHeader dbfHeader = new DbaseFileHeader();
				

				dbfHeader.AddColumn("Station_ID",'C',20,0);
				//One column for each species in project
				foreach(SpeciesStats spcst in projq.AllStatsBySpecies)
				{
					dbfHeader.AddColumn(spcst.SpeciesName,'N',20,0);
				}
				
				dbfHeader.NumRecords = gc.Count;
				
				//DBF Writer
				DbaseFileWriter dbfWriter = new DbaseFileWriter(shapefileName+".dbf");
				dbfWriter.Write(dbfHeader);
				
				//Loop through Business Object to get Features
				foreach (IPolygon p in gc.Geometries)
				{
					StationStats data = (StationStats)p.UserData;
					
					
					ArrayList columnValues = new System.Collections.ArrayList();
					//Add Values
					
					columnValues.Add(data.StationID);
					foreach(SpeciesStats s in data.SpeciesStats)
					{
						columnValues.Add(s.SpeciesPictures);
					}
					
					dbfWriter.Write(columnValues);

				}
				
				//Close File
				dbfWriter.Close();
			} catch (Exception ex) {
				throw ex;
			}
			

		}