Exemplo 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);
            }
        }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
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();
		}
Exemplo n.º 9
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));
        }
		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;
			}
			

		}