private void CreateShape(string path, DataTable dataTable) { Shapefile myShapefile; try { myShapefile = new Shapefile(); string fileName = Path.GetFileNameWithoutExtension(txtBox_excel.Text); string shapeilePath = Path.GetDirectoryName(path) + "\\" + fileName + ".shp"; Directory.CreateDirectory(Path.GetDirectoryName(shapeilePath)); myShapefile.CreateNew(shapeilePath, ShpfileType.SHP_POINT); //Create new field MapWinGIS.Field myField = new Field(); //Set the field properties myField.Name = "ID"; myField.Type = FieldType.INTEGER_FIELD; myField.Width = 10; //Add the filed for the shapefile table int intFieldIndex = 1; myShapefile.EditInsertField(myField, ref intFieldIndex, null); int myCounter = 0; int myShapeIndex = 0; //First Create header CreateHeader(dataTable.Columns, myShapefile); for (int i = 0; i < dataTable.Rows.Count; i++) { MapWinGIS.Shape myShape = new Shape(); myShape.Create(ShpfileType.SHP_POINT); MapWinGIS.Point myPoint = new MapWinGIS.Point(); myPoint.x = Convert.ToDouble(dataTable.Rows[i][x_Index]); myPoint.y = Convert.ToDouble(dataTable.Rows[i][y_Index]); object fld_Value = dataTable.Rows[i][0]; int myPointIndex = 0; myShape.InsertPoint(myPoint, ref myPointIndex); myShapefile.EditInsertShape(myShape, ref myShapeIndex); myShapefile.EditCellValue(0, myShapeIndex, fld_Value); CreatePointData(dataTable, myShapefile, myShapeIndex, i); myShapeIndex++; myCounter++; } GeoProjection proj = new GeoProjection(); // EPSG code proj.ImportFromEPSG(4326); // WGS84 myShapefile.GeoProjection = proj; myShapefile.StopEditingShapes(true, true, null); myShapefile.Close(); } catch (Exception) { throw; } }
private void CreateShapefile() { _shapefile = new Shapefile(); _shapefile.CreateNewWithShapeID(Filename, ShapefileType); _shapefile.InteractiveEditing = true; foreach (var fld in Fields) { _shapefile.EditInsertField(fld, _shapefile.NumFields); } }
public static void Main(string[] args) { //Create new shapefile Shapefile myShapefile = new Shapefile(); //Define the path of the new shapefile and geometry type myShapefile.CreateNew(@"D:\GISLesson04\road.shp", ShpfileType.SHP_POINT); //Create new field MapWinGIS.Field myField = new Field(); //Set the field properties myField.Name = "ID"; myField.Type = FieldType.INTEGER_FIELD; myField.Width = 10; //Add the filed for the shapefile table int intFieldIndex = 0; myShapefile.EditInsertField(myField, ref intFieldIndex, null); int myCounter = 0; int myShapeIndex = 0; string myLine; // Read the file and display it line by line. System.IO.StreamReader myFile = new System.IO.StreamReader(@"D:\GISLesson04\road.csv"); // Using while loop to read csv file line by line while ((myLine = myFile.ReadLine()) != null) { if (myCounter > 0) { MapWinGIS.Shape myShape = new Shape(); myShape.Create(ShpfileType.SHP_POINT); MapWinGIS.Point myPoint = new Point(); myPoint.x = GetX(myLine); myPoint.y = GetY(myLine); int myPointIndex = 0; myShape.InsertPoint(myPoint, ref myPointIndex); myShapefile.EditInsertShape(myShape, ref myShapeIndex); myShapeIndex++; } myCounter++; } myShapefile.StopEditingShapes(true, true, null); myFile.Close(); Console.ReadKey(); }
private void CreateHeader(DataColumnCollection sentence, Shapefile myShapefile) { int intFieldIndex = 1; foreach (object header in sentence) { MapWinGIS.Field myField = new Field(); //Set the field properties myField.Name = header.ToString(); myField.Type = FieldType.STRING_FIELD; myField.Width = 10; //Add the filed for the shapefile table bool isInserted = myShapefile.EditInsertField(myField, ref intFieldIndex, null); intFieldIndex++; } }
// <summary> // Calculates a minimal distance from each building to the river. // </summary> public void MinimalDistance(AxMap axMap1, string dataPath, ToolStripStatusLabel label) { axMap1.Projection = tkMapProjection.PROJECTION_NONE; axMap1.GrabProjectionFromData = true; string filename1 = dataPath + "waterways.shp"; string filename2 = dataPath + "buildings.shp"; if (!File.Exists(filename1) || !File.Exists(filename2)) { MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath); } else { Shapefile sfRivers = new Shapefile(); sfRivers.Open(filename1, null); Utils utils = new Utils(); sfRivers.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue); sfRivers.DefaultDrawingOptions.LineWidth = 5.0f; Shapefile sfBuildings = new Shapefile(); sfBuildings.Open(filename2, null); // adds a field in the table Field field = new Field(); field.Name = "RiverDist"; field.Type = FieldType.DOUBLE_FIELD; field.Precision = 10; int fieldIndex = sfBuildings.NumFields; sfBuildings.StartEditingShapes(true, null); sfBuildings.EditInsertField(field, ref fieldIndex, null); ShapefileCategory ct = sfBuildings.Categories.Add("Named buildings"); ct.Expression = "[Name] <> \"\""; sfBuildings.Categories.ApplyExpressions(); sfRivers.StartEditingShapes(false, null); for (int i = 0; i < sfBuildings.NumShapes; i++) { if (sfBuildings.ShapeCategory[i] == 0) { label.Text = "Processing building: " + (i + 1) + " / " + sfBuildings.NumShapes; Application.DoEvents(); Shape shp = sfBuildings.Shape[i]; double minDist = Double.MaxValue; for (int j = 0; j < sfRivers.NumShapes; j++) { Shape shp2 = sfRivers.Shape[j]; double distance = shp.Distance(shp2); if (distance < minDist) { minDist = distance; } } if (minDist != Double.MaxValue) { sfBuildings.EditCellValue(fieldIndex, i, minDist); } } else { sfBuildings.EditCellValue(fieldIndex, i, 0.0); } } sfRivers.StopEditingShapes(false, true, null); sfBuildings.Categories.Generate(fieldIndex, tkClassificationType.ctNaturalBreaks, 8); ColorScheme scheme = new ColorScheme(); scheme.SetColors2(tkMapColor.Blue, tkMapColor.Yellow); sfBuildings.Categories.ApplyColorScheme(tkColorSchemeType.ctSchemeGraduated, scheme); sfBuildings.Labels.Generate("[Name] + \"\n\" + [RiverDist] + \" m\"", tkLabelPositioning.lpCentroid, true); sfBuildings.Labels.TextRenderingHint = tkTextRenderingHint.SystemDefault; sfBuildings.VisibilityExpression = "[Name] <> \"\""; axMap1.AddLayer(sfRivers, true); axMap1.AddLayer(sfBuildings, true); label.Text = ""; } }
// <summary> // Calculates the length of intersection of rivers and land parcels // </summary> public void IntersectionLength(AxMap axMap1, ToolStripStatusLabel label, string dataPath) { axMap1.Projection = tkMapProjection.PROJECTION_NONE; axMap1.GrabProjectionFromData = true; string filename1 = dataPath + "landuse.shp"; string filename2 = dataPath + "waterways.shp"; if (!File.Exists(filename1) || !File.Exists(filename2)) { MessageBox.Show("The necessary files (waterways.shp, building.shp) are missing: " + dataPath); } else { Shapefile sfParcels = new Shapefile(); sfParcels.Open(filename1, null); sfParcels.StartEditingShapes(true, null); Field field = new Field { Name = "Length", Type = FieldType.DOUBLE_FIELD, Precision = 10 }; int fieldIndex = sfParcels.NumShapes; sfParcels.EditInsertField(field, ref fieldIndex, null); Shapefile sfRivers = new Shapefile(); sfRivers.Open(filename2, null); sfRivers.StartEditingShapes(true, null); Utils utils = new Utils(); sfRivers.DefaultDrawingOptions.LineWidth = 2; sfRivers.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.Blue); Shapefile sfNew = sfRivers.Clone(); ShapeDrawingOptions options = sfNew.DefaultDrawingOptions; LinePattern pattern = new LinePattern(); pattern.AddLine(utils.ColorByName(tkMapColor.Blue), 8, tkDashStyle.dsSolid); pattern.AddLine(utils.ColorByName(tkMapColor.LightBlue), 4, tkDashStyle.dsSolid); options.LinePattern = pattern; options.UseLinePattern = true; for (int i = 0; i < sfParcels.NumShapes; i++) { Shape shp1 = sfParcels.Shape[i]; double length = 0.0; // the length of intersection for (int j = 0; j < sfRivers.NumShapes; j++) { Shape shp2 = sfRivers.Shape[j]; if (shp1.Intersects(shp2)) { Shape result = shp1.Clip(shp2, tkClipOperation.clIntersection); if (result != null) { int index = sfNew.EditAddShape(result); length += result.Length; } } } sfParcels.EditCellValue(fieldIndex, i, length); label.Text = string.Format("Parcel: {0}/{1}", i + 1, sfParcels.NumShapes); Application.DoEvents(); } // generating charts var chartField = new ChartField(); chartField.Name = "Length"; chartField.Color = utils.ColorByName(tkMapColor.LightBlue); chartField.Index = fieldIndex; sfParcels.Charts.AddField(chartField); sfParcels.Charts.Generate(tkLabelPositioning.lpInteriorPoint); sfParcels.Charts.ChartType = tkChartType.chtBarChart; sfParcels.Charts.BarHeight = 100; sfParcels.Charts.ValuesVisible = true; sfParcels.Charts.Visible = true; axMap1.AddLayer(sfParcels, true); axMap1.AddLayer(sfRivers, true); axMap1.AddLayer(sfNew, true); axMap1.ZoomToMaxExtents(); } }