public void Fields() { var filename = Path.Combine(UsaShapefilesPath, "cities.shp"); using (var fs = new FeatureSet(filename)) { foreach (var ft in fs.Features) { Debug.Print("Number of points: " + ft.Geometry.Points.Count); for (int i = 0; i < ft.NumFields; i++) { var fld = ft.GetField(i); Debug.Print("{0}: {1}", fld.Name, ft.GetValue(i)); } //var values = ft.Values; //for (int i = 0; i < values.Count; i++) //{ // var fld = values.GetField(i); // Debug.Print("{0}: {1}", fld.Name, values.GetAsString(i)); //} } fs.Close(); } }
/// <summary> /// Update projection column for a specified layers /// </summary> private void UpdateProjection(string filename, DataGridView dgv, int rowIndex) { if (ControlType != CustomType.Projection) { return; } string name = ""; var sf = new FeatureSet(filename); name = sf.Projection.Name; var db = _context.Projections; if (db != null) { var cs = db.GetCoordinateSystem(sf.Projection, ProjectionSearchType.UseDialects); if (cs != null) { name = cs.Name; } } dgv[CustomColumnIndex, rowIndex].Value = name; sf.Close(); if (name == "") { dgv[CustomColumnIndex, rowIndex].Value = "Undefined"; } }
public void FeatureSet(string shapefilename) { using (var fs = new FeatureSet(Path.Combine(FranceProjectPath, "Shapefiles", shapefilename))) { Debug.Print("Geometry type: " + fs.GeometryType); Debug.Print("Number of features: " + fs.Features.Count()); using (var g = fs.Features.First().Geometry) { foreach (var pnt in g.Points) { Debug.Print("X: {0}; Y: {1}", pnt.X, pnt.Y); } var bytes = g.ExportToBinary(); Assert.IsNotNull(bytes, "ExportToBinary returned null"); Debug.Print("Number of points: " + g.Points.Count); using (var g2 = new Geometry(g.GeometryType)) { var result = g2.ImportFromBinary(bytes); Assert.IsTrue(result, "ImportFromBinary failed"); Debug.Print("Imported: {0}", g2.Points.Count); } } fs.Close(); } }
public override string CreateFeature(ProjectionInfo proj_info, string directory) { if (FlowRate != null) { string filename = Path.Combine(directory, this.Name + ".shp"); var grid = (Owner as Modflow).Grid as MFGrid; FeatureSet fs = new FeatureSet(FeatureType.Point); fs.Name = this.Name; fs.Projection = proj_info; fs.DataTable.Columns.Add(new DataColumn("CELL_ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Layer", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Row", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Column", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Elevation", typeof(float))); for (int i = 0; i < NBDTIM; i++) { fs.DataTable.Columns.Add(new DataColumn("Flux Rate" + (i + 1), typeof(float))); } fs.DataTable.Columns.Add(new DataColumn("Name", typeof(string))); fs.DataTable.Columns.Add(new DataColumn(RegularGrid.ParaValueField, typeof(int))); var mat = FlowRate; for (int i = 0; i < FlowRate.Size[2]; i++) { int layer = (int)mat[0, 0, i]; int row = (int)mat[1, 0, i]; int col = (int)mat[2, 0, i]; var coor = grid.LocateCentroid(col, row); Point geom = new Point(coor); IFeature feature = fs.AddFeature(geom); feature.DataRow.BeginEdit(); feature.DataRow["CELL_ID"] = grid.Topology.GetID(row - 1, col - 1); feature.DataRow["Layer"] = layer; feature.DataRow["Row"] = row; feature.DataRow["Column"] = col; feature.DataRow["ID"] = (i + 1); feature.DataRow["Elevation"] = grid.GetElevationAt(row - 1, col - 1, layer - 1); feature.DataRow["Name"] = "Flow " + (i + 1); for (int j = 0; j < NBDTIM; j++) { feature.DataRow["Flux Rate" + (j + 1)] = mat[j + 4, 0, i]; } feature.DataRow[RegularGrid.ParaValueField] = 0; feature.DataRow.EndEdit(); } fs.SaveAs(filename, true); fs.Close(); return(filename); } else { return(null); } }
public void AttributeTable(string shapefilename) { using (var fs = new FeatureSet(Path.Combine(FranceProjectPath, "Shapefiles", shapefilename))) { Assert.IsTrue(fs.Table.Fields.Count > 0, "Shapefile has no attribute fields"); fs.Close(); } }
public void HasInvalidShapes(string shapefilename) { using (var fs = new FeatureSet(Path.Combine(FranceProjectPath, "Shapefiles", shapefilename))) { Assert.IsFalse(fs.HasInvalidShapes(), "Shapefile has invalid shapes"); // Close feature set: fs.Close(); } }
public void IsVector(string shapefilename) { using (var fs = new FeatureSet(Path.Combine(FranceProjectPath, "Shapefiles", shapefilename))) { Assert.IsTrue(fs.IsVector, "File is not a shapefile"); // Close feature set: fs.Close(); } }
public void Callback() { var cb = new CustomCallback(); GlobalListeners.Attach(cb); var fs = new FeatureSet(Path.Combine(UsaShapefilesPath, "cities.shp")); fs.StartEditingShapes(); fs.Close(); }
public void EditShapefile(string shapefilename) { using (var fs = new FeatureSet(Path.Combine(FranceProjectPath, "Shapefiles", shapefilename))) { // Go to edit mode: fs.StartEditingShapes(); Assert.IsTrue(fs.EditingShapes, "Could not set in edit mode"); // Save shapefile: fs.Save(); // Close feature set: fs.Close(); } }
/// <summary> /// Assignes selected projection and displays results /// </summary> public bool Assign(string filename, ISpatialReference proj) { var projWgs84 = new SpatialReference(); if (!projWgs84.ImportFromEpsg(4326)) { MessageService.Current.Warn("Failed to initialize WGS84 coordinate system."); return(false); } bool sameProj = proj.IsSame(projWgs84); int count = 0; bool success = false; int rowIndex = dgv.Rows.Add(); _shapefile = new FeatureSet(filename); Text = @"Assigning projection: " + Path.GetFileName(filename); lblProjection.Text = @"Projection: " + proj.Name; // it will be faster to assing new instance of class // as ImportFromEPSG() is slow according to GDAL documentation _shapefile.Projection.CopyFrom(proj); if (!sameProj) { // we can't show preview on map without reprojection if ((_shapefile.StartEditingShapes())) { if (_shapefile.ReprojectInPlace(projWgs84, ref count)) { success = true; } } } else { success = true; } if (success) { AddShapefile(_shapefile); return(true); } // no success in reprojection _shapefile.Close(); return(false); }
public void CopyAttributesToClipped() { var target = new ClipPolygonWithPolygon(); // load input 1 Shapefile as IFeatureSet IFeatureSet europeShape = Shapefile.OpenFile(Common.AbsolutePath(@"Data\ClipPolygonWithPolygonTests\EUR_countries.shp")); // load input 2 Shapefile as IFeatureSet IFeatureSet belgiumShape = Shapefile.OpenFile(Common.AbsolutePath(@"Data\ClipPolygonWithPolygonTests\Belgium.shp")); // set output file as IFeatureSet shapefile IFeatureSet outputShape = new FeatureSet() { Filename = FileTools.GetTempFileName(".shp") }; target.Execute(europeShape, belgiumShape, outputShape, new MockProgressHandler()); // the output file needs to be closed and re-opened, because the DataTable in memory does not match the DataTable on disk outputShape.Close(); var outputFile = FeatureSet.Open(outputShape.Filename); try { // output shapefile attribute columns should match the input 1 attribute columns Assert.That(outputFile.DataTable.Columns[0].Caption.Equals("ID")); Assert.That(outputFile.DataTable.Columns[1].Caption.Equals("Name")); string[,] dataValues = { { "BE", "Belgium" }, { "DE", "Germany" }, { "LU", "Luxembourg" } }; var mpCount = 0; foreach (var feature in outputFile.Features) { Assert.That(feature.DataRow.ItemArray.Length == 2 && feature.DataRow.ItemArray[0].Equals(dataValues[mpCount, 0]) && feature.DataRow.ItemArray[1].Equals(dataValues[mpCount, 1])); mpCount++; } Assert.That(mpCount == 3); } finally { FileTools.DeleteShapeFile(outputShape.Filename); } }
public void Build(string filename) { FeatureSet fs = new FeatureSet(FeatureType.Polygon); fs.Name = "Grid"; fs.Projection = this.Projection; fs.DataTable.Columns.Add(new DataColumn("HRU_ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("CELL_ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("ROW", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("COLUMN", typeof(int))); fs.DataTable.Columns.Add(new DataColumn(ParaValueField, typeof(double))); if (Origin == null) { Origin = new Coordinate(0, 0); } int active = 1; for (int r = 0; r < RowCount; r++) { for (int c = 0; c < ColumnCount; c++) { if (IBound[0, r, c] != 0) { // create a geometry (square polygon) var vertices = LocateNodes(c, r); ILinearRing ring = new LinearRing(vertices); Polygon geom = new Polygon(ring); // add the geometry to the featureset. IFeature feature = fs.AddFeature(geom); // now the resulting features knows what columns it has // add values for the columns feature.DataRow.BeginEdit(); feature.DataRow["HRU_ID"] = active; feature.DataRow["CELL_ID"] = Topology.GetID(r, c); feature.DataRow["ROW"] = r + 1; feature.DataRow["COLUMN"] = c + 1; feature.DataRow[ParaValueField] = 0; feature.DataRow.EndEdit(); active++; } } } fs.SaveAs(filename, true); fs.Close(); }
public void LoadShapefile() { using (var fs = new FeatureSet(Path.Combine(UsaShapefilesPath, "cities.shp"))) { var fill = fs.Style.Fill; fill.Color = Color.LightGreen; fill.Type = FillType.Solid; //fill.HatchStyle = HatchStyle.Cross; //fill.BackgroundHatchColor = Color.Moccasin; //fill.BackgroundHatchTransparent = false; //foreach (var ft in fs.Features) //{ // ft.Selected = true; //} if (fs.Categories.GenerateUniqueValues("Type")) { Debug.Print("Number of categories generated: " + fs.Categories.Count); foreach (var ct in fs.Categories) { Debug.Print("Expression: " + ct.Expression); } } else { Assert.Fail("Could not generate categories"); } fs.Categories.ApplyExpressions(); fs.Categories.ApplyColorScheme(SchemeType.Graduated, new ColorRamp(Color.Yellow, Color.Red)); fs.Labels.Generate("[Type]", LabelPosition.Centroid); var style = fs.Labels.Style; style.FontColor = Color.White; style.FrameBackColor = Color.Gray; //mapControl1.Layers.Add(fs); //mapControl1.MapCursor = MapCursor.SelectByPolygon; //fs = new FeatureSet(@"d:\data\sf\landuse.shp"); //mapControl1.Layers.Add(fs); fs.Close(); } }
public override string CreateFeature(ProjectionInfo proj_info, string directory) { if (Observations != null) { string filename = Path.Combine(directory, this.Name + ".shp"); var grid = (Owner as Modflow).Grid as MFGrid; FeatureSet fs = new FeatureSet(FeatureType.Point); fs.Name = this.Name; fs.Projection = proj_info; fs.DataTable.Columns.Add(new DataColumn("SiteID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("SiteName", typeof(string))); fs.DataTable.Columns.Add(new DataColumn("CELL_ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Row", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Column", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Elevation", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("HSIM", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("HOBS", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("DepOBS", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("DepSIM", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("DepDIF", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("HDIF", typeof(float))); fs.DataTable.Columns.Add(new DataColumn("Name", typeof(string))); fs.DataTable.Columns.Add(new DataColumn(RegularGrid.ParaValueField, typeof(int))); fs.DataTable.Columns.Add(new DataColumn("IsTR", typeof(int))); int i = 1; foreach (var vv in Observations) { var hob = vv as HeadObservation; var coor = grid.LocateCentroid(hob.Column, hob.Row); Point geom = new Point(coor); IFeature feature = fs.AddFeature(geom); feature.DataRow.BeginEdit(); feature.DataRow["CELL_ID"] = grid.Topology.GetID(hob.Row, hob.Column); feature.DataRow["Row"] = hob.Row; feature.DataRow["Column"] = hob.Column; feature.DataRow["ID"] = hob.ID; feature.DataRow["Elevation"] = hob.Elevation; feature.DataRow["HSIM"] = 0; feature.DataRow["HOBS"] = 0; feature.DataRow["Name"] = hob.Name; feature.DataRow[RegularGrid.ParaValueField] = 0; feature.DataRow["IsTR"] = 1; feature.DataRow["SiteID"] = i; feature.DataRow["SiteName"] = hob.Name; i++; feature.DataRow.EndEdit(); } fs.SaveAs(filename, true); fs.Close(); return(filename); } else { return(null); } }
public override string CreateFeature(ProjectionInfo proj_info, string directory) { if (Locations != null) { string filename = Path.Combine(directory, this.Name + ".shp"); var grid = (Owner as Modflow).Grid as MFGrid; FeatureSet fs = new FeatureSet(FeatureType.Point); fs.Name = this.Name; fs.Projection = proj_info; fs.DataTable.Columns.Add(new DataColumn("CELL_ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Layer", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Row", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Column", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("ID", typeof(int))); fs.DataTable.Columns.Add(new DataColumn("Elevation", typeof(double))); fs.DataTable.Columns.Add(new DataColumn("Name", typeof(string))); fs.DataTable.Columns.Add(new DataColumn(RegularGrid.ParaValueField, typeof(int))); int np = TimeService.StressPeriods.Count; for (int i = 0; i < np; i++) { fs.DataTable.Columns.Add(new DataColumn("Flux Rate" + (i + 1), typeof(float))); } for (int i = 0; i < NWell; i++) { int layer = Locations[0, i, 0]; int row = Locations[0, i, 1]; int col = Locations[0, i, 2]; var coor = grid.LocateCentroid(col, row); Point geom = new Point(coor); IFeature feature = fs.AddFeature(geom); feature.DataRow.BeginEdit(); feature.DataRow["CELL_ID"] = grid.Topology.GetID(row - 1, col - 1); feature.DataRow["Layer"] = layer; feature.DataRow["Row"] = row; feature.DataRow["Column"] = col; feature.DataRow["ID"] = i + 1; feature.DataRow["Elevation"] = grid.GetElevationAt(row - 1, col - 1, layer - 1); feature.DataRow["Name"] = "Well" + (i + 1); feature.DataRow[RegularGrid.ParaValueField] = 0; for (int j = 0; j < np; j++) { if (FluxRates.Flags[j, 0] == TimeVarientFlag.Individual) { feature.DataRow["Flux Rate" + (j + 1)] = FluxRates[j, 0, i]; } else if (FluxRates.Flags[j, 0] == TimeVarientFlag.Constant) { feature.DataRow["Flux Rate" + (j + 1)] = FluxRates.Constants[j, 0]; } else if (FluxRates.Flags[j, 0] == TimeVarientFlag.Repeat) { feature.DataRow["Flux Rate" + (j + 1)] = FluxRates[j, 0, i]; } } feature.DataRow.EndEdit(); } fs.SaveAs(filename, true); fs.Close(); return(filename); } else { return(null); } }