public SHPFile(string filename) { try { FileInfo fi = new FileInfo(filename); if (!fi.Exists) { return; } _file_SHP = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".shp"; _file_SHX = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".shx"; _file_SBX = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".sbx"; _file_IDX = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".idx"; _file_DBF = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".dbf"; _file_PRJ = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".prj"; fi = new FileInfo(_file_SHP); if (!fi.Exists) { return; } fi = new FileInfo(_file_SHX); if (!fi.Exists) { return; } _entities = (fi.Length - 100) / 8; _title = fi.Name.Substring(0, fi.Name.Length - 4); this.Open(); ReadHeader(); fi = new FileInfo(_file_DBF); if (fi.Exists) { _dbfFile = new DBFFile(_file_DBF); } } catch { Close(); } }
public DBFDataReader(DBFFile file, string fieldnames) { if (file == null) { return; } _file = file; _sr = new StreamReader(_file.Filename); _br = new BinaryReader(_sr.BaseStream); string[] names = null; fieldnames = fieldnames.Replace(" ", ""); if (fieldnames != "*") { names = fieldnames.Split(','); } _tab = _file.DataTable(names); }
public SHPFile(SHPFile file) { try { _file_SHP = file._file_SHP; _file_SHX = file._file_SHX; _file_IDX = file._file_IDX; _file_DBF = file._file_DBF; FileInfo fi = new FileInfo(_file_SHP); if (!fi.Exists) { return; } fi = new FileInfo(_file_SHX); if (!fi.Exists) { return; } _entities = (fi.Length - 100) / 8; _title = fi.Name.Substring(0, fi.Name.Length - 4); this.Open(); ReadHeader(); fi = new FileInfo(_file_DBF); if (fi.Exists) { _dbfFile = new DBFFile(_file_DBF); } } catch { Close(); } }
public static bool Create(string filename, IGeometryDef geomDef, Fields fields) { if (geomDef == null || fields == null) { return(false); } try { FileInfo fi = new FileInfo(filename); string file_SHP = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".shp"; string file_SHX = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".shx"; string file_DBF = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".dbf"; FileInfo fi_SHP = new FileInfo(file_SHP); FileInfo fi_SHX = new FileInfo(file_SHX); FileInfo fi_DBF = new FileInfo(file_DBF); if (fi_SHP.Exists) { fi_SHP.Delete(); } if (fi_SHX.Exists) { fi_SHX.Delete(); } if (fi_DBF.Exists) { fi_DBF.Delete(); } #region DBF if (!DBFFile.Create(file_DBF, fields)) { return(false); } #endregion #region SHP ShapeType type = ShapeType.NullShape; switch (geomDef.GeometryType) { case geometryType.Point: type = ShapeType.Point; if (geomDef.HasZ) { type = ShapeType.PointZ; } break; case geometryType.Multipoint: type = ShapeType.MultiPoint; if (geomDef.HasZ) { type = ShapeType.MultiPointZ; } break; case geometryType.Polyline: type = ShapeType.PolyLine; if (geomDef.HasZ) { type = ShapeType.PolyLineZ; } break; case geometryType.Polygon: type = ShapeType.Polygon; if (geomDef.HasZ) { type = ShapeType.PolygonZ; } break; } StreamWriter sw = new StreamWriter(file_SHP); BinaryWriter bw = new BinaryWriter(sw.BaseStream); bw.Write((int)SwapWord((uint)9994)); // FileCode bw.Write((int)0); // Unused1; bw.Write((int)0); // Unused2; bw.Write((int)0); // Unused3; bw.Write((int)0); // Unused4; bw.Write((int)0); // Unused5; bw.Write((int)SwapWord((uint)50)); // FileLength; bw.Write((int)1000); // Version bw.Write((int)type); // ShapeType bw.Write((double)0.0); // Xmin bw.Write((double)0.0); // Ymin bw.Write((double)0.0); // Xmax bw.Write((double)0.0); // Ymax bw.Write((double)0.0); // Zmin bw.Write((double)0.0); // Zmax bw.Write((double)0.0); // Mmin bw.Write((double)0.0); // Mmax bw.Flush(); sw.Flush(); sw.Close(); #endregion #region SHX sw = new StreamWriter(file_SHX); bw = new BinaryWriter(sw.BaseStream); bw.Write((int)SwapWord((uint)9994)); // FileCode bw.Write((int)0); // Unused1; bw.Write((int)0); // Unused2; bw.Write((int)0); // Unused3; bw.Write((int)0); // Unused4; bw.Write((int)0); // Unused5; bw.Write((int)SwapWord((uint)50)); // FileLength; bw.Write((int)1000); // Version bw.Write((int)type); // ShapeType bw.Write((double)0.0); // Xmin bw.Write((double)0.0); // Ymin bw.Write((double)0.0); // Xmax bw.Write((double)0.0); // Ymax bw.Write((double)0.0); // Zmin bw.Write((double)0.0); // Zmax bw.Write((double)0.0); // Mmin bw.Write((double)0.0); // Mmax bw.Flush(); sw.Flush(); sw.Close(); #endregion return(true); } catch { return(false); } }