Dot Spatial Sample Code

March 24, 2018 | Author: UNETI | Category: C (Programming Language), Esri, Pi, Polygon, Areas Of Computer Science


Comments



Description

Sample code that demonstrates how to get the value of a single cell in an attribute table.using DotSpatial.Data; private void btnBuffer_Click(object sender, EventArgs e) { FeatureSet fs = new FeatureSet(); fs.FillAttributes(); fs.Open("C:\\Temp\\roads.shp"); DataTable dt = fs.DataTable; for (int row = 0; row < dt.Rows.Count; row++) { object val = dtOriginal.Rows[row]["LENGTH"]; } } Sample code for loop through a Feature Set's attribute table and get all the values using System.Windows.Forms; using DotSpatial.Topology; using DotSpatial.Data; private void btnBuffer_Click(object sender, EventArgs e) { FeatureSet fs = new FeatureSet(); fs.FillAttributes(); fs.Open("C:\\Temp\\roads.shp"); DataTable dtOriginal = fs.DataTable; for (int row = 0; row < dtOriginal.Rows.Count; row++) { object[] original = dtOriginal.Rows[row].ItemArray; } } This code demonstrates how to open an existing shapefile as a new feature set using the System.Spatial.Desktop library. using DotSpatial.Data; public void OpeningFS() { //Declare a new feature set FeatureSet fs = new FeatureSet(); //Pass in the file path for the standard shapefile that will be opened fs.Open("C:\\Temp\\roads.shp"); } // create a new shape file Shapefile s = new Shapefile(); // define the feature type for this file FeatureSet fs = new FeatureSet( FeatureType.Polygon ); // Add Some Columns fs.DataTable.Columns.Add( new DataColumn( "ID", typeof( int ))); fs.DataTable.Columns.Add( new DataColumn( "Text", typeof( string ))); // create a geometry (square polycon) List vertices = new List(); vertices.Add(new Coordinate(0, 0)); vertices.Add( new Coordinate( 0,100 )); vertices.Add(new Coordinate(100, 100)); vertices.Add(new Coordinate(100, 0)); Polygon geom = new Polygon( vertices ); // add the geometry to the featureset. IFeature feature = fs.AddFeature(geom); // now the resulting features knows what columns it has Data.// add values for the columns feature. true). // save the feature set fs.DataRow. .shp". This code generates a random linestring from a coordinate array using the Geometry library then creates a line feature using the desktop library.BeginEdit().DataRow"ID" = 1. namespace SampleCode { public partial class Form1 : Form { public Form1() { InitializeComponent(). using DotSpatial. feature.DataRow.EndEdit(). feature.SaveAs("d:\\test. using DotSpatial.Geometries. feature.DataRow"Text" = "Hello World". //for loop that will generate 36 random numbers for (int i = 0. } //creates a linestring from the coordinate array LineString ls = new LineString(c). FeatureSet fs = new FeatureSet(f.Add(f). i++) { c[i] = new Coordinate((rnd.Features. //creates a new coordiante array Coordinate[] c = new Coordinate[36].180.NextDouble() * 180) . //creates a feature from the linestring Feature f = new Feature(ls).90).FeatureType).} private void button1_Click(object sender. i < 36.NextDouble() * 360) . (rnd. EventArgs e) { //Creates a random number generator Random rnd = new Random(). fs. } } . true). } Sample code that demonstrates how to get the number of rows in a feature set.Spatial.Spatial.This code demonstrates how to open an existing shapefile as a new feature set using the System. true). private void button1_Click(object sender. //An IFeatureSet that contains the factory that will buffer the feature set IFeatureSet iF = fs. .Desktop library and then buffer the feature set. //Saves the buffered feature set as a new file. fs.shp"). EventArgs e) { //Declare a new feature set FeatureSet fs = new FeatureSet(). //Pass in the file path for the standard shapefile that will be opened fs. using System.shp".FillAttributes().SaveAs("C:\\Temp\roads_buffer. iF.Buffer(10.Data.Open("C:\\Temp\\roads. Pressing the move button sequentially should cause the points to appear to move around randomly.FillAttributes(). Make sure you have downloaded something more recent than changeset e91fbe2d79ca or else the "move" operation listed here will not work. EventArgs e) { FeatureSet fs = new FeatureSet(). The second button moves the points to a slightly different random location. The first button adds the points.shp"). This sample is also part of the "TestViewer" application that is in the "Demo Projects" folder of the source code repository.using System. fs. . private void btnBuffer_Click(object sender.NumRows(). create two buttons.Windows.Open("C:\\Temp\\roads. To run this. This example was a test case for a bug fix on 11/7/2010.Data. double numRows = fs.Forms. fs. using DotSpatial. then move them around. } Create random points. //using DotSpatial. private void AddPoints() { // Create the featureset if one hasn't been created yet.Drawing.Point).Data. //using DotSpatial. if (_myPoints == null) _myPoints = new FeatureSet(FeatureType.//using DotSpatial. .Topology. //using System.Symbology. ymin).Maximum.Extents.Minimum. i < 10. for(int i = 0.Minimum. double xmin = map1.Features.NextDouble() * (xmax .Y. and get the current map extents.Maximum.NextDouble() * (ymax .Extents. i++) { double x = xmin + rnd. double ymin = map1.Extents. IMapPointLayer pointLayer = map1.// Assume background layers have been added.Layers.xmin). } // Add a layer to the map. double y = ymin + rnd. and we know it is a point layer so cast it specifically.Add(c). // Randomly generate 10 points that are in the map extent Random rnd = new Random().Extents. Coordinate c = new Coordinate(x. y).X. . double ymax = map1.Add(_myPoints) as IMapPointLayer. _myPoints.X. double xmax = map1.Y. // Control what the points look like through a symbolizer (or pointLayer..ProjToPixel(feature. } } private void MovePoints() { Random rnd = new Random().NextDouble() .Y += (rnd.Features) { // Coordinates can be updated geographically like // feature.5).Blue.5). pointLayer.Coordinates[0]).LegendText = "MovingPoints".Symbology for categories) if(pointLayer != null) { pointLayer.Symbolizer = new PointSymbolizer(Color. 7).. .X += (rnd.Coordinates[0]. // Or controled in pixels with the help of the map Point pixelLocation = map1. foreach(IFeature feature in _myPoints. PointShape.NextDouble() .Coordinates[0]. // feature.Ellipse. } // Refresh the cached representation because we moved points around. feature.5) * 50)..Coordinates[0] = c.MapFrame.. map1.ToInt32((rnd. map1. // shift left or right 5 pixels int dy = Convert.Y = pixelLocation.PixelToProj(pixelLocation). Coordinate c = map1.Invalidate().Y + dy. // Convert the pixel motions back to geographic motions. pixelLocation. } Sample code that demonstrates how to create a Multi Polygon Feature Set .ToInt32((rnd.X = pixelLocation.X + dx. // shift up or down 5 pixels pixelLocation.Invalidate().// Control movement in terms of pixels int dx = Convert.NextDouble() .NextDouble() .5) * 50). X + Math. } coord[35] = new Coordinate(coord[0].180.FeatureType).shp".90). fs.Y).SaveAs("C:\\Temp\\test.Geometries.using DotSpatial.Next(50) * 360) .Add(pg[i]). Feature f = new Feature(). } fs.Features.Next(60) * 180) . center.PI / 10). true). Polygon[] pg = new Polygon[100]. using DotSpatial. FeatureSet fs = new FeatureSet(f. EventArgs e) { Random rnd = new Random(). . Coordinate[] coord = new Coordinate[50].Data. private void button1_Click(object sender.PI / 10). i < 100. pg[i] = new Polygon(coord).Y + (ii * 10) * Math. for (int i = 0.Cos((ii * 10) * Math. ii < 50. ii++) { coord[ii] = new Coordinate(center. coord[0]. (rnd. i++) { Coordinate center = new Coordinate((rnd. for (int ii = 0.X. for (int i = 0. true).} Sample code that demonstrates how to create a new Multi Point Feature Set using DotSpatial.FeatureType). using DotSpatial.Geometries.90.shp". i < 50.SaveAs("C:\\Temp\\test. i++) { c[i] = new Coordinate((rnd.Features. EventArgs e) { Coordinate[] c = new Coordinate[50]. } Union Intersecting Shapes in a FeatureSet This example shows how to use a newly added extension method that allows for intersecting shapes in the same Shapefile .Next(0. 50) + 360) .Data. FeatureSet fs = new FeatureSet(f. Feature f = new Feature().Add(c[i]). Random rnd = new Random(). } fs.180).NextDouble() * 360) . fs. (rnd. private void button1_Click(object sender. but leaving shapes that don't overlap unchanged. IFeatureSet result = fs.OK) return.shp".Filter = "*Shapefiles (*.to be Unioned.FileName. public void UnionShapes() { FeatureSet fs = new FeatureSet().Open(). SaveFileDialog sfd = new SaveFileDialog().SaveAs(sfd.ShowDialog() != DialogResult.UnionShapes(ShapeRelateType. true). if (sfd. fs. sfd. } .shp)|*. result.Intersecting). //Pass in the file path for the standard shapefile that will be opened fs.shp".Sample code that demonstrates how to open an existing shapefile and save that shapefile with a new file name. using DotSpatial. .Data.Geometries.Open("C:\\Temp\\roads.Data. private void button1_Click(object sender. true). //Saves the open shapefile fs. } Sample code that demonstrates how to create a Multi Line Feature Set using DotSpatial. EventArgs e) { //Declare a new feature set FeatureSet fs = new FeatureSet().SaveAs("C:\\Temp\\roads_test.shp"). using DotSpatial. NextDouble() * 360) . EventArgs e) { Random rnd = new Random(). for (int ii = 0. } fs.Add(f).Features.private void button1_Click(object sender. f = new Feature(ls). ii < 40.180.SaveAs("C:\\Temp\\test. (rnd. Feature f = new Feature(). for (int i = 0. fs.Net Sample Code Imports DotSpatial.FeatureType).90). i++) { coord[i] = new Coordinate((rnd. } Sample code to project a point using the ESRI Projection File VB. i < 36.Projections .NextDouble() * 180) . true). } LineString ls = new LineString(coord).shp". FeatureSet fs = new FeatureSet(f. ii++) { Coordinate[] coord = new Coordinate[36]. e As EventArgs) 'declares a new ProjectionInfo for the startind and ending coordinate systems 'sets the start GCS to WGS_1984 Dim pStart As ProjectionInfo = KnownCoordinateSystems.prj file pESRIEnd.WGS1984 Dim pESRIEnd As New ProjectionInfo() 'declares the point(s) that will be reprojected Dim xy As Double() = New Double(1) {} Dim z As Double() = New Double(0) {} 'initiates a StreamReader to read the ESRI . z.prj file Dim re As StreamReader = File. pStart.'Sample code that will conduct a reprojection by reading in the ESRI.World.") End Sub Sample code that demonstrates how to read a prog4 string and then project a point .ReprojectPoints(xy.ReadLine()) 'calls the reprojection function Reproject.OpenText("C:\Program Files\ArcGIS\Coordinate Systems\Projected Coordinate Systems\UTM\WGS 1984\WGS 1984 UTM Zone 1N.prj") 'sets the ending PCS to the ESRI .Show("Points have been projected successfully. 1) MessageBox.ReadEsriString(re. 0.prj file Private Sub btnProjection_Click(sender As Object.Geographic. pESRIEnd. ReprojectPoints(xy. //Declares the point to be project.C# Code using DotSpatial. } VB.Geographic. pEnd. 1). 0.Projections. //Declares a new ProjectionInfo and allows the user to directly input a Proj4 sring ProjectionInfo pEnd = new ProjectionInfo("+proj=aea +lat_1=20 +lat_2=-23 +lat_0=0 +lon_0=25 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs ").WGS1984. pStart = KnownCoordinateSystems. z. double[] z = new double[1]. //Code that allows the user to input a Proj4 string and reproject a WGS 1984 GCS to a Proj4 PCS private void btnProjection_Click(object sender.World."). starts out as 0. pStart.net Code . //calls the reproject function and reprojects the points Reproject.0 double[] xy = new double[2]. EventArgs e) { //Declares a new ProjectionInfo and sets it to GCS_WGS_1984 ProjectionInfo pStart = new ProjectionInfo().Show("Reprojection is compelte. MessageBox. pStart.Show("Reprojection is compelte.Geographic.WGS1984 'Declares a new ProjectionInfo and allows the user to directly input a Proj4 sring Dim pEnd As New ProjectionInfo("+proj=aea +lat_1=20 +lat_2=-23 +lat_0=0 +lon_0=25 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs ") 'Declares the point to be project. 1) MessageBox.Spatial. 0. starts out as 0.Imports DotSpatial.World.ReprojectPoints(xy.Projections . VB Sample Code: Imports System.") End Sub This code will add a geographic coordinate system (GCS) to a feature set that does not contain a GCS. z. pEnd.Projections 'Code that allows the user to input a Proj4 string and reproject a WGS 1984 GCS to a Proj4 PCS Private Sub btnProjection_Click(sender As Object. e As EventArgs) 'Declares a new ProjectionInfo and sets it to GCS_WGS_1984 Dim pStart As New ProjectionInfo() pStart = KnownCoordinateSystems.0 Dim xy As Double() = New Double(1) {} Dim z As Double() = New Double(0) {} 'calls the reproject function and reprojects the points Reproject. Projection = dest 'Saves the feature set CopyFS.Object.Item(0).WGS1984 'Adds the geographic coordinate system to the feature set CopyFS.SaveAs("C:\Temp\US_Cities_GCS_WGS1984.Click Dim fs As New FeatureSet Dim CopyFS As New FeatureSet Dim dest As ProjectionInfo 'Get's the first layer in the Table of Contents fs = MapControl.'Code for defining a geographic coordinate system for a feature set Private Sub BbtnDefineGeographicProjection_Click(ByVal sender As System.Geographic.EventArgs) Handles BbtnDefineGeographicProjection. True) MessageBox.") End Sub . ByVal e As System.DataSet 'Copies the selected layer to a new feature set 'Prevents the original file from being edited CopyFS.shp".Layers. True) dest = KnownCoordinateSystems.Show("The feature was successfully been projected.CopyFeatures(fs.World. //Saves the feature set CopyFS.SaveAs("C:\\Temp\\US_Cities_GCS_WGS1984. //Get's the first layer in the Table of Contents fs = MapControl. //Copies the selected layer to a new feature set //Prevents the original file from being edited CopyFS.World. FeatureSet CopyFS = new FeatureSet(). ProjectionInfo dest = default(ProjectionInfo).WGS1984. System. true).shp".Layers.CopyFeatures(fs. dest = KnownCoordinateSystems. .Object sender.C# Sample Code: //Code for defining a geographic coordinate system for a feature set private void BbtnDefineGeographicProjection_Click(System.Item(0).DataSet. //Adds the geographic coordinate system to the feature set CopyFS.Geographic.EventArgs e) { FeatureSet fs = new FeatureSet(). true).Projection = dest. using DotSpatial. private void btnLineString_Click(object sender.Topology.Show("Point p is: x= " + p. //displayes the new point's x and y coordiantes MessageBox.MessageBox. private void btnCoord_Click(object sender. using DotSpatial. using DotSpatial. 2.4. EventArgs e) { //creates a new coordinate Coordinate c = new Coordinate(2.Common.Y). //passes the coordinate to a new point Point p = new Point(c). } This code demonstrates the method for declaring a new coordinate and creating a point from that coordinate. + p.Topology.X + " & y= " } Creating a new linestring and calculating the length using DotSpatial.Common.Show("The feature was successfully been projected."). EventArgs e) { .4). //creates a new coordinate array Coordinate[] coords = new Coordinate[36].Common. //creates a random point variable Random rnd = new Random(). } //creates a new linstring from the array of coordinates LineString ls = new LineString(coords). .90). i++) { coords[i] = new Coordinate((rnd. //new variable for the length of the linstring Double length.NextDouble() * 180) .NextDouble() * 360) .180. length = ls. i < 36. (rnd.Show("The length of the linstring is: " + length).Topology. //Displays the length of the linstring MessageBox. } Sample code that demonstrates the creation of a new polygon from random points and calculating the area using DotSpatial. //a for loop that generates a new random X and Y value and feeds those values into the coordinate array for (int i = 0.Length. using DotSpatial. area = pg. //displays the area of the polygon MessageBox. //creates a random point variable Random rnd = new Random().PI / 18).private void btnPolygon_Click(object sender. //a for loop that generates a new random X and Y value and feeds those values into the coordinate array for (int i = 0.Cos((i * 2) * Math. center.PI / 18). //Creates the center coordiante for the new polygon Coordinate center = new Coordinate((rnd.X + Math.Y + (i * 2) * Math. i < 10.NextDouble() * 180) . EventArgs e) { //creates a new coordinate array Coordinate[] coords = new Coordinate[10].Show("The Area of the polygon is: " + area). } //creates a new polygon from the coordinate array Polygon pg = new Polygon(coords).90). (rnd.NextDouble() * 360) . } .Area. i++) { coords[i] = new Coordinate(center. //new variable for the area of the polgyon Double area.180. X + Math. using DotSpatial. using DotSpatial.X. coords[0].NextDouble() * 360) .Sample code showing how to generate a polygon from random points that contains a hole.NextDouble() * 180) .PI / 10). this 'closes' the polygon coords[19] = new Coordinate(coords[0].Topology. . } //sets the last coordinate equal to the first. for (int i = 0. center.Cos((i * 10) * Math. i++) { //generates random coordinates and adds those coordinates to the array coords[i] = new Coordinate(center. //Defines a new random number generator Random rnd = new Random().Y).Y + (i * 10) * Math. public void PolgygonHolesSC() { //Defines a new coordinate array Coordinate[] coords = new Coordinate[20].Geometries.PI / 10).90).180.Geometries. i < 19. //defines a new LingRing from the coordinates LinearRing Ring = new LinearRing(coords). (rnd. //defines a randomly generated center for teh polygon Coordinate center = new Coordinate((rnd. PI / 20).Geometries.Y + (i * 10) * Math. i < 20.Cos((i * 10) * Math. this will be the hole in the polgyon Coordinate[] coordshole = new Coordinate[20]. thus a Array of Hole is required ILinearRing[] Holes = new ILinearRing[1].X. //This steps addes the hole LinerRing to a ILinearRing Array //A Polgyon can contain multiple holes.//Repeates the process. coordshole[0]. private void button1_Click(object sender.Y).X + Math.Topology. } coordshole[19] = new Coordinate(coordshole[0].Geometries. i++) { coordshole[i] = new Coordinate(center. EventArgs e) { . for (int i = 0. the polygon shell. Holes). the holes Polygon pg = new Polygon(Ring. and the Holes Array.PI / 20). Holes[0] = Hole. center. but generates a LinearRing with a smaller area. //This passes the Ring. LinearRing Hole = new LinearRing(coordshole). using DotSpatial. } Sample code that demonstrates how to create a new mulitpolygon from randomly generated coordinates using DotSpatial. X + Math. pg[i] = new Polygon(coord). for (int i = 0. Coordinate[] coord = new Coordinate[36].PI / 10). coord[0].NextDouble() * 180) . ii < 36. Polygon[] pg = new Polygon[50]. (rnd. i++) { Coordinate center = new Coordinate((rnd.Y + (ii * 10) * Math.Geometries. } Sample code that demonstrates how to create a multilinestring from randomly generated coordinates. using DotSpatial. ii++) { coord[ii] = new Coordinate(center. public void samplecode() { .Random rnd = new Random().90). for (int ii = 0. using DotSpatial.X. center.PI / 10).Y).Geometries. } coord[35] = new Coordinate(coord[0].180.Cos((ii * 10) * Math.Topology.NextDouble() * 360) . } MultiPolygon mpg = new MultiPolygon(pg). i < 50. Geometries. (rnd.Topology. for (int i = 0. } Sample code that demonstrates how to buffer a randomly generated point. LineString[] ls = new LineString[40]. i < 36.Random rnd = new Random().NextDouble() * 360) . i++) { coord[i] = new Coordinate((rnd. MultiLineString Mls = new MultiLineString(). Random rnd = new Random(). EventArgs e) { Coordinate coords = new Coordinate(). private void button1_Click(object sender.180. } ls[ii] = new LineString(coord).NextDouble() * 180) .90). ii < 40. using DotSpatial. } Mls = new MultiLineString(ls). ii++) { Coordinate[] coord = new Coordinate[36]. for (int ii = 0. using DotSpatial. .Geometries. (rnd.NextDouble() + 360) . and calculates the length of the linestring.90).180. using DotSpatial.NextDouble() * 180) . } MultiPoint Mps = new MultiPoint(c). } Sample code that will generate random an array of coordinates.Geometries.180. (rnd.Area. //This will get the area of the buffer.NextDouble() * 180) . private void button1_Click(object sender.coords = new Coordinate((rnd. EventArgs e) { Coordinate[] c = new Coordinate[36]. for (int i = 0. i++) { c[i] = new Coordinate((rnd. .Geometries.90). Point p = new Point(coords).NextDouble() * 360) .Buffer(500). creates a linestring from the array of coordinates.Topology. Random rnd = new Random(). double area = p. } Sample that demonstrates how to create a multipoint from randomly generated coordinates using DotSpatial. i < 36. length = ls.90). i < 36.Geometries.NextDouble() * 180) .using DotSpatial. //A for loop that will generate random coordinate's and add those coordinates to the array for (int i = 0. i++) { coords[i] = new Coordinate((rnd. using DotSpatial. } .Topology.Length.NextDouble() * 360) .180. (rnd.Geometries. //Calculates the length of the linestring Double length. //Creates a random number generator Random rnd = new Random(). public void PolgygonHolesSC() { //Creates a coordinate arrary Coordinate[] coords = new Coordinate[36]. } //Creates a linestring from the array of coordinates LineString ls = new LineString(coords). PI / 10). //Creates a random number generator Random rnd = new Random(). i++) { coords[i] = new Coordinate(center. using DotSpatial.NextDouble() * 180) .Y + (i * 10) * Math.180.NextDouble() * 360) . thus 'closing' the polygon coords[19] = new Coordinate(coords[0]. } //Set the last coordinate equal to the first coordinate in the array.Geometries.Cos((i * 10) * Math. coords[0].Topology. and calculate the area of the polygon. //A For Loop that will randomly create coordinates and feeds the coordinates into the array of coordiantes for (int i = 0. create a polygon from the coordinate array. //Creates a new polygon from the coordinate array . (rnd.Geometries. { //Creates a new array of coordinates Coordinate[] coords = new Coordinate[20]. using DotSpatial. center.X + Math.Sample code that will generate a coordinate array. //Createa a center point for the polygon Coordinate center = new Coordinate((rnd. i < 19.Y).90).PI / 10).X. //Determines that area of the polygon Double area = pg.Polygon pg = new Polygon(coords). } .Area.
Copyright © 2024 DOKUMEN.SITE Inc.