示例#1
0
        public static ShapefileReader ReadShapefile(string path)
        {
            ShapefileReader shpfileReader = null;

            try
            {
                if (ValidShapefileParts(path))
                {
                    shpfileReader = new ShapefileReader(path, true);
                    Readers.Add(shpfileReader);
                }
                else
                {
                    throw new Exception("Missing shapefile components at " + path);
                }
            }
            catch (Exception e)
            {
                ReaderExceptions.Add(e);
                shpfileReader = null;
            }
            finally
            {
                if (shpfileReader != null)
                {
                    shpfileReader.CloseFileStream();
                }
            }
            return(shpfileReader);
        }
        public static object[] ReaderToGeometryCollection(ShapefileReader shpReader)
        {
            if (shpReader != null && shpReader.Header != null)
            {
                var geometryType = shpReader.ShapeType.ToString();
                switch (geometryType)
                {
                case "Point":
                    return(ReaderToGeometryCollection <Point>(shpReader, CreatePoint));

                case "Multipoint":
                    return(ReaderToGeometryCollection <Multipoint>(shpReader, CreateMultipoint));

                case "Polyline":
                    return(ReaderToGeometryCollection <Polyline>(shpReader, CreatePolyline));

                case "Polygon":
                    return(ReaderToGeometryCollection <Polygon>(shpReader, CreatePolygon));

                case "Null":
                    return(null);

                default:
                    throw new Exception("Unknown geometry type \"" + geometryType + "\" provided for shapefile at " + shpReader.Path);
                }
            }
            else
            {
                throw new NullReferenceException("Shapefile reader has not been initialized correctly or the main header file has not been read. Cannot determine geometry type.");
            }
        }
        public static Type GetGeometryType(ShapefileReader shpReader)
        {
            if (shpReader != null)
            {
                var geometryType = shpReader.ShapeType.ToString();
                switch (geometryType)
                {
                case "Point":
                    return(typeof(Point));

                case "Multipoint":
                    return(typeof(Multipoint));

                case "Polyline":
                    return(typeof(Polyline));

                case "Polygon":
                    return(typeof(Polygon));

                default:
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
示例#4
0
        public static ShapefileReader AddReader(string path)
        {
            ShapefileReader reader = new ShapefileReader(path);

            ShapefileReaderManager.Readers.Add(reader);
            return(reader);
        }
示例#5
0
        public static ShapefileReader GetReaderForPath(string path)
        {
            ShapefileReader reader = null;

            if (ActiveReaderPaths().Contains(path))
            {
                //Not actually working.
                return(Readers[0]);
            }
            return(reader);
        }
示例#6
0
 public static bool CloseReader(ShapefileReader reader)
 {
     if (Readers.Contains(reader))
     {
         reader.Dispose();
         Readers.Remove(reader);
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#7
0
        public static bool CloseReader(string path)
        {
            ShapefileReader reader = GetReaderForPath(path);

            if (reader != null)
            {
                return(CloseReader(reader));
            }
            else
            {
                return(false);
            }
        }
        private static T[] ReaderToGeometryCollection <T>(ShapefileReader shpReader, Func <byte[], T> CreateGeometry)
        {
            List <T> records = new List <T>();

            shpReader.OpenComponentFile("shp");
            while (true)
            {
                byte[] nextRecord = shpReader.GetNextRecord();
                if (nextRecord != null)
                {
                    records.Add(CreateGeometry(nextRecord));
                }
                else
                {
                    shpReader.CloseFileStream();
                    return(records.ToArray());
                }
            }
        }
示例#9
0
 public static ShapefileHeader[] GetAllMainHeadersInDirectory(string directory)
 {
     if (Directory.Exists(directory))
     {
         string[] allShpFiles           = Directory.GetFiles(directory, "*.shp");
         List <ShapefileHeader> headers = new List <ShapefileHeader>();
         foreach (string file in allShpFiles)
         {
             ShapefileReader sfr = new ShapefileReader(file);
             if (sfr.ReadMainHeader(false))
             {
                 headers.Add(sfr.Header);
             }
         }
         return(headers.ToArray());
     }
     else
     {
         return(new ShapefileHeader[] { });
     }
 }