public static void main(string[] args) { ExtendedCoordinateSequenceFactory seqFact = ExtendedCoordinateSequenceFactory.Instance(); ExtendedCoordinate[] array1 = new ExtendedCoordinate[] { new ExtendedCoordinate(0, 0, 0, 91), new ExtendedCoordinate(10, 0, 0, 92), new ExtendedCoordinate(10, 10, 0, 93), new ExtendedCoordinate(0, 10, 0, 94), new ExtendedCoordinate(0, 0, 0, 91)}; ICoordinateSequence seq1 = seqFact.Create(array1); ICoordinateSequence seq2 = seqFact.Create(new ExtendedCoordinate[] { new ExtendedCoordinate(5, 5, 0, 91), new ExtendedCoordinate(15, 5, 0, 92), new ExtendedCoordinate(15, 15, 0, 93), new ExtendedCoordinate(5, 15, 0, 94), new ExtendedCoordinate(5, 5, 0, 91)}); GeometryFactory fact = new GeometryFactory(ExtendedCoordinateSequenceFactory.Instance()); IGeometry g1 = fact.CreatePolygon(fact.CreateLinearRing(seq1), null); IGeometry g2 = fact.CreatePolygon(fact.CreateLinearRing(seq2), null); Console.WriteLine("WKT for g1: " + g1); Console.WriteLine("Internal rep for g1: " + ((IPolygon) g1).ExteriorRing.CoordinateSequence); Console.WriteLine("WKT for g2: " + g2); Console.WriteLine("Internal rep for g2: " + ((IPolygon)g2).ExteriorRing.CoordinateSequence); IGeometry gInt = g1.Intersection(g2); Console.WriteLine("WKT for gInt: " + gInt); Console.WriteLine("Internal rep for gInt: " + ((IPolygon)gInt).ExteriorRing.CoordinateSequence); }
/// <summary> /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array. /// </summary> /// <param name="geometries"></param> /// <param name="factory"></param> /// <returns></returns> public static Geometry[] ToNTSGeometry(Geometries.Geometry[] geometries, GeometryFactory factory) { Geometry[] converted = new Geometry[geometries.Length]; int index = 0; foreach (Geometries.Geometry geometry in geometries) converted[index++] = ToNTSGeometry(geometry, factory); if ((geometries.Length != converted.Length)) throw new ApplicationException("Conversion error"); return converted; }
public virtual void Intersection(string wktA, string wktB, PrecisionModel pm) { Console.WriteLine("Running example using Precision Model = " + pm); GeometryFactory fact = new GeometryFactory(pm); WKTReader wktRdr = new WKTReader(fact); IGeometry A = wktRdr.Read(wktA); IGeometry B = wktRdr.Read(wktB); IGeometry C = A.Intersection(B); Console.WriteLine("A intersection B = " + C); }
/// <summary> /// /// </summary> /// <param name="geom"></param> /// <returns></returns> public IGeometry Reduce(IGeometry geom) { GeometryEditor geomEdit; if (changePrecisionModel) { GeometryFactory newFactory = new GeometryFactory(newPrecisionModel); geomEdit = new GeometryEditor(newFactory); } else // don't change point factory geomEdit = new GeometryEditor(); return geomEdit.Edit(geom, new PrecisionReducerCoordinateOperation(this)); }
public static void main(string[] args) { // create a factory using default values (e.g. floating precision) GeometryFactory fact = new GeometryFactory(); IPoint p1 = fact.CreatePoint(new Coordinate(0, 0)); Console.WriteLine(p1); IPoint p2 = fact.CreatePoint(new Coordinate(1, 1)); Console.WriteLine(p1); IMultiPoint mpt = fact.CreateMultiPoint(new ICoordinate[]{ new Coordinate(0, 0), new Coordinate(1, 1), }); Console.WriteLine(mpt); }
/// <summary> /// /// </summary> public virtual void Run() { GeometryFactory fact = new GeometryFactory(); WKTReader wktRdr = new WKTReader(fact); string wktA = "POLYGON((40 100, 40 20, 120 20, 120 100, 40 100))"; string wktB = "LINESTRING(20 80, 80 60, 100 140)"; IGeometry A = wktRdr.Read(wktA); IGeometry B = wktRdr.Read(wktB); IGeometry C = A.Intersection(B); Console.WriteLine("A = " + A); Console.WriteLine("B = " + B); Console.WriteLine("A intersection B = " + C); Console.WriteLine("A relate C = " + A.Relate(B)); }
public static void main(string[] args) { // read a point from a WKT string (using the default point factory) IGeometry g1 = new WKTReader().Read("LINESTRING (0 0, 10 10, 20 20)"); Console.WriteLine("Geometry 1: " + g1); // create a point by specifying the coordinates directly ICoordinate[] coordinates = new ICoordinate[] { new Coordinate(0, 0), new Coordinate(10, 10), new Coordinate(20, 20) }; // use the default factory, which gives full double-precision IGeometry g2 = new GeometryFactory().CreateLineString(coordinates); Console.WriteLine("Geometry 2: " + g2); // compute the intersection of the two geometries IGeometry g3 = g1.Intersection(g2); Console.WriteLine("G1 intersection G2: " + g3); }
/// <summary> /// This method returns a FeatureDataTable containing all the rows from the shapefile that intersect the testGeometry. /// The ShapeFile.ExecuteIntersectionQuery method only tests bounding boxes so we use the FilterDelegate property to add a true /// intersection test using NetTopologySuite /// </summary> /// <param name="pathToShapefile">The path to the shapefile</param> /// <param name="testGeometry">The geometry that we want to test against</param> /// <returns></returns> public FeatureDataTable GetIntersectingFeaturesUsingFilterDelegate(string pathToShapefile, Geometry testGeometry) { //create a new shapefile provider using (ShapeFile shapefile = new ShapeFile(pathToShapefile)) { //create an nts GeometryFactory GeometryFactory geometryFactory = new GeometryFactory(); //convert the testGeometry into the equivalent NTS geometry GeoAPI.Geometries.IGeometry testGeometryAsNtsGeom = GeometryConverter.ToNTSGeometry(testGeometry, geometryFactory); Geometry check = GeometryConverter.ToSharpMapGeometry(testGeometryAsNtsGeom); if (!check.Equals(testGeometry)) throw new ApplicationException("conversion error"); //set the shapefile providers' FilterDelegate property to a new anonymous method //this delegate method will be called for each potential row shapefile.FilterDelegate = delegate(FeatureDataRow featureDataRow) { //get the geometry from the featureDataRow Geometry rowGeometry = featureDataRow.Geometry; //convert it to the equivalent NTS geometry GeoAPI.Geometries.IGeometry compareGeometryAsNtsGeometry = GeometryConverter.ToNTSGeometry(rowGeometry, geometryFactory); //do the test. Note that the testGeometryAsNtsGeometry is available here because it is //declared in the same scope as the anonymous method. bool intersects = testGeometryAsNtsGeom.Intersects(compareGeometryAsNtsGeometry); //return the result return intersects; }; //create a new FeatureDataSet FeatureDataSet featureDataSet = new FeatureDataSet(); //open the shapefile shapefile.Open(); //call ExecuteIntersectionQuery. The FilterDelegate will be used to limit the result set shapefile.ExecuteIntersectionQuery(testGeometry, featureDataSet); //close the shapefile shapefile.Close(); //return the populated FeatureDataTable return featureDataSet.Tables[0]; } }
private void askGeometry(string filename, string epsg) { try { GeometryFactory f = new GeometryFactory(new PrecisionModel(), Convert.ToInt16(epsg)); using (ShapefileDataReader dr = new ShapefileDataReader(filename, f)) { if (dr.RecordCount > 0) { if (dr.ShapeHeader.ShapeType.ToString() == "Polygon") { while (dr.Read()) { _polygons.Add((IPolygon)dr.Geometry); } } else { throw new Exception("Geometry type is not Polygon"); } } else { throw new Exception("The selected shapefile does not contain any row"); } } } catch (Exception ex) { throw ex; } }
internal static GisSharpBlog.NetTopologySuite.Geometries.Point ToNTSPoint(Point point, GeometryFactory factory) { return factory.CreatePoint(ToNTSCoordinate(point, factory)) as GisSharpBlog.NetTopologySuite.Geometries.Point; }
/// <summary> /// Create a shape factory which will create shapes using the given GeometryFactory. /// </summary> /// <param name="geomFact">The factory to use.</param> public GeometricShapeFactory(GeometryFactory geomFact) { this.geomFact = geomFact; }
/* BEGIN ADDED BY MPAUL42: monoGIS team */ /// <summary> /// Constructs a <c>Polygon</c> with the given exterior boundary. /// </summary> /// <param name="shell"> /// The outer boundary of the new <c>Polygon</c>, /// or <c>null</c> or an empty <c>LinearRing</c> if the empty /// polygon is to be created. /// </param> /// <param name="factory"></param> public Polygon(LinearRing shell, GeometryFactory factory) : this(shell, null, factory) { }
/// <summary> /// Returns an ShapefileDataReader representing the data in a shapefile. /// </summary> /// <param name="filename">The filename (minus the . and extension) to read.</param> /// <param name="geometryFactory">The geometry factory to use when creating the objects.</param> /// <returns>An ShapefileDataReader representing the data in the shape file.</returns> public static ShapefileDataReader CreateDataReader(string filename, GeometryFactory geometryFactory) { if (filename == null) throw new ArgumentNullException("filename"); if (geometryFactory == null) throw new ArgumentNullException("geometryFactory"); ShapefileDataReader shpDataReader= new ShapefileDataReader(filename,geometryFactory); return shpDataReader; }
internal static MultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon multiPolygon, GeometryFactory factory) { Polygon[] polygons = new Polygon[multiPolygon.Polygons.Count]; int index = 0; foreach (Geometries.Polygon polygon in multiPolygon.Polygons) polygons[index++] = ToNTSPolygon(polygon, factory); return factory.CreateMultiPolygon(polygons) as MultiPolygon; }
public static IGeometry RemoveCurvePoint(IGeometry geometry, int index, bool keepLineStringEndPoints=false) { var vertices = new List<ICoordinate>(geometry.Coordinates); vertices.RemoveAt(index); var geometryFactory = new GeometryFactory(); var lastIndex = geometry.Coordinates.Length - 1; if (geometry is ILineString) { if (vertices.Count < 2) { return null; } if (keepLineStringEndPoints && (index == 0 || index == lastIndex)) { return null; } return geometryFactory.CreateLineString(vertices.ToArray()); } if (geometry is IPolygon) { // If first or last index is removed -> remove corresponding duplicate at the other end and close the ring. if (index == lastIndex) { vertices[0] = vertices[lastIndex]; } if (index == 0) { vertices[lastIndex - 1] = vertices[0]; } if (vertices.Count < 4) { return null; } return geometryFactory.CreatePolygon(geometryFactory.CreateLinearRing(vertices.ToArray()), null); } if (index < geometry.Coordinates.Length) { var coordinate = geometry.Coordinates[index]; var point = geometryFactory.CreatePoint(coordinate); return geometry.Difference(point); } return geometry; }
/// <summary> /// Creates a <coordinate>GDBReader</coordinate> that creates objects using the given /// <coordinate>GeometryFactory</coordinate>. /// </summary> /// <param name="factory">The factory used to create <coordinate>Geometry</coordinate>s.</param> public GDBReader(GeometryFactory factory) : base(factory) { }
public XmlTestFactory(PrecisionModel pm) { m_objGeometryFactory = new GeometryFactory(pm); m_objReader = new WKTReader(m_objGeometryFactory); }
/// <summary> /// Initializes a new instance of the <see cref="NtsProvider"/> class /// using the given <paramref name="precisionModel"/>. /// </summary> /// <param name="precisionModel"> /// The <see cref="GisSharpBlog.NetTopologySuite.Geometries.PrecisionModel"/> /// to use for define the precision of the geometry operations. /// </param> /// <seealso cref="PrecisionModels"/> /// <seealso cref="GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory"/> protected internal NtsProvider(GisSharpBlog.NetTopologySuite.Geometries.PrecisionModel precisionModel) { geometryFactory = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(precisionModel); }
/// <summary> /// Converts any <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> to the correspondant /// <see cref="SharpMap.Geometries.Geometry"/>. /// </summary> /// <param name="geometry"></param> /// <returns></returns> public static Geometry ToNTSGeometry(Geometries.Geometry geometry, GeometryFactory factory) { if (geometry == null) throw new NullReferenceException("geometry"); if (geometry.GetType() == typeof (Point)) return ToNTSPoint(geometry as Point, factory); else if (geometry.GetType() == typeof (Geometries.LineString)) return ToNTSLineString(geometry as Geometries.LineString, factory); else if (geometry.GetType() == typeof (Geometries.Polygon)) return ToNTSPolygon(geometry as Geometries.Polygon, factory); else if (geometry.GetType() == typeof (Geometries.MultiPoint)) return ToNTSMultiPoint(geometry as Geometries.MultiPoint, factory); else if (geometry.GetType() == typeof (Geometries.MultiLineString)) return ToNTSMultiLineString(geometry as Geometries.MultiLineString, factory); else if (geometry.GetType() == typeof (Geometries.MultiPolygon)) return ToNTSMultiPolygon(geometry as Geometries.MultiPolygon, factory); else if (geometry.GetType() == typeof (Geometries.GeometryCollection)) return ToNTSGeometryCollection(geometry as Geometries.GeometryCollection, factory); else throw new NotSupportedException("Type " + geometry.GetType().FullName + " not supported"); }
/// <summary> /// Initialize reader with the given <c>GeometryFactory</c>. /// </summary> /// <param name="factory"></param> public ShapeReader(GeometryFactory factory) { this.factory = factory; }
/// <summary> /// Initializes a new instance of the <see cref="T:NtsProvider"/> class /// using the given <paramref name="precisionModel"/>. /// </summary> /// <param name="precisionModel"> /// The <see cref="GisSharpBlog.NetTopologySuite.Geometries.PrecisionModel"/> /// to use for define the precision of the geometry operations. /// </param> /// <seealso cref="GisSharpBlog.NetTopologySuite.Geometries.PrecisionModels"/> /// <seealso cref="GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory"/> protected internal NtsProvider(PrecisionModel precisionModel) { geometryFactory = new GeometryFactory(precisionModel); }
internal static Polygon ToNTSPolygon(Geometries.Polygon polygon, GeometryFactory factory) { LinearRing shell = ToNTSLinearRing(polygon.ExteriorRing, factory); LinearRing[] holes = new LinearRing[polygon.InteriorRings.Count]; int index = 0; foreach (Geometries.LinearRing hole in polygon.InteriorRings) holes[index++] = ToNTSLinearRing(hole, factory); return factory.CreatePolygon(shell, holes) as Polygon; }
/// <summary> /// This method takes a pre-populated FeatureDataTable and removes rows that do not truly intersect testGeometry /// </summary> /// <param name="featureDataTable">The FeatureDataTable instance to filter</param> /// <param name="testGeometry">the geometry to compare against</param> public void PostFilterExistingFeatureDataTable(FeatureDataTable featureDataTable, Geometry testGeometry) { //first we create a new GeometryFactory. GeometryFactory geometryFactory = new GeometryFactory(); //then we convert the testGeometry into the equivalent NTS geometry GeoAPI.Geometries.IGeometry testGeometryAsNtsGeom = GeometryConverter.ToNTSGeometry(testGeometry, geometryFactory); //now we loop backwards through the FeatureDataTable for (int i = featureDataTable.Rows.Count - 1; i > -1; i--) { //we get each row FeatureDataRow featureDataRow = featureDataTable.Rows[i] as FeatureDataRow; //and get the rows' geometry Geometry compareGeometry = featureDataRow.Geometry; //convert the rows' geometry into the equivalent NTS geometry GeoAPI.Geometries.IGeometry compareGeometryAsNts = GeometryConverter.ToNTSGeometry(compareGeometry, geometryFactory); //now test for intesection (note other operations such as Contains, Within, Disjoint etc can all be done the same way) bool intersects = testGeometryAsNtsGeom.Intersects(compareGeometryAsNts); //if it doesn't intersect remove the row. if (!intersects) featureDataTable.Rows.RemoveAt(i); } }
public static IGeometry InsertCurvePoint(IGeometry geometry, ICoordinate coordinate, int index) { var vertices = new List<ICoordinate>(geometry.Coordinates); vertices.Insert(index, coordinate); var geometryFactory = new GeometryFactory(); if (geometry is ILineString) { return geometryFactory.CreateLineString(vertices.ToArray()); } if (geometry is IPolygon) { return geometryFactory.CreatePolygon(geometryFactory.CreateLinearRing(vertices.ToArray()), null); } return geometry.Union(geometryFactory.CreatePoint(coordinate)); }
/// <summary> /// Writes a Geometry to the given binary wirter. /// </summary> /// <param name="geometry">The geometry to write.</param> /// <param name="file">The file stream to write to.</param> /// <param name="geometryFactory">The geometry factory to use.</param> public override void Write(IGeometry geometry, BinaryWriter file, IGeometryFactory geometryFactory) { // This check seems to be not useful and slow the operations... // if (!geometry.IsValid) // Trace.WriteLine("Invalid polygon being written."); IGeometryCollection multi; if (geometry is IGeometryCollection) multi = (IGeometryCollection) geometry; else { GeometryFactory gf = new GeometryFactory(geometry.PrecisionModel); multi = gf.CreateMultiPolygon(new IPolygon[] { (IPolygon) geometry, } ); } file.Write(int.Parse(Enum.Format(typeof(ShapeGeometryType), ShapeType, "d"))); IEnvelope box = multi.EnvelopeInternal; IEnvelope bounds = GetEnvelopeExternal(geometryFactory.PrecisionModel, box); file.Write(bounds.MinX); file.Write(bounds.MinY); file.Write(bounds.MaxX); file.Write(bounds.MaxY); int numParts = GetNumParts(multi); int numPoints = multi.NumPoints; file.Write(numParts); file.Write(numPoints); // write the offsets to the points int offset = 0; for (int part = 0; part < multi.NumGeometries; part++) { // offset to the shell points IPolygon polygon = (IPolygon) multi.Geometries[part]; file.Write(offset); offset = offset + polygon.ExteriorRing.NumPoints; // offstes to the holes foreach (ILinearRing ring in polygon.InteriorRings) { file.Write(offset); offset = offset + ring.NumPoints; } } // write the points for (int part = 0; part < multi.NumGeometries; part++) { IPolygon poly = (IPolygon) multi.Geometries[part]; ICoordinate[] points = poly.ExteriorRing.Coordinates; WriteCoords(new CoordinateList(points), file, geometryFactory); foreach(ILinearRing ring in poly.InteriorRings) { ICoordinate[] points2 = ring.Coordinates; WriteCoords(new CoordinateList(points2), file, geometryFactory); } } }
/// <summary> /// Initialize reader with the given <c>GeometryFactory</c>. /// </summary> /// <param name="factory"></param> public GMLReader(GeometryFactory factory) { this.factory = factory; }
private List<IPolygon> tessellator(int cellWidth, int cellHeight, int numRows, int numColumns) { try { GeometryFactory f = new GeometryFactory(); List<IPolygon> grid = new List<IPolygon>(); double x = _origin.X;// pol.Coordinates[0].X; double y = _origin.Y;//pol.Coordinates[0].Y; for (int i = 1; i <= numColumns; i++) { for (int j = 1; j <= numRows; j++) { Coordinate[] coords; coords = new Coordinate[] { new Coordinate(x, y), new Coordinate(x + cellWidth, y), new Coordinate(x + cellWidth, y + cellHeight), new Coordinate(x, y+cellHeight), new Coordinate(x, y)}; LinearRing ring = (LinearRing) f.CreateLinearRing(coords); IPolygon newpol=(IPolygon) f.CreatePolygon(ring, null); newpol.UserData=i+" - "+j; grid.Add(newpol); y += cellHeight; } if(i!=0) { x += cellWidth; } y = _origin.Y; } return grid; } catch (Exception ex) { throw ex; } }
/// <summary> /// Creates a DataTable representing the information in a shape file. /// </summary> /// <param name="filename">The filename (minus the . and extension) to read.</param> /// <param name="tableName">The name to give to the table.</param> /// <param name="geometryFactory">The geometry factory to use when creating the objects.</param> /// <returns>DataTable representing the data </returns> public static DataTable CreateDataTable(string filename, string tableName, GeometryFactory geometryFactory) { if (filename == null) throw new ArgumentNullException("filename"); if (tableName == null) throw new ArgumentNullException("tableName"); if (geometryFactory == null) throw new ArgumentNullException("geometryFactory"); ShapefileDataReader shpfileDataReader= new ShapefileDataReader(filename, geometryFactory); DataTable table = new DataTable(tableName); // use ICustomTypeDescriptor to get the properies/ fields. This way we can get the // length of the dbase char fields. Because the dbase char field is translated into a string // property, we lost the length of the field. We need to know the length of the // field when creating the table in the database. IEnumerator enumerator = shpfileDataReader.GetEnumerator(); bool moreRecords = enumerator.MoveNext(); ICustomTypeDescriptor typeDescriptor = (ICustomTypeDescriptor) enumerator.Current; foreach (PropertyDescriptor property in typeDescriptor.GetProperties()) { ColumnStructure column = (ColumnStructure) property; Type fieldType = column.PropertyType; DataColumn datacolumn = new DataColumn(column.Name, fieldType); if (fieldType== typeof(string)) // use MaxLength to pass the length of the field in the dbase file datacolumn.MaxLength=column.Length; table.Columns.Add( datacolumn ); } // add the rows - need a do-while loop because we read one row in order to determine the fields int iRecordCount = 0; object[] values = new object[shpfileDataReader.FieldCount]; do { iRecordCount++; shpfileDataReader.GetValues(values); table.Rows.Add(values); moreRecords = enumerator.MoveNext(); } while (moreRecords); return table; }
/// <summary> /// /// </summary> /// <param name="factory"></param> public MyShapeFileReader(GeometryFactory factory) { shapeReader = new ShapeReader(factory); }
/// <summary> /// Imports a shapefile into a database table. /// </summary> /// <remarks> /// This method assumes a table has already been crated in the database. /// Calling this method does not close the connection that is passed in. /// </remarks> /// <param name="filename"></param> /// <param name="connectionstring"></param> /// <param name="tableName"></param> /// <returns></returns> public static int ImportShapefile(string filename, string connectionstring, string tableName) { using (SqlConnection connection = new SqlConnection(connectionstring)) { int rowsAdded = -1; PrecisionModel pm = new PrecisionModel(); GeometryFactory geometryFactory = new GeometryFactory(pm, -1); DataTable shpDataTable = CreateDataTable(filename, tableName, geometryFactory); string createTableSql = CreateDbTable(shpDataTable, true); SqlCommand createTableCommand = new SqlCommand(createTableSql, connection); connection.Open(); createTableCommand.ExecuteNonQuery(); string sqlSelect = String.Format("select * from {0}", tableName); SqlDataAdapter selectCommand = new SqlDataAdapter(sqlSelect, connection); // use a data adaptor - saves donig the inserts ourselves SqlDataAdapter dataAdapter = new SqlDataAdapter(); dataAdapter.SelectCommand = new SqlCommand(sqlSelect, connection); SqlCommandBuilder custCB = new SqlCommandBuilder(dataAdapter); DataSet ds = new DataSet(); // fill dataset dataAdapter.Fill(ds, shpDataTable.TableName); // copy rows from our datatable to the empty table in the DataSet int i = 0; foreach (DataRow row in shpDataTable.Rows) { DataRow newRow = ds.Tables[0].NewRow(); newRow.ItemArray = row.ItemArray; //gotcha! - new row still needs to be added to the table. //NewRow() just creates a new row with the same schema as the table. It does //not add it to the table. ds.Tables[0].Rows.Add(newRow); i++; } // update all the rows in batch rowsAdded = dataAdapter.Update(ds, shpDataTable.TableName); int iRows = shpDataTable.Rows.Count; Debug.Assert(rowsAdded != iRows, String.Format("{0} of {1] rows were added to the database.", rowsAdded, shpDataTable.Rows.Count)); return rowsAdded; } }