internal static IEnumerable <Feature> Load(string v) { var shapeFileDataReader = Shapefile.CreateDataReader(v, new GeometryFactory()); ShapefileHeader shpHeader = shapeFileDataReader.ShapeHeader; DbaseFileHeader header = shapeFileDataReader.DbaseHeader; shapeFileDataReader.Reset(); //Read through all records of the shapefile (geometry and attributes) into a feature collection List <Feature> features = new List <Feature>(); int j = 1; while (shapeFileDataReader.Read()) { Feature feature = new Feature(); AttributesTable attributesTable = new AttributesTable(); string[] keys = new string[header.NumFields]; var pm = new PrecisionModel(10.0); var pop = new NetTopologySuite.Precision.GeometryPrecisionReducer(pm); Geometry geometry = NetTopologySuite.Simplify.DouglasPeuckerSimplifier.Simplify(pop.Reduce((Geometry)shapeFileDataReader.Geometry), 0.5); // geometry = NetTopologySuite.Operation.BoundaryOp.GetBoundary(geometry); // var pol = new NetTopologySuite.Operation.Polygonize.Polygonizer(); // pol.Add() if (geometry.IsEmpty) { continue; } for (int i = 0; i < header.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = header.Fields[i]; keys[i] = fldDescriptor.Name; attributesTable.Add(fldDescriptor.Name, shapeFileDataReader.GetValue(i + 1)); } if (!attributesTable.GetNames().Contains("NAME", StringComparer.InvariantCulture)) { attributesTable.Add("NAME", j); } feature.Geometry = geometry; feature.Attributes = attributesTable; features.Add(feature); j++; } //Close and free up any resources shapeFileDataReader.Close(); shapeFileDataReader.Dispose(); return(features); }
public ShapeFile(string fileName) { if (String.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(); } m_MapName = fileName; IEditSpatialIndex index = new SpatialIndex(); ShapefileDataReader sfdr = Shapefile.CreateDataReader(fileName, new GeometryFactory()); ShapefileHeader hdr = sfdr.ShapeHeader; Envelope ex = hdr.Bounds; m_Extent = new Window(ex.MinX, ex.MinY, ex.MaxX, ex.MaxY); foreach (object o in sfdr) { // You get back an instance of GisSharpBlog.NetTopologySuite.IO.RowStructure, but // that's internal, so cast to the interface it implements (we'll attach this to // the geometry we wrap). //ICustomTypeDescriptor row = (ICustomTypeDescriptor)o; AdhocPropertyList row = CreatePropertyList(sfdr); NTS.Geometry geom = sfdr.Geometry; geom.UserData = row; List <NTS.Geometry> geoms = GetBasicGeometries(geom); foreach (NTS.Geometry g in geoms) { g.UserData = row; if (g is NTS.Point) { index.Add(new PointWrapper((NTS.Point)g)); } else { index.Add(new GeometryWrapper(g)); } } } // Don't permit any further additions m_Index = index; }
public static USA Create2013USA() { var tempDirPath = Path.GetTempFileName(); File.Delete(tempDirPath); Directory.CreateDirectory(tempDirPath); var shapeFilePath = Copy2013USStateShapeFilesToDirectory(tempDirPath); try { var geometryFactory = new GeometryFactory(); using (var shapeReader = Shapefile.CreateDataReader(shapeFilePath, geometryFactory)) { return(new USA(ReadStates(shapeReader))); } } finally { Directory.Delete(tempDirPath, recursive: true); } }